Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include <algorithm> |
| 12 | #include <string> |
kwiberg | 529662a | 2017-09-04 05:43:17 -0700 | [diff] [blame] | 13 | #include <utility> |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "api/array_view.h" |
| 17 | #include "rtc_base/buffer.h" |
| 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/gunit.h" |
| 20 | #include "test/gmock.h" |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 21 | |
| 22 | namespace rtc { |
| 23 | |
| 24 | namespace { |
kwiberg | bd43172 | 2016-09-05 04:20:54 -0700 | [diff] [blame] | 25 | |
danilchap | a28780e | 2016-11-18 01:46:23 -0800 | [diff] [blame] | 26 | using ::testing::ElementsAre; |
| 27 | using ::testing::IsEmpty; |
| 28 | |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 29 | template <typename T> |
| 30 | void Call(ArrayView<T>) {} |
kwiberg | bd43172 | 2016-09-05 04:20:54 -0700 | [diff] [blame] | 31 | |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 32 | } // namespace |
| 33 | |
| 34 | TEST(ArrayViewTest, TestConstructFromPtrAndArray) { |
| 35 | char arr[] = "Arrr!"; |
| 36 | const char carr[] = "Carrr!"; |
| 37 | Call<const char>(arr); |
| 38 | Call<const char>(carr); |
| 39 | Call<char>(arr); |
| 40 | // Call<char>(carr); // Compile error, because can't drop const. |
| 41 | // Call<int>(arr); // Compile error, because incompatible types. |
| 42 | ArrayView<int*> x; |
| 43 | EXPECT_EQ(0u, x.size()); |
| 44 | EXPECT_EQ(nullptr, x.data()); |
| 45 | ArrayView<char> y = arr; |
| 46 | EXPECT_EQ(6u, y.size()); |
| 47 | EXPECT_EQ(arr, y.data()); |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 48 | ArrayView<char, 6> yf = arr; |
| 49 | static_assert(yf.size() == 6, ""); |
| 50 | EXPECT_EQ(arr, yf.data()); |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 51 | ArrayView<const char> z(arr + 1, 3); |
| 52 | EXPECT_EQ(3u, z.size()); |
| 53 | EXPECT_EQ(arr + 1, z.data()); |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 54 | ArrayView<const char, 3> zf(arr + 1, 3); |
| 55 | static_assert(zf.size() == 3, ""); |
| 56 | EXPECT_EQ(arr + 1, zf.data()); |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 57 | ArrayView<const char> w(arr, 2); |
| 58 | EXPECT_EQ(2u, w.size()); |
| 59 | EXPECT_EQ(arr, w.data()); |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 60 | ArrayView<const char, 2> wf(arr, 2); |
| 61 | static_assert(wf.size() == 2, ""); |
| 62 | EXPECT_EQ(arr, wf.data()); |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 63 | ArrayView<char> q(arr, 0); |
| 64 | EXPECT_EQ(0u, q.size()); |
| 65 | EXPECT_EQ(nullptr, q.data()); |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 66 | ArrayView<char, 0> qf(arr, 0); |
| 67 | static_assert(qf.size() == 0, ""); |
| 68 | EXPECT_EQ(nullptr, qf.data()); |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 69 | #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 70 | // DCHECK error (nullptr with nonzero size). |
| 71 | EXPECT_DEATH(ArrayView<int>(static_cast<int*>(nullptr), 5), ""); |
| 72 | #endif |
| 73 | // These are compile errors, because incompatible types. |
| 74 | // ArrayView<int> m = arr; |
| 75 | // ArrayView<float> n(arr + 2, 2); |
| 76 | } |
| 77 | |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 78 | TEST(ArrayViewTest, TestCopyConstructorVariable) { |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 79 | char arr[] = "Arrr!"; |
| 80 | ArrayView<char> x = arr; |
| 81 | EXPECT_EQ(6u, x.size()); |
| 82 | EXPECT_EQ(arr, x.data()); |
| 83 | ArrayView<char> y = x; // Copy non-const -> non-const. |
| 84 | EXPECT_EQ(6u, y.size()); |
| 85 | EXPECT_EQ(arr, y.data()); |
| 86 | ArrayView<const char> z = x; // Copy non-const -> const. |
| 87 | EXPECT_EQ(6u, z.size()); |
| 88 | EXPECT_EQ(arr, z.data()); |
| 89 | ArrayView<const char> w = z; // Copy const -> const. |
| 90 | EXPECT_EQ(6u, w.size()); |
| 91 | EXPECT_EQ(arr, w.data()); |
| 92 | // ArrayView<char> v = z; // Compile error, because can't drop const. |
| 93 | } |
| 94 | |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 95 | TEST(ArrayViewTest, TestCopyConstructorFixed) { |
| 96 | char arr[] = "Arrr!"; |
| 97 | ArrayView<char, 6> x = arr; |
| 98 | static_assert(x.size() == 6, ""); |
| 99 | EXPECT_EQ(arr, x.data()); |
| 100 | |
| 101 | // Copy fixed -> fixed. |
| 102 | ArrayView<char, 6> y = x; // Copy non-const -> non-const. |
| 103 | static_assert(y.size() == 6, ""); |
| 104 | EXPECT_EQ(arr, y.data()); |
| 105 | ArrayView<const char, 6> z = x; // Copy non-const -> const. |
| 106 | static_assert(z.size() == 6, ""); |
| 107 | EXPECT_EQ(arr, z.data()); |
| 108 | ArrayView<const char, 6> w = z; // Copy const -> const. |
| 109 | static_assert(w.size() == 6, ""); |
| 110 | EXPECT_EQ(arr, w.data()); |
| 111 | // ArrayView<char, 6> v = z; // Compile error, because can't drop const. |
| 112 | |
| 113 | // Copy fixed -> variable. |
| 114 | ArrayView<char> yv = x; // Copy non-const -> non-const. |
| 115 | EXPECT_EQ(6u, yv.size()); |
| 116 | EXPECT_EQ(arr, yv.data()); |
| 117 | ArrayView<const char> zv = x; // Copy non-const -> const. |
| 118 | EXPECT_EQ(6u, zv.size()); |
| 119 | EXPECT_EQ(arr, zv.data()); |
| 120 | ArrayView<const char> wv = z; // Copy const -> const. |
| 121 | EXPECT_EQ(6u, wv.size()); |
| 122 | EXPECT_EQ(arr, wv.data()); |
| 123 | // ArrayView<char> vv = z; // Compile error, because can't drop const. |
| 124 | } |
| 125 | |
| 126 | TEST(ArrayViewTest, TestCopyAssignmentVariable) { |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 127 | char arr[] = "Arrr!"; |
| 128 | ArrayView<char> x(arr); |
| 129 | EXPECT_EQ(6u, x.size()); |
| 130 | EXPECT_EQ(arr, x.data()); |
| 131 | ArrayView<char> y; |
| 132 | y = x; // Copy non-const -> non-const. |
| 133 | EXPECT_EQ(6u, y.size()); |
| 134 | EXPECT_EQ(arr, y.data()); |
| 135 | ArrayView<const char> z; |
| 136 | z = x; // Copy non-const -> const. |
| 137 | EXPECT_EQ(6u, z.size()); |
| 138 | EXPECT_EQ(arr, z.data()); |
| 139 | ArrayView<const char> w; |
| 140 | w = z; // Copy const -> const. |
| 141 | EXPECT_EQ(6u, w.size()); |
| 142 | EXPECT_EQ(arr, w.data()); |
| 143 | // ArrayView<char> v; |
| 144 | // v = z; // Compile error, because can't drop const. |
| 145 | } |
| 146 | |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 147 | TEST(ArrayViewTest, TestCopyAssignmentFixed) { |
| 148 | char arr[] = "Arrr!"; |
| 149 | char init[] = "Init!"; |
| 150 | ArrayView<char, 6> x(arr); |
| 151 | EXPECT_EQ(arr, x.data()); |
| 152 | |
| 153 | // Copy fixed -> fixed. |
| 154 | ArrayView<char, 6> y(init); |
| 155 | y = x; // Copy non-const -> non-const. |
| 156 | EXPECT_EQ(arr, y.data()); |
| 157 | ArrayView<const char, 6> z(init); |
| 158 | z = x; // Copy non-const -> const. |
| 159 | EXPECT_EQ(arr, z.data()); |
| 160 | ArrayView<const char, 6> w(init); |
| 161 | w = z; // Copy const -> const. |
| 162 | EXPECT_EQ(arr, w.data()); |
| 163 | // ArrayView<char, 6> v(init); |
| 164 | // v = z; // Compile error, because can't drop const. |
| 165 | |
| 166 | // Copy fixed -> variable. |
| 167 | ArrayView<char> yv; |
| 168 | yv = x; // Copy non-const -> non-const. |
| 169 | EXPECT_EQ(6u, yv.size()); |
| 170 | EXPECT_EQ(arr, yv.data()); |
| 171 | ArrayView<const char> zv; |
| 172 | zv = x; // Copy non-const -> const. |
| 173 | EXPECT_EQ(6u, zv.size()); |
| 174 | EXPECT_EQ(arr, zv.data()); |
| 175 | ArrayView<const char> wv; |
| 176 | wv = z; // Copy const -> const. |
| 177 | EXPECT_EQ(6u, wv.size()); |
| 178 | EXPECT_EQ(arr, wv.data()); |
| 179 | // ArrayView<char> v; |
| 180 | // v = z; // Compile error, because can't drop const. |
| 181 | } |
| 182 | |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 183 | TEST(ArrayViewTest, TestStdVector) { |
| 184 | std::vector<int> v; |
| 185 | v.push_back(3); |
| 186 | v.push_back(11); |
| 187 | Call<const int>(v); |
| 188 | Call<int>(v); |
| 189 | // Call<unsigned int>(v); // Compile error, because incompatible types. |
| 190 | ArrayView<int> x = v; |
| 191 | EXPECT_EQ(2u, x.size()); |
| 192 | EXPECT_EQ(v.data(), x.data()); |
| 193 | ArrayView<const int> y; |
| 194 | y = v; |
| 195 | EXPECT_EQ(2u, y.size()); |
| 196 | EXPECT_EQ(v.data(), y.data()); |
| 197 | // ArrayView<double> d = v; // Compile error, because incompatible types. |
| 198 | const std::vector<int> cv; |
| 199 | Call<const int>(cv); |
| 200 | // Call<int>(cv); // Compile error, because can't drop const. |
| 201 | ArrayView<const int> z = cv; |
| 202 | EXPECT_EQ(0u, z.size()); |
| 203 | EXPECT_EQ(nullptr, z.data()); |
| 204 | // ArrayView<int> w = cv; // Compile error, because can't drop const. |
| 205 | } |
| 206 | |
| 207 | TEST(ArrayViewTest, TestRtcBuffer) { |
| 208 | rtc::Buffer b = "so buffer"; |
| 209 | Call<const uint8_t>(b); |
| 210 | Call<uint8_t>(b); |
| 211 | // Call<int8_t>(b); // Compile error, because incompatible types. |
| 212 | ArrayView<uint8_t> x = b; |
| 213 | EXPECT_EQ(10u, x.size()); |
| 214 | EXPECT_EQ(b.data(), x.data()); |
| 215 | ArrayView<const uint8_t> y; |
| 216 | y = b; |
| 217 | EXPECT_EQ(10u, y.size()); |
| 218 | EXPECT_EQ(b.data(), y.data()); |
| 219 | // ArrayView<char> d = b; // Compile error, because incompatible types. |
| 220 | const rtc::Buffer cb = "very const"; |
| 221 | Call<const uint8_t>(cb); |
| 222 | // Call<uint8_t>(cb); // Compile error, because can't drop const. |
| 223 | ArrayView<const uint8_t> z = cb; |
| 224 | EXPECT_EQ(11u, z.size()); |
| 225 | EXPECT_EQ(cb.data(), z.data()); |
| 226 | // ArrayView<uint8_t> w = cb; // Compile error, because can't drop const. |
| 227 | } |
| 228 | |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 229 | TEST(ArrayViewTest, TestSwapVariable) { |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 230 | const char arr[] = "Arrr!"; |
| 231 | const char aye[] = "Aye, Cap'n!"; |
| 232 | ArrayView<const char> x(arr); |
| 233 | EXPECT_EQ(6u, x.size()); |
| 234 | EXPECT_EQ(arr, x.data()); |
| 235 | ArrayView<const char> y(aye); |
| 236 | EXPECT_EQ(12u, y.size()); |
| 237 | EXPECT_EQ(aye, y.data()); |
| 238 | using std::swap; |
| 239 | swap(x, y); |
| 240 | EXPECT_EQ(12u, x.size()); |
| 241 | EXPECT_EQ(aye, x.data()); |
| 242 | EXPECT_EQ(6u, y.size()); |
| 243 | EXPECT_EQ(arr, y.data()); |
| 244 | // ArrayView<char> z; |
| 245 | // swap(x, z); // Compile error, because can't drop const. |
| 246 | } |
| 247 | |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 248 | TEST(FixArrayViewTest, TestSwapFixed) { |
| 249 | const char arr[] = "Arr!"; |
| 250 | char aye[] = "Aye!"; |
| 251 | ArrayView<const char, 5> x(arr); |
| 252 | EXPECT_EQ(arr, x.data()); |
| 253 | ArrayView<const char, 5> y(aye); |
| 254 | EXPECT_EQ(aye, y.data()); |
| 255 | using std::swap; |
| 256 | swap(x, y); |
| 257 | EXPECT_EQ(aye, x.data()); |
| 258 | EXPECT_EQ(arr, y.data()); |
| 259 | // ArrayView<char, 5> z(aye); |
| 260 | // swap(x, z); // Compile error, because can't drop const. |
| 261 | // ArrayView<const char, 4> w(aye, 4); |
| 262 | // swap(x, w); // Compile error, because different sizes. |
| 263 | } |
| 264 | |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 265 | TEST(ArrayViewTest, TestIndexing) { |
| 266 | char arr[] = "abcdefg"; |
| 267 | ArrayView<char> x(arr); |
| 268 | const ArrayView<char> y(arr); |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 269 | ArrayView<const char, 8> z(arr); |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 270 | EXPECT_EQ(8u, x.size()); |
| 271 | EXPECT_EQ(8u, y.size()); |
| 272 | EXPECT_EQ(8u, z.size()); |
| 273 | EXPECT_EQ('b', x[1]); |
| 274 | EXPECT_EQ('c', y[2]); |
| 275 | EXPECT_EQ('d', z[3]); |
| 276 | x[3] = 'X'; |
| 277 | y[2] = 'Y'; |
| 278 | // z[1] = 'Z'; // Compile error, because z's element type is const char. |
| 279 | EXPECT_EQ('b', x[1]); |
| 280 | EXPECT_EQ('Y', y[2]); |
| 281 | EXPECT_EQ('X', z[3]); |
| 282 | #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 283 | EXPECT_DEATH(z[8], ""); // DCHECK error (index out of bounds). |
| 284 | #endif |
| 285 | } |
| 286 | |
| 287 | TEST(ArrayViewTest, TestIterationEmpty) { |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 288 | // Variable-size. |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 289 | ArrayView<std::vector<std::vector<std::vector<std::string>>>> av; |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 290 | EXPECT_EQ(av.begin(), av.end()); |
| 291 | EXPECT_EQ(av.cbegin(), av.cend()); |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 292 | for (auto& e : av) { |
| 293 | EXPECT_TRUE(false); |
| 294 | EXPECT_EQ(42u, e.size()); // Dummy use of e to prevent unused var warning. |
| 295 | } |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 296 | |
| 297 | // Fixed-size. |
| 298 | ArrayView<std::vector<std::vector<std::vector<std::string>>>, 0> af; |
| 299 | EXPECT_EQ(af.begin(), af.end()); |
| 300 | EXPECT_EQ(af.cbegin(), af.cend()); |
| 301 | for (auto& e : af) { |
| 302 | EXPECT_TRUE(false); |
| 303 | EXPECT_EQ(42u, e.size()); // Dummy use of e to prevent unused var warning. |
| 304 | } |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 305 | } |
| 306 | |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 307 | TEST(ArrayViewTest, TestIterationVariable) { |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 308 | char arr[] = "Arrr!"; |
| 309 | ArrayView<char> av(arr); |
| 310 | EXPECT_EQ('A', *av.begin()); |
| 311 | EXPECT_EQ('A', *av.cbegin()); |
| 312 | EXPECT_EQ('\0', *(av.end() - 1)); |
| 313 | EXPECT_EQ('\0', *(av.cend() - 1)); |
| 314 | char i = 0; |
| 315 | for (auto& e : av) { |
| 316 | EXPECT_EQ(arr + i, &e); |
| 317 | e = 's' + i; |
| 318 | ++i; |
| 319 | } |
| 320 | i = 0; |
| 321 | for (auto& e : ArrayView<const char>(av)) { |
| 322 | EXPECT_EQ(arr + i, &e); |
| 323 | // e = 'q' + i; // Compile error, because e is a const char&. |
| 324 | ++i; |
| 325 | } |
| 326 | } |
| 327 | |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 328 | TEST(ArrayViewTest, TestIterationFixed) { |
| 329 | char arr[] = "Arrr!"; |
| 330 | ArrayView<char, 6> av(arr); |
| 331 | EXPECT_EQ('A', *av.begin()); |
| 332 | EXPECT_EQ('A', *av.cbegin()); |
| 333 | EXPECT_EQ('\0', *(av.end() - 1)); |
| 334 | EXPECT_EQ('\0', *(av.cend() - 1)); |
| 335 | char i = 0; |
| 336 | for (auto& e : av) { |
| 337 | EXPECT_EQ(arr + i, &e); |
| 338 | e = 's' + i; |
| 339 | ++i; |
| 340 | } |
| 341 | i = 0; |
| 342 | for (auto& e : ArrayView<const char, 6>(av)) { |
| 343 | EXPECT_EQ(arr + i, &e); |
| 344 | // e = 'q' + i; // Compile error, because e is a const char&. |
| 345 | ++i; |
| 346 | } |
| 347 | } |
| 348 | |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 349 | TEST(ArrayViewTest, TestEmpty) { |
| 350 | EXPECT_TRUE(ArrayView<int>().empty()); |
| 351 | const int a[] = {1, 2, 3}; |
| 352 | EXPECT_FALSE(ArrayView<const int>(a).empty()); |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 353 | |
| 354 | static_assert(ArrayView<int, 0>::empty(), ""); |
| 355 | static_assert(!ArrayView<int, 3>::empty(), ""); |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | TEST(ArrayViewTest, TestCompare) { |
| 359 | int a[] = {1, 2, 3}; |
| 360 | int b[] = {1, 2, 3}; |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 361 | |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 362 | EXPECT_EQ(ArrayView<int>(a), ArrayView<int>(a)); |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 363 | EXPECT_EQ((ArrayView<int, 3>(a)), (ArrayView<int, 3>(a))); |
| 364 | EXPECT_EQ(ArrayView<int>(a), (ArrayView<int, 3>(a))); |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 365 | EXPECT_EQ(ArrayView<int>(), ArrayView<int>()); |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 366 | EXPECT_EQ(ArrayView<int>(), ArrayView<int>(a, 0)); |
| 367 | EXPECT_EQ(ArrayView<int>(a, 0), ArrayView<int>(b, 0)); |
| 368 | EXPECT_EQ((ArrayView<int, 0>(a, 0)), ArrayView<int>()); |
| 369 | |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 370 | EXPECT_NE(ArrayView<int>(a), ArrayView<int>(b)); |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 371 | EXPECT_NE((ArrayView<int, 3>(a)), (ArrayView<int, 3>(b))); |
| 372 | EXPECT_NE((ArrayView<int, 3>(a)), ArrayView<int>(b)); |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 373 | EXPECT_NE(ArrayView<int>(a), ArrayView<int>()); |
| 374 | EXPECT_NE(ArrayView<int>(a), ArrayView<int>(a, 2)); |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 375 | EXPECT_NE((ArrayView<int, 3>(a)), (ArrayView<int, 2>(a, 2))); |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 376 | } |
| 377 | |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 378 | TEST(ArrayViewTest, TestSubViewVariable) { |
danilchap | a28780e | 2016-11-18 01:46:23 -0800 | [diff] [blame] | 379 | int a[] = {1, 2, 3}; |
| 380 | ArrayView<int> av(a); |
| 381 | |
| 382 | EXPECT_EQ(av.subview(0), av); |
| 383 | |
| 384 | EXPECT_THAT(av.subview(1), ElementsAre(2, 3)); |
| 385 | EXPECT_THAT(av.subview(2), ElementsAre(3)); |
| 386 | EXPECT_THAT(av.subview(3), IsEmpty()); |
| 387 | EXPECT_THAT(av.subview(4), IsEmpty()); |
| 388 | |
| 389 | EXPECT_THAT(av.subview(1, 0), IsEmpty()); |
| 390 | EXPECT_THAT(av.subview(1, 1), ElementsAre(2)); |
| 391 | EXPECT_THAT(av.subview(1, 2), ElementsAre(2, 3)); |
| 392 | EXPECT_THAT(av.subview(1, 3), ElementsAre(2, 3)); |
| 393 | } |
| 394 | |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 395 | TEST(ArrayViewTest, TestSubViewFixed) { |
| 396 | int a[] = {1, 2, 3}; |
| 397 | ArrayView<int, 3> av(a); |
| 398 | |
| 399 | EXPECT_EQ(av.subview(0), av); |
| 400 | |
| 401 | EXPECT_THAT(av.subview(1), ElementsAre(2, 3)); |
| 402 | EXPECT_THAT(av.subview(2), ElementsAre(3)); |
| 403 | EXPECT_THAT(av.subview(3), IsEmpty()); |
| 404 | EXPECT_THAT(av.subview(4), IsEmpty()); |
| 405 | |
| 406 | EXPECT_THAT(av.subview(1, 0), IsEmpty()); |
| 407 | EXPECT_THAT(av.subview(1, 1), ElementsAre(2)); |
| 408 | EXPECT_THAT(av.subview(1, 2), ElementsAre(2, 3)); |
| 409 | EXPECT_THAT(av.subview(1, 3), ElementsAre(2, 3)); |
| 410 | } |
| 411 | |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 412 | } // namespace rtc |