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