blob: 637964e4842aed119a5f15c94a8c9a388a4b9e2a [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"
Andreas Gampee21dc3d2014-12-08 16:59:43 -080018
19#include <algorithm>
20#include <cstdio>
21
Brian Carlstromba150c32013-08-27 17:31:03 -070022#include "gtest/gtest.h"
23#include "thread-inl.h"
Mathieu Chartier193bad92013-08-29 18:46:00 -070024
25namespace art {
26
Mathieu Chartier193bad92013-08-29 18:46:00 -070027class DedupeHashFunc {
28 public:
29 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};
Brian Carlstrom413e89f2013-10-21 23:53:49 -070039TEST(DedupeSetTest, Test) {
Mathieu Chartier193bad92013-08-29 18:46:00 -070040 Thread* self = Thread::Current();
41 typedef std::vector<uint8_t> ByteArray;
Andreas Gampee21dc3d2014-12-08 16:59:43 -080042 SwapAllocator<void> swap(nullptr);
43 DedupeSet<ByteArray, SwapVector<uint8_t>, size_t, DedupeHashFunc> deduplicator("test", swap);
44 SwapVector<uint8_t>* array1;
Mathieu Chartier193bad92013-08-29 18:46:00 -070045 {
46 ByteArray test1;
47 test1.push_back(10);
48 test1.push_back(20);
49 test1.push_back(30);
50 test1.push_back(45);
Andreas Gampee21dc3d2014-12-08 16:59:43 -080051
Mathieu Chartier193bad92013-08-29 18:46:00 -070052 array1 = deduplicator.Add(self, test1);
Andreas Gampee21dc3d2014-12-08 16:59:43 -080053 ASSERT_NE(array1, nullptr);
54 ASSERT_TRUE(std::equal(test1.begin(), test1.end(), array1->begin()));
Mathieu Chartier193bad92013-08-29 18:46:00 -070055 }
56
Andreas Gampee21dc3d2014-12-08 16:59:43 -080057 SwapVector<uint8_t>* array2;
Mathieu Chartier193bad92013-08-29 18:46:00 -070058 {
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);
Andreas Gampee21dc3d2014-12-08 16:59:43 -080066 ASSERT_TRUE(std::equal(test1.begin(), test1.end(), array2->begin()));
Mathieu Chartier193bad92013-08-29 18:46:00 -070067 }
68
Andreas Gampee21dc3d2014-12-08 16:59:43 -080069 SwapVector<uint8_t>* array3;
Mathieu Chartier193bad92013-08-29 18:46:00 -070070 {
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);
Andreas Gampee21dc3d2014-12-08 16:59:43 -080077 ASSERT_NE(array3, nullptr);
78 ASSERT_TRUE(std::equal(test1.begin(), test1.end(), array3->begin()));
Mathieu Chartier193bad92013-08-29 18:46:00 -070079 }
80}
81
82} // namespace art