Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 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/tint/utils/unique_vector.h" |
| 16 | #include "src/tint/utils/reverse.h" |
| 17 | |
| 18 | #include "gtest/gtest.h" |
| 19 | |
dan sinclair | fe4bfd4 | 2022-04-07 16:55:55 +0000 | [diff] [blame] | 20 | namespace tint::utils { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | TEST(UniqueVectorTest, Empty) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 24 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | TEST(UniqueVectorTest, MoveConstructor) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 31 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | TEST(UniqueVectorTest, AddUnique) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 41 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | TEST(UniqueVectorTest, AddDuplicates) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 62 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | TEST(UniqueVectorTest, AsVector) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 86 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 93 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 94 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | TEST(UniqueVectorTest, PopBack) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 109 | UniqueVector<int> unique_vec; |
| 110 | unique_vec.add(0); |
| 111 | unique_vec.add(2); |
| 112 | unique_vec.add(1); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 113 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 114 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 119 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 120 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 124 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 125 | unique_vec.add(1); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 126 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 127 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 131 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 132 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 136 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 137 | EXPECT_EQ(unique_vec.pop_back(), 0); |
| 138 | EXPECT_EQ(unique_vec.size(), 0u); |
| 139 | EXPECT_EQ(unique_vec.empty(), true); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Ben Clayton | d99af03 | 2022-05-20 09:03:50 +0000 | [diff] [blame^] | 142 | TEST(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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 151 | } // namespace |
dan sinclair | fe4bfd4 | 2022-04-07 16:55:55 +0000 | [diff] [blame] | 152 | } // namespace tint::utils |