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" |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 18 | #include "gtest/gtest.h" |
| 19 | #include "thread-inl.h" |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 20 | |
| 21 | namespace art { |
| 22 | |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 23 | class 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 Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 35 | TEST(DedupeSetTest, Test) { |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 36 | Thread* self = Thread::Current(); |
| 37 | typedef std::vector<uint8_t> ByteArray; |
Ian Rogers | d133b97 | 2013-09-05 11:01:30 -0700 | [diff] [blame] | 38 | DedupeSet<ByteArray, size_t, DedupeHashFunc> deduplicator("test"); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 39 | 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 |