8aee013273eacc33cee02f3d56b036217a93739b
[iec.git] / src / type3_AndroidCloud / anbox-master / external / android-emugl / shared / emugl / common / unique_integer_map_unittest.cpp
1 // Copyright (C) 2014 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "emugl/common/unique_integer_map.h"
16
17 #include <gtest/gtest.h>
18
19 #include <stdio.h>
20
21 namespace emugl {
22
23 typedef UniqueIntegerMap<uintptr_t,uint32_t> MyMap;
24
25 TEST(UniqueIntegerMap, Empty) {
26     MyMap map;
27
28     EXPECT_TRUE(map.empty());
29     EXPECT_EQ(0U, map.size());
30     EXPECT_EQ(0U, map.find(0U));
31     EXPECT_EQ(0U, map.find(1U));
32     EXPECT_EQ(0U, map.find(2U));
33     EXPECT_EQ(0U, map.find(4U));
34 }
35
36 TEST(UniqueIntegerMap, AddOne) {
37     MyMap map;
38     uintptr_t key1 = 1U;
39     uint32_t val1 = map.add(key1);
40
41     EXPECT_NE(0U, val1);
42     EXPECT_EQ(val1, map.find(key1));
43     EXPECT_EQ(key1, map.findKeyFor(val1));
44
45     EXPECT_FALSE(map.empty());
46     EXPECT_EQ(1U, map.size());
47
48     EXPECT_EQ(0U, map.find(0));
49     EXPECT_EQ(0U, map.findKeyFor(0));
50
51     EXPECT_EQ(0U, map.find(key1 + 1));
52     EXPECT_EQ(0U, map.findKeyFor(val1 + 1));
53 }
54
55 TEST(UniqueIntegerMap, AddMultiple) {
56     MyMap map;
57     const size_t kCount = 100;
58     const size_t kKeyMultiplier = 3U;  // must be >= 2.
59     uint32_t values[kCount];
60
61     for (size_t n = 0; n < kCount; ++n) {
62         uintptr_t key = 1U + n * kKeyMultiplier;
63         values[n] = map.add(key);
64         EXPECT_NE(0U, values[n]) << "key #" << n;
65     }
66
67     EXPECT_EQ(kCount, map.size());
68
69     for (size_t n = 0; n < kCount; ++n) {
70         uintptr_t key = 1U + n * kKeyMultiplier;
71         EXPECT_EQ(values[n], map.find(key)) << "key #" << n;
72         EXPECT_EQ(0U, map.find(key + 1U)) << "key #" << n;
73     }
74
75     for (size_t n = 0; n < kCount; ++n) {
76         uintptr_t key = 1U + n * kKeyMultiplier;
77         EXPECT_EQ(key, map.findKeyFor(values[n]));
78     }
79 }
80
81 TEST(UniqueIntegerMap, Del) {
82     MyMap map;
83     const size_t kCount = 100;
84     const size_t kKeyMultiplier = 3U;  // must be >= 2.
85     uint32_t values[kCount];
86
87     for (size_t n = 0; n < kCount; ++n) {
88         uintptr_t key = 1U + n * kKeyMultiplier;
89         values[n] = map.add(key);
90     }
91
92     for (size_t n = 0; n < kCount; ++n) {
93         uintptr_t key = 1U + n * kKeyMultiplier;
94         map.del(key);
95         EXPECT_EQ(kCount - 1U - n, map.size());
96         EXPECT_EQ(0U, map.find(key));
97         EXPECT_EQ(0U, map.findKeyFor(values[n]));
98     }
99
100     EXPECT_TRUE(map.empty());
101 }
102
103 }  // namespace emugl