1 // Copyright 2016 The Android Open Source Project
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.
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.
12 #include "anbox/common/type_traits.h"
14 #include <gtest/gtest.h>
23 TEST(TypeTraits, IsCallable) {
27 auto lambda = [c](bool) -> C* { return nullptr; };
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,
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,
46 static_assert(!is_callable_as<int(int, int), int(int)>::value,
49 static_assert(!is_callable_as<int(int), int>::value,
50 "bad required signature");
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");
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");
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");
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");
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");