Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(878)

Side by Side Diff: webrtc/base/array_view_unittest.cc

Issue 2312473002: Restrict the 1-argument ArrayView constructor to types with .size() and .data() (Closed)
Patch Set: comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/array_view.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include <algorithm> 11 #include <algorithm>
12 #include <string> 12 #include <string>
13 #include <vector> 13 #include <vector>
14 14
15 #include "webrtc/base/array_view.h" 15 #include "webrtc/base/array_view.h"
16 #include "webrtc/base/buffer.h" 16 #include "webrtc/base/buffer.h"
17 #include "webrtc/base/checks.h" 17 #include "webrtc/base/checks.h"
18 #include "webrtc/base/gunit.h" 18 #include "webrtc/base/gunit.h"
19 19
20 namespace rtc { 20 namespace rtc {
21 21
22 namespace { 22 namespace {
23
24 namespace test_has_data_and_size {
25
26 template <typename C, typename T>
27 using DS = internal::HasDataAndSize<C, T>;
28
29 template <typename DR, typename SR>
30 struct Test1 {
31 DR data();
32 SR size();
33 };
34 static_assert(DS<Test1<int*, int>, int>::value, "");
35 static_assert(DS<Test1<int*, int>, const int>::value, "");
36 static_assert(DS<Test1<const int*, int>, const int>::value, "");
37 static_assert(!DS<Test1<const int*, int>, int>::value, ""); // Wrong const.
38 static_assert(!DS<Test1<char*, size_t>, int>::value, ""); // Wrong ptr type.
39
40 struct Test2 {
41 int* data;
42 size_t size;
43 };
44 static_assert(!DS<Test2, int>::value, ""); // Because they aren't methods.
45
46 struct Test3 {
47 int* data();
48 };
49 static_assert(!DS<Test3, int>::value, ""); // Because .size() is missing.
50
51 class Test4 {
52 int* data();
53 size_t size();
54 };
55 static_assert(!DS<Test4, int>::value, ""); // Because methods are private.
56
57 } // namespace test_has_data_and_size
58
23 template <typename T> 59 template <typename T>
24 void Call(ArrayView<T>) {} 60 void Call(ArrayView<T>) {}
61
25 } // namespace 62 } // namespace
26 63
27 TEST(ArrayViewTest, TestConstructFromPtrAndArray) { 64 TEST(ArrayViewTest, TestConstructFromPtrAndArray) {
28 char arr[] = "Arrr!"; 65 char arr[] = "Arrr!";
29 const char carr[] = "Carrr!"; 66 const char carr[] = "Carrr!";
30 Call<const char>(arr); 67 Call<const char>(arr);
31 Call<const char>(carr); 68 Call<const char>(carr);
32 Call<char>(arr); 69 Call<char>(arr);
33 // Call<char>(carr); // Compile error, because can't drop const. 70 // Call<char>(carr); // Compile error, because can't drop const.
34 // Call<int>(arr); // Compile error, because incompatible types. 71 // Call<int>(arr); // Compile error, because incompatible types.
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 int a[] = {1, 2, 3}; 261 int a[] = {1, 2, 3};
225 int b[] = {1, 2, 3}; 262 int b[] = {1, 2, 3};
226 EXPECT_EQ(ArrayView<int>(a), ArrayView<int>(a)); 263 EXPECT_EQ(ArrayView<int>(a), ArrayView<int>(a));
227 EXPECT_EQ(ArrayView<int>(), ArrayView<int>()); 264 EXPECT_EQ(ArrayView<int>(), ArrayView<int>());
228 EXPECT_NE(ArrayView<int>(a), ArrayView<int>(b)); 265 EXPECT_NE(ArrayView<int>(a), ArrayView<int>(b));
229 EXPECT_NE(ArrayView<int>(a), ArrayView<int>()); 266 EXPECT_NE(ArrayView<int>(a), ArrayView<int>());
230 EXPECT_NE(ArrayView<int>(a), ArrayView<int>(a, 2)); 267 EXPECT_NE(ArrayView<int>(a), ArrayView<int>(a, 2));
231 } 268 }
232 269
233 } // namespace rtc 270 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/array_view.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698