TypeTraits_test.cpp revision fba972f9d7f87c47ac0820b7f99420acc7e5dc36
1/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "TypeTraits_test"
19
20#include <gtest/gtest.h>
21
22#include <media/stagefright/foundation/TypeTraits.h>
23
24namespace android {
25
26class TypeTraitsTest : public ::testing::Test {
27protected:
28    enum A { };
29    enum UA : uint32_t { };
30    enum IA : int32_t { };
31};
32
33// =========== basic sanity tests for type-support templates
34TEST_F(TypeTraitsTest, StaticTests) {
35    static_assert(!std::is_integral<A>::value, "enums should not be integral");
36    static_assert(!std::is_integral<UA>::value, "enums should not be integral");
37    static_assert(!std::is_integral<IA>::value, "enums should not be integral");
38    static_assert(is_integral_or_enum<A>::value, "enums should be integral_or_enum");
39    static_assert(is_integral_or_enum<UA>::value, "enums should be integral_or_enum");
40    static_assert(is_integral_or_enum<IA>::value, "enums should be integral_or_enum");
41    static_assert(is_integral_or_enum<int>::value, "ints should be integral_or_enum");
42    static_assert(is_integral_or_enum<unsigned>::value, "unsigned ints should be integral_or_enum");
43    static_assert(!is_integral_or_enum<float>::value, "floats should not be integral_or_enum");
44
45    static_assert(!std::is_unsigned<UA>::value,
46                  "unsigned enums should not be unsigned");
47    static_assert(!std::is_unsigned<IA>::value,
48                  "unsigned enums should not be unsigned");
49    static_assert(std::is_unsigned<typename std::underlying_type<UA>::type>::value,
50                  "underlying type of unsigned enums should be unsigned");
51    static_assert(!std::is_unsigned<typename std::underlying_type<IA>::type>::value,
52                  "underlying type of unsigned enums should be unsigned");
53    static_assert(is_unsigned_integral<UA>::value,
54                  "unsigned enums should be unsigned_integral");
55    static_assert(!is_unsigned_integral<IA>::value,
56                  "signed enums should not be unsigned_integral");
57    static_assert(is_unsigned_integral<unsigned>::value,
58                  "unsigned ints should be unsigned_integral");
59    static_assert(!is_unsigned_integral<int>::value,
60                  "ints should not be unsigned_integral");
61    static_assert(!is_unsigned_integral<float>::value,
62                  "floats should not be unsigned_integral");
63
64    static_assert(!std::is_signed<UA>::value,
65                  "unsigned enums should not be signed");
66    static_assert(!std::is_signed<IA>::value,
67                  "unsigned enums should not be signed");
68    static_assert(!std::is_signed<typename std::underlying_type<UA>::type>::value,
69                  "underlying type of unsigned enums should be signed");
70    static_assert(std::is_signed<typename std::underlying_type<IA>::type>::value,
71                  "underlying type of unsigned enums should be signed");
72    static_assert(!is_signed_integral<UA>::value,
73                  "unsigned enums should not be signed_integral");
74    static_assert(is_signed_integral<IA>::value,
75                  "signed enums should be signed_integral");
76    static_assert(!is_signed_integral<unsigned>::value,
77                  "unsigned ints should not be signed_integral");
78    static_assert(is_signed_integral<int>::value,
79                  "ints should be signed_integral");
80    static_assert(!is_signed_integral<float>::value,
81                  "floats should not be signed_integral");
82
83    static_assert(std::is_same<uint64_t, typename underlying_integral_type<uint64_t>::type>::value,
84                  "underlying integral type of uint64_t should be uint64_t");
85    static_assert(std::is_same<uint32_t, typename underlying_integral_type<UA>::type>::value,
86                  "underlying integral type of uint32_t based enums should be uint32_t");
87    static_assert(std::is_same<int64_t, typename underlying_integral_type<int64_t>::type>::value,
88                  "underlying integral type of int64_t should be int64_t");
89    static_assert(std::is_same<int32_t, typename underlying_integral_type<IA>::type>::value,
90                  "underlying integral type of int32_t based enums should be int32_t");
91    //typedef underlying_integral_type<float>::type no_type;
92    static_assert(std::is_same<void, typename underlying_integral_type<float, void>::type>::value,
93                  "underlying integral type of float cannot be specified");
94}
95
96} // namespace android
97