• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_feature_macros.h"
19 #include "pw_polyfill/standard.h"
20 #include "pw_polyfill/standard_library/cstddef.h"
21 #include "pw_polyfill/standard_library/iterator.h"
22 
23 namespace pw {
24 namespace polyfill {
25 namespace {
26 
27 PW_INLINE_VARIABLE constexpr int foo = 42;
28 
29 static_assert(foo == 42, "Error!");
30 
31 static_assert(PW_CXX_STANDARD_IS_SUPPORTED(98), "C++98 must be supported");
32 static_assert(PW_CXX_STANDARD_IS_SUPPORTED(11), "C++11 must be supported");
33 static_assert(PW_CXX_STANDARD_IS_SUPPORTED(14), "C++14 must be supported");
34 
35 #if __cplusplus >= 201703L
36 static_assert(PW_CXX_STANDARD_IS_SUPPORTED(17), "C++17 must be not supported");
37 #else
38 static_assert(!PW_CXX_STANDARD_IS_SUPPORTED(17), "C++17 must be supported");
39 #endif  // __cplusplus >= 201703L
40 
41 #if __cplusplus >= 202002L
42 static_assert(PW_CXX_STANDARD_IS_SUPPORTED(20), "C++20 must be supported");
43 #else
44 static_assert(!PW_CXX_STANDARD_IS_SUPPORTED(20), "C++20 must not be supported");
45 #endif  // __cplusplus >= 202002L
46 
TEST(Cstddef,Byte_Operators)47 TEST(Cstddef, Byte_Operators) {
48   std::byte value = std::byte(0);
49   EXPECT_EQ((value | std::byte(0x12)), std::byte(0x12));
50   EXPECT_EQ((value & std::byte(0x12)), std::byte(0));
51   EXPECT_EQ((value ^ std::byte(0x12)), std::byte(0x12));
52   EXPECT_EQ(~std::byte(0), std::byte(-1));
53   EXPECT_EQ((std::byte(1) << 3), std::byte(0x8));
54   EXPECT_EQ((std::byte(0x8) >> 3), std::byte(1));
55 }
56 
TEST(Cstddef,Byte_AssignmentOperators)57 TEST(Cstddef, Byte_AssignmentOperators) {
58   std::byte value = std::byte(0);
59   EXPECT_EQ((value |= std::byte(0x12)), std::byte(0x12));
60   EXPECT_EQ((value &= std::byte(0x0F)), std::byte(0x02));
61   EXPECT_EQ((value ^= std::byte(0xFF)), std::byte(0xFD));
62   EXPECT_EQ((value <<= 4), std::byte(0xD0));
63   EXPECT_EQ((value >>= 5), std::byte(0x6));
64 }
65 
66 // Check that consteval is at least equivalent to constexpr.
ConstevalFunction()67 PW_CONSTEVAL int ConstevalFunction() { return 123; }
68 static_assert(ConstevalFunction() == 123,
69               "Function should work in static_assert");
70 
71 int c_array[5423] = {};
72 std::array<int, 32> array;
73 
TEST(Iterator,Size)74 TEST(Iterator, Size) {
75   EXPECT_EQ(std::size(c_array), sizeof(c_array) / sizeof(*c_array));
76   EXPECT_EQ(std::size(array), array.size());
77 }
78 
TEST(Iterator,Data)79 TEST(Iterator, Data) {
80   EXPECT_EQ(std::data(c_array), c_array);
81   EXPECT_EQ(std::data(array), array.data());
82 }
83 
84 PW_CONSTINIT bool mutable_value = true;
85 
TEST(Constinit,ValueIsMutable)86 TEST(Constinit, ValueIsMutable) {
87   ASSERT_TRUE(mutable_value);
88   mutable_value = false;
89   ASSERT_FALSE(mutable_value);
90   mutable_value = true;
91 }
92 
93 }  // namespace
94 }  // namespace polyfill
95 }  // namespace pw
96