blob: cf9a24c3ac7227c252542e6f31597d39409863c0 [file] [log] [blame]
Wyatt Heplerc542a5d2020-01-15 15:43:10 -08001// 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 Hepler6c331ae2020-08-04 10:05:11 -070018#include "pw_polyfill/language_feature_macros.h"
Wyatt Heplerc542a5d2020-01-15 15:43:10 -080019#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
24namespace pw {
25namespace polyfill {
26namespace {
27
28PW_INLINE_VARIABLE constexpr int foo = 42;
29
30static_assert(foo == 42, "Error!");
31
32static_assert(PW_CXX_STANDARD_IS_SUPPORTED(98), "C++98 must be supported");
33static_assert(PW_CXX_STANDARD_IS_SUPPORTED(11), "C++11 must be supported");
34
35#if __cplusplus >= 201402L
36static_assert(PW_CXX_STANDARD_IS_SUPPORTED(14), "C++14 must be not supported");
37#else
38static_assert(!PW_CXX_STANDARD_IS_SUPPORTED(14), "C++14 must be supported");
39#endif // __cplusplus >= 201402L
40
41#if __cplusplus >= 201703L
42static_assert(PW_CXX_STANDARD_IS_SUPPORTED(17), "C++17 must be not supported");
43#else
44static_assert(!PW_CXX_STANDARD_IS_SUPPORTED(17), "C++17 must be supported");
45#endif // __cplusplus >= 201703L
46
47TEST(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
57TEST(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 Hepler6c331ae2020-08-04 10:05:11 -070066// Check that consteval is at least equivalent to constexpr.
67consteval int ConstevalFunction() { return 123; }
68static_assert(ConstevalFunction() == 123);
69
70int c_array[5423] = {};
Wyatt Heplerc542a5d2020-01-15 15:43:10 -080071std::array<int, 32> array;
72
73TEST(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
78TEST(Iterator, Data) {
79 EXPECT_TRUE(std::data(c_array) == c_array);
80 EXPECT_TRUE(std::data(array) == array.data());
81}
82
Wyatt Hepler6c331ae2020-08-04 10:05:11 -070083constinit bool mutable_value = true;
84
85TEST(Constinit, ValueIsMutable) {
86 ASSERT_TRUE(mutable_value);
87 mutable_value = false;
88 ASSERT_FALSE(mutable_value);
89 mutable_value = true;
90}
91
Wyatt Heplerc542a5d2020-01-15 15:43:10 -080092TEST(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