xref: /aosp_15_r20/external/perfetto/src/base/flat_set_unittest.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2018 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 "perfetto/base/flat_set.h"
18 
19 #include <random>
20 #include <set>
21 
22 #include "test/gtest_and_gmock.h"
23 
24 using testing::ElementsAre;
25 using testing::ElementsAreArray;
26 
27 namespace perfetto {
28 namespace base {
29 namespace {
30 
TEST(FlatSetTest,InsertAndLookup)31 TEST(FlatSetTest, InsertAndLookup) {
32   FlatSet<long> flat_set;
33   for (int i = 0; i < 3; i++) {
34     EXPECT_TRUE(flat_set.empty());
35     EXPECT_EQ(flat_set.size(), 0u);
36     EXPECT_EQ(flat_set.begin(), flat_set.end());
37     EXPECT_FALSE(flat_set.count(42));
38     EXPECT_EQ(flat_set.find(42), flat_set.end());
39     EXPECT_EQ(flat_set.find(42), flat_set.begin());
40 
41     auto it_and_inserted = flat_set.insert(1);
42     EXPECT_EQ(it_and_inserted.first, flat_set.find(1));
43     EXPECT_TRUE(it_and_inserted.second);
44     EXPECT_FALSE(flat_set.empty());
45     EXPECT_EQ(flat_set.size(), 1u);
46     {
47       auto it = flat_set.find(1);
48       EXPECT_EQ(it, flat_set.begin());
49       EXPECT_NE(it, flat_set.end());
50       EXPECT_EQ(*it, 1);
51     }
52 
53     EXPECT_TRUE(flat_set.count(1));
54     EXPECT_NE(flat_set.begin(), flat_set.end());
55     EXPECT_EQ(std::distance(flat_set.begin(), flat_set.end()), 1);
56 
57     it_and_inserted = flat_set.insert(1);
58     EXPECT_EQ(it_and_inserted.first, flat_set.find(1));
59     EXPECT_FALSE(it_and_inserted.second);
60     EXPECT_EQ(flat_set.size(), 1u);
61     EXPECT_TRUE(flat_set.count(1));
62     EXPECT_FALSE(flat_set.count(0));
63     EXPECT_FALSE(flat_set.count(-1));
64 
65     EXPECT_TRUE(flat_set.erase(1));
66     EXPECT_FALSE(flat_set.count(1));
67     EXPECT_EQ(flat_set.size(), 0u);
68 
69     EXPECT_TRUE(flat_set.insert(7).second);
70     EXPECT_TRUE(flat_set.insert(-4).second);
71     EXPECT_TRUE(flat_set.insert(11).second);
72     EXPECT_TRUE(flat_set.insert(-13).second);
73     EXPECT_TRUE(flat_set.count(7));
74     EXPECT_TRUE(flat_set.count(-4));
75     EXPECT_TRUE(flat_set.count(11));
76     EXPECT_TRUE(flat_set.count(-13));
77     EXPECT_THAT(flat_set, ElementsAre(-13, -4, 7, 11));
78 
79     EXPECT_TRUE(flat_set.erase(-4));
80     EXPECT_TRUE(flat_set.erase(7));
81     EXPECT_THAT(flat_set, ElementsAre(-13, 11));
82 
83     flat_set.clear();
84   }
85 
86   // Test that the initializer-ctor dedupes entries.
87   flat_set = FlatSet<long>({1, 2, 1, 2, 3});
88   EXPECT_EQ(flat_set.size(), 3u);
89   EXPECT_THAT(flat_set, ElementsAre(1, 2, 3));
90 }
91 
TEST(FlatSetTest,GoldenTest)92 TEST(FlatSetTest, GoldenTest) {
93   FlatSet<int> flat_set;
94   std::set<int> gold_set;
95   static std::minstd_rand rng(0);
96   std::uniform_int_distribution<int> int_dist(-200, 200);
97 
98   for (int i = 0; i < 10000; i++) {
99     const int val = int_dist(rng);
100     if (i % 3) {
101       auto flat_result = flat_set.insert(val);
102       auto gold_result = gold_set.insert(val);
103       EXPECT_EQ(flat_result.first, flat_set.find(val));
104       EXPECT_EQ(gold_result.first, gold_set.find(val));
105       EXPECT_EQ(flat_result.second, gold_result.second);
106     } else {
107       flat_set.erase(val);
108       gold_set.erase(val);
109     }
110     ASSERT_EQ(flat_set.size(), gold_set.size());
111   }
112 
113   EXPECT_THAT(flat_set, ElementsAreArray(gold_set));
114 }
115 
116 }  // namespace
117 }  // namespace base
118 }  // namespace perfetto
119