blob: f785889c35ec26862ec6ff83a955a54797384fce [file] [log] [blame]
Austin Engcc2516a2023-10-17 20:57:54 +00001// Copyright 2017 The Dawn & Tint Authors
Corentin Wallez38246eb2017-06-15 13:08:25 -04002//
Austin Engcc2516a2023-10-17 20:57:54 +00003// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are met:
Corentin Wallez38246eb2017-06-15 13:08:25 -04005//
Austin Engcc2516a2023-10-17 20:57:54 +00006// 1. Redistributions of source code must retain the above copyright notice, this
7// list of conditions and the following disclaimer.
Corentin Wallez38246eb2017-06-15 13:08:25 -04008//
Austin Engcc2516a2023-10-17 20:57:54 +00009// 2. Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12//
13// 3. Neither the name of the copyright holder nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Corentin Wallez38246eb2017-06-15 13:08:25 -040027
dan sinclair13e96992022-04-20 00:58:34 +000028#include <utility>
29#include <vector>
Corentin Wallez38246eb2017-06-15 13:08:25 -040030
Ben Claytond9ab69a2022-02-04 12:51:25 +000031#include "dawn/common/SerialQueue.h"
32#include "dawn/common/TypedInteger.h"
dan sinclair13e96992022-04-20 00:58:34 +000033#include "gtest/gtest.h"
Corentin Wallez38246eb2017-06-15 13:08:25 -040034
Austin Eng2731b762023-05-17 18:41:02 +000035namespace dawn {
36namespace {
37
Corentin Wallez145f1152020-09-28 12:28:13 +000038using TestSerialQueue = SerialQueue<uint64_t, int>;
Corentin Wallez38246eb2017-06-15 13:08:25 -040039
40// A number of basic tests for SerialQueue that are difficult to split from one another
41TEST(SerialQueue, BasicTest) {
Corentin Wallezfd589f32017-07-10 13:46:05 -040042 TestSerialQueue queue;
Corentin Wallez38246eb2017-06-15 13:08:25 -040043
44 // Queue starts empty
45 ASSERT_TRUE(queue.Empty());
46
47 // Iterating on empty queue 1) works 2) doesn't produce any values
Austin Eng8eb36b42024-04-02 13:01:27 +000048 for ([[maybe_unused]] int value : queue.IterateAll()) {
Corentin Wallez38246eb2017-06-15 13:08:25 -040049 ASSERT_TRUE(false);
50 }
51
52 // Enqueuing values as const ref or rvalue ref
53 queue.Enqueue(1, 0);
54 queue.Enqueue(2, 0);
55 queue.Enqueue(std::move(3), 1);
56
57 // Iterating over a non-empty queue produces the expected result
58 std::vector<int> expectedValues = {1, 2, 3};
59 for (int value : queue.IterateAll()) {
60 EXPECT_EQ(expectedValues.front(), value);
61 ASSERT_FALSE(expectedValues.empty());
62 expectedValues.erase(expectedValues.begin());
63 }
64 ASSERT_TRUE(expectedValues.empty());
65
66 // Clear works and makes the queue empty and iteration does nothing.
67 queue.Clear();
68 ASSERT_TRUE(queue.Empty());
69
Austin Eng8eb36b42024-04-02 13:01:27 +000070 for ([[maybe_unused]] int value : queue.IterateAll()) {
Corentin Wallez38246eb2017-06-15 13:08:25 -040071 ASSERT_TRUE(false);
72 }
73}
74
75// Test enqueuing vectors works
76TEST(SerialQueue, EnqueueVectors) {
Corentin Wallezfd589f32017-07-10 13:46:05 -040077 TestSerialQueue queue;
Corentin Wallez38246eb2017-06-15 13:08:25 -040078
79 std::vector<int> vector1 = {1, 2, 3, 4};
80 std::vector<int> vector2 = {5, 6, 7, 8};
81 std::vector<int> vector3 = {9, 0};
82
83 queue.Enqueue(vector1, 0);
84 queue.Enqueue(std::move(vector2), 0);
85 queue.Enqueue(vector3, 1);
86
87 std::vector<int> expectedValues = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
88 for (int value : queue.IterateAll()) {
89 EXPECT_EQ(expectedValues.front(), value);
90 ASSERT_FALSE(expectedValues.empty());
91 expectedValues.erase(expectedValues.begin());
92 }
93 ASSERT_TRUE(expectedValues.empty());
94}
95
96// Test IterateUpTo
97TEST(SerialQueue, IterateUpTo) {
Corentin Wallezfd589f32017-07-10 13:46:05 -040098 TestSerialQueue queue;
Corentin Wallez38246eb2017-06-15 13:08:25 -040099
100 std::vector<int> vector1 = {1, 2, 3, 4};
101 std::vector<int> vector2 = {5, 6, 7, 8};
102 std::vector<int> vector3 = {9, 0};
103
104 queue.Enqueue(vector1, 0);
105 queue.Enqueue(std::move(vector2), 1);
106 queue.Enqueue(vector3, 2);
107
108 std::vector<int> expectedValues = {1, 2, 3, 4, 5, 6, 7, 8};
109 for (int value : queue.IterateUpTo(1)) {
110 EXPECT_EQ(expectedValues.front(), value);
111 ASSERT_FALSE(expectedValues.empty());
112 expectedValues.erase(expectedValues.begin());
113 }
114 ASSERT_TRUE(expectedValues.empty());
Bryan Bernhart74e95ff2019-01-29 00:10:07 +0000115 EXPECT_EQ(queue.LastSerial(), 2u);
Corentin Wallez38246eb2017-06-15 13:08:25 -0400116}
117
118// Test ClearUpTo
119TEST(SerialQueue, ClearUpTo) {
Corentin Wallezfd589f32017-07-10 13:46:05 -0400120 TestSerialQueue queue;
Corentin Wallez38246eb2017-06-15 13:08:25 -0400121
122 std::vector<int> vector1 = {1, 2, 3, 4};
123 std::vector<int> vector2 = {5, 6, 7, 8};
124 std::vector<int> vector3 = {9, 0};
125
126 queue.Enqueue(vector1, 0);
127 queue.Enqueue(std::move(vector2), 0);
128 queue.Enqueue(vector3, 1);
129
130 queue.ClearUpTo(0);
Bryan Bernhart74e95ff2019-01-29 00:10:07 +0000131 EXPECT_EQ(queue.LastSerial(), 1u);
Corentin Wallez38246eb2017-06-15 13:08:25 -0400132
133 std::vector<int> expectedValues = {9, 0};
134 for (int value : queue.IterateAll()) {
135 EXPECT_EQ(expectedValues.front(), value);
136 ASSERT_FALSE(expectedValues.empty());
137 expectedValues.erase(expectedValues.begin());
138 }
139 ASSERT_TRUE(expectedValues.empty());
140}
Austin Enge480a072017-06-19 14:18:49 -0400141
142// Test FirstSerial
143TEST(SerialQueue, FirstSerial) {
Corentin Wallezfd589f32017-07-10 13:46:05 -0400144 TestSerialQueue queue;
Austin Enge480a072017-06-19 14:18:49 -0400145
146 std::vector<int> vector1 = {1, 2, 3, 4};
147 std::vector<int> vector2 = {5, 6, 7, 8};
148 std::vector<int> vector3 = {9, 0};
149
150 queue.Enqueue(vector1, 0);
151 queue.Enqueue(std::move(vector2), 1);
152 queue.Enqueue(vector3, 2);
153
Corentin Walleze046e6b2018-08-03 15:56:01 +0200154 EXPECT_EQ(queue.FirstSerial(), 0u);
Austin Enge480a072017-06-19 14:18:49 -0400155
156 queue.ClearUpTo(1);
Corentin Walleze046e6b2018-08-03 15:56:01 +0200157 EXPECT_EQ(queue.FirstSerial(), 2u);
Austin Enge480a072017-06-19 14:18:49 -0400158
159 queue.Clear();
160 queue.Enqueue(vector1, 6);
Corentin Walleze046e6b2018-08-03 15:56:01 +0200161 EXPECT_EQ(queue.FirstSerial(), 6u);
Austin Enge480a072017-06-19 14:18:49 -0400162}
Bryan Bernhart74e95ff2019-01-29 00:10:07 +0000163
164// Test LastSerial
165TEST(SerialQueue, LastSerial) {
166 TestSerialQueue queue;
167
168 queue.Enqueue({1}, 0);
169 EXPECT_EQ(queue.LastSerial(), 0u);
170
171 queue.Enqueue({2}, 1);
172 EXPECT_EQ(queue.LastSerial(), 1u);
Kai Ninomiya2afea0c2020-07-10 20:33:08 +0000173}
Corentin Wallez145f1152020-09-28 12:28:13 +0000174
175// Test basic functionality with type integers
176TEST(SerialQueue, TypedInteger) {
177 using MySerial = TypedInteger<struct MySerialT, uint64_t>;
178 using MySerialQueue = SerialQueue<MySerial, int>;
179
180 MySerialQueue queue;
181 queue.Enqueue(1, MySerial(0));
182 queue.Enqueue(2, MySerial(0));
183
184 std::vector<int> expectedValues = {1, 2};
185 for (int value : queue.IterateAll()) {
186 EXPECT_EQ(expectedValues.front(), value);
187 ASSERT_FALSE(expectedValues.empty());
188 expectedValues.erase(expectedValues.begin());
189 }
190 ASSERT_TRUE(expectedValues.empty());
191}
Austin Eng2731b762023-05-17 18:41:02 +0000192
193} // anonymous namespace
194} // namespace dawn