1 /*
2 * Copyright (C) 2023 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 "src/shared_lib/intern_map.h"
18
19 #include "test/gtest_and_gmock.h"
20
21 namespace perfetto {
22 namespace {
23
TEST(InternMapTest,SmallValue)24 TEST(InternMapTest, SmallValue) {
25 const char kSmallValue[] = "A";
26 InternMap iids;
27
28 auto res1 = iids.FindOrAssign(/*type=*/0, kSmallValue, sizeof(kSmallValue));
29
30 EXPECT_TRUE(res1.newly_assigned);
31 EXPECT_NE(res1.iid, 0u);
32
33 auto res2 = iids.FindOrAssign(/*type=*/0, kSmallValue, sizeof(kSmallValue));
34
35 EXPECT_FALSE(res2.newly_assigned);
36 EXPECT_EQ(res1.iid, res1.iid);
37 }
38
TEST(InternMapTest,BigValue)39 TEST(InternMapTest, BigValue) {
40 const char kBigValue[] = "ABCDEFGHIJKLMNOP";
41 InternMap iids;
42
43 auto res1 = iids.FindOrAssign(/*type=*/0, kBigValue, sizeof(kBigValue));
44
45 EXPECT_TRUE(res1.newly_assigned);
46 EXPECT_NE(res1.iid, 0u);
47
48 auto res2 = iids.FindOrAssign(/*type=*/0, kBigValue, sizeof(kBigValue));
49
50 EXPECT_FALSE(res2.newly_assigned);
51 EXPECT_EQ(res1.iid, res1.iid);
52 }
53
TEST(InternMapTest,TwoValuesSameType)54 TEST(InternMapTest, TwoValuesSameType) {
55 const char kValue1[] = "A";
56 const char kValue2[] = "ABCDEFGHIJKLMNOP";
57 InternMap iids;
58
59 auto res1 = iids.FindOrAssign(/*type=*/0, kValue1, sizeof(kValue1));
60
61 EXPECT_TRUE(res1.newly_assigned);
62 EXPECT_NE(res1.iid, 0u);
63
64 auto res2 = iids.FindOrAssign(/*type=*/0, kValue2, sizeof(kValue2));
65
66 EXPECT_TRUE(res1.newly_assigned);
67 EXPECT_NE(res1.iid, res2.iid);
68 }
69
TEST(InternMapTest,SameValueDifferentTypes)70 TEST(InternMapTest, SameValueDifferentTypes) {
71 const char kValue[] = "A";
72 InternMap iids;
73
74 auto res1 = iids.FindOrAssign(/*type=*/0, kValue, sizeof(kValue));
75
76 EXPECT_TRUE(res1.newly_assigned);
77 EXPECT_NE(res1.iid, 0u);
78
79 auto res2 = iids.FindOrAssign(/*type=*/1, kValue, sizeof(kValue));
80
81 EXPECT_TRUE(res1.newly_assigned);
82 EXPECT_NE(res2.iid, 0u);
83 }
84
85 } // namespace
86 } // namespace perfetto
87