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