1 // Copyright (C) 2014 The Android Open Source Project
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include "emugl/common/unique_integer_map.h"
17 #include <gtest/gtest.h>
23 typedef UniqueIntegerMap<uintptr_t,uint32_t> MyMap;
25 TEST(UniqueIntegerMap, Empty) {
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));
36 TEST(UniqueIntegerMap, AddOne) {
39 uint32_t val1 = map.add(key1);
42 EXPECT_EQ(val1, map.find(key1));
43 EXPECT_EQ(key1, map.findKeyFor(val1));
45 EXPECT_FALSE(map.empty());
46 EXPECT_EQ(1U, map.size());
48 EXPECT_EQ(0U, map.find(0));
49 EXPECT_EQ(0U, map.findKeyFor(0));
51 EXPECT_EQ(0U, map.find(key1 + 1));
52 EXPECT_EQ(0U, map.findKeyFor(val1 + 1));
55 TEST(UniqueIntegerMap, AddMultiple) {
57 const size_t kCount = 100;
58 const size_t kKeyMultiplier = 3U; // must be >= 2.
59 uint32_t values[kCount];
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;
67 EXPECT_EQ(kCount, map.size());
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;
75 for (size_t n = 0; n < kCount; ++n) {
76 uintptr_t key = 1U + n * kKeyMultiplier;
77 EXPECT_EQ(key, map.findKeyFor(values[n]));
81 TEST(UniqueIntegerMap, Del) {
83 const size_t kCount = 100;
84 const size_t kKeyMultiplier = 3U; // must be >= 2.
85 uint32_t values[kCount];
87 for (size_t n = 0; n < kCount; ++n) {
88 uintptr_t key = 1U + n * kKeyMultiplier;
89 values[n] = map.add(key);
92 for (size_t n = 0; n < kCount; ++n) {
93 uintptr_t key = 1U + n * kKeyMultiplier;
95 EXPECT_EQ(kCount - 1U - n, map.size());
96 EXPECT_EQ(0U, map.find(key));
97 EXPECT_EQ(0U, map.findKeyFor(values[n]));
100 EXPECT_TRUE(map.empty());