• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "api/array_view.h"
12 
13 #include <algorithm>
14 #include <array>
15 #include <string>
16 #include <utility>
17 #include <vector>
18 
19 #include "rtc_base/buffer.h"
20 #include "rtc_base/checks.h"
21 #include "rtc_base/gunit.h"
22 #include "test/gmock.h"
23 
24 namespace rtc {
25 
26 namespace {
27 
28 using ::testing::ElementsAre;
29 using ::testing::IsEmpty;
30 
31 template <typename T>
Call(ArrayView<T> av)32 size_t Call(ArrayView<T> av) {
33   return av.size();
34 }
35 
36 template <typename T, size_t N>
CallFixed(ArrayView<T,N> av)37 void CallFixed(ArrayView<T, N> av) {}
38 
39 }  // namespace
40 
TEST(ArrayViewDeathTest,TestConstructFromPtrAndArray)41 TEST(ArrayViewDeathTest, TestConstructFromPtrAndArray) {
42   char arr[] = "Arrr!";
43   const char carr[] = "Carrr!";
44   EXPECT_EQ(6u, Call<const char>(arr));
45   EXPECT_EQ(7u, Call<const char>(carr));
46   EXPECT_EQ(6u, Call<char>(arr));
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());
55   ArrayView<char, 6> yf = arr;
56   static_assert(yf.size() == 6, "");
57   EXPECT_EQ(arr, yf.data());
58   ArrayView<const char> z(arr + 1, 3);
59   EXPECT_EQ(3u, z.size());
60   EXPECT_EQ(arr + 1, z.data());
61   ArrayView<const char, 3> zf(arr + 1, 3);
62   static_assert(zf.size() == 3, "");
63   EXPECT_EQ(arr + 1, zf.data());
64   ArrayView<const char> w(arr, 2);
65   EXPECT_EQ(2u, w.size());
66   EXPECT_EQ(arr, w.data());
67   ArrayView<const char, 2> wf(arr, 2);
68   static_assert(wf.size() == 2, "");
69   EXPECT_EQ(arr, wf.data());
70   ArrayView<char> q(arr, 0);
71   EXPECT_EQ(0u, q.size());
72   EXPECT_EQ(nullptr, q.data());
73   ArrayView<char, 0> qf(arr, 0);
74   static_assert(qf.size() == 0, "");
75   EXPECT_EQ(nullptr, qf.data());
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 
TEST(ArrayViewTest,TestCopyConstructorVariableLvalue)85 TEST(ArrayViewTest, TestCopyConstructorVariableLvalue) {
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 
TEST(ArrayViewTest,TestCopyConstructorVariableRvalue)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 
TEST(ArrayViewTest,TestCopyConstructorFixedLvalue)119 TEST(ArrayViewTest, TestCopyConstructorFixedLvalue) {
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 
TEST(ArrayViewTest,TestCopyConstructorFixedRvalue)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 
TEST(ArrayViewTest,TestCopyAssignmentVariableLvalue)181 TEST(ArrayViewTest, TestCopyAssignmentVariableLvalue) {
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 
TEST(ArrayViewTest,TestCopyAssignmentVariableRvalue)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 
TEST(ArrayViewTest,TestCopyAssignmentFixedLvalue)223 TEST(ArrayViewTest, TestCopyAssignmentFixedLvalue) {
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 
TEST(ArrayViewTest,TestCopyAssignmentFixedRvalue)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 
TEST(ArrayViewTest,TestStdArray)295 TEST(ArrayViewTest, TestStdArray) {
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});
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 
TEST(ArrayViewTest,TestConstStdArray)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 
TEST(ArrayViewTest,TestStdVector)329 TEST(ArrayViewTest, TestStdVector) {
330   EXPECT_EQ(3u, Call<const int>(std::vector<int>{4, 5, 6}));
331   std::vector<int> v;
332   v.push_back(3);
333   v.push_back(11);
334   EXPECT_EQ(2u, Call<const int>(v));
335   EXPECT_EQ(2u, Call<int>(v));
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;
346   EXPECT_EQ(0u, Call<const int>(cv));
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 
TEST(ArrayViewTest,TestRtcBuffer)354 TEST(ArrayViewTest, TestRtcBuffer) {
355   rtc::Buffer b = "so buffer";
356   EXPECT_EQ(10u, Call<const uint8_t>(b));
357   EXPECT_EQ(10u, Call<uint8_t>(b));
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";
368   EXPECT_EQ(11u, Call<const uint8_t>(cb));
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 
TEST(ArrayViewTest,TestSwapVariable)376 TEST(ArrayViewTest, TestSwapVariable) {
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 
TEST(FixArrayViewTest,TestSwapFixed)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 
TEST(ArrayViewDeathTest,TestIndexing)412 TEST(ArrayViewDeathTest, TestIndexing) {
413   char arr[] = "abcdefg";
414   ArrayView<char> x(arr);
415   const ArrayView<char> y(arr);
416   ArrayView<const char, 8> z(arr);
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 
TEST(ArrayViewTest,TestIterationEmpty)434 TEST(ArrayViewTest, TestIterationEmpty) {
435   // Variable-size.
436   ArrayView<std::vector<std::vector<std::vector<std::string>>>> av;
437   EXPECT_EQ(av.begin(), av.end());
438   EXPECT_EQ(av.cbegin(), av.cend());
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   }
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   }
452 }
453 
TEST(ArrayViewTest,TestReverseIterationEmpty)454 TEST(ArrayViewTest, TestReverseIterationEmpty) {
455   // Variable-size.
456   ArrayView<std::vector<std::vector<std::vector<std::string>>>> av;
457   EXPECT_EQ(av.rbegin(), av.rend());
458   EXPECT_EQ(av.crbegin(), av.crend());
459   EXPECT_TRUE(av.empty());
460 
461   // Fixed-size.
462   ArrayView<std::vector<std::vector<std::vector<std::string>>>, 0> af;
463   EXPECT_EQ(af.begin(), af.end());
464   EXPECT_EQ(af.cbegin(), af.cend());
465   EXPECT_TRUE(af.empty());
466 }
467 
TEST(ArrayViewTest,TestIterationVariable)468 TEST(ArrayViewTest, TestIterationVariable) {
469   char arr[] = "Arrr!";
470   ArrayView<char> av(arr);
471   EXPECT_EQ('A', *av.begin());
472   EXPECT_EQ('A', *av.cbegin());
473   EXPECT_EQ('\0', *(av.end() - 1));
474   EXPECT_EQ('\0', *(av.cend() - 1));
475   char i = 0;
476   for (auto& e : av) {
477     EXPECT_EQ(arr + i, &e);
478     e = 's' + i;
479     ++i;
480   }
481   i = 0;
482   for (auto& e : ArrayView<const char>(av)) {
483     EXPECT_EQ(arr + i, &e);
484     // e = 'q' + i;  // Compile error, because e is a const char&.
485     ++i;
486   }
487 }
488 
TEST(ArrayViewTest,TestReverseIterationVariable)489 TEST(ArrayViewTest, TestReverseIterationVariable) {
490   char arr[] = "Arrr!";
491   ArrayView<char> av(arr);
492   EXPECT_EQ('\0', *av.rbegin());
493   EXPECT_EQ('\0', *av.crbegin());
494   EXPECT_EQ('A', *(av.rend() - 1));
495   EXPECT_EQ('A', *(av.crend() - 1));
496 
497   const char* cit = av.cend() - 1;
498   for (auto crit = av.crbegin(); crit != av.crend(); ++crit, --cit) {
499     EXPECT_EQ(*cit, *crit);
500   }
501 
502   char* it = av.end() - 1;
503   for (auto rit = av.rbegin(); rit != av.rend(); ++rit, --it) {
504     EXPECT_EQ(*it, *rit);
505   }
506 }
507 
TEST(ArrayViewTest,TestIterationFixed)508 TEST(ArrayViewTest, TestIterationFixed) {
509   char arr[] = "Arrr!";
510   ArrayView<char, 6> av(arr);
511   EXPECT_EQ('A', *av.begin());
512   EXPECT_EQ('A', *av.cbegin());
513   EXPECT_EQ('\0', *(av.end() - 1));
514   EXPECT_EQ('\0', *(av.cend() - 1));
515   char i = 0;
516   for (auto& e : av) {
517     EXPECT_EQ(arr + i, &e);
518     e = 's' + i;
519     ++i;
520   }
521   i = 0;
522   for (auto& e : ArrayView<const char, 6>(av)) {
523     EXPECT_EQ(arr + i, &e);
524     // e = 'q' + i;  // Compile error, because e is a const char&.
525     ++i;
526   }
527 }
528 
TEST(ArrayViewTest,TestReverseIterationFixed)529 TEST(ArrayViewTest, TestReverseIterationFixed) {
530   char arr[] = "Arrr!";
531   ArrayView<char, 6> av(arr);
532   EXPECT_EQ('\0', *av.rbegin());
533   EXPECT_EQ('\0', *av.crbegin());
534   EXPECT_EQ('A', *(av.rend() - 1));
535   EXPECT_EQ('A', *(av.crend() - 1));
536 
537   const char* cit = av.cend() - 1;
538   for (auto crit = av.crbegin(); crit != av.crend(); ++crit, --cit) {
539     EXPECT_EQ(*cit, *crit);
540   }
541 
542   char* it = av.end() - 1;
543   for (auto rit = av.rbegin(); rit != av.rend(); ++rit, --it) {
544     EXPECT_EQ(*it, *rit);
545   }
546 }
547 
TEST(ArrayViewTest,TestEmpty)548 TEST(ArrayViewTest, TestEmpty) {
549   EXPECT_TRUE(ArrayView<int>().empty());
550   const int a[] = {1, 2, 3};
551   EXPECT_FALSE(ArrayView<const int>(a).empty());
552 
553   static_assert(ArrayView<int, 0>::empty(), "");
554   static_assert(!ArrayView<int, 3>::empty(), "");
555 }
556 
TEST(ArrayViewTest,TestCompare)557 TEST(ArrayViewTest, TestCompare) {
558   int a[] = {1, 2, 3};
559   int b[] = {1, 2, 3};
560 
561   EXPECT_EQ(ArrayView<int>(a), ArrayView<int>(a));
562   EXPECT_EQ((ArrayView<int, 3>(a)), (ArrayView<int, 3>(a)));
563   EXPECT_EQ(ArrayView<int>(a), (ArrayView<int, 3>(a)));
564   EXPECT_EQ(ArrayView<int>(), ArrayView<int>());
565   EXPECT_EQ(ArrayView<int>(), ArrayView<int>(a, 0));
566   EXPECT_EQ(ArrayView<int>(a, 0), ArrayView<int>(b, 0));
567   EXPECT_EQ((ArrayView<int, 0>(a, 0)), ArrayView<int>());
568 
569   EXPECT_NE(ArrayView<int>(a), ArrayView<int>(b));
570   EXPECT_NE((ArrayView<int, 3>(a)), (ArrayView<int, 3>(b)));
571   EXPECT_NE((ArrayView<int, 3>(a)), ArrayView<int>(b));
572   EXPECT_NE(ArrayView<int>(a), ArrayView<int>());
573   EXPECT_NE(ArrayView<int>(a), ArrayView<int>(a, 2));
574   EXPECT_NE((ArrayView<int, 3>(a)), (ArrayView<int, 2>(a, 2)));
575 }
576 
TEST(ArrayViewTest,TestSubViewVariable)577 TEST(ArrayViewTest, TestSubViewVariable) {
578   int a[] = {1, 2, 3};
579   ArrayView<int> av(a);
580 
581   EXPECT_EQ(av.subview(0), av);
582 
583   EXPECT_THAT(av.subview(1), ElementsAre(2, 3));
584   EXPECT_THAT(av.subview(2), ElementsAre(3));
585   EXPECT_THAT(av.subview(3), IsEmpty());
586   EXPECT_THAT(av.subview(4), IsEmpty());
587 
588   EXPECT_THAT(av.subview(1, 0), IsEmpty());
589   EXPECT_THAT(av.subview(1, 1), ElementsAre(2));
590   EXPECT_THAT(av.subview(1, 2), ElementsAre(2, 3));
591   EXPECT_THAT(av.subview(1, 3), ElementsAre(2, 3));
592 }
593 
TEST(ArrayViewTest,TestSubViewFixed)594 TEST(ArrayViewTest, TestSubViewFixed) {
595   int a[] = {1, 2, 3};
596   ArrayView<int, 3> av(a);
597 
598   EXPECT_EQ(av.subview(0), av);
599 
600   EXPECT_THAT(av.subview(1), ElementsAre(2, 3));
601   EXPECT_THAT(av.subview(2), ElementsAre(3));
602   EXPECT_THAT(av.subview(3), IsEmpty());
603   EXPECT_THAT(av.subview(4), IsEmpty());
604 
605   EXPECT_THAT(av.subview(1, 0), IsEmpty());
606   EXPECT_THAT(av.subview(1, 1), ElementsAre(2));
607   EXPECT_THAT(av.subview(1, 2), ElementsAre(2, 3));
608   EXPECT_THAT(av.subview(1, 3), ElementsAre(2, 3));
609 }
610 
TEST(ArrayViewTest,TestReinterpretCastFixedSize)611 TEST(ArrayViewTest, TestReinterpretCastFixedSize) {
612   uint8_t bytes[] = {1, 2, 3};
613   ArrayView<uint8_t, 3> uint8_av(bytes);
614   ArrayView<int8_t, 3> int8_av = reinterpret_array_view<int8_t>(uint8_av);
615   EXPECT_EQ(int8_av.size(), uint8_av.size());
616   EXPECT_EQ(int8_av[0], 1);
617   EXPECT_EQ(int8_av[1], 2);
618   EXPECT_EQ(int8_av[2], 3);
619 }
620 
TEST(ArrayViewTest,TestReinterpretCastVariableSize)621 TEST(ArrayViewTest, TestReinterpretCastVariableSize) {
622   std::vector<int8_t> v = {1, 2, 3};
623   ArrayView<int8_t> int8_av(v);
624   ArrayView<uint8_t> uint8_av = reinterpret_array_view<uint8_t>(int8_av);
625   EXPECT_EQ(int8_av.size(), uint8_av.size());
626   EXPECT_EQ(uint8_av[0], 1);
627   EXPECT_EQ(uint8_av[1], 2);
628   EXPECT_EQ(uint8_av[2], 3);
629 }
630 }  // namespace rtc
631