Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 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 | |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 17 | #include "dedupe_set.h" |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
| 20 | #include <cstdio> |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 21 | #include <vector> |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 22 | |
David Brazdil | d9c9037 | 2016-09-14 16:53:55 +0100 | [diff] [blame] | 23 | #include "base/array_ref.h" |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 24 | #include "dedupe_set-inl.h" |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 25 | #include "gtest/gtest.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 26 | #include "thread-current-inl.h" |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 30 | class DedupeSetTestHashFunc { |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 31 | public: |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 32 | size_t operator()(const ArrayRef<const uint8_t>& array) const { |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 33 | 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 Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 42 | |
| 43 | class 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 Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 54 | TEST(DedupeSetTest, Test) { |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 55 | Thread* self = Thread::Current(); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 56 | 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 Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 63 | { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 64 | uint8_t raw_test1[] = { 10u, 20u, 30u, 45u }; |
| 65 | ArrayRef<const uint8_t> test1(raw_test1); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 66 | array1 = deduplicator.Add(self, test1); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 67 | ASSERT_NE(array1, nullptr); |
| 68 | ASSERT_TRUE(std::equal(test1.begin(), test1.end(), array1->begin())); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 71 | const std::vector<uint8_t>* array2; |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 72 | { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 73 | uint8_t raw_test2[] = { 10u, 20u, 30u, 45u }; |
| 74 | ArrayRef<const uint8_t> test2(raw_test2); |
| 75 | array2 = deduplicator.Add(self, test2); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 76 | ASSERT_EQ(array2, array1); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 77 | ASSERT_TRUE(std::equal(test2.begin(), test2.end(), array2->begin())); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 80 | const std::vector<uint8_t>* array3; |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 81 | { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 82 | uint8_t raw_test3[] = { 10u, 22u, 30u, 47u }; |
| 83 | ArrayRef<const uint8_t> test3(raw_test3); |
| 84 | array3 = deduplicator.Add(self, test3); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 85 | ASSERT_NE(array3, nullptr); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 86 | ASSERT_NE(array3, array1); |
| 87 | ASSERT_TRUE(std::equal(test3.begin(), test3.end(), array3->begin())); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | } // namespace art |