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> |
| 21 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 22 | #include "gtest/gtest.h" |
| 23 | #include "thread-inl.h" |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 27 | class 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 Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 39 | TEST(DedupeSetTest, Test) { |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 40 | Thread* self = Thread::Current(); |
| 41 | typedef std::vector<uint8_t> ByteArray; |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 42 | SwapAllocator<void> swap(nullptr); |
| 43 | DedupeSet<ByteArray, SwapVector<uint8_t>, size_t, DedupeHashFunc> deduplicator("test", swap); |
| 44 | SwapVector<uint8_t>* array1; |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 45 | { |
| 46 | ByteArray test1; |
| 47 | test1.push_back(10); |
| 48 | test1.push_back(20); |
| 49 | test1.push_back(30); |
| 50 | test1.push_back(45); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 51 | |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 52 | array1 = deduplicator.Add(self, test1); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 53 | ASSERT_NE(array1, nullptr); |
| 54 | ASSERT_TRUE(std::equal(test1.begin(), test1.end(), array1->begin())); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 57 | SwapVector<uint8_t>* array2; |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 58 | { |
| 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 Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 66 | ASSERT_TRUE(std::equal(test1.begin(), test1.end(), array2->begin())); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 69 | SwapVector<uint8_t>* array3; |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 70 | { |
| 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 Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 77 | ASSERT_NE(array3, nullptr); |
| 78 | ASSERT_TRUE(std::equal(test1.begin(), test1.end(), array3->begin())); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | } // namespace art |