Line data Source code
1 : // Copyright 2017 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/base/template-utils.h"
6 :
7 : #include "test/unittests/test-utils.h"
8 :
9 : namespace v8 {
10 : namespace base {
11 : namespace template_utils_unittest {
12 :
13 : ////////////////////////////
14 : // Test make_array.
15 : ////////////////////////////
16 :
17 : namespace {
18 : template <typename T, size_t Size>
19 2 : void CheckArrayEquals(const std::array<T, Size>& arr1,
20 : const std::array<T, Size>& arr2) {
21 8 : for (size_t i = 0; i < Size; ++i) {
22 6 : CHECK_EQ(arr1[i], arr2[i]);
23 : }
24 2 : }
25 : } // namespace
26 :
27 13158 : TEST(TemplateUtilsTest, MakeArraySimple) {
28 1 : auto computed_array = base::make_array<3>([](int i) { return 1 + (i * 3); });
29 1 : std::array<int, 3> expected{{1, 4, 7}};
30 1 : CheckArrayEquals(computed_array, expected);
31 1 : }
32 :
33 : namespace {
34 : constexpr int doubleIntValue(int i) { return i * 2; }
35 : }; // namespace
36 :
37 13158 : TEST(TemplateUtilsTest, MakeArrayConstexpr) {
38 1 : constexpr auto computed_array = base::make_array<3>(doubleIntValue);
39 1 : constexpr std::array<int, 3> expected{{0, 2, 4}};
40 1 : CheckArrayEquals(computed_array, expected);
41 1 : }
42 :
43 : ////////////////////////////
44 : // Test pass_value_or_ref.
45 : ////////////////////////////
46 :
47 : // Wrap into this helper struct, such that the type is printed on errors.
48 : template <typename T1, typename T2>
49 : struct CheckIsSame {
50 : static_assert(std::is_same<T1, T2>::value, "test failure");
51 : };
52 :
53 : #define TEST_PASS_VALUE_OR_REF0(remove_extend, expected, given) \
54 : static_assert( \
55 : sizeof(CheckIsSame<expected, \
56 : pass_value_or_ref<given, remove_extend>::type>) > 0, \
57 : "check")
58 :
59 : #define TEST_PASS_VALUE_OR_REF(expected, given) \
60 : static_assert( \
61 : sizeof(CheckIsSame<expected, pass_value_or_ref<given>::type>) > 0, \
62 : "check")
63 :
64 : TEST_PASS_VALUE_OR_REF(int, int&);
65 : TEST_PASS_VALUE_OR_REF(int, int&&);
66 : TEST_PASS_VALUE_OR_REF(const char*, const char[14]);
67 : TEST_PASS_VALUE_OR_REF(const char*, const char*&&);
68 : TEST_PASS_VALUE_OR_REF(const char*, const char (&)[14]);
69 : TEST_PASS_VALUE_OR_REF(const std::string&, std::string);
70 : TEST_PASS_VALUE_OR_REF(const std::string&, std::string&);
71 : TEST_PASS_VALUE_OR_REF(const std::string&, const std::string&);
72 : TEST_PASS_VALUE_OR_REF(int, const int);
73 : TEST_PASS_VALUE_OR_REF(int, const int&);
74 : TEST_PASS_VALUE_OR_REF(const int*, const int*);
75 : TEST_PASS_VALUE_OR_REF(const int*, const int* const);
76 : TEST_PASS_VALUE_OR_REF0(false, const char[14], const char[14]);
77 : TEST_PASS_VALUE_OR_REF0(false, const char[14], const char (&)[14]);
78 : TEST_PASS_VALUE_OR_REF0(false, const std::string&, std::string);
79 : TEST_PASS_VALUE_OR_REF0(false, const std::string&, std::string&);
80 : TEST_PASS_VALUE_OR_REF0(false, const std::string&, const std::string&);
81 : TEST_PASS_VALUE_OR_REF0(false, int, const int);
82 : TEST_PASS_VALUE_OR_REF0(false, int, const int&);
83 :
84 : //////////////////////////////
85 : // Test has_output_operator.
86 : //////////////////////////////
87 :
88 : // Intrinsic types:
89 : static_assert(has_output_operator<int>::value, "int can be output");
90 : static_assert(has_output_operator<void*>::value, "void* can be output");
91 : static_assert(has_output_operator<uint64_t>::value, "int can be output");
92 :
93 : // Classes:
94 : class TestClass1 {};
95 : class TestClass2 {};
96 : extern std::ostream& operator<<(std::ostream& str, TestClass2&);
97 : static_assert(!has_output_operator<TestClass1>::value,
98 : "TestClass1 can not be output");
99 : static_assert(has_output_operator<TestClass2>::value,
100 : "non-const TestClass2 can be output");
101 : static_assert(!has_output_operator<const TestClass2>::value,
102 : "const TestClass2 can not be output");
103 :
104 : } // namespace template_utils_unittest
105 : } // namespace base
106 7893 : } // namespace v8
|