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 
22 #include "gtest/gtest.h"
23 #include "thread-inl.h"
24 
25 namespace art {
26 
27 class DedupeHashFunc {
28  public:
operator ()(const std::vector<uint8_t> & array) const29   size_t operator()(const std::vector<uint8_t>& array) const {
30     size_t hash = 0;
31     for (uint8_t c : array) {
32       hash += c;
33       hash += hash << 10;
34       hash += hash >> 6;
35     }
36     return hash;
37   }
38 };
TEST(DedupeSetTest,Test)39 TEST(DedupeSetTest, Test) {
40   Thread* self = Thread::Current();
41   typedef std::vector<uint8_t> ByteArray;
42   SwapAllocator<void> swap(nullptr);
43   DedupeSet<ByteArray, SwapVector<uint8_t>, size_t, DedupeHashFunc> deduplicator("test", swap);
44   SwapVector<uint8_t>* array1;
45   {
46     ByteArray test1;
47     test1.push_back(10);
48     test1.push_back(20);
49     test1.push_back(30);
50     test1.push_back(45);
51 
52     array1 = deduplicator.Add(self, test1);
53     ASSERT_NE(array1, nullptr);
54     ASSERT_TRUE(std::equal(test1.begin(), test1.end(), array1->begin()));
55   }
56 
57   SwapVector<uint8_t>* array2;
58   {
59     ByteArray test1;
60     test1.push_back(10);
61     test1.push_back(20);
62     test1.push_back(30);
63     test1.push_back(45);
64     array2 = deduplicator.Add(self, test1);
65     ASSERT_EQ(array2, array1);
66     ASSERT_TRUE(std::equal(test1.begin(), test1.end(), array2->begin()));
67   }
68 
69   SwapVector<uint8_t>* array3;
70   {
71     ByteArray test1;
72     test1.push_back(10);
73     test1.push_back(22);
74     test1.push_back(30);
75     test1.push_back(47);
76     array3 = deduplicator.Add(self, test1);
77     ASSERT_NE(array3, nullptr);
78     ASSERT_TRUE(std::equal(test1.begin(), test1.end(), array3->begin()));
79   }
80 }
81 
82 }  // namespace art
83