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/id_to_object_map.h"
17 #include <gtest/gtest.h>
23 typedef IdToObjectMapBase::KeyType KeyType;
28 Foo(int val) : mVal(val) {}
30 int val() const { return mVal; }
31 void setVal(int val) { mVal = val; }
38 TEST(IdToObjectMap, Empty) {
39 IdToObjectMap<Foo> map;
40 EXPECT_TRUE(map.empty());
41 EXPECT_EQ(0U, map.size());
44 TEST(IdToObjectMap, SetIntegerRange) {
45 IdToObjectMap<Foo> map;
48 // Add all items in the map.
49 for (KeyType n = 0; n < kMax; ++n) {
50 EXPECT_FALSE(map.set(n, new Foo(n))) << "For key " << n;
54 EXPECT_EQ(static_cast<size_t>(kMax), map.size());
56 // Find all items in the map.
57 for (KeyType n = 0; n < kMax; ++n) {
58 EXPECT_TRUE(map.contains(n)) << "For key " << n;
60 EXPECT_TRUE(map.find(n, &foo)) << "For key " << n;
62 EXPECT_EQ(static_cast<int>(n), foo->val()) << "For key " << n;
67 TEST(IdToObjectMap, RemoveAll) {
68 IdToObjectMap<Foo> map;
71 // Add all items in the map.
72 for (KeyType n = 0; n < kMax; ++n) {
73 EXPECT_FALSE(map.set(n, new Foo(n))) << "For key " << n;
76 EXPECT_EQ(static_cast<size_t>(kMax), map.size());
78 for (KeyType n = 0; n < kMax; ++n) {
79 EXPECT_TRUE(map.remove(n)) << "For key " << n;
81 EXPECT_EQ(0U, map.size());
84 TEST(IdToObjectMap, RemoveOdd) {
85 IdToObjectMap<Foo> map;
88 // Add all items in the map.
89 for (KeyType n = 0; n < kMax; ++n) {
90 EXPECT_FALSE(map.set(n, new Foo(n))) << "For key " << n;
93 EXPECT_EQ(static_cast<size_t>(kMax), map.size());
95 for (KeyType n = 0; n < kMax; ++n) {
97 EXPECT_TRUE(map.remove(n)) << "For key " << n;
100 EXPECT_EQ(static_cast<size_t>(kMax / 2), map.size());
102 for (KeyType n = 0; n < kMax; ++n) {
104 EXPECT_FALSE(map.contains(n)) << "For key " << n;
106 EXPECT_TRUE(map.contains(n)) << "For key " << n;
108 EXPECT_TRUE(map.find(n, &foo)) << "For key " << n;
110 EXPECT_EQ(static_cast<int>(n), foo->val());