• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "aemu/base/TypeTraits.h"
16 
17 #include <gtest/gtest.h>
18 
19 #include <array>
20 #include <functional>
21 #include <list>
22 #include <vector>
23 
24 namespace android {
25 namespace base {
26 
TEST(TypeTraits,IsCallable)27 TEST(TypeTraits, IsCallable) {
28     class C;
29 
30     auto lambda = [](bool) -> C* { return nullptr; };
31 
32     static_assert(is_callable_as<void(), void()>::value, "simple function");
33     static_assert(is_callable_as<void(&)(), void()>::value, "function reference");
34     static_assert(is_callable_as<void(*)(), void()>::value, "function pointer");
35     static_assert(is_callable_as<int(C&, C*), int(C&, C*)>::value,
36                   "function with arguments and return type");
37     static_assert(is_callable_as<decltype(lambda), C*(bool)>::value, "lambda");
38     static_assert(is_callable_as<std::function<bool(int)>, bool(int)>::value,
39                   "std::function");
40 
41     static_assert(!is_callable_as<int, void()>::value, "int should not be callable");
42     static_assert(!is_callable_as<C, void()>::value, "incomplete type");
43     static_assert(!is_callable_as<void(), void(int)>::value, "different arguments");
44     static_assert(!is_callable_as<int(), void()>::value, "different return types");
45     static_assert(!is_callable_as<int(), short()>::value,
46                   "slightly different return types");
47     static_assert(!is_callable_as<int(int), int(int, int)>::value,
48                   "more arguments");
49     static_assert(!is_callable_as<int(int, int), int(int)>::value,
50                   "less arguments");
51 
52     static_assert(!is_callable_as<int(int), int>::value,
53                   "bad required signature");
54 
55 
56     static_assert(is_callable_with_args<void(), void()>::value, "simple function");
57     static_assert(is_callable_with_args<void(&)(), void()>::value, "function reference");
58     static_assert(is_callable_with_args<void(*)(), void()>::value, "function pointer");
59     static_assert(is_callable_with_args<int(C&, C*), int(C&, C*)>::value,
60                   "function with arguments and return type");
61     static_assert(is_callable_with_args<decltype(lambda), C*(bool)>::value, "lambda");
62     static_assert(is_callable_with_args<std::function<bool(int)>, bool(int)>::value,
63                   "std::function");
64 
65     static_assert(!is_callable_with_args<int, void()>::value, "int should not be callable");
66     static_assert(!is_callable_with_args<C, void()>::value, "incomplete type");
67     static_assert(!is_callable_with_args<void(), void(int)>::value, "different arguments");
68     static_assert(is_callable_with_args<int(), void()>::value, "different return types are ignored");
69     static_assert(is_callable_with_args<int(), short()>::value, "slightly different return types are ignored");
70     static_assert(!is_callable_with_args<int(int), int(int, int)>::value,
71                   "more arguments");
72     static_assert(!is_callable_with_args<int(int, int), int(int)>::value,
73                   "less arguments");
74 
75     static_assert(!is_callable_with_args<int(int), int>::value,
76                   "bad required signature");
77 }
78 
TEST(TypeTraits,IsTemplateInstantiation)79 TEST(TypeTraits, IsTemplateInstantiation) {
80     static_assert(!is_template_instantiation_of<int, std::vector>::value,
81                   "int is not an instance of vector");
82     static_assert(!is_template_instantiation_of<std::list<std::vector<int>>, std::vector>::value,
83                   "list is not an instance of vector");
84 
85     static_assert(is_template_instantiation_of<std::vector<int>, std::vector>::value,
86                   "std::vector<int> is an instance of vector");
87     static_assert(is_template_instantiation_of<std::vector<std::vector<std::vector<int>>>, std::vector>::value,
88                   "nested std::vector<> is an instance of vector");
89 }
90 
TEST(TypeTraits,IsRange)91 TEST(TypeTraits, IsRange) {
92     static_assert(is_range<std::vector<int>>::value,
93                   "vector<> should be detected as a range");
94     static_assert(is_range<const std::list<std::function<void()>>>::value,
95                   "const list<> should be detected as a range");
96     static_assert(is_range<std::array<std::vector<int>, 10>>::value,
97                   "array<> should be detected as a range");
98     char arr[100];
99     static_assert(is_range<decltype(arr)>::value,
100                   "C array should be detected as a range");
101     static_assert(is_range<decltype("string")>::value,
102                   "String literal should be detected as a range");
103 
104     static_assert(!is_range<int>::value, "int shouldn't be a range");
105     static_assert(!is_range<int*>::value, "int* shouldn't be a range");
106     static_assert(!is_range<const int*>::value,
107                   "even const int* shouldn't be a range");
108 }
109 
110 }  // namespace base
111 }  // namespace android
112