| Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 1 | /* |
| Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 2 | * Copyright (C) 2016 The Android Open Source Project |
| Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 3 | * |
| 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 Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 17 | #include "../AttributeFinder.h" |
| Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 18 | |
| Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 19 | #include <android-base/macros.h> |
| Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 20 | #include <gtest/gtest.h> |
| 21 | |
| 22 | using android::BackTrackingAttributeFinder; |
| 23 | |
| 24 | class MockAttributeFinder : public BackTrackingAttributeFinder<MockAttributeFinder, int> { |
| Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 25 | 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 Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 30 | |
| Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 31 | ~MockAttributeFinder() { delete attrs_; } |
| Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 32 | |
| Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 33 | inline uint32_t GetAttribute(const int index) const { return attrs_[index]; } |
| Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 34 | |
| Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 35 | private: |
| 36 | uint32_t* attrs_; |
| Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
| Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 39 | static const uint32_t kSortedAttributes[] = {0x01010000, 0x01010001, 0x01010002, 0x01010004, |
| 40 | 0x02010001, 0x02010010, 0x7f010001}; |
| Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 41 | |
| Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 42 | static const uint32_t kPackageUnsortedAttributes[] = { |
| 43 | 0x02010001, 0x02010010, 0x01010000, 0x01010001, 0x01010002, 0x01010004, 0x7f010001}; |
| Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 44 | |
| Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 45 | static const uint32_t kSinglePackageAttributes[] = {0x7f010007, 0x7f01000a, 0x7f01000d, 0x00000000}; |
| Adam Lesinski | 5dce5e6 | 2014-12-10 10:47:53 -0800 | [diff] [blame] | 46 | |
| Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 47 | TEST(AttributeFinderTest, IteratesSequentially) { |
| Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 48 | const int end = arraysize(kSortedAttributes); |
| 49 | MockAttributeFinder finder(kSortedAttributes, end); |
| Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 50 | |
| Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 51 | 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 Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | TEST(AttributeFinderTest, PackagesAreOutOfOrder) { |
| Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 62 | const int end = arraysize(kSortedAttributes); |
| 63 | MockAttributeFinder finder(kSortedAttributes, end); |
| Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 64 | |
| Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 65 | 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 Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | TEST(AttributeFinderTest, SomeAttributesAreNotFound) { |
| Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 76 | const int end = arraysize(kSortedAttributes); |
| 77 | MockAttributeFinder finder(kSortedAttributes, end); |
| Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 78 | |
| Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 79 | 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 Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | TEST(AttributeFinderTest, FindAttributesInPackageUnsortedAttributeList) { |
| Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 91 | const int end = arraysize(kPackageUnsortedAttributes); |
| 92 | MockAttributeFinder finder(kPackageUnsortedAttributes, end); |
| Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 93 | |
| Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 94 | 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 Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 105 | } |
| Adam Lesinski | 5dce5e6 | 2014-12-10 10:47:53 -0800 | [diff] [blame] | 106 | |
| 107 | TEST(AttributeFinderTest, FindAttributesInSinglePackageAttributeList) { |
| Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 108 | const int end = arraysize(kSinglePackageAttributes); |
| 109 | MockAttributeFinder finder(kSinglePackageAttributes, end); |
| Adam Lesinski | 5dce5e6 | 2014-12-10 10:47:53 -0800 | [diff] [blame] | 110 | |
| Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 111 | 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 Lesinski | 5dce5e6 | 2014-12-10 10:47:53 -0800 | [diff] [blame] | 118 | } |