1 // Copyright 2018 The Dawn Authors
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 <gtest/gtest.h>
16
17 #include "common/SerialMap.h"
18 #include "common/TypedInteger.h"
19
20 using TestSerialMap = SerialMap<uint64_t, int>;
21
22 // A number of basic tests for SerialMap that are difficult to split from one another
TEST(SerialMap,BasicTest)23 TEST(SerialMap, BasicTest) {
24 TestSerialMap map;
25
26 // Map starts empty
27 ASSERT_TRUE(map.Empty());
28
29 // Iterating on empty map 1) works 2) doesn't produce any values
30 for (int value : map.IterateAll()) {
31 DAWN_UNUSED(value);
32 ASSERT_TRUE(false);
33 }
34
35 // Enqueuing values as const ref or rvalue ref
36 map.Enqueue(1, 0);
37 map.Enqueue(2, 0);
38 map.Enqueue(std::move(3), 1);
39
40 // Iterating over a non-empty map produces the expected result
41 std::vector<int> expectedValues = {1, 2, 3};
42 for (int value : map.IterateAll()) {
43 EXPECT_EQ(expectedValues.front(), value);
44 ASSERT_FALSE(expectedValues.empty());
45 expectedValues.erase(expectedValues.begin());
46 }
47 ASSERT_TRUE(expectedValues.empty());
48
49 // Clear works and makes the map empty and iteration does nothing.
50 map.Clear();
51 ASSERT_TRUE(map.Empty());
52
53 for (int value : map.IterateAll()) {
54 DAWN_UNUSED(value);
55 ASSERT_TRUE(false);
56 }
57 }
58
59 // Test that items can be enqueued in an arbitrary order
TEST(SerialMap,EnqueueOrder)60 TEST(SerialMap, EnqueueOrder) {
61 TestSerialMap map;
62
63 // Enqueue values in an arbitrary order
64 map.Enqueue(3, 1);
65 map.Enqueue(1, 0);
66 map.Enqueue(4, 2);
67 map.Enqueue(5, 2);
68 map.Enqueue(2, 0);
69
70 // Iterating over a non-empty map produces the expected result
71 std::vector<int> expectedValues = {1, 2, 3, 4, 5};
72 for (int value : map.IterateAll()) {
73 EXPECT_EQ(expectedValues.front(), value);
74 ASSERT_FALSE(expectedValues.empty());
75 expectedValues.erase(expectedValues.begin());
76 }
77 ASSERT_TRUE(expectedValues.empty());
78 }
79
80 // Test enqueuing vectors works
TEST(SerialMap,EnqueueVectors)81 TEST(SerialMap, EnqueueVectors) {
82 TestSerialMap map;
83
84 std::vector<int> vector1 = {1, 2, 3, 4};
85 std::vector<int> vector2 = {5, 6, 7, 8};
86 std::vector<int> vector3 = {9, 0};
87
88 map.Enqueue(vector1, 0);
89 map.Enqueue(std::move(vector2), 0);
90 map.Enqueue(vector3, 1);
91
92 std::vector<int> expectedValues = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
93 for (int value : map.IterateAll()) {
94 EXPECT_EQ(expectedValues.front(), value);
95 ASSERT_FALSE(expectedValues.empty());
96 expectedValues.erase(expectedValues.begin());
97 }
98 ASSERT_TRUE(expectedValues.empty());
99 }
100
101 // Test IterateUpTo
TEST(SerialMap,IterateUpTo)102 TEST(SerialMap, IterateUpTo) {
103 TestSerialMap map;
104
105 std::vector<int> vector1 = {1, 2, 3, 4};
106 std::vector<int> vector2 = {5, 6, 7, 8};
107 std::vector<int> vector3 = {9, 0};
108
109 map.Enqueue(vector1, 0);
110 map.Enqueue(std::move(vector2), 1);
111 map.Enqueue(vector3, 2);
112
113 std::vector<int> expectedValues = {1, 2, 3, 4, 5, 6, 7, 8};
114 for (int value : map.IterateUpTo(1)) {
115 EXPECT_EQ(expectedValues.front(), value);
116 ASSERT_FALSE(expectedValues.empty());
117 expectedValues.erase(expectedValues.begin());
118 }
119 ASSERT_TRUE(expectedValues.empty());
120 }
121
122 // Test ClearUpTo
TEST(SerialMap,ClearUpTo)123 TEST(SerialMap, ClearUpTo) {
124 TestSerialMap map;
125
126 std::vector<int> vector1 = {1, 2, 3, 4};
127 std::vector<int> vector2 = {5, 6, 7, 8};
128 std::vector<int> vector3 = {9, 0};
129
130 map.Enqueue(vector1, 0);
131 map.Enqueue(std::move(vector2), 0);
132 map.Enqueue(vector3, 1);
133
134 map.ClearUpTo(0);
135
136 std::vector<int> expectedValues = {9, 0};
137 for (int value : map.IterateAll()) {
138 EXPECT_EQ(expectedValues.front(), value);
139 ASSERT_FALSE(expectedValues.empty());
140 expectedValues.erase(expectedValues.begin());
141 }
142 ASSERT_TRUE(expectedValues.empty());
143 }
144
145 // Test FirstSerial
TEST(SerialMap,FirstSerial)146 TEST(SerialMap, FirstSerial) {
147 TestSerialMap map;
148
149 std::vector<int> vector1 = {1, 2, 3, 4};
150 std::vector<int> vector2 = {5, 6, 7, 8};
151 std::vector<int> vector3 = {9, 0};
152
153 map.Enqueue(vector1, 0);
154 map.Enqueue(std::move(vector2), 1);
155 map.Enqueue(vector3, 2);
156
157 EXPECT_EQ(map.FirstSerial(), 0u);
158
159 map.ClearUpTo(1);
160 EXPECT_EQ(map.FirstSerial(), 2u);
161
162 map.Clear();
163 map.Enqueue(vector1, 6);
164 EXPECT_EQ(map.FirstSerial(), 6u);
165 }
166
167 // Test basic functionality with type integers
TEST(SerialMap,TypedInteger)168 TEST(SerialMap, TypedInteger) {
169 using MySerial = TypedInteger<struct MySerialT, uint64_t>;
170 using MySerialMap = SerialMap<MySerial, int>;
171
172 MySerialMap map;
173 map.Enqueue(1, MySerial(0));
174 map.Enqueue(2, MySerial(0));
175
176 std::vector<int> expectedValues = {1, 2};
177 for (int value : map.IterateAll()) {
178 EXPECT_EQ(expectedValues.front(), value);
179 ASSERT_FALSE(expectedValues.empty());
180 expectedValues.erase(expectedValues.begin());
181 }
182 ASSERT_TRUE(expectedValues.empty());
183 }
184