blob: 035ebf8de726ffcf68ad1ef7def5b6749319a2bc [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001// Copyright 2021 The Tint 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 "src/tint/utils/unique_vector.h"
16#include "src/tint/utils/reverse.h"
17
18#include "gtest/gtest.h"
19
dan sinclairfe4bfd42022-04-07 16:55:55 +000020namespace tint::utils {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000021namespace {
22
23TEST(UniqueVectorTest, Empty) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000024 UniqueVector<int> unique_vec;
25 EXPECT_EQ(unique_vec.size(), 0u);
26 EXPECT_EQ(unique_vec.empty(), true);
27 EXPECT_EQ(unique_vec.begin(), unique_vec.end());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000028}
29
30TEST(UniqueVectorTest, MoveConstructor) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000031 UniqueVector<int> unique_vec(std::vector<int>{0, 3, 2, 1, 2});
32 EXPECT_EQ(unique_vec.size(), 4u);
33 EXPECT_EQ(unique_vec.empty(), false);
34 EXPECT_EQ(unique_vec[0], 0);
35 EXPECT_EQ(unique_vec[1], 3);
36 EXPECT_EQ(unique_vec[2], 2);
37 EXPECT_EQ(unique_vec[3], 1);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000038}
39
40TEST(UniqueVectorTest, AddUnique) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000041 UniqueVector<int> unique_vec;
42 unique_vec.add(0);
43 unique_vec.add(1);
44 unique_vec.add(2);
45 EXPECT_EQ(unique_vec.size(), 3u);
46 EXPECT_EQ(unique_vec.empty(), false);
47 int i = 0;
48 for (auto n : unique_vec) {
49 EXPECT_EQ(n, i);
50 i++;
51 }
52 for (auto n : Reverse(unique_vec)) {
53 i--;
54 EXPECT_EQ(n, i);
55 }
56 EXPECT_EQ(unique_vec[0], 0);
57 EXPECT_EQ(unique_vec[1], 1);
58 EXPECT_EQ(unique_vec[2], 2);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000059}
60
61TEST(UniqueVectorTest, AddDuplicates) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000062 UniqueVector<int> unique_vec;
63 unique_vec.add(0);
64 unique_vec.add(0);
65 unique_vec.add(0);
66 unique_vec.add(1);
67 unique_vec.add(1);
68 unique_vec.add(2);
69 EXPECT_EQ(unique_vec.size(), 3u);
70 EXPECT_EQ(unique_vec.empty(), false);
71 int i = 0;
72 for (auto n : unique_vec) {
73 EXPECT_EQ(n, i);
74 i++;
75 }
76 for (auto n : Reverse(unique_vec)) {
77 i--;
78 EXPECT_EQ(n, i);
79 }
80 EXPECT_EQ(unique_vec[0], 0);
81 EXPECT_EQ(unique_vec[1], 1);
82 EXPECT_EQ(unique_vec[2], 2);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000083}
84
85TEST(UniqueVectorTest, AsVector) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000086 UniqueVector<int> unique_vec;
87 unique_vec.add(0);
88 unique_vec.add(0);
89 unique_vec.add(0);
90 unique_vec.add(1);
91 unique_vec.add(1);
92 unique_vec.add(2);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000093
dan sinclair41e4d9a2022-05-01 14:40:55 +000094 const std::vector<int>& vec = unique_vec;
95 EXPECT_EQ(vec.size(), 3u);
96 EXPECT_EQ(unique_vec.empty(), false);
97 int i = 0;
98 for (auto n : vec) {
99 EXPECT_EQ(n, i);
100 i++;
101 }
102 for (auto n : Reverse(unique_vec)) {
103 i--;
104 EXPECT_EQ(n, i);
105 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000106}
107
108TEST(UniqueVectorTest, PopBack) {
dan sinclair41e4d9a2022-05-01 14:40:55 +0000109 UniqueVector<int> unique_vec;
110 unique_vec.add(0);
111 unique_vec.add(2);
112 unique_vec.add(1);
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000113
dan sinclair41e4d9a2022-05-01 14:40:55 +0000114 EXPECT_EQ(unique_vec.pop_back(), 1);
115 EXPECT_EQ(unique_vec.size(), 2u);
116 EXPECT_EQ(unique_vec.empty(), false);
117 EXPECT_EQ(unique_vec[0], 0);
118 EXPECT_EQ(unique_vec[1], 2);
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000119
dan sinclair41e4d9a2022-05-01 14:40:55 +0000120 EXPECT_EQ(unique_vec.pop_back(), 2);
121 EXPECT_EQ(unique_vec.size(), 1u);
122 EXPECT_EQ(unique_vec.empty(), false);
123 EXPECT_EQ(unique_vec[0], 0);
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000124
dan sinclair41e4d9a2022-05-01 14:40:55 +0000125 unique_vec.add(1);
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000126
dan sinclair41e4d9a2022-05-01 14:40:55 +0000127 EXPECT_EQ(unique_vec.size(), 2u);
128 EXPECT_EQ(unique_vec.empty(), false);
129 EXPECT_EQ(unique_vec[0], 0);
130 EXPECT_EQ(unique_vec[1], 1);
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000131
dan sinclair41e4d9a2022-05-01 14:40:55 +0000132 EXPECT_EQ(unique_vec.pop_back(), 1);
133 EXPECT_EQ(unique_vec.size(), 1u);
134 EXPECT_EQ(unique_vec.empty(), false);
135 EXPECT_EQ(unique_vec[0], 0);
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000136
dan sinclair41e4d9a2022-05-01 14:40:55 +0000137 EXPECT_EQ(unique_vec.pop_back(), 0);
138 EXPECT_EQ(unique_vec.size(), 0u);
139 EXPECT_EQ(unique_vec.empty(), true);
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000140}
141
Ben Claytond99af032022-05-20 09:03:50 +0000142TEST(UniqueVectorTest, Data) {
143 UniqueVector<int> unique_vec;
144 EXPECT_EQ(unique_vec.data(), nullptr);
145
146 unique_vec.add(42);
147 EXPECT_EQ(unique_vec.data(), &unique_vec[0]);
148 EXPECT_EQ(*unique_vec.data(), 42);
149}
150
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000151} // namespace
dan sinclairfe4bfd42022-04-07 16:55:55 +0000152} // namespace tint::utils