1 // Copyright 2019 The Pigweed Authors
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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
15 // Include all of the provided headers, even if they aren't tested.
25 #include <initializer_list>
29 #include <string_view>
30 #include <type_traits>
33 #include "pw_preprocessor/compiler.h"
37 // In order to test this file without dependencies on the C++ standard library,
38 // this file needs to be fully isolated from the regular Pigweed testing
39 // infrastructure. pw_unit_test's dependencies do compile with the C++ standard
40 // library, which could conflict with pw_minimal_cpp_stdlib.
42 // SimpleTest provides the basic features of pw_unit_test without dependencies.
45 virtual ~SimpleTest() = default;
47 static bool RunAllTests() {
48 for (SimpleTest* test = all_tests; test != nullptr; test = test->next_) {
58 SimpleTest() : next_(all_tests) { all_tests = this; }
60 void RecordTestFailure() { passed_ = false; }
63 virtual void Run() = 0;
65 static SimpleTest* all_tests;
71 SimpleTest* SimpleTest::all_tests = nullptr;
73 #define EXPECT_EQ(lhs, rhs) \
75 if ((lhs) != (rhs)) { \
76 RecordTestFailure(); \
80 #define EXPECT_TRUE(expr) EXPECT_EQ(true, expr)
81 #define EXPECT_FALSE(expr) EXPECT_EQ(false, expr)
82 #define EXPECT_STREQ(lhs, rhs) EXPECT_EQ(std::strcmp((lhs), (rhs)), 0)
84 #define TEST(suite, name) \
85 class SimpleTest_##suite##_##name : public SimpleTest { \
86 void Run() override; \
87 } test_##suite##_##name; \
89 void SimpleTest_##suite##_##name::Run()
91 TEST(Algorithm, Basic) {
92 static_assert(std::min(1, 2) == 1);
93 static_assert(std::max(1, 2) == 2);
95 EXPECT_EQ(std::forward<int>(2), 2);
98 TEST(Algorithm, Copy) {
99 constexpr size_t kCopyOffset = 1;
100 std::array<int, 3> foo{3, 2, 1};
101 std::array<int, 5> bar{0};
103 // Ensure zero-element iterator doesn't modify the destination object when
106 std::copy(foo.end(), foo.end(), bar.begin());
107 EXPECT_EQ(foo[0], temp);
109 // Copy a single element.
110 std::array<int, 1> one{-101};
111 std::copy(one.begin(), one.end(), foo.begin());
112 EXPECT_EQ(foo[0], -101);
114 auto copy_end = std::copy(foo.begin(), foo.end(), bar.begin() + kCopyOffset);
115 // Verify the iterator points to the end of the copied region.
116 EXPECT_EQ(copy_end, bar.begin() + foo.size() + kCopyOffset);
118 // Verify all the values were properly copied from foo to bar.
121 for (auto it = bar.begin() + kCopyOffset; it != copy_end; ++it) {
122 EXPECT_EQ(*it, foo[i++]);
127 TEST(Algorithm, Find) {
128 std::array<int, 5> foo{3, 2, 1, 42, 17};
129 // Ensure a value in the middle of the array is properly found.
130 EXPECT_EQ(*std::find(std::begin(foo), std::end(foo), 42), 42);
132 // Ensure the iterator returned by find() matches the expected location of the
134 EXPECT_EQ(std::find(std::begin(foo), std::end(foo), 42), std::begin(foo) + 3);
136 // Ensure an element at the beginning of an array is found.
137 EXPECT_EQ(*std::find(std::begin(foo), std::end(foo), 3), foo[0]);
139 // Ensure an element at the end of an array is found.
140 EXPECT_EQ(*std::find(std::begin(foo), std::end(foo), 17),
141 foo[foo.size() - 1]);
144 TEST(Algorithm, NotFound) {
145 std::array<int, 3> foo{3, 2, 1};
147 // Ensure that if an element is not found, an iterator matching foo.end() is
149 EXPECT_EQ(std::find(std::begin(foo), std::end(foo), -99), std::end(foo));
151 // Ensure that a zero-element iterator range returns the end iterator passed
153 EXPECT_EQ(std::find(std::end(foo), std::end(foo), 3), std::end(foo));
157 constexpr std::array<int, 4> array{0, 1, 2, 3};
159 static_assert(array[2] == 2);
161 for (int i = 0; i < static_cast<int>(array.size()); ++i) {
162 EXPECT_EQ(i, array[i]);
166 TEST(Cmath, Basic) PW_NO_SANITIZE("float-divide-by-zero") {
167 EXPECT_EQ(std::abs(-1), 1);
168 EXPECT_EQ(std::abs(1), 1);
170 // Although Clang/LLVM do not fully support __STDC_IEC_559__, they do have the
171 // necessary IEEE 754 support for floating point division by zero.
172 EXPECT_TRUE(std::isfinite(1.0));
173 EXPECT_FALSE(std::isfinite(1.0 / 0.0));
175 EXPECT_FALSE(std::isnan(1.0));
176 EXPECT_TRUE(std::isnan(0.0 / 0.0));
178 EXPECT_FALSE(std::signbit(1.0));
179 EXPECT_TRUE(std::signbit(-1.0));
182 TEST(Cstddef, Basic) {
185 EXPECT_EQ(foo, byte{12});
188 TEST(Iterator, Basic) {
189 std::array<int, 3> foo{3, 2, 1};
191 EXPECT_EQ(std::data(foo), foo.data());
192 EXPECT_EQ(std::size(foo), foo.size());
194 EXPECT_EQ(*std::begin(foo), foo[0]);
195 EXPECT_EQ(std::end(foo), std::begin(foo) + foo.size());
198 EXPECT_EQ(foo[0], 99);
199 EXPECT_EQ(foo[1], 99);
200 EXPECT_EQ(foo[2], 99);
203 template <typename T>
204 int SumFromInitializerList(std::initializer_list<T> values) {
206 for (auto value : values) {
211 TEST(InitializerList, Empty) {
212 std::initializer_list<int> mt;
213 EXPECT_EQ(0, SumFromInitializerList(mt));
215 EXPECT_EQ(0, SumFromInitializerList<float>({}));
218 TEST(InitializerList, Declared) {
219 std::initializer_list<char> list{'\3', '\3', '\4'};
220 EXPECT_EQ(10, SumFromInitializerList(list));
223 TEST(InitializerList, Inline) {
224 EXPECT_EQ(42, SumFromInitializerList<long>({42}));
225 EXPECT_EQ(2, SumFromInitializerList<bool>({true, false, true}));
226 EXPECT_EQ(15, SumFromInitializerList({1, 2, 3, 4, 5}));
229 TEST(Limits, Basic) {
230 static_assert(std::numeric_limits<unsigned char>::is_specialized);
231 static_assert(std::numeric_limits<unsigned char>::is_integer);
232 static_assert(std::numeric_limits<unsigned char>::min() == 0u);
233 static_assert(std::numeric_limits<unsigned char>::max() == 255u);
235 static_assert(std::numeric_limits<signed char>::is_specialized);
236 static_assert(std::numeric_limits<signed char>::is_integer);
237 static_assert(std::numeric_limits<signed char>::min() == -128);
238 static_assert(std::numeric_limits<signed char>::max() == 127);
240 // Assume 64-bit long long
241 static_assert(std::numeric_limits<long long>::is_specialized);
242 static_assert(std::numeric_limits<long long>::is_integer);
243 static_assert(std::numeric_limits<long long>::min() ==
244 (-9223372036854775807ll - 1));
245 static_assert(std::numeric_limits<long long>::max() == 9223372036854775807ll);
247 static_assert(std::numeric_limits<unsigned long long>::is_specialized);
248 static_assert(std::numeric_limits<unsigned long long>::is_integer);
249 static_assert(std::numeric_limits<unsigned long long>::min() == 0u);
250 static_assert(std::numeric_limits<unsigned long long>::max() ==
251 18446744073709551615ull);
254 TEST(New, PlacementNew) {
255 alignas(sizeof(int)) unsigned char value[sizeof(int)];
256 new (value) int(1234);
259 std::memcpy(&int_value, value, sizeof(int_value));
260 EXPECT_EQ(1234, int_value);
264 unsigned char value[4];
265 int* int_ptr = std::launder(reinterpret_cast<int*>(value));
266 EXPECT_EQ(static_cast<void*>(int_ptr), static_cast<void*>(value));
269 TEST(StringView, Basic) {
270 constexpr std::string_view value("1234567890");
271 static_assert(value.size() == 10);
272 static_assert(value[1] == '2');
274 char buffer[] = "!!!!!";
275 constexpr size_t buffer_size = sizeof(buffer) - 1; // always keep the \0
277 value.copy(buffer, buffer_size, 10);
278 EXPECT_STREQ(buffer, "!!!!!");
280 value.copy(buffer, buffer_size, 9);
281 EXPECT_STREQ(buffer, "0!!!!");
283 value.copy(buffer, buffer_size, 2);
284 EXPECT_STREQ(buffer, "34567");
286 value.copy(buffer, buffer_size);
287 EXPECT_STREQ(buffer, "12345");
290 TEST(TypeTraits, Basic) {
291 static_assert(std::is_integral_v<bool>);
292 static_assert(!std::is_integral_v<float>);
294 static_assert(std::is_floating_point_v<float>);
295 static_assert(!std::is_floating_point_v<bool>);
297 static_assert(std::is_same_v<float, float>);
298 static_assert(!std::is_same_v<char, unsigned char>);
302 MoveTester(int value) : magic_value(value), moved(false) {}
304 MoveTester(const MoveTester&) = default;
306 MoveTester(MoveTester&& other) : magic_value(other.magic_value), moved(true) {
307 other.magic_value = 0xffff;
314 TEST(Utility, Move) {
315 MoveTester test(123);
317 MoveTester copied(test);
318 EXPECT_EQ(copied.magic_value, 123);
319 EXPECT_FALSE(copied.moved);
321 MoveTester moved(std::move(copied));
322 EXPECT_EQ(123, moved.magic_value);
323 EXPECT_EQ(0xffff, copied.magic_value);
324 EXPECT_TRUE(moved.moved);
329 namespace pw::minimal_cpp_stdlib {
331 bool RunAllTests() { return SimpleTest::RunAllTests(); }
333 } // namespace pw::minimal_cpp_stdlib