blob: 7264b812737c2b1905df14409d736cf5a3d842bc [file] [log] [blame]
Adam Lesinskia7d1d732014-10-01 18:24:54 -07001/*
Adam Lesinski7a37b742016-10-12 14:05:55 -07002 * Copyright (C) 2016 The Android Open Source Project
Adam Lesinskia7d1d732014-10-01 18:24:54 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinski4c67a472016-11-10 16:43:59 -080017#include "androidfw/AttributeFinder.h"
Adam Lesinskia7d1d732014-10-01 18:24:54 -070018
Adam Lesinski4c67a472016-11-10 16:43:59 -080019#include "android-base/macros.h"
20#include "gtest/gtest.h"
Adam Lesinskia7d1d732014-10-01 18:24:54 -070021
Adam Lesinski4c67a472016-11-10 16:43:59 -080022namespace android {
Adam Lesinskia7d1d732014-10-01 18:24:54 -070023
Adam Lesinski4c67a472016-11-10 16:43:59 -080024class MockAttributeFinder
25 : public BackTrackingAttributeFinder<MockAttributeFinder, int> {
Adam Lesinski7a37b742016-10-12 14:05:55 -070026 public:
Adam Lesinski4c67a472016-11-10 16:43:59 -080027 MockAttributeFinder(const uint32_t* attrs, int len)
28 : BackTrackingAttributeFinder(0, len) {
Adam Lesinski7a37b742016-10-12 14:05:55 -070029 attrs_ = new uint32_t[len];
30 memcpy(attrs_, attrs, sizeof(*attrs) * len);
31 }
Adam Lesinskia7d1d732014-10-01 18:24:54 -070032
Adam Lesinski7a37b742016-10-12 14:05:55 -070033 ~MockAttributeFinder() { delete attrs_; }
Adam Lesinskia7d1d732014-10-01 18:24:54 -070034
Adam Lesinski7a37b742016-10-12 14:05:55 -070035 inline uint32_t GetAttribute(const int index) const { return attrs_[index]; }
Adam Lesinskia7d1d732014-10-01 18:24:54 -070036
Adam Lesinski7a37b742016-10-12 14:05:55 -070037 private:
38 uint32_t* attrs_;
Adam Lesinskia7d1d732014-10-01 18:24:54 -070039};
40
Adam Lesinski4c67a472016-11-10 16:43:59 -080041static const uint32_t kSortedAttributes[] = {0x01010000, 0x01010001, 0x01010002,
42 0x01010004, 0x02010001, 0x02010010,
43 0x7f010001};
Adam Lesinskia7d1d732014-10-01 18:24:54 -070044
Adam Lesinski7a37b742016-10-12 14:05:55 -070045static const uint32_t kPackageUnsortedAttributes[] = {
Adam Lesinski4c67a472016-11-10 16:43:59 -080046 0x02010001, 0x02010010, 0x01010000, 0x01010001,
47 0x01010002, 0x01010004, 0x7f010001};
Adam Lesinskia7d1d732014-10-01 18:24:54 -070048
Adam Lesinski4c67a472016-11-10 16:43:59 -080049static const uint32_t kSinglePackageAttributes[] = {0x7f010007, 0x7f01000a,
50 0x7f01000d, 0x00000000};
Adam Lesinski5dce5e62014-12-10 10:47:53 -080051
Adam Lesinskia7d1d732014-10-01 18:24:54 -070052TEST(AttributeFinderTest, IteratesSequentially) {
Adam Lesinski7a37b742016-10-12 14:05:55 -070053 const int end = arraysize(kSortedAttributes);
54 MockAttributeFinder finder(kSortedAttributes, end);
Adam Lesinskia7d1d732014-10-01 18:24:54 -070055
Adam Lesinski7a37b742016-10-12 14:05:55 -070056 EXPECT_EQ(0, finder.Find(0x01010000));
57 EXPECT_EQ(1, finder.Find(0x01010001));
58 EXPECT_EQ(2, finder.Find(0x01010002));
59 EXPECT_EQ(3, finder.Find(0x01010004));
60 EXPECT_EQ(4, finder.Find(0x02010001));
61 EXPECT_EQ(5, finder.Find(0x02010010));
62 EXPECT_EQ(6, finder.Find(0x7f010001));
63 EXPECT_EQ(end, finder.Find(0x7f010002));
Adam Lesinskia7d1d732014-10-01 18:24:54 -070064}
65
66TEST(AttributeFinderTest, PackagesAreOutOfOrder) {
Adam Lesinski7a37b742016-10-12 14:05:55 -070067 const int end = arraysize(kSortedAttributes);
68 MockAttributeFinder finder(kSortedAttributes, end);
Adam Lesinskia7d1d732014-10-01 18:24:54 -070069
Adam Lesinski7a37b742016-10-12 14:05:55 -070070 EXPECT_EQ(6, finder.Find(0x7f010001));
71 EXPECT_EQ(end, finder.Find(0x7f010002));
72 EXPECT_EQ(4, finder.Find(0x02010001));
73 EXPECT_EQ(5, finder.Find(0x02010010));
74 EXPECT_EQ(0, finder.Find(0x01010000));
75 EXPECT_EQ(1, finder.Find(0x01010001));
76 EXPECT_EQ(2, finder.Find(0x01010002));
77 EXPECT_EQ(3, finder.Find(0x01010004));
Adam Lesinskia7d1d732014-10-01 18:24:54 -070078}
79
80TEST(AttributeFinderTest, SomeAttributesAreNotFound) {
Adam Lesinski7a37b742016-10-12 14:05:55 -070081 const int end = arraysize(kSortedAttributes);
82 MockAttributeFinder finder(kSortedAttributes, end);
Adam Lesinskia7d1d732014-10-01 18:24:54 -070083
Adam Lesinski7a37b742016-10-12 14:05:55 -070084 EXPECT_EQ(0, finder.Find(0x01010000));
85 EXPECT_EQ(1, finder.Find(0x01010001));
86 EXPECT_EQ(2, finder.Find(0x01010002));
87 EXPECT_EQ(end, finder.Find(0x01010003));
88 EXPECT_EQ(3, finder.Find(0x01010004));
89 EXPECT_EQ(end, finder.Find(0x01010005));
90 EXPECT_EQ(end, finder.Find(0x01010006));
91 EXPECT_EQ(4, finder.Find(0x02010001));
92 EXPECT_EQ(end, finder.Find(0x02010002));
Adam Lesinskia7d1d732014-10-01 18:24:54 -070093}
94
95TEST(AttributeFinderTest, FindAttributesInPackageUnsortedAttributeList) {
Adam Lesinski7a37b742016-10-12 14:05:55 -070096 const int end = arraysize(kPackageUnsortedAttributes);
97 MockAttributeFinder finder(kPackageUnsortedAttributes, end);
Adam Lesinskia7d1d732014-10-01 18:24:54 -070098
Adam Lesinski7a37b742016-10-12 14:05:55 -070099 EXPECT_EQ(2, finder.Find(0x01010000));
100 EXPECT_EQ(3, finder.Find(0x01010001));
101 EXPECT_EQ(4, finder.Find(0x01010002));
102 EXPECT_EQ(end, finder.Find(0x01010003));
103 EXPECT_EQ(5, finder.Find(0x01010004));
104 EXPECT_EQ(end, finder.Find(0x01010005));
105 EXPECT_EQ(end, finder.Find(0x01010006));
106 EXPECT_EQ(0, finder.Find(0x02010001));
107 EXPECT_EQ(end, finder.Find(0x02010002));
108 EXPECT_EQ(1, finder.Find(0x02010010));
109 EXPECT_EQ(6, finder.Find(0x7f010001));
Adam Lesinskia7d1d732014-10-01 18:24:54 -0700110}
Adam Lesinski5dce5e62014-12-10 10:47:53 -0800111
112TEST(AttributeFinderTest, FindAttributesInSinglePackageAttributeList) {
Adam Lesinski7a37b742016-10-12 14:05:55 -0700113 const int end = arraysize(kSinglePackageAttributes);
114 MockAttributeFinder finder(kSinglePackageAttributes, end);
Adam Lesinski5dce5e62014-12-10 10:47:53 -0800115
Adam Lesinski7a37b742016-10-12 14:05:55 -0700116 EXPECT_EQ(end, finder.Find(0x010100f4));
117 EXPECT_EQ(end, finder.Find(0x010100f5));
118 EXPECT_EQ(end, finder.Find(0x010100f6));
119 EXPECT_EQ(end, finder.Find(0x010100f7));
120 EXPECT_EQ(end, finder.Find(0x010100f8));
121 EXPECT_EQ(end, finder.Find(0x010100fa));
122 EXPECT_EQ(0, finder.Find(0x7f010007));
Adam Lesinski5dce5e62014-12-10 10:47:53 -0800123}
Adam Lesinski4c67a472016-11-10 16:43:59 -0800124
125} // namespace android