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" |
Wyatt Hepler | 6c331ae | 2020-08-04 10:05:11 -0700 | [diff] [blame] | 18 | #include "pw_polyfill/language_feature_macros.h" |
Wyatt Hepler | c542a5d | 2020-01-15 15:43:10 -0800 | [diff] [blame] | 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 | |
Wyatt Hepler | 6c331ae | 2020-08-04 10:05:11 -0700 | [diff] [blame] | 66 | // Check that consteval is at least equivalent to constexpr. |
| 67 | consteval int ConstevalFunction() { return 123; } |
| 68 | static_assert(ConstevalFunction() == 123); |
| 69 | |
| 70 | int c_array[5423] = {}; |
Wyatt Hepler | c542a5d | 2020-01-15 15:43:10 -0800 | [diff] [blame] | 71 | std::array<int, 32> array; |
| 72 | |
| 73 | TEST(Iterator, Size) { |
| 74 | EXPECT_TRUE(std::size(c_array) == sizeof(c_array) / sizeof(*c_array)); |
| 75 | EXPECT_TRUE(std::size(array) == array.size()); |
| 76 | } |
| 77 | |
| 78 | TEST(Iterator, Data) { |
| 79 | EXPECT_TRUE(std::data(c_array) == c_array); |
| 80 | EXPECT_TRUE(std::data(array) == array.data()); |
| 81 | } |
| 82 | |
Wyatt Hepler | 6c331ae | 2020-08-04 10:05:11 -0700 | [diff] [blame] | 83 | constinit bool mutable_value = true; |
| 84 | |
| 85 | TEST(Constinit, ValueIsMutable) { |
| 86 | ASSERT_TRUE(mutable_value); |
| 87 | mutable_value = false; |
| 88 | ASSERT_FALSE(mutable_value); |
| 89 | mutable_value = true; |
| 90 | } |
| 91 | |
Wyatt Hepler | c542a5d | 2020-01-15 15:43:10 -0800 | [diff] [blame] | 92 | TEST(TypeTraits, Aliases) { |
| 93 | static_assert( |
| 94 | std::is_same<std::aligned_storage_t<40, 40>, |
| 95 | typename std::aligned_storage<40, 40>::type>::value, |
| 96 | "Alias must be defined"); |
| 97 | |
| 98 | static_assert(std::is_same<std::common_type_t<int, bool>, |
| 99 | typename std::common_type<int, bool>::type>::value, |
| 100 | "Alias must be defined"); |
| 101 | |
| 102 | static_assert( |
| 103 | std::is_same<std::conditional_t<false, int, char>, |
| 104 | typename std::conditional<false, int, char>::type>::value, |
| 105 | "Alias must be defined"); |
| 106 | |
| 107 | static_assert( |
| 108 | std::is_same<std::decay_t<int>, typename std::decay<int>::type>::value, |
| 109 | "Alias must be defined"); |
| 110 | |
| 111 | static_assert(std::is_same<std::enable_if_t<true, int>, |
| 112 | typename std::enable_if<true, int>::type>::value, |
| 113 | "Alias must be defined"); |
| 114 | |
| 115 | static_assert(std::is_same<std::make_signed_t<int>, |
| 116 | typename std::make_signed<int>::type>::value, |
| 117 | "Alias must be defined"); |
| 118 | |
| 119 | static_assert(std::is_same<std::make_unsigned_t<int>, |
| 120 | typename std::make_unsigned<int>::type>::value, |
| 121 | "Alias must be defined"); |
| 122 | |
| 123 | static_assert(std::is_same<std::remove_cv_t<int>, |
| 124 | typename std::remove_cv<int>::type>::value, |
| 125 | "Alias must be defined"); |
| 126 | |
| 127 | static_assert(std::is_same<std::remove_pointer_t<int>, |
| 128 | typename std::remove_pointer<int>::type>::value, |
| 129 | "Alias must be defined"); |
| 130 | |
| 131 | static_assert(std::is_same<std::remove_reference_t<int>, |
| 132 | typename std::remove_reference<int>::type>::value, |
| 133 | "Alias must be defined"); |
| 134 | } |
| 135 | |
| 136 | } // namespace |
| 137 | } // namespace polyfill |
| 138 | } // namespace pw |