blob: b390508ed465c2c57a208e5eb740a7e7a930c6ab [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>
Vladimir Marko35831e82015-09-11 11:59:18 +010021#include <vector>
Andreas Gampee21dc3d2014-12-08 16:59:43 -080022
David Brazdild9c90372016-09-14 16:53:55 +010023#include "base/array_ref.h"
Vladimir Marko35831e82015-09-11 11:59:18 +010024#include "dedupe_set-inl.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070025#include "gtest/gtest.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070026#include "thread-current-inl.h"
Mathieu Chartier193bad92013-08-29 18:46:00 -070027
28namespace art {
29
Vladimir Marko35831e82015-09-11 11:59:18 +010030class DedupeSetTestHashFunc {
Mathieu Chartier193bad92013-08-29 18:46:00 -070031 public:
Vladimir Marko35831e82015-09-11 11:59:18 +010032 size_t operator()(const ArrayRef<const uint8_t>& array) const {
Mathieu Chartier193bad92013-08-29 18:46:00 -070033 size_t hash = 0;
34 for (uint8_t c : array) {
35 hash += c;
36 hash += hash << 10;
37 hash += hash >> 6;
38 }
39 return hash;
40 }
41};
Vladimir Marko35831e82015-09-11 11:59:18 +010042
43class DedupeSetTestAlloc {
44 public:
45 const std::vector<uint8_t>* Copy(const ArrayRef<const uint8_t>& src) {
46 return new std::vector<uint8_t>(src.begin(), src.end());
47 }
48
49 void Destroy(const std::vector<uint8_t>* key) {
50 delete key;
51 }
52};
53
Brian Carlstrom413e89f2013-10-21 23:53:49 -070054TEST(DedupeSetTest, Test) {
Mathieu Chartier193bad92013-08-29 18:46:00 -070055 Thread* self = Thread::Current();
Vladimir Marko35831e82015-09-11 11:59:18 +010056 DedupeSetTestAlloc alloc;
57 DedupeSet<ArrayRef<const uint8_t>,
58 std::vector<uint8_t>,
59 DedupeSetTestAlloc,
60 size_t,
61 DedupeSetTestHashFunc> deduplicator("test", alloc);
62 const std::vector<uint8_t>* array1;
Mathieu Chartier193bad92013-08-29 18:46:00 -070063 {
Vladimir Marko35831e82015-09-11 11:59:18 +010064 uint8_t raw_test1[] = { 10u, 20u, 30u, 45u };
65 ArrayRef<const uint8_t> test1(raw_test1);
Mathieu Chartier193bad92013-08-29 18:46:00 -070066 array1 = deduplicator.Add(self, test1);
Andreas Gampee21dc3d2014-12-08 16:59:43 -080067 ASSERT_NE(array1, nullptr);
68 ASSERT_TRUE(std::equal(test1.begin(), test1.end(), array1->begin()));
Mathieu Chartier193bad92013-08-29 18:46:00 -070069 }
70
Vladimir Marko35831e82015-09-11 11:59:18 +010071 const std::vector<uint8_t>* array2;
Mathieu Chartier193bad92013-08-29 18:46:00 -070072 {
Vladimir Marko35831e82015-09-11 11:59:18 +010073 uint8_t raw_test2[] = { 10u, 20u, 30u, 45u };
74 ArrayRef<const uint8_t> test2(raw_test2);
75 array2 = deduplicator.Add(self, test2);
Mathieu Chartier193bad92013-08-29 18:46:00 -070076 ASSERT_EQ(array2, array1);
Vladimir Marko35831e82015-09-11 11:59:18 +010077 ASSERT_TRUE(std::equal(test2.begin(), test2.end(), array2->begin()));
Mathieu Chartier193bad92013-08-29 18:46:00 -070078 }
79
Vladimir Marko35831e82015-09-11 11:59:18 +010080 const std::vector<uint8_t>* array3;
Mathieu Chartier193bad92013-08-29 18:46:00 -070081 {
Vladimir Marko35831e82015-09-11 11:59:18 +010082 uint8_t raw_test3[] = { 10u, 22u, 30u, 47u };
83 ArrayRef<const uint8_t> test3(raw_test3);
84 array3 = deduplicator.Add(self, test3);
Andreas Gampee21dc3d2014-12-08 16:59:43 -080085 ASSERT_NE(array3, nullptr);
Vladimir Marko35831e82015-09-11 11:59:18 +010086 ASSERT_NE(array3, array1);
87 ASSERT_TRUE(std::equal(test3.begin(), test3.end(), array3->begin()));
Mathieu Chartier193bad92013-08-29 18:46:00 -070088 }
89}
90
91} // namespace art