blob: d9ed48ebe9532641aed3f8c77e7d58650853307a [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 Lesinski7a37b742016-10-12 14:05:55 -070017#include "../AttributeFinder.h"
Adam Lesinskia7d1d732014-10-01 18:24:54 -070018
Adam Lesinski7a37b742016-10-12 14:05:55 -070019#include <android-base/macros.h>
Adam Lesinskia7d1d732014-10-01 18:24:54 -070020#include <gtest/gtest.h>
21
22using android::BackTrackingAttributeFinder;
23
24class MockAttributeFinder : public BackTrackingAttributeFinder<MockAttributeFinder, int> {
Adam Lesinski7a37b742016-10-12 14:05:55 -070025 public:
26 MockAttributeFinder(const uint32_t* attrs, int len) : BackTrackingAttributeFinder(0, len) {
27 attrs_ = new uint32_t[len];
28 memcpy(attrs_, attrs, sizeof(*attrs) * len);
29 }
Adam Lesinskia7d1d732014-10-01 18:24:54 -070030
Adam Lesinski7a37b742016-10-12 14:05:55 -070031 ~MockAttributeFinder() { delete attrs_; }
Adam Lesinskia7d1d732014-10-01 18:24:54 -070032
Adam Lesinski7a37b742016-10-12 14:05:55 -070033 inline uint32_t GetAttribute(const int index) const { return attrs_[index]; }
Adam Lesinskia7d1d732014-10-01 18:24:54 -070034
Adam Lesinski7a37b742016-10-12 14:05:55 -070035 private:
36 uint32_t* attrs_;
Adam Lesinskia7d1d732014-10-01 18:24:54 -070037};
38
Adam Lesinski7a37b742016-10-12 14:05:55 -070039static const uint32_t kSortedAttributes[] = {0x01010000, 0x01010001, 0x01010002, 0x01010004,
40 0x02010001, 0x02010010, 0x7f010001};
Adam Lesinskia7d1d732014-10-01 18:24:54 -070041
Adam Lesinski7a37b742016-10-12 14:05:55 -070042static const uint32_t kPackageUnsortedAttributes[] = {
43 0x02010001, 0x02010010, 0x01010000, 0x01010001, 0x01010002, 0x01010004, 0x7f010001};
Adam Lesinskia7d1d732014-10-01 18:24:54 -070044
Adam Lesinski7a37b742016-10-12 14:05:55 -070045static const uint32_t kSinglePackageAttributes[] = {0x7f010007, 0x7f01000a, 0x7f01000d, 0x00000000};
Adam Lesinski5dce5e62014-12-10 10:47:53 -080046
Adam Lesinskia7d1d732014-10-01 18:24:54 -070047TEST(AttributeFinderTest, IteratesSequentially) {
Adam Lesinski7a37b742016-10-12 14:05:55 -070048 const int end = arraysize(kSortedAttributes);
49 MockAttributeFinder finder(kSortedAttributes, end);
Adam Lesinskia7d1d732014-10-01 18:24:54 -070050
Adam Lesinski7a37b742016-10-12 14:05:55 -070051 EXPECT_EQ(0, finder.Find(0x01010000));
52 EXPECT_EQ(1, finder.Find(0x01010001));
53 EXPECT_EQ(2, finder.Find(0x01010002));
54 EXPECT_EQ(3, finder.Find(0x01010004));
55 EXPECT_EQ(4, finder.Find(0x02010001));
56 EXPECT_EQ(5, finder.Find(0x02010010));
57 EXPECT_EQ(6, finder.Find(0x7f010001));
58 EXPECT_EQ(end, finder.Find(0x7f010002));
Adam Lesinskia7d1d732014-10-01 18:24:54 -070059}
60
61TEST(AttributeFinderTest, PackagesAreOutOfOrder) {
Adam Lesinski7a37b742016-10-12 14:05:55 -070062 const int end = arraysize(kSortedAttributes);
63 MockAttributeFinder finder(kSortedAttributes, end);
Adam Lesinskia7d1d732014-10-01 18:24:54 -070064
Adam Lesinski7a37b742016-10-12 14:05:55 -070065 EXPECT_EQ(6, finder.Find(0x7f010001));
66 EXPECT_EQ(end, finder.Find(0x7f010002));
67 EXPECT_EQ(4, finder.Find(0x02010001));
68 EXPECT_EQ(5, finder.Find(0x02010010));
69 EXPECT_EQ(0, finder.Find(0x01010000));
70 EXPECT_EQ(1, finder.Find(0x01010001));
71 EXPECT_EQ(2, finder.Find(0x01010002));
72 EXPECT_EQ(3, finder.Find(0x01010004));
Adam Lesinskia7d1d732014-10-01 18:24:54 -070073}
74
75TEST(AttributeFinderTest, SomeAttributesAreNotFound) {
Adam Lesinski7a37b742016-10-12 14:05:55 -070076 const int end = arraysize(kSortedAttributes);
77 MockAttributeFinder finder(kSortedAttributes, end);
Adam Lesinskia7d1d732014-10-01 18:24:54 -070078
Adam Lesinski7a37b742016-10-12 14:05:55 -070079 EXPECT_EQ(0, finder.Find(0x01010000));
80 EXPECT_EQ(1, finder.Find(0x01010001));
81 EXPECT_EQ(2, finder.Find(0x01010002));
82 EXPECT_EQ(end, finder.Find(0x01010003));
83 EXPECT_EQ(3, finder.Find(0x01010004));
84 EXPECT_EQ(end, finder.Find(0x01010005));
85 EXPECT_EQ(end, finder.Find(0x01010006));
86 EXPECT_EQ(4, finder.Find(0x02010001));
87 EXPECT_EQ(end, finder.Find(0x02010002));
Adam Lesinskia7d1d732014-10-01 18:24:54 -070088}
89
90TEST(AttributeFinderTest, FindAttributesInPackageUnsortedAttributeList) {
Adam Lesinski7a37b742016-10-12 14:05:55 -070091 const int end = arraysize(kPackageUnsortedAttributes);
92 MockAttributeFinder finder(kPackageUnsortedAttributes, end);
Adam Lesinskia7d1d732014-10-01 18:24:54 -070093
Adam Lesinski7a37b742016-10-12 14:05:55 -070094 EXPECT_EQ(2, finder.Find(0x01010000));
95 EXPECT_EQ(3, finder.Find(0x01010001));
96 EXPECT_EQ(4, finder.Find(0x01010002));
97 EXPECT_EQ(end, finder.Find(0x01010003));
98 EXPECT_EQ(5, finder.Find(0x01010004));
99 EXPECT_EQ(end, finder.Find(0x01010005));
100 EXPECT_EQ(end, finder.Find(0x01010006));
101 EXPECT_EQ(0, finder.Find(0x02010001));
102 EXPECT_EQ(end, finder.Find(0x02010002));
103 EXPECT_EQ(1, finder.Find(0x02010010));
104 EXPECT_EQ(6, finder.Find(0x7f010001));
Adam Lesinskia7d1d732014-10-01 18:24:54 -0700105}
Adam Lesinski5dce5e62014-12-10 10:47:53 -0800106
107TEST(AttributeFinderTest, FindAttributesInSinglePackageAttributeList) {
Adam Lesinski7a37b742016-10-12 14:05:55 -0700108 const int end = arraysize(kSinglePackageAttributes);
109 MockAttributeFinder finder(kSinglePackageAttributes, end);
Adam Lesinski5dce5e62014-12-10 10:47:53 -0800110
Adam Lesinski7a37b742016-10-12 14:05:55 -0700111 EXPECT_EQ(end, finder.Find(0x010100f4));
112 EXPECT_EQ(end, finder.Find(0x010100f5));
113 EXPECT_EQ(end, finder.Find(0x010100f6));
114 EXPECT_EQ(end, finder.Find(0x010100f7));
115 EXPECT_EQ(end, finder.Find(0x010100f8));
116 EXPECT_EQ(end, finder.Find(0x010100fa));
117 EXPECT_EQ(0, finder.Find(0x7f010007));
Adam Lesinski5dce5e62014-12-10 10:47:53 -0800118}