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 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 11 | #include "api/array_view.h" |
| 12 | |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 13 | #include <algorithm> |
Alessio Bazzica | 858c4d7 | 2018-05-14 16:33:58 +0200 | [diff] [blame] | 14 | #include <array> |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 15 | #include <string> |
kwiberg | 529662a | 2017-09-04 05:43:17 -0700 | [diff] [blame] | 16 | #include <utility> |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/buffer.h" |
| 20 | #include "rtc_base/checks.h" |
| 21 | #include "rtc_base/gunit.h" |
| 22 | #include "test/gmock.h" |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 23 | |
| 24 | namespace rtc { |
| 25 | |
| 26 | namespace { |
kwiberg | bd43172 | 2016-09-05 04:20:54 -0700 | [diff] [blame] | 27 | |
danilchap | a28780e | 2016-11-18 01:46:23 -0800 | [diff] [blame] | 28 | using ::testing::ElementsAre; |
| 29 | using ::testing::IsEmpty; |
| 30 | |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 31 | template <typename T> |
Karl Wiberg | 30abc36 | 2019-02-04 13:07:18 +0100 | [diff] [blame] | 32 | size_t Call(ArrayView<T> av) { |
| 33 | return av.size(); |
| 34 | } |
| 35 | |
| 36 | template <typename T, size_t N> |
| 37 | void CallFixed(ArrayView<T, N> av) {} |
kwiberg | bd43172 | 2016-09-05 04:20:54 -0700 | [diff] [blame] | 38 | |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 39 | } // namespace |
| 40 | |
Tommi | 909f3a5 | 2020-05-18 16:47:56 +0200 | [diff] [blame] | 41 | TEST(ArrayViewDeathTest, TestConstructFromPtrAndArray) { |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 42 | char arr[] = "Arrr!"; |
| 43 | const char carr[] = "Carrr!"; |
Karl Wiberg | 30abc36 | 2019-02-04 13:07:18 +0100 | [diff] [blame] | 44 | EXPECT_EQ(6u, Call<const char>(arr)); |
| 45 | EXPECT_EQ(7u, Call<const char>(carr)); |
| 46 | EXPECT_EQ(6u, Call<char>(arr)); |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 47 | // Call<char>(carr); // Compile error, because can't drop const. |
| 48 | // Call<int>(arr); // Compile error, because incompatible types. |
| 49 | ArrayView<int*> x; |
| 50 | EXPECT_EQ(0u, x.size()); |
| 51 | EXPECT_EQ(nullptr, x.data()); |
| 52 | ArrayView<char> y = arr; |
| 53 | EXPECT_EQ(6u, y.size()); |
| 54 | EXPECT_EQ(arr, y.data()); |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 55 | ArrayView<char, 6> yf = arr; |
| 56 | static_assert(yf.size() == 6, ""); |
| 57 | EXPECT_EQ(arr, yf.data()); |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 58 | ArrayView<const char> z(arr + 1, 3); |
| 59 | EXPECT_EQ(3u, z.size()); |
| 60 | EXPECT_EQ(arr + 1, z.data()); |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 61 | ArrayView<const char, 3> zf(arr + 1, 3); |
| 62 | static_assert(zf.size() == 3, ""); |
| 63 | EXPECT_EQ(arr + 1, zf.data()); |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 64 | ArrayView<const char> w(arr, 2); |
| 65 | EXPECT_EQ(2u, w.size()); |
| 66 | EXPECT_EQ(arr, w.data()); |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 67 | ArrayView<const char, 2> wf(arr, 2); |
| 68 | static_assert(wf.size() == 2, ""); |
| 69 | EXPECT_EQ(arr, wf.data()); |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 70 | ArrayView<char> q(arr, 0); |
| 71 | EXPECT_EQ(0u, q.size()); |
| 72 | EXPECT_EQ(nullptr, q.data()); |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 73 | ArrayView<char, 0> qf(arr, 0); |
| 74 | static_assert(qf.size() == 0, ""); |
| 75 | EXPECT_EQ(nullptr, qf.data()); |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 76 | #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 77 | // DCHECK error (nullptr with nonzero size). |
| 78 | EXPECT_DEATH(ArrayView<int>(static_cast<int*>(nullptr), 5), ""); |
| 79 | #endif |
| 80 | // These are compile errors, because incompatible types. |
| 81 | // ArrayView<int> m = arr; |
| 82 | // ArrayView<float> n(arr + 2, 2); |
| 83 | } |
| 84 | |
Karl Wiberg | c62e4c5 | 2020-02-27 22:23:06 +0100 | [diff] [blame] | 85 | TEST(ArrayViewTest, TestCopyConstructorVariableLvalue) { |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 86 | char arr[] = "Arrr!"; |
| 87 | ArrayView<char> x = arr; |
| 88 | EXPECT_EQ(6u, x.size()); |
| 89 | EXPECT_EQ(arr, x.data()); |
| 90 | ArrayView<char> y = x; // Copy non-const -> non-const. |
| 91 | EXPECT_EQ(6u, y.size()); |
| 92 | EXPECT_EQ(arr, y.data()); |
| 93 | ArrayView<const char> z = x; // Copy non-const -> const. |
| 94 | EXPECT_EQ(6u, z.size()); |
| 95 | EXPECT_EQ(arr, z.data()); |
| 96 | ArrayView<const char> w = z; // Copy const -> const. |
| 97 | EXPECT_EQ(6u, w.size()); |
| 98 | EXPECT_EQ(arr, w.data()); |
| 99 | // ArrayView<char> v = z; // Compile error, because can't drop const. |
| 100 | } |
| 101 | |
Karl Wiberg | c62e4c5 | 2020-02-27 22:23:06 +0100 | [diff] [blame] | 102 | TEST(ArrayViewTest, TestCopyConstructorVariableRvalue) { |
| 103 | char arr[] = "Arrr!"; |
| 104 | ArrayView<char> x = arr; |
| 105 | EXPECT_EQ(6u, x.size()); |
| 106 | EXPECT_EQ(arr, x.data()); |
| 107 | ArrayView<char> y = std::move(x); // Copy non-const -> non-const. |
| 108 | EXPECT_EQ(6u, y.size()); |
| 109 | EXPECT_EQ(arr, y.data()); |
| 110 | ArrayView<const char> z = std::move(x); // Copy non-const -> const. |
| 111 | EXPECT_EQ(6u, z.size()); |
| 112 | EXPECT_EQ(arr, z.data()); |
| 113 | ArrayView<const char> w = std::move(z); // Copy const -> const. |
| 114 | EXPECT_EQ(6u, w.size()); |
| 115 | EXPECT_EQ(arr, w.data()); |
| 116 | // ArrayView<char> v = std::move(z); // Error, because can't drop const. |
| 117 | } |
| 118 | |
Karl Wiberg | ff61f3a | 2020-02-28 10:01:18 +0100 | [diff] [blame] | 119 | TEST(ArrayViewTest, TestCopyConstructorFixedLvalue) { |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 120 | char arr[] = "Arrr!"; |
| 121 | ArrayView<char, 6> x = arr; |
| 122 | static_assert(x.size() == 6, ""); |
| 123 | EXPECT_EQ(arr, x.data()); |
| 124 | |
| 125 | // Copy fixed -> fixed. |
| 126 | ArrayView<char, 6> y = x; // Copy non-const -> non-const. |
| 127 | static_assert(y.size() == 6, ""); |
| 128 | EXPECT_EQ(arr, y.data()); |
| 129 | ArrayView<const char, 6> z = x; // Copy non-const -> const. |
| 130 | static_assert(z.size() == 6, ""); |
| 131 | EXPECT_EQ(arr, z.data()); |
| 132 | ArrayView<const char, 6> w = z; // Copy const -> const. |
| 133 | static_assert(w.size() == 6, ""); |
| 134 | EXPECT_EQ(arr, w.data()); |
| 135 | // ArrayView<char, 6> v = z; // Compile error, because can't drop const. |
| 136 | |
| 137 | // Copy fixed -> variable. |
| 138 | ArrayView<char> yv = x; // Copy non-const -> non-const. |
| 139 | EXPECT_EQ(6u, yv.size()); |
| 140 | EXPECT_EQ(arr, yv.data()); |
| 141 | ArrayView<const char> zv = x; // Copy non-const -> const. |
| 142 | EXPECT_EQ(6u, zv.size()); |
| 143 | EXPECT_EQ(arr, zv.data()); |
| 144 | ArrayView<const char> wv = z; // Copy const -> const. |
| 145 | EXPECT_EQ(6u, wv.size()); |
| 146 | EXPECT_EQ(arr, wv.data()); |
| 147 | // ArrayView<char> vv = z; // Compile error, because can't drop const. |
| 148 | } |
| 149 | |
Karl Wiberg | ff61f3a | 2020-02-28 10:01:18 +0100 | [diff] [blame] | 150 | TEST(ArrayViewTest, TestCopyConstructorFixedRvalue) { |
| 151 | char arr[] = "Arrr!"; |
| 152 | ArrayView<char, 6> x = arr; |
| 153 | static_assert(x.size() == 6, ""); |
| 154 | EXPECT_EQ(arr, x.data()); |
| 155 | |
| 156 | // Copy fixed -> fixed. |
| 157 | ArrayView<char, 6> y = std::move(x); // Copy non-const -> non-const. |
| 158 | static_assert(y.size() == 6, ""); |
| 159 | EXPECT_EQ(arr, y.data()); |
| 160 | ArrayView<const char, 6> z = std::move(x); // Copy non-const -> const. |
| 161 | static_assert(z.size() == 6, ""); |
| 162 | EXPECT_EQ(arr, z.data()); |
| 163 | ArrayView<const char, 6> w = std::move(z); // Copy const -> const. |
| 164 | static_assert(w.size() == 6, ""); |
| 165 | EXPECT_EQ(arr, w.data()); |
| 166 | // ArrayView<char, 6> v = std::move(z); // Error, because can't drop const. |
| 167 | |
| 168 | // Copy fixed -> variable. |
| 169 | ArrayView<char> yv = std::move(x); // Copy non-const -> non-const. |
| 170 | EXPECT_EQ(6u, yv.size()); |
| 171 | EXPECT_EQ(arr, yv.data()); |
| 172 | ArrayView<const char> zv = std::move(x); // Copy non-const -> const. |
| 173 | EXPECT_EQ(6u, zv.size()); |
| 174 | EXPECT_EQ(arr, zv.data()); |
| 175 | ArrayView<const char> wv = std::move(z); // Copy const -> const. |
| 176 | EXPECT_EQ(6u, wv.size()); |
| 177 | EXPECT_EQ(arr, wv.data()); |
| 178 | // ArrayView<char> vv = std::move(z); // Error, because can't drop const. |
| 179 | } |
| 180 | |
Karl Wiberg | c62e4c5 | 2020-02-27 22:23:06 +0100 | [diff] [blame] | 181 | TEST(ArrayViewTest, TestCopyAssignmentVariableLvalue) { |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 182 | char arr[] = "Arrr!"; |
| 183 | ArrayView<char> x(arr); |
| 184 | EXPECT_EQ(6u, x.size()); |
| 185 | EXPECT_EQ(arr, x.data()); |
| 186 | ArrayView<char> y; |
| 187 | y = x; // Copy non-const -> non-const. |
| 188 | EXPECT_EQ(6u, y.size()); |
| 189 | EXPECT_EQ(arr, y.data()); |
| 190 | ArrayView<const char> z; |
| 191 | z = x; // Copy non-const -> const. |
| 192 | EXPECT_EQ(6u, z.size()); |
| 193 | EXPECT_EQ(arr, z.data()); |
| 194 | ArrayView<const char> w; |
| 195 | w = z; // Copy const -> const. |
| 196 | EXPECT_EQ(6u, w.size()); |
| 197 | EXPECT_EQ(arr, w.data()); |
| 198 | // ArrayView<char> v; |
| 199 | // v = z; // Compile error, because can't drop const. |
| 200 | } |
| 201 | |
Karl Wiberg | c62e4c5 | 2020-02-27 22:23:06 +0100 | [diff] [blame] | 202 | TEST(ArrayViewTest, TestCopyAssignmentVariableRvalue) { |
| 203 | char arr[] = "Arrr!"; |
| 204 | ArrayView<char> x(arr); |
| 205 | EXPECT_EQ(6u, x.size()); |
| 206 | EXPECT_EQ(arr, x.data()); |
| 207 | ArrayView<char> y; |
| 208 | y = std::move(x); // Copy non-const -> non-const. |
| 209 | EXPECT_EQ(6u, y.size()); |
| 210 | EXPECT_EQ(arr, y.data()); |
| 211 | ArrayView<const char> z; |
| 212 | z = std::move(x); // Copy non-const -> const. |
| 213 | EXPECT_EQ(6u, z.size()); |
| 214 | EXPECT_EQ(arr, z.data()); |
| 215 | ArrayView<const char> w; |
| 216 | w = std::move(z); // Copy const -> const. |
| 217 | EXPECT_EQ(6u, w.size()); |
| 218 | EXPECT_EQ(arr, w.data()); |
| 219 | // ArrayView<char> v; |
| 220 | // v = std::move(z); // Compile error, because can't drop const. |
| 221 | } |
| 222 | |
Karl Wiberg | ff61f3a | 2020-02-28 10:01:18 +0100 | [diff] [blame] | 223 | TEST(ArrayViewTest, TestCopyAssignmentFixedLvalue) { |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 224 | char arr[] = "Arrr!"; |
| 225 | char init[] = "Init!"; |
| 226 | ArrayView<char, 6> x(arr); |
| 227 | EXPECT_EQ(arr, x.data()); |
| 228 | |
| 229 | // Copy fixed -> fixed. |
| 230 | ArrayView<char, 6> y(init); |
| 231 | y = x; // Copy non-const -> non-const. |
| 232 | EXPECT_EQ(arr, y.data()); |
| 233 | ArrayView<const char, 6> z(init); |
| 234 | z = x; // Copy non-const -> const. |
| 235 | EXPECT_EQ(arr, z.data()); |
| 236 | ArrayView<const char, 6> w(init); |
| 237 | w = z; // Copy const -> const. |
| 238 | EXPECT_EQ(arr, w.data()); |
| 239 | // ArrayView<char, 6> v(init); |
| 240 | // v = z; // Compile error, because can't drop const. |
| 241 | |
| 242 | // Copy fixed -> variable. |
| 243 | ArrayView<char> yv; |
| 244 | yv = x; // Copy non-const -> non-const. |
| 245 | EXPECT_EQ(6u, yv.size()); |
| 246 | EXPECT_EQ(arr, yv.data()); |
| 247 | ArrayView<const char> zv; |
| 248 | zv = x; // Copy non-const -> const. |
| 249 | EXPECT_EQ(6u, zv.size()); |
| 250 | EXPECT_EQ(arr, zv.data()); |
| 251 | ArrayView<const char> wv; |
| 252 | wv = z; // Copy const -> const. |
| 253 | EXPECT_EQ(6u, wv.size()); |
| 254 | EXPECT_EQ(arr, wv.data()); |
| 255 | // ArrayView<char> v; |
| 256 | // v = z; // Compile error, because can't drop const. |
| 257 | } |
| 258 | |
Karl Wiberg | ff61f3a | 2020-02-28 10:01:18 +0100 | [diff] [blame] | 259 | TEST(ArrayViewTest, TestCopyAssignmentFixedRvalue) { |
| 260 | char arr[] = "Arrr!"; |
| 261 | char init[] = "Init!"; |
| 262 | ArrayView<char, 6> x(arr); |
| 263 | EXPECT_EQ(arr, x.data()); |
| 264 | |
| 265 | // Copy fixed -> fixed. |
| 266 | ArrayView<char, 6> y(init); |
| 267 | y = std::move(x); // Copy non-const -> non-const. |
| 268 | EXPECT_EQ(arr, y.data()); |
| 269 | ArrayView<const char, 6> z(init); |
| 270 | z = std::move(x); // Copy non-const -> const. |
| 271 | EXPECT_EQ(arr, z.data()); |
| 272 | ArrayView<const char, 6> w(init); |
| 273 | w = std::move(z); // Copy const -> const. |
| 274 | EXPECT_EQ(arr, w.data()); |
| 275 | // ArrayView<char, 6> v(init); |
| 276 | // v = std::move(z); // Compile error, because can't drop const. |
| 277 | |
| 278 | // Copy fixed -> variable. |
| 279 | ArrayView<char> yv; |
| 280 | yv = std::move(x); // Copy non-const -> non-const. |
| 281 | EXPECT_EQ(6u, yv.size()); |
| 282 | EXPECT_EQ(arr, yv.data()); |
| 283 | ArrayView<const char> zv; |
| 284 | zv = std::move(x); // Copy non-const -> const. |
| 285 | EXPECT_EQ(6u, zv.size()); |
| 286 | EXPECT_EQ(arr, zv.data()); |
| 287 | ArrayView<const char> wv; |
| 288 | wv = std::move(z); // Copy const -> const. |
| 289 | EXPECT_EQ(6u, wv.size()); |
| 290 | EXPECT_EQ(arr, wv.data()); |
| 291 | // ArrayView<char> v; |
| 292 | // v = std::move(z); // Compile error, because can't drop const. |
| 293 | } |
| 294 | |
Alessio Bazzica | 858c4d7 | 2018-05-14 16:33:58 +0200 | [diff] [blame] | 295 | TEST(ArrayViewTest, TestStdArray) { |
Karl Wiberg | 30abc36 | 2019-02-04 13:07:18 +0100 | [diff] [blame] | 296 | EXPECT_EQ(4u, Call<const int>(std::array<int, 4>{1, 2, 3, 4})); |
| 297 | CallFixed<const int, 3>(std::array<int, 3>{2, 3, 4}); |
Alessio Bazzica | 858c4d7 | 2018-05-14 16:33:58 +0200 | [diff] [blame] | 298 | constexpr size_t size = 5; |
| 299 | std::array<float, size> arr{}; |
| 300 | // Fixed size view. |
| 301 | rtc::ArrayView<float, size> arr_view_fixed(arr); |
| 302 | EXPECT_EQ(arr.data(), arr_view_fixed.data()); |
| 303 | static_assert(size == arr_view_fixed.size(), ""); |
| 304 | // Variable size view. |
| 305 | rtc::ArrayView<float> arr_view(arr); |
| 306 | EXPECT_EQ(arr.data(), arr_view.data()); |
| 307 | EXPECT_EQ(size, arr_view.size()); |
| 308 | } |
| 309 | |
Alessio Bazzica | 28a325b | 2018-05-15 14:57:51 +0200 | [diff] [blame] | 310 | TEST(ArrayViewTest, TestConstStdArray) { |
| 311 | constexpr size_t size = 5; |
| 312 | |
| 313 | constexpr std::array<float, size> constexpr_arr{}; |
| 314 | rtc::ArrayView<const float, size> constexpr_arr_view(constexpr_arr); |
| 315 | EXPECT_EQ(constexpr_arr.data(), constexpr_arr_view.data()); |
| 316 | static_assert(constexpr_arr.size() == constexpr_arr_view.size(), ""); |
| 317 | |
| 318 | const std::array<float, size> const_arr{}; |
| 319 | rtc::ArrayView<const float, size> const_arr_view(const_arr); |
| 320 | EXPECT_EQ(const_arr.data(), const_arr_view.data()); |
| 321 | static_assert(const_arr.size() == const_arr_view.size(), ""); |
| 322 | |
| 323 | std::array<float, size> non_const_arr{}; |
| 324 | rtc::ArrayView<const float, size> non_const_arr_view(non_const_arr); |
| 325 | EXPECT_EQ(non_const_arr.data(), non_const_arr_view.data()); |
| 326 | static_assert(non_const_arr.size() == non_const_arr_view.size(), ""); |
| 327 | } |
| 328 | |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 329 | TEST(ArrayViewTest, TestStdVector) { |
Karl Wiberg | 30abc36 | 2019-02-04 13:07:18 +0100 | [diff] [blame] | 330 | EXPECT_EQ(3u, Call<const int>(std::vector<int>{4, 5, 6})); |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 331 | std::vector<int> v; |
| 332 | v.push_back(3); |
| 333 | v.push_back(11); |
Karl Wiberg | 30abc36 | 2019-02-04 13:07:18 +0100 | [diff] [blame] | 334 | EXPECT_EQ(2u, Call<const int>(v)); |
| 335 | EXPECT_EQ(2u, Call<int>(v)); |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 336 | // Call<unsigned int>(v); // Compile error, because incompatible types. |
| 337 | ArrayView<int> x = v; |
| 338 | EXPECT_EQ(2u, x.size()); |
| 339 | EXPECT_EQ(v.data(), x.data()); |
| 340 | ArrayView<const int> y; |
| 341 | y = v; |
| 342 | EXPECT_EQ(2u, y.size()); |
| 343 | EXPECT_EQ(v.data(), y.data()); |
| 344 | // ArrayView<double> d = v; // Compile error, because incompatible types. |
| 345 | const std::vector<int> cv; |
Karl Wiberg | 30abc36 | 2019-02-04 13:07:18 +0100 | [diff] [blame] | 346 | EXPECT_EQ(0u, Call<const int>(cv)); |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 347 | // Call<int>(cv); // Compile error, because can't drop const. |
| 348 | ArrayView<const int> z = cv; |
| 349 | EXPECT_EQ(0u, z.size()); |
| 350 | EXPECT_EQ(nullptr, z.data()); |
| 351 | // ArrayView<int> w = cv; // Compile error, because can't drop const. |
| 352 | } |
| 353 | |
| 354 | TEST(ArrayViewTest, TestRtcBuffer) { |
| 355 | rtc::Buffer b = "so buffer"; |
Karl Wiberg | 30abc36 | 2019-02-04 13:07:18 +0100 | [diff] [blame] | 356 | EXPECT_EQ(10u, Call<const uint8_t>(b)); |
| 357 | EXPECT_EQ(10u, Call<uint8_t>(b)); |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 358 | // Call<int8_t>(b); // Compile error, because incompatible types. |
| 359 | ArrayView<uint8_t> x = b; |
| 360 | EXPECT_EQ(10u, x.size()); |
| 361 | EXPECT_EQ(b.data(), x.data()); |
| 362 | ArrayView<const uint8_t> y; |
| 363 | y = b; |
| 364 | EXPECT_EQ(10u, y.size()); |
| 365 | EXPECT_EQ(b.data(), y.data()); |
| 366 | // ArrayView<char> d = b; // Compile error, because incompatible types. |
| 367 | const rtc::Buffer cb = "very const"; |
Karl Wiberg | 30abc36 | 2019-02-04 13:07:18 +0100 | [diff] [blame] | 368 | EXPECT_EQ(11u, Call<const uint8_t>(cb)); |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 369 | // Call<uint8_t>(cb); // Compile error, because can't drop const. |
| 370 | ArrayView<const uint8_t> z = cb; |
| 371 | EXPECT_EQ(11u, z.size()); |
| 372 | EXPECT_EQ(cb.data(), z.data()); |
| 373 | // ArrayView<uint8_t> w = cb; // Compile error, because can't drop const. |
| 374 | } |
| 375 | |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 376 | TEST(ArrayViewTest, TestSwapVariable) { |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 377 | const char arr[] = "Arrr!"; |
| 378 | const char aye[] = "Aye, Cap'n!"; |
| 379 | ArrayView<const char> x(arr); |
| 380 | EXPECT_EQ(6u, x.size()); |
| 381 | EXPECT_EQ(arr, x.data()); |
| 382 | ArrayView<const char> y(aye); |
| 383 | EXPECT_EQ(12u, y.size()); |
| 384 | EXPECT_EQ(aye, y.data()); |
| 385 | using std::swap; |
| 386 | swap(x, y); |
| 387 | EXPECT_EQ(12u, x.size()); |
| 388 | EXPECT_EQ(aye, x.data()); |
| 389 | EXPECT_EQ(6u, y.size()); |
| 390 | EXPECT_EQ(arr, y.data()); |
| 391 | // ArrayView<char> z; |
| 392 | // swap(x, z); // Compile error, because can't drop const. |
| 393 | } |
| 394 | |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 395 | TEST(FixArrayViewTest, TestSwapFixed) { |
| 396 | const char arr[] = "Arr!"; |
| 397 | char aye[] = "Aye!"; |
| 398 | ArrayView<const char, 5> x(arr); |
| 399 | EXPECT_EQ(arr, x.data()); |
| 400 | ArrayView<const char, 5> y(aye); |
| 401 | EXPECT_EQ(aye, y.data()); |
| 402 | using std::swap; |
| 403 | swap(x, y); |
| 404 | EXPECT_EQ(aye, x.data()); |
| 405 | EXPECT_EQ(arr, y.data()); |
| 406 | // ArrayView<char, 5> z(aye); |
| 407 | // swap(x, z); // Compile error, because can't drop const. |
| 408 | // ArrayView<const char, 4> w(aye, 4); |
| 409 | // swap(x, w); // Compile error, because different sizes. |
| 410 | } |
| 411 | |
Tommi | 909f3a5 | 2020-05-18 16:47:56 +0200 | [diff] [blame] | 412 | TEST(ArrayViewDeathTest, TestIndexing) { |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 413 | char arr[] = "abcdefg"; |
| 414 | ArrayView<char> x(arr); |
| 415 | const ArrayView<char> y(arr); |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 416 | ArrayView<const char, 8> z(arr); |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 417 | EXPECT_EQ(8u, x.size()); |
| 418 | EXPECT_EQ(8u, y.size()); |
| 419 | EXPECT_EQ(8u, z.size()); |
| 420 | EXPECT_EQ('b', x[1]); |
| 421 | EXPECT_EQ('c', y[2]); |
| 422 | EXPECT_EQ('d', z[3]); |
| 423 | x[3] = 'X'; |
| 424 | y[2] = 'Y'; |
| 425 | // z[1] = 'Z'; // Compile error, because z's element type is const char. |
| 426 | EXPECT_EQ('b', x[1]); |
| 427 | EXPECT_EQ('Y', y[2]); |
| 428 | EXPECT_EQ('X', z[3]); |
| 429 | #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 430 | EXPECT_DEATH(z[8], ""); // DCHECK error (index out of bounds). |
| 431 | #endif |
| 432 | } |
| 433 | |
| 434 | TEST(ArrayViewTest, TestIterationEmpty) { |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 435 | // Variable-size. |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 436 | ArrayView<std::vector<std::vector<std::vector<std::string>>>> av; |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 437 | EXPECT_EQ(av.begin(), av.end()); |
| 438 | EXPECT_EQ(av.cbegin(), av.cend()); |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 439 | for (auto& e : av) { |
| 440 | EXPECT_TRUE(false); |
| 441 | EXPECT_EQ(42u, e.size()); // Dummy use of e to prevent unused var warning. |
| 442 | } |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 443 | |
| 444 | // Fixed-size. |
| 445 | ArrayView<std::vector<std::vector<std::vector<std::string>>>, 0> af; |
| 446 | EXPECT_EQ(af.begin(), af.end()); |
| 447 | EXPECT_EQ(af.cbegin(), af.cend()); |
| 448 | for (auto& e : af) { |
| 449 | EXPECT_TRUE(false); |
| 450 | EXPECT_EQ(42u, e.size()); // Dummy use of e to prevent unused var warning. |
| 451 | } |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 452 | } |
| 453 | |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 454 | TEST(ArrayViewTest, TestIterationVariable) { |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 455 | char arr[] = "Arrr!"; |
| 456 | ArrayView<char> av(arr); |
| 457 | EXPECT_EQ('A', *av.begin()); |
| 458 | EXPECT_EQ('A', *av.cbegin()); |
| 459 | EXPECT_EQ('\0', *(av.end() - 1)); |
| 460 | EXPECT_EQ('\0', *(av.cend() - 1)); |
| 461 | char i = 0; |
| 462 | for (auto& e : av) { |
| 463 | EXPECT_EQ(arr + i, &e); |
| 464 | e = 's' + i; |
| 465 | ++i; |
| 466 | } |
| 467 | i = 0; |
| 468 | for (auto& e : ArrayView<const char>(av)) { |
| 469 | EXPECT_EQ(arr + i, &e); |
| 470 | // e = 'q' + i; // Compile error, because e is a const char&. |
| 471 | ++i; |
| 472 | } |
| 473 | } |
| 474 | |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 475 | TEST(ArrayViewTest, TestIterationFixed) { |
| 476 | char arr[] = "Arrr!"; |
| 477 | ArrayView<char, 6> av(arr); |
| 478 | EXPECT_EQ('A', *av.begin()); |
| 479 | EXPECT_EQ('A', *av.cbegin()); |
| 480 | EXPECT_EQ('\0', *(av.end() - 1)); |
| 481 | EXPECT_EQ('\0', *(av.cend() - 1)); |
| 482 | char i = 0; |
| 483 | for (auto& e : av) { |
| 484 | EXPECT_EQ(arr + i, &e); |
| 485 | e = 's' + i; |
| 486 | ++i; |
| 487 | } |
| 488 | i = 0; |
| 489 | for (auto& e : ArrayView<const char, 6>(av)) { |
| 490 | EXPECT_EQ(arr + i, &e); |
| 491 | // e = 'q' + i; // Compile error, because e is a const char&. |
| 492 | ++i; |
| 493 | } |
| 494 | } |
| 495 | |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 496 | TEST(ArrayViewTest, TestEmpty) { |
| 497 | EXPECT_TRUE(ArrayView<int>().empty()); |
| 498 | const int a[] = {1, 2, 3}; |
| 499 | EXPECT_FALSE(ArrayView<const int>(a).empty()); |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 500 | |
| 501 | static_assert(ArrayView<int, 0>::empty(), ""); |
| 502 | static_assert(!ArrayView<int, 3>::empty(), ""); |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | TEST(ArrayViewTest, TestCompare) { |
| 506 | int a[] = {1, 2, 3}; |
| 507 | int b[] = {1, 2, 3}; |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 508 | |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 509 | EXPECT_EQ(ArrayView<int>(a), ArrayView<int>(a)); |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 510 | EXPECT_EQ((ArrayView<int, 3>(a)), (ArrayView<int, 3>(a))); |
| 511 | EXPECT_EQ(ArrayView<int>(a), (ArrayView<int, 3>(a))); |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 512 | EXPECT_EQ(ArrayView<int>(), ArrayView<int>()); |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 513 | EXPECT_EQ(ArrayView<int>(), ArrayView<int>(a, 0)); |
| 514 | EXPECT_EQ(ArrayView<int>(a, 0), ArrayView<int>(b, 0)); |
| 515 | EXPECT_EQ((ArrayView<int, 0>(a, 0)), ArrayView<int>()); |
| 516 | |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 517 | EXPECT_NE(ArrayView<int>(a), ArrayView<int>(b)); |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 518 | EXPECT_NE((ArrayView<int, 3>(a)), (ArrayView<int, 3>(b))); |
| 519 | EXPECT_NE((ArrayView<int, 3>(a)), ArrayView<int>(b)); |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 520 | EXPECT_NE(ArrayView<int>(a), ArrayView<int>()); |
| 521 | EXPECT_NE(ArrayView<int>(a), ArrayView<int>(a, 2)); |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 522 | EXPECT_NE((ArrayView<int, 3>(a)), (ArrayView<int, 2>(a, 2))); |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 523 | } |
| 524 | |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 525 | TEST(ArrayViewTest, TestSubViewVariable) { |
danilchap | a28780e | 2016-11-18 01:46:23 -0800 | [diff] [blame] | 526 | int a[] = {1, 2, 3}; |
| 527 | ArrayView<int> av(a); |
| 528 | |
| 529 | EXPECT_EQ(av.subview(0), av); |
| 530 | |
| 531 | EXPECT_THAT(av.subview(1), ElementsAre(2, 3)); |
| 532 | EXPECT_THAT(av.subview(2), ElementsAre(3)); |
| 533 | EXPECT_THAT(av.subview(3), IsEmpty()); |
| 534 | EXPECT_THAT(av.subview(4), IsEmpty()); |
| 535 | |
| 536 | EXPECT_THAT(av.subview(1, 0), IsEmpty()); |
| 537 | EXPECT_THAT(av.subview(1, 1), ElementsAre(2)); |
| 538 | EXPECT_THAT(av.subview(1, 2), ElementsAre(2, 3)); |
| 539 | EXPECT_THAT(av.subview(1, 3), ElementsAre(2, 3)); |
| 540 | } |
| 541 | |
kwiberg | bfc7f02 | 2017-03-02 12:33:50 -0800 | [diff] [blame] | 542 | TEST(ArrayViewTest, TestSubViewFixed) { |
| 543 | int a[] = {1, 2, 3}; |
| 544 | ArrayView<int, 3> av(a); |
| 545 | |
| 546 | EXPECT_EQ(av.subview(0), av); |
| 547 | |
| 548 | EXPECT_THAT(av.subview(1), ElementsAre(2, 3)); |
| 549 | EXPECT_THAT(av.subview(2), ElementsAre(3)); |
| 550 | EXPECT_THAT(av.subview(3), IsEmpty()); |
| 551 | EXPECT_THAT(av.subview(4), IsEmpty()); |
| 552 | |
| 553 | EXPECT_THAT(av.subview(1, 0), IsEmpty()); |
| 554 | EXPECT_THAT(av.subview(1, 1), ElementsAre(2)); |
| 555 | EXPECT_THAT(av.subview(1, 2), ElementsAre(2, 3)); |
| 556 | EXPECT_THAT(av.subview(1, 3), ElementsAre(2, 3)); |
| 557 | } |
| 558 | |
Amit Hilbuch | e155dd0 | 2019-03-22 10:16:07 -0700 | [diff] [blame] | 559 | TEST(ArrayViewTest, TestReinterpretCastFixedSize) { |
| 560 | uint8_t bytes[] = {1, 2, 3}; |
| 561 | ArrayView<uint8_t, 3> uint8_av(bytes); |
| 562 | ArrayView<int8_t, 3> int8_av = reinterpret_array_view<int8_t>(uint8_av); |
| 563 | EXPECT_EQ(int8_av.size(), uint8_av.size()); |
| 564 | EXPECT_EQ(int8_av[0], 1); |
| 565 | EXPECT_EQ(int8_av[1], 2); |
| 566 | EXPECT_EQ(int8_av[2], 3); |
| 567 | } |
| 568 | |
| 569 | TEST(ArrayViewTest, TestReinterpretCastVariableSize) { |
| 570 | std::vector<int8_t> v = {1, 2, 3}; |
| 571 | ArrayView<int8_t> int8_av(v); |
| 572 | ArrayView<uint8_t> uint8_av = reinterpret_array_view<uint8_t>(int8_av); |
| 573 | EXPECT_EQ(int8_av.size(), uint8_av.size()); |
| 574 | EXPECT_EQ(uint8_av[0], 1); |
| 575 | EXPECT_EQ(uint8_av[1], 2); |
| 576 | EXPECT_EQ(uint8_av[2], 3); |
| 577 | } |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 578 | } // namespace rtc |