Home | History | Annotate | Download | only in utils
      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 
     17 #include "common_test.h"
     18 #include "dedupe_set.h"
     19 
     20 namespace art {
     21 
     22 class DedupeSetTest : public testing::Test {
     23  public:
     24 };
     25 
     26 class DedupeHashFunc {
     27  public:
     28   size_t operator()(const std::vector<uint8_t>& array) const {
     29     size_t hash = 0;
     30     for (uint8_t c : array) {
     31       hash += c;
     32       hash += hash << 10;
     33       hash += hash >> 6;
     34     }
     35     return hash;
     36   }
     37 };
     38 TEST_F(DedupeSetTest, Test) {
     39   Thread* self = Thread::Current();
     40   typedef std::vector<uint8_t> ByteArray;
     41   DedupeSet<ByteArray, size_t, DedupeHashFunc> deduplicator;
     42   ByteArray* array1;
     43   {
     44     ByteArray test1;
     45     test1.push_back(10);
     46     test1.push_back(20);
     47     test1.push_back(30);
     48     test1.push_back(45);
     49     array1 = deduplicator.Add(self, test1);
     50     ASSERT_EQ(test1, *array1);
     51   }
     52 
     53   ByteArray* array2;
     54   {
     55     ByteArray test1;
     56     test1.push_back(10);
     57     test1.push_back(20);
     58     test1.push_back(30);
     59     test1.push_back(45);
     60     array2 = deduplicator.Add(self, test1);
     61     ASSERT_EQ(array2, array1);
     62     ASSERT_EQ(test1, *array2);
     63   }
     64 
     65   ByteArray* array3;
     66   {
     67     ByteArray test1;
     68     test1.push_back(10);
     69     test1.push_back(22);
     70     test1.push_back(30);
     71     test1.push_back(47);
     72     array3 = deduplicator.Add(self, test1);
     73     ASSERT_NE(array3, &test1);
     74     ASSERT_EQ(test1, *array3);
     75   }
     76 }
     77 
     78 }  // namespace art
     79