huangcm
2025-07-03 a76b2fadf6ad4adf86e241e3753a63efe03ef80c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#include "dedupe_set.h"
 
#include <algorithm>
#include <cstdio>
#include <vector>
 
#include "base/array_ref.h"
#include "dedupe_set-inl.h"
#include "gtest/gtest.h"
#include "thread-current-inl.h"
 
namespace art {
 
class DedupeSetTestHashFunc {
 public:
  size_t operator()(const ArrayRef<const uint8_t>& array) const {
    size_t hash = 0;
    for (uint8_t c : array) {
      hash += c;
      hash += hash << 10;
      hash += hash >> 6;
    }
    return hash;
  }
};
 
class DedupeSetTestAlloc {
 public:
  const std::vector<uint8_t>* Copy(const ArrayRef<const uint8_t>& src) {
    return new std::vector<uint8_t>(src.begin(), src.end());
  }
 
  void Destroy(const std::vector<uint8_t>* key) {
    delete key;
  }
};
 
TEST(DedupeSetTest, Test) {
  Thread* self = Thread::Current();
  DedupeSetTestAlloc alloc;
  DedupeSet<ArrayRef<const uint8_t>,
            std::vector<uint8_t>,
            DedupeSetTestAlloc,
            size_t,
            DedupeSetTestHashFunc> deduplicator("test", alloc);
  const std::vector<uint8_t>* array1;
  {
    uint8_t raw_test1[] = { 10u, 20u, 30u, 45u };
    ArrayRef<const uint8_t> test1(raw_test1);
    array1 = deduplicator.Add(self, test1);
    ASSERT_NE(array1, nullptr);
    ASSERT_TRUE(std::equal(test1.begin(), test1.end(), array1->begin()));
  }
 
  const std::vector<uint8_t>* array2;
  {
    uint8_t raw_test2[] = { 10u, 20u, 30u, 45u };
    ArrayRef<const uint8_t> test2(raw_test2);
    array2 = deduplicator.Add(self, test2);
    ASSERT_EQ(array2, array1);
    ASSERT_TRUE(std::equal(test2.begin(), test2.end(), array2->begin()));
  }
 
  const std::vector<uint8_t>* array3;
  {
    uint8_t raw_test3[] = { 10u, 22u, 30u, 47u };
    ArrayRef<const uint8_t> test3(raw_test3);
    array3 = deduplicator.Add(self, test3);
    ASSERT_NE(array3, nullptr);
    ASSERT_NE(array3, array1);
    ASSERT_TRUE(std::equal(test3.begin(), test3.end(), array3->begin()));
  }
}
 
}  // namespace art