blob: 8abe6debc12d4798357a3385551fb32e3a778e19 [file] [log] [blame]
Mathieu Chartier193bad92013-08-29 18:46:00 -07001/*
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
Mathieu Chartier193bad92013-08-29 18:46:00 -070017#include "dedupe_set.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070018#include "gtest/gtest.h"
19#include "thread-inl.h"
Mathieu Chartier193bad92013-08-29 18:46:00 -070020
21namespace art {
22
Mathieu Chartier193bad92013-08-29 18:46:00 -070023class DedupeHashFunc {
24 public:
25 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};
Brian Carlstrom413e89f2013-10-21 23:53:49 -070035TEST(DedupeSetTest, Test) {
Mathieu Chartier193bad92013-08-29 18:46:00 -070036 Thread* self = Thread::Current();
37 typedef std::vector<uint8_t> ByteArray;
Ian Rogersd133b972013-09-05 11:01:30 -070038 DedupeSet<ByteArray, size_t, DedupeHashFunc> deduplicator("test");
Mathieu Chartier193bad92013-08-29 18:46:00 -070039 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