1fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar/* 2fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar * Copyright 2016 The Android Open Source Project 3fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar * 4fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar * Licensed under the Apache License, Version 2.0 (the "License"); 5fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar * you may not use this file except in compliance with the License. 6fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar * You may obtain a copy of the License at 7fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar * 8fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar * http://www.apache.org/licenses/LICENSE-2.0 9fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar * 10fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar * Unless required by applicable law or agreed to in writing, software 11fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar * distributed under the License is distributed on an "AS IS" BASIS, 12fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar * See the License for the specific language governing permissions and 14fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar * limitations under the License. 15fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar */ 16fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar 17fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar//#define LOG_NDEBUG 0 18fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar#define LOG_TAG "TypeTraits_test" 19fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar 20fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar#include <gtest/gtest.h> 21fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar 22fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar#include <media/stagefright/foundation/TypeTraits.h> 23fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar 24fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnarnamespace android { 25fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar 26fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnarclass TypeTraitsTest : public ::testing::Test { 27fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnarprotected: 28fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar enum A { }; 29fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar enum UA : uint32_t { }; 30fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar enum IA : int32_t { }; 31fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar}; 32fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar 33fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar// =========== basic sanity tests for type-support templates 34fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos MolnarTEST_F(TypeTraitsTest, StaticTests) { 35e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar 36e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar // ============ is_integral_or_enum 37e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar 38fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(!std::is_integral<A>::value, "enums should not be integral"); 39fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(!std::is_integral<UA>::value, "enums should not be integral"); 40fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(!std::is_integral<IA>::value, "enums should not be integral"); 41fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(is_integral_or_enum<A>::value, "enums should be integral_or_enum"); 42fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(is_integral_or_enum<UA>::value, "enums should be integral_or_enum"); 43fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(is_integral_or_enum<IA>::value, "enums should be integral_or_enum"); 44fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(is_integral_or_enum<int>::value, "ints should be integral_or_enum"); 45fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(is_integral_or_enum<unsigned>::value, "unsigned ints should be integral_or_enum"); 46fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(!is_integral_or_enum<float>::value, "floats should not be integral_or_enum"); 47fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar 48e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar // ============ is_unsigned_integral 49e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar 50fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(!std::is_unsigned<UA>::value, 51fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "unsigned enums should not be unsigned"); 52fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(!std::is_unsigned<IA>::value, 53fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "unsigned enums should not be unsigned"); 54fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(std::is_unsigned<typename std::underlying_type<UA>::type>::value, 55fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "underlying type of unsigned enums should be unsigned"); 56fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(!std::is_unsigned<typename std::underlying_type<IA>::type>::value, 57fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "underlying type of unsigned enums should be unsigned"); 58fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(is_unsigned_integral<UA>::value, 59fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "unsigned enums should be unsigned_integral"); 60fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(!is_unsigned_integral<IA>::value, 61fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "signed enums should not be unsigned_integral"); 62fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(is_unsigned_integral<unsigned>::value, 63fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "unsigned ints should be unsigned_integral"); 64fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(!is_unsigned_integral<int>::value, 65fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "ints should not be unsigned_integral"); 66fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(!is_unsigned_integral<float>::value, 67fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "floats should not be unsigned_integral"); 68fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar 69e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar // ============ is_signed_integral 70e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar 71fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(!std::is_signed<UA>::value, 72fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "unsigned enums should not be signed"); 73fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(!std::is_signed<IA>::value, 74fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "unsigned enums should not be signed"); 75fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(!std::is_signed<typename std::underlying_type<UA>::type>::value, 76fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "underlying type of unsigned enums should be signed"); 77fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(std::is_signed<typename std::underlying_type<IA>::type>::value, 78fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "underlying type of unsigned enums should be signed"); 79fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(!is_signed_integral<UA>::value, 80fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "unsigned enums should not be signed_integral"); 81fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(is_signed_integral<IA>::value, 82fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "signed enums should be signed_integral"); 83fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(!is_signed_integral<unsigned>::value, 84fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "unsigned ints should not be signed_integral"); 85fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(is_signed_integral<int>::value, 86fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "ints should be signed_integral"); 87fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(!is_signed_integral<float>::value, 88fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "floats should not be signed_integral"); 89fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar 90e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar // ============ underlying_integral_type 91e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar 92fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(std::is_same<uint64_t, typename underlying_integral_type<uint64_t>::type>::value, 93fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "underlying integral type of uint64_t should be uint64_t"); 94fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(std::is_same<uint32_t, typename underlying_integral_type<UA>::type>::value, 95fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "underlying integral type of uint32_t based enums should be uint32_t"); 96fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(std::is_same<int64_t, typename underlying_integral_type<int64_t>::type>::value, 97fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "underlying integral type of int64_t should be int64_t"); 98fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(std::is_same<int32_t, typename underlying_integral_type<IA>::type>::value, 99fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "underlying integral type of int32_t based enums should be int32_t"); 100fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar //typedef underlying_integral_type<float>::type no_type; 101fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar static_assert(std::is_same<void, typename underlying_integral_type<float, void>::type>::value, 102fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar "underlying integral type of float cannot be specified"); 103e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar 104e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar // ============ is_one_of 105e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar 106e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(!is_one_of<int>::value, "int shouldn't be one of {}"); 107e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(!is_one_of<int, unsigned>::value, "int shouldn't be one of {unsigned}"); 108e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(!is_one_of<int, unsigned, float>::value, 109e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar "int shouldn't be one of {unsigned, float}"); 110e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(is_one_of<int, int>::value, "int should be one of {int}"); 111e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(is_one_of<int, int, float>::value, "int should be one of {int, float}"); 112e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(is_one_of<int, float, int>::value, "int should be one of {float, int}"); 113e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(is_one_of<int, float, int, unsigned>::value, 114e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar "int should be one of {float, int, unsigned}"); 115e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(is_one_of<int, float, unsigned, int>::value, 116e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar "int should be one of {float, unsigned, int}"); 117e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(!is_one_of<int, int&>::value, "int shouldn't be one of {int&}"); 118e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar 119e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar // ============ are_unique 120e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar 121e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(are_unique<>::value, "{} should be unique"); 122e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(are_unique<int>::value, "{int} should be unique"); 123e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(are_unique<int, float>::value, "{int, float} should be unique"); 124e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(!are_unique<int, int>::value, "{int, int} shouldn't be unique"); 125e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(!are_unique<int, float, int>::value, "{int, float, int} shouldn't be unique"); 126e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(!are_unique<float, int, int>::value, "{float, int, int} shouldn't be unique"); 127e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(!are_unique<int, int, float>::value, "{int, int, float} shouldn't be unique"); 128e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar 129e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar // ============ find_first 130e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar 131e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(find_first<int>::index == 0, "int is not in {}"); 132e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(find_first<int, unsigned>::index == 0, "int is not in {unsigned}"); 133e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(find_first<int, unsigned, float>::index == 0, "int is not in {unsigned, float}"); 134e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(find_first<int, int>::index == 1, "int is 1st in {int}"); 135e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(find_first<int, int, float>::index == 1, "int is 1st in {int, float}"); 136e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(find_first<int, float, int>::index == 2, "int is 2nd in {float, int}"); 137e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(find_first<int, float, int, unsigned>::index == 2, 138e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar "int is 2nd in {float, int, unsigned}"); 139e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(find_first<int, float, int, unsigned>::index == 2, 140e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar "int is 2nd and 3rd in {float, int, int, unsigned}"); 141e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(find_first<int, float, unsigned, int>::index == 3, 142e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar "int is 3rd in {float, unsigned, int}"); 143e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(find_first<int, int&>::index == 0, "int is not in {int&}"); 144e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar 145e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar // ============ find_first_convertible_to 146e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar 147e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(find_first_convertible_to<int>::index == 0, "int is not convertible to {}"); 148e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(find_first_convertible_to<int, unsigned*>::index == 0, 149e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar "int is not convertible to {unsigned*}"); 150e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(find_first_convertible_to<int, unsigned*, float&>::index == 0, 151e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar "int is not convertible to {unsigned, float&}"); 152e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(find_first_convertible_to<int, int>::index == 1, "int is convertible to {int}"); 153e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(find_first_convertible_to<int, unsigned, int>::index == 1, 154e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar "int is convertible to 1st of {unsigned, int}"); 155e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(find_first_convertible_to<int, int&, float>::index == 2, 156e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar "int is convertible to 2nd of {int&, float}"); 157e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(find_first_convertible_to<float, float*, int, unsigned>::index == 2, 158e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar "float is convertible to 2nd of {float*, int, unsigned}"); 159e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(find_first_convertible_to<float, void, float[1], int>::index == 3, 160e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar "int is 3rd convertible to {void, float[], int}"); 161e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(find_first_convertible_to<int&, const int&>::index == 1, 162e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar "int& is convertible to {const int&}"); 163e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar static_assert(find_first_convertible_to<const int&, int&>::index == 0, 164e322cc51459b3fac11e7a080c3eb8ee1bfb36fc8Lajos Molnar "const int& is not convertible to {int&}"); 165fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar} 166fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar 167fba972f9d7f87c47ac0820b7f99420acc7e5dc36Lajos Molnar} // namespace android 168