Wyatt Hepler | c542a5d | 2020-01-15 15:43:10 -0800 | [diff] [blame] | 1 | // Copyright 2020 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://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, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | |
| 15 | #include <array> |
| 16 | |
| 17 | #include "gtest/gtest.h" |
| 18 | #include "pw_polyfill/language_features.h" |
| 19 | #include "pw_polyfill/standard.h" |
| 20 | #include "pw_polyfill/standard_library/cstddef.h" |
| 21 | #include "pw_polyfill/standard_library/iterator.h" |
| 22 | #include "pw_polyfill/standard_library/type_traits.h" |
| 23 | |
| 24 | namespace pw { |
| 25 | namespace polyfill { |
| 26 | namespace { |
| 27 | |
| 28 | PW_INLINE_VARIABLE constexpr int foo = 42; |
| 29 | |
| 30 | static_assert(foo == 42, "Error!"); |
| 31 | |
| 32 | static_assert(PW_CXX_STANDARD_IS_SUPPORTED(98), "C++98 must be supported"); |
| 33 | static_assert(PW_CXX_STANDARD_IS_SUPPORTED(11), "C++11 must be supported"); |
| 34 | |
| 35 | #if __cplusplus >= 201402L |
| 36 | static_assert(PW_CXX_STANDARD_IS_SUPPORTED(14), "C++14 must be not supported"); |
| 37 | #else |
| 38 | static_assert(!PW_CXX_STANDARD_IS_SUPPORTED(14), "C++14 must be supported"); |
| 39 | #endif // __cplusplus >= 201402L |
| 40 | |
| 41 | #if __cplusplus >= 201703L |
| 42 | static_assert(PW_CXX_STANDARD_IS_SUPPORTED(17), "C++17 must be not supported"); |
| 43 | #else |
| 44 | static_assert(!PW_CXX_STANDARD_IS_SUPPORTED(17), "C++17 must be supported"); |
| 45 | #endif // __cplusplus >= 201703L |
| 46 | |
| 47 | TEST(Cstddef, Byte_Operators) { |
| 48 | std::byte value = std::byte(0); |
| 49 | EXPECT_TRUE((value | std::byte(0x12)) == std::byte(0x12)); |
| 50 | EXPECT_TRUE((value & std::byte(0x12)) == std::byte(0)); |
| 51 | EXPECT_TRUE((value ^ std::byte(0x12)) == std::byte(0x12)); |
| 52 | EXPECT_TRUE(~std::byte(0) == std::byte(-1)); |
| 53 | EXPECT_TRUE((std::byte(1) << 3) == std::byte(0x8)); |
| 54 | EXPECT_TRUE((std::byte(0x8) >> 3) == std::byte(1)); |
| 55 | } |
| 56 | |
| 57 | TEST(Cstddef, Byte_AssignmentOperators) { |
| 58 | std::byte value = std::byte(0); |
| 59 | EXPECT_TRUE((value |= std::byte(0x12)) == std::byte(0x12)); |
| 60 | EXPECT_TRUE((value &= std::byte(0x0F)) == std::byte(0x02)); |
| 61 | EXPECT_TRUE((value ^= std::byte(0xFF)) == std::byte(0xFD)); |
| 62 | EXPECT_TRUE((value <<= 4) == std::byte(0xD0)); |
| 63 | EXPECT_TRUE((value >>= 5) == std::byte(0x6)); |
| 64 | } |
| 65 | |
| 66 | int c_array[5423]; |
| 67 | std::array<int, 32> array; |
| 68 | |
| 69 | TEST(Iterator, Size) { |
| 70 | EXPECT_TRUE(std::size(c_array) == sizeof(c_array) / sizeof(*c_array)); |
| 71 | EXPECT_TRUE(std::size(array) == array.size()); |
| 72 | } |
| 73 | |
| 74 | TEST(Iterator, Data) { |
| 75 | EXPECT_TRUE(std::data(c_array) == c_array); |
| 76 | EXPECT_TRUE(std::data(array) == array.data()); |
| 77 | } |
| 78 | |
| 79 | TEST(TypeTraits, Aliases) { |
| 80 | static_assert( |
| 81 | std::is_same<std::aligned_storage_t<40, 40>, |
| 82 | typename std::aligned_storage<40, 40>::type>::value, |
| 83 | "Alias must be defined"); |
| 84 | |
| 85 | static_assert(std::is_same<std::common_type_t<int, bool>, |
| 86 | typename std::common_type<int, bool>::type>::value, |
| 87 | "Alias must be defined"); |
| 88 | |
| 89 | static_assert( |
| 90 | std::is_same<std::conditional_t<false, int, char>, |
| 91 | typename std::conditional<false, int, char>::type>::value, |
| 92 | "Alias must be defined"); |
| 93 | |
| 94 | static_assert( |
| 95 | std::is_same<std::decay_t<int>, typename std::decay<int>::type>::value, |
| 96 | "Alias must be defined"); |
| 97 | |
| 98 | static_assert(std::is_same<std::enable_if_t<true, int>, |
| 99 | typename std::enable_if<true, int>::type>::value, |
| 100 | "Alias must be defined"); |
| 101 | |
| 102 | static_assert(std::is_same<std::make_signed_t<int>, |
| 103 | typename std::make_signed<int>::type>::value, |
| 104 | "Alias must be defined"); |
| 105 | |
| 106 | static_assert(std::is_same<std::make_unsigned_t<int>, |
| 107 | typename std::make_unsigned<int>::type>::value, |
| 108 | "Alias must be defined"); |
| 109 | |
| 110 | static_assert(std::is_same<std::remove_cv_t<int>, |
| 111 | typename std::remove_cv<int>::type>::value, |
| 112 | "Alias must be defined"); |
| 113 | |
| 114 | static_assert(std::is_same<std::remove_pointer_t<int>, |
| 115 | typename std::remove_pointer<int>::type>::value, |
| 116 | "Alias must be defined"); |
| 117 | |
| 118 | static_assert(std::is_same<std::remove_reference_t<int>, |
| 119 | typename std::remove_reference<int>::type>::value, |
| 120 | "Alias must be defined"); |
| 121 | } |
| 122 | |
| 123 | } // namespace |
| 124 | } // namespace polyfill |
| 125 | } // namespace pw |