a62421ef7374f9eead5eff1d97412d2e19f31ebc
[iec.git] / src / type3_AndroidCloud / anbox-master / tests / anbox / common / type_traits_tests.cpp
1 // Copyright 2016 The Android Open Source Project
2 //
3 // This software is licensed under the terms of the GNU General Public
4 // License version 2, as published by the Free Software Foundation, and
5 // may be copied, distributed, and modified under those terms.
6 //
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License for more details.
11
12 #include "anbox/common/type_traits.h"
13
14 #include <gtest/gtest.h>
15
16 #include <array>
17 #include <functional>
18 #include <list>
19 #include <vector>
20
21 namespace anbox {
22 namespace common {
23 TEST(TypeTraits, IsCallable) {
24     class C;
25     C* c = nullptr;
26
27     auto lambda = [c](bool) -> C* { return nullptr; };
28
29     static_assert(is_callable_as<void(), void()>::value, "simple function");
30     static_assert(is_callable_as<void(&)(), void()>::value, "function reference");
31     static_assert(is_callable_as<void(*)(), void()>::value, "function pointer");
32     static_assert(is_callable_as<int(C&, C*), int(C&, C*)>::value,
33                   "function with arguments and return type");
34     static_assert(is_callable_as<decltype(lambda), C*(bool)>::value, "lambda");
35     static_assert(is_callable_as<std::function<bool(int)>, bool(int)>::value,
36                   "std::function");
37
38     static_assert(!is_callable_as<int, void()>::value, "int should not be callable");
39     static_assert(!is_callable_as<C, void()>::value, "incomplete type");
40     static_assert(!is_callable_as<void(), void(int)>::value, "different arguments");
41     static_assert(!is_callable_as<int(), void()>::value, "different return types");
42     static_assert(!is_callable_as<int(), short()>::value,
43                   "slightly different return types");
44     static_assert(!is_callable_as<int(int), int(int, int)>::value,
45                   "more arguments");
46     static_assert(!is_callable_as<int(int, int), int(int)>::value,
47                   "less arguments");
48
49     static_assert(!is_callable_as<int(int), int>::value,
50                   "bad required signature");
51 }
52
53 TEST(TypeTraits, IsTemplateInstantiation) {
54     static_assert(!is_template_instantiation_of<int, std::vector>::value,
55                   "int is not an instance of vector");
56     static_assert(!is_template_instantiation_of<std::list<std::vector<int>>, std::vector>::value,
57                   "list is not an instance of vector");
58
59     static_assert(is_template_instantiation_of<std::vector<int>, std::vector>::value,
60                   "std::vector<int> is an instance of vector");
61     static_assert(is_template_instantiation_of<std::vector<std::vector<std::vector<int>>>, std::vector>::value,
62                   "nested std::vector<> is an instance of vector");
63 }
64
65 TEST(TypeTraits, IsRange) {
66     static_assert(is_range<std::vector<int>>::value,
67                   "vector<> should be detected as a range");
68     static_assert(is_range<const std::list<std::function<void()>>>::value,
69                   "const list<> should be detected as a range");
70     static_assert(is_range<std::array<std::vector<int>, 10>>::value,
71                   "array<> should be detected as a range");
72     char arr[100];
73     static_assert(is_range<decltype(arr)>::value,
74                   "C array should be detected as a range");
75     static_assert(is_range<decltype("string")>::value,
76                   "String literal should be detected as a range");
77
78     static_assert(!is_range<int>::value, "int shouldn't be a range");
79     static_assert(!is_range<int*>::value, "int* shouldn't be a range");
80     static_assert(!is_range<const int*>::value,
81                   "even const int* shouldn't be a range");
82 }
83 }  // namespace common
84 }  // namespace anbox