1 //
2 // Copyright 2018 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // ResourceMap_unittest:
7 // Unit tests for the ResourceMap template class.
8 //
9
10 #include <gtest/gtest.h>
11
12 #include "libANGLE/ResourceMap.h"
13
14 using namespace gl;
15
16 namespace
17 {
18 // Tests assigning slots in the map and then deleting elements.
TEST(ResourceMapTest,AssignAndErase)19 TEST(ResourceMapTest, AssignAndErase)
20 {
21 constexpr size_t kSize = 64;
22 ResourceMap<size_t, GLuint> resourceMap;
23 std::vector<size_t> objects(kSize, 1);
24 for (size_t index = 0; index < kSize; ++index)
25 {
26 resourceMap.assign(index + 1, &objects[index]);
27 }
28
29 for (size_t index = 0; index < kSize; ++index)
30 {
31 size_t *found = nullptr;
32 ASSERT_TRUE(resourceMap.erase(index + 1, &found));
33 ASSERT_EQ(&objects[index], found);
34 }
35
36 ASSERT_TRUE(resourceMap.empty());
37 }
38
39 // Tests assigning slots in the map and then using clear() to free it.
TEST(ResourceMapTest,AssignAndClear)40 TEST(ResourceMapTest, AssignAndClear)
41 {
42 constexpr size_t kSize = 64;
43 ResourceMap<size_t, GLuint> resourceMap;
44 std::vector<size_t> objects(kSize, 1);
45 for (size_t index = 0; index < kSize; ++index)
46 {
47 resourceMap.assign(index + 1, &objects[index]);
48 }
49
50 resourceMap.clear();
51 ASSERT_TRUE(resourceMap.empty());
52 }
53
54 // Tests growing a map more than double the size.
TEST(ResourceMapTest,BigGrowth)55 TEST(ResourceMapTest, BigGrowth)
56 {
57 constexpr size_t kSize = 8;
58
59 ResourceMap<size_t, GLuint> resourceMap;
60 std::vector<size_t> objects;
61
62 for (size_t index = 0; index < kSize; ++index)
63 {
64 objects.push_back(index);
65 }
66
67 // Assign a large value.
68 constexpr size_t kLargeIndex = 128;
69 objects.push_back(kLargeIndex);
70
71 for (size_t &object : objects)
72 {
73 resourceMap.assign(object, &object);
74 }
75
76 for (size_t object : objects)
77 {
78 size_t *found = nullptr;
79 ASSERT_TRUE(resourceMap.erase(object, &found));
80 ASSERT_EQ(object, *found);
81 }
82
83 ASSERT_TRUE(resourceMap.empty());
84 }
85
86 // Tests querying unassigned or erased values.
TEST(ResourceMapTest,QueryUnassigned)87 TEST(ResourceMapTest, QueryUnassigned)
88 {
89 constexpr size_t kSize = 8;
90
91 ResourceMap<size_t, GLuint> resourceMap;
92 std::vector<size_t> objects;
93
94 for (size_t index = 0; index < kSize; ++index)
95 {
96 objects.push_back(index);
97 }
98
99 ASSERT_FALSE(resourceMap.contains(0));
100 ASSERT_EQ(nullptr, resourceMap.query(0));
101 ASSERT_FALSE(resourceMap.contains(100));
102 ASSERT_EQ(nullptr, resourceMap.query(100));
103
104 for (size_t &object : objects)
105 {
106 resourceMap.assign(object, &object);
107 }
108
109 ASSERT_FALSE(resourceMap.empty());
110
111 for (size_t &object : objects)
112 {
113 ASSERT_TRUE(resourceMap.contains(object));
114 ASSERT_EQ(&object, resourceMap.query(object));
115 }
116
117 ASSERT_FALSE(resourceMap.contains(10));
118 ASSERT_EQ(nullptr, resourceMap.query(10));
119 ASSERT_FALSE(resourceMap.contains(100));
120 ASSERT_EQ(nullptr, resourceMap.query(100));
121
122 for (size_t object : objects)
123 {
124 size_t *found = nullptr;
125 ASSERT_TRUE(resourceMap.erase(object, &found));
126 ASSERT_EQ(object, *found);
127 }
128
129 ASSERT_TRUE(resourceMap.empty());
130
131 ASSERT_FALSE(resourceMap.contains(0));
132 ASSERT_EQ(nullptr, resourceMap.query(0));
133 ASSERT_FALSE(resourceMap.contains(100));
134 ASSERT_EQ(nullptr, resourceMap.query(100));
135 }
136 } // anonymous namespace
137