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>
13 #include <vector>
14 
15 #include "webrtc/base/array_view.h"
16 #include "webrtc/base/buffer.h"
17 #include "webrtc/base/checks.h"
18 #include "webrtc/base/gunit.h"
19 
20 namespace rtc {
21 
22 namespace {
23 template <typename T>
Call(ArrayView<T>)24 void Call(ArrayView<T>) {}
25 }  // namespace
26 
TEST(ArrayViewTest,TestConstructFromPtrAndArray)27 TEST(ArrayViewTest, TestConstructFromPtrAndArray) {
28   char arr[] = "Arrr!";
29   const char carr[] = "Carrr!";
30   Call<const char>(arr);
31   Call<const char>(carr);
32   Call<char>(arr);
33   // Call<char>(carr);  // Compile error, because can't drop const.
34   // Call<int>(arr);  // Compile error, because incompatible types.
35   ArrayView<int*> x;
36   EXPECT_EQ(0u, x.size());
37   EXPECT_EQ(nullptr, x.data());
38   ArrayView<char> y = arr;
39   EXPECT_EQ(6u, y.size());
40   EXPECT_EQ(arr, y.data());
41   ArrayView<const char> z(arr + 1, 3);
42   EXPECT_EQ(3u, z.size());
43   EXPECT_EQ(arr + 1, z.data());
44   ArrayView<const char> w(arr, 2);
45   EXPECT_EQ(2u, w.size());
46   EXPECT_EQ(arr, w.data());
47   ArrayView<char> q(arr, 0);
48   EXPECT_EQ(0u, q.size());
49   EXPECT_EQ(nullptr, q.data());
50 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
51   // DCHECK error (nullptr with nonzero size).
52   EXPECT_DEATH(ArrayView<int>(static_cast<int*>(nullptr), 5), "");
53 #endif
54   // These are compile errors, because incompatible types.
55   // ArrayView<int> m = arr;
56   // ArrayView<float> n(arr + 2, 2);
57 }
58 
TEST(ArrayViewTest,TestCopyConstructor)59 TEST(ArrayViewTest, TestCopyConstructor) {
60   char arr[] = "Arrr!";
61   ArrayView<char> x = arr;
62   EXPECT_EQ(6u, x.size());
63   EXPECT_EQ(arr, x.data());
64   ArrayView<char> y = x;  // Copy non-const -> non-const.
65   EXPECT_EQ(6u, y.size());
66   EXPECT_EQ(arr, y.data());
67   ArrayView<const char> z = x;  // Copy non-const -> const.
68   EXPECT_EQ(6u, z.size());
69   EXPECT_EQ(arr, z.data());
70   ArrayView<const char> w = z;  // Copy const -> const.
71   EXPECT_EQ(6u, w.size());
72   EXPECT_EQ(arr, w.data());
73   // ArrayView<char> v = z;  // Compile error, because can't drop const.
74 }
75 
TEST(ArrayViewTest,TestCopyAssignment)76 TEST(ArrayViewTest, TestCopyAssignment) {
77   char arr[] = "Arrr!";
78   ArrayView<char> x(arr);
79   EXPECT_EQ(6u, x.size());
80   EXPECT_EQ(arr, x.data());
81   ArrayView<char> y;
82   y = x;  // Copy non-const -> non-const.
83   EXPECT_EQ(6u, y.size());
84   EXPECT_EQ(arr, y.data());
85   ArrayView<const char> z;
86   z = x;  // Copy non-const -> const.
87   EXPECT_EQ(6u, z.size());
88   EXPECT_EQ(arr, z.data());
89   ArrayView<const char> w;
90   w = z;  // Copy const -> const.
91   EXPECT_EQ(6u, w.size());
92   EXPECT_EQ(arr, w.data());
93   // ArrayView<char> v;
94   // v = z;  // Compile error, because can't drop const.
95 }
96 
TEST(ArrayViewTest,TestStdVector)97 TEST(ArrayViewTest, TestStdVector) {
98   std::vector<int> v;
99   v.push_back(3);
100   v.push_back(11);
101   Call<const int>(v);
102   Call<int>(v);
103   // Call<unsigned int>(v);  // Compile error, because incompatible types.
104   ArrayView<int> x = v;
105   EXPECT_EQ(2u, x.size());
106   EXPECT_EQ(v.data(), x.data());
107   ArrayView<const int> y;
108   y = v;
109   EXPECT_EQ(2u, y.size());
110   EXPECT_EQ(v.data(), y.data());
111   // ArrayView<double> d = v;  // Compile error, because incompatible types.
112   const std::vector<int> cv;
113   Call<const int>(cv);
114   // Call<int>(cv);  // Compile error, because can't drop const.
115   ArrayView<const int> z = cv;
116   EXPECT_EQ(0u, z.size());
117   EXPECT_EQ(nullptr, z.data());
118   // ArrayView<int> w = cv;  // Compile error, because can't drop const.
119 }
120 
TEST(ArrayViewTest,TestRtcBuffer)121 TEST(ArrayViewTest, TestRtcBuffer) {
122   rtc::Buffer b = "so buffer";
123   Call<const uint8_t>(b);
124   Call<uint8_t>(b);
125   // Call<int8_t>(b);  // Compile error, because incompatible types.
126   ArrayView<uint8_t> x = b;
127   EXPECT_EQ(10u, x.size());
128   EXPECT_EQ(b.data(), x.data());
129   ArrayView<const uint8_t> y;
130   y = b;
131   EXPECT_EQ(10u, y.size());
132   EXPECT_EQ(b.data(), y.data());
133   // ArrayView<char> d = b;  // Compile error, because incompatible types.
134   const rtc::Buffer cb = "very const";
135   Call<const uint8_t>(cb);
136   // Call<uint8_t>(cb);  // Compile error, because can't drop const.
137   ArrayView<const uint8_t> z = cb;
138   EXPECT_EQ(11u, z.size());
139   EXPECT_EQ(cb.data(), z.data());
140   // ArrayView<uint8_t> w = cb;  // Compile error, because can't drop const.
141 }
142 
TEST(ArrayViewTest,TestSwap)143 TEST(ArrayViewTest, TestSwap) {
144   const char arr[] = "Arrr!";
145   const char aye[] = "Aye, Cap'n!";
146   ArrayView<const char> x(arr);
147   EXPECT_EQ(6u, x.size());
148   EXPECT_EQ(arr, x.data());
149   ArrayView<const char> y(aye);
150   EXPECT_EQ(12u, y.size());
151   EXPECT_EQ(aye, y.data());
152   using std::swap;
153   swap(x, y);
154   EXPECT_EQ(12u, x.size());
155   EXPECT_EQ(aye, x.data());
156   EXPECT_EQ(6u, y.size());
157   EXPECT_EQ(arr, y.data());
158   // ArrayView<char> z;
159   // swap(x, z);  // Compile error, because can't drop const.
160 }
161 
TEST(ArrayViewTest,TestIndexing)162 TEST(ArrayViewTest, TestIndexing) {
163   char arr[] = "abcdefg";
164   ArrayView<char> x(arr);
165   const ArrayView<char> y(arr);
166   ArrayView<const char> z(arr);
167   EXPECT_EQ(8u, x.size());
168   EXPECT_EQ(8u, y.size());
169   EXPECT_EQ(8u, z.size());
170   EXPECT_EQ('b', x[1]);
171   EXPECT_EQ('c', y[2]);
172   EXPECT_EQ('d', z[3]);
173   x[3] = 'X';
174   y[2] = 'Y';
175   // z[1] = 'Z';  // Compile error, because z's element type is const char.
176   EXPECT_EQ('b', x[1]);
177   EXPECT_EQ('Y', y[2]);
178   EXPECT_EQ('X', z[3]);
179 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
180   EXPECT_DEATH(z[8], "");  // DCHECK error (index out of bounds).
181 #endif
182 }
183 
TEST(ArrayViewTest,TestIterationEmpty)184 TEST(ArrayViewTest, TestIterationEmpty) {
185   ArrayView<std::vector<std::vector<std::vector<std::string>>>> av;
186   EXPECT_FALSE(av.begin());
187   EXPECT_FALSE(av.cbegin());
188   EXPECT_FALSE(av.end());
189   EXPECT_FALSE(av.cend());
190   for (auto& e : av) {
191     EXPECT_TRUE(false);
192     EXPECT_EQ(42u, e.size());  // Dummy use of e to prevent unused var warning.
193   }
194 }
195 
TEST(ArrayViewTest,TestIteration)196 TEST(ArrayViewTest, TestIteration) {
197   char arr[] = "Arrr!";
198   ArrayView<char> av(arr);
199   EXPECT_EQ('A', *av.begin());
200   EXPECT_EQ('A', *av.cbegin());
201   EXPECT_EQ('\0', *(av.end() - 1));
202   EXPECT_EQ('\0', *(av.cend() - 1));
203   char i = 0;
204   for (auto& e : av) {
205     EXPECT_EQ(arr + i, &e);
206     e = 's' + i;
207     ++i;
208   }
209   i = 0;
210   for (auto& e : ArrayView<const char>(av)) {
211     EXPECT_EQ(arr + i, &e);
212     // e = 'q' + i;  // Compile error, because e is a const char&.
213     ++i;
214   }
215 }
216 
TEST(ArrayViewTest,TestEmpty)217 TEST(ArrayViewTest, TestEmpty) {
218   EXPECT_TRUE(ArrayView<int>().empty());
219   const int a[] = {1, 2, 3};
220   EXPECT_FALSE(ArrayView<const int>(a).empty());
221 }
222 
TEST(ArrayViewTest,TestCompare)223 TEST(ArrayViewTest, TestCompare) {
224   int a[] = {1, 2, 3};
225   int b[] = {1, 2, 3};
226   EXPECT_EQ(ArrayView<int>(a), ArrayView<int>(a));
227   EXPECT_EQ(ArrayView<int>(), ArrayView<int>());
228   EXPECT_NE(ArrayView<int>(a), ArrayView<int>(b));
229   EXPECT_NE(ArrayView<int>(a), ArrayView<int>());
230   EXPECT_NE(ArrayView<int>(a), ArrayView<int>(a, 2));
231 }
232 
233 }  // namespace rtc
234