1 // 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/utils/unique_vector.h"
16 #include "src/utils/reverse.h"
17
18 #include "gtest/gtest.h"
19
20 namespace tint {
21 namespace utils {
22 namespace {
23
TEST(UniqueVectorTest,Empty)24 TEST(UniqueVectorTest, Empty) {
25 UniqueVector<int> unique_vec;
26 EXPECT_EQ(unique_vec.size(), 0u);
27 EXPECT_EQ(unique_vec.empty(), true);
28 EXPECT_EQ(unique_vec.begin(), unique_vec.end());
29 }
30
TEST(UniqueVectorTest,MoveConstructor)31 TEST(UniqueVectorTest, MoveConstructor) {
32 UniqueVector<int> unique_vec(std::vector<int>{0, 3, 2, 1, 2});
33 EXPECT_EQ(unique_vec.size(), 4u);
34 EXPECT_EQ(unique_vec.empty(), false);
35 EXPECT_EQ(unique_vec[0], 0);
36 EXPECT_EQ(unique_vec[1], 3);
37 EXPECT_EQ(unique_vec[2], 2);
38 EXPECT_EQ(unique_vec[3], 1);
39 }
40
TEST(UniqueVectorTest,AddUnique)41 TEST(UniqueVectorTest, AddUnique) {
42 UniqueVector<int> unique_vec;
43 unique_vec.add(0);
44 unique_vec.add(1);
45 unique_vec.add(2);
46 EXPECT_EQ(unique_vec.size(), 3u);
47 EXPECT_EQ(unique_vec.empty(), false);
48 int i = 0;
49 for (auto n : unique_vec) {
50 EXPECT_EQ(n, i);
51 i++;
52 }
53 for (auto n : Reverse(unique_vec)) {
54 i--;
55 EXPECT_EQ(n, i);
56 }
57 EXPECT_EQ(unique_vec[0], 0);
58 EXPECT_EQ(unique_vec[1], 1);
59 EXPECT_EQ(unique_vec[2], 2);
60 }
61
TEST(UniqueVectorTest,AddDuplicates)62 TEST(UniqueVectorTest, AddDuplicates) {
63 UniqueVector<int> unique_vec;
64 unique_vec.add(0);
65 unique_vec.add(0);
66 unique_vec.add(0);
67 unique_vec.add(1);
68 unique_vec.add(1);
69 unique_vec.add(2);
70 EXPECT_EQ(unique_vec.size(), 3u);
71 EXPECT_EQ(unique_vec.empty(), false);
72 int i = 0;
73 for (auto n : unique_vec) {
74 EXPECT_EQ(n, i);
75 i++;
76 }
77 for (auto n : Reverse(unique_vec)) {
78 i--;
79 EXPECT_EQ(n, i);
80 }
81 EXPECT_EQ(unique_vec[0], 0);
82 EXPECT_EQ(unique_vec[1], 1);
83 EXPECT_EQ(unique_vec[2], 2);
84 }
85
TEST(UniqueVectorTest,AsVector)86 TEST(UniqueVectorTest, AsVector) {
87 UniqueVector<int> unique_vec;
88 unique_vec.add(0);
89 unique_vec.add(0);
90 unique_vec.add(0);
91 unique_vec.add(1);
92 unique_vec.add(1);
93 unique_vec.add(2);
94
95 const std::vector<int>& vec = unique_vec;
96 EXPECT_EQ(vec.size(), 3u);
97 EXPECT_EQ(unique_vec.empty(), false);
98 int i = 0;
99 for (auto n : vec) {
100 EXPECT_EQ(n, i);
101 i++;
102 }
103 for (auto n : Reverse(unique_vec)) {
104 i--;
105 EXPECT_EQ(n, i);
106 }
107 }
108
TEST(UniqueVectorTest,PopBack)109 TEST(UniqueVectorTest, PopBack) {
110 UniqueVector<int> unique_vec;
111 unique_vec.add(0);
112 unique_vec.add(2);
113 unique_vec.add(1);
114
115 EXPECT_EQ(unique_vec.pop_back(), 1);
116 EXPECT_EQ(unique_vec.size(), 2u);
117 EXPECT_EQ(unique_vec.empty(), false);
118 EXPECT_EQ(unique_vec[0], 0);
119 EXPECT_EQ(unique_vec[1], 2);
120
121 EXPECT_EQ(unique_vec.pop_back(), 2);
122 EXPECT_EQ(unique_vec.size(), 1u);
123 EXPECT_EQ(unique_vec.empty(), false);
124 EXPECT_EQ(unique_vec[0], 0);
125
126 unique_vec.add(1);
127
128 EXPECT_EQ(unique_vec.size(), 2u);
129 EXPECT_EQ(unique_vec.empty(), false);
130 EXPECT_EQ(unique_vec[0], 0);
131 EXPECT_EQ(unique_vec[1], 1);
132
133 EXPECT_EQ(unique_vec.pop_back(), 1);
134 EXPECT_EQ(unique_vec.size(), 1u);
135 EXPECT_EQ(unique_vec.empty(), false);
136 EXPECT_EQ(unique_vec[0], 0);
137
138 EXPECT_EQ(unique_vec.pop_back(), 0);
139 EXPECT_EQ(unique_vec.size(), 0u);
140 EXPECT_EQ(unique_vec.empty(), true);
141 }
142
143 } // namespace
144 } // namespace utils
145 } // namespace tint
146