1 /* 2 * Copyright (C) 2013 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 "dedupe_set.h" 18 19 #include <algorithm> 20 #include <cstdio> 21 #include <vector> 22 23 #include "dedupe_set-inl.h" 24 #include "gtest/gtest.h" 25 #include "thread-inl.h" 26 #include "utils/array_ref.h" 27 28 namespace art { 29 30 class DedupeSetTestHashFunc { 31 public: 32 size_t operator()(const ArrayRef<const uint8_t>& array) const { 33 size_t hash = 0; 34 for (uint8_t c : array) { 35 hash += c; 36 hash += hash << 10; 37 hash += hash >> 6; 38 } 39 return hash; 40 } 41 }; 42 43 class DedupeSetTestAlloc { 44 public: 45 const std::vector<uint8_t>* Copy(const ArrayRef<const uint8_t>& src) { 46 return new std::vector<uint8_t>(src.begin(), src.end()); 47 } 48 49 void Destroy(const std::vector<uint8_t>* key) { 50 delete key; 51 } 52 }; 53 54 TEST(DedupeSetTest, Test) { 55 Thread* self = Thread::Current(); 56 DedupeSetTestAlloc alloc; 57 DedupeSet<ArrayRef<const uint8_t>, 58 std::vector<uint8_t>, 59 DedupeSetTestAlloc, 60 size_t, 61 DedupeSetTestHashFunc> deduplicator("test", alloc); 62 const std::vector<uint8_t>* array1; 63 { 64 uint8_t raw_test1[] = { 10u, 20u, 30u, 45u }; 65 ArrayRef<const uint8_t> test1(raw_test1); 66 array1 = deduplicator.Add(self, test1); 67 ASSERT_NE(array1, nullptr); 68 ASSERT_TRUE(std::equal(test1.begin(), test1.end(), array1->begin())); 69 } 70 71 const std::vector<uint8_t>* array2; 72 { 73 uint8_t raw_test2[] = { 10u, 20u, 30u, 45u }; 74 ArrayRef<const uint8_t> test2(raw_test2); 75 array2 = deduplicator.Add(self, test2); 76 ASSERT_EQ(array2, array1); 77 ASSERT_TRUE(std::equal(test2.begin(), test2.end(), array2->begin())); 78 } 79 80 const std::vector<uint8_t>* array3; 81 { 82 uint8_t raw_test3[] = { 10u, 22u, 30u, 47u }; 83 ArrayRef<const uint8_t> test3(raw_test3); 84 array3 = deduplicator.Add(self, test3); 85 ASSERT_NE(array3, nullptr); 86 ASSERT_NE(array3, array1); 87 ASSERT_TRUE(std::equal(test3.begin(), test3.end(), array3->begin())); 88 } 89 } 90 91 } // namespace art 92