blob: dfc50c11d7eafa2c4a1ea5e67fb7ad31f335f635 [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"
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
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
66int c_array[5423];
67std::array<int, 32> array;
68
69TEST(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
74TEST(Iterator, Data) {
75 EXPECT_TRUE(std::data(c_array) == c_array);
76 EXPECT_TRUE(std::data(array) == array.data());
77}
78
79TEST(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