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