blob: 1d9c6c3190fa67b9418e5e6d84799895610a5a3a [file] [log] [blame]
Wyatt Hepler0412a7d2020-01-28 16:27:32 -08001// Copyright 2019 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 all of the provided headers, even if they aren't tested.
16#include <algorithm>
17#include <array>
18#include <cinttypes>
19#include <cmath>
20#include <cstdarg>
21#include <cstddef>
22#include <cstdint>
23#include <cstdio>
24#include <cstring>
25#include <iterator>
26#include <limits>
27#include <new>
28#include <string_view>
29#include <type_traits>
30#include <utility>
31
32#include "gtest/gtest.h"
33
34namespace {
35
36TEST(Algorithm, Basic) {
37 static_assert(std::min(1, 2) == 1);
38 static_assert(std::max(1, 2) == 2);
39
40 EXPECT_EQ(std::forward<int>(2), 2);
41}
42
43TEST(Array, Basic) {
44 constexpr std::array<int, 4> array{0, 1, 2, 3};
45
46 static_assert(array[2] == 2);
47
48 for (int i = 0; i < static_cast<int>(array.size()); ++i) {
49 EXPECT_EQ(i, array[i]);
50 }
51}
52
53TEST(Cmath, Basic) {
54 EXPECT_EQ(std::abs(-1), 1);
55 EXPECT_EQ(std::abs(1), 1);
56
57 EXPECT_TRUE(std::isfinite(1.0));
58 EXPECT_FALSE(std::isfinite(1.0 / 0.0));
59
60 EXPECT_FALSE(std::isnan(1.0));
61 EXPECT_TRUE(std::isnan(0.0 / 0.0));
62
63 EXPECT_FALSE(std::signbit(1.0));
64 EXPECT_TRUE(std::signbit(-1.0));
65}
66
67TEST(Cstddef, Basic) {
68 using std::byte;
69 byte foo = byte{12};
70 EXPECT_EQ(foo, byte{12});
71}
72
73TEST(Iterator, Basic) {
74 std::array<int, 3> foo{3, 2, 1};
75
76 EXPECT_EQ(std::data(foo), foo.data());
77 EXPECT_EQ(std::size(foo), foo.size());
78
79 foo.fill(99);
80 EXPECT_EQ(foo[0], 99);
81 EXPECT_EQ(foo[1], 99);
82 EXPECT_EQ(foo[2], 99);
83}
84
85TEST(Limits, Basic) {
86 static_assert(std::numeric_limits<unsigned char>::is_specialized);
87 static_assert(std::numeric_limits<unsigned char>::is_integer);
88 static_assert(std::numeric_limits<unsigned char>::min() == 0u);
89 static_assert(std::numeric_limits<unsigned char>::max() == 255u);
90
91 static_assert(std::numeric_limits<signed char>::is_specialized);
92 static_assert(std::numeric_limits<signed char>::is_integer);
93 static_assert(std::numeric_limits<signed char>::min() == -128);
94 static_assert(std::numeric_limits<signed char>::max() == 127);
95
96 // Assume 64-bit long long
97 static_assert(std::numeric_limits<long long>::is_specialized);
98 static_assert(std::numeric_limits<long long>::is_integer);
99 static_assert(std::numeric_limits<long long>::min() ==
100 (-9223372036854775807ll - 1));
101 static_assert(std::numeric_limits<long long>::max() == 9223372036854775807ll);
102
103 static_assert(std::numeric_limits<unsigned long long>::is_specialized);
104 static_assert(std::numeric_limits<unsigned long long>::is_integer);
105 static_assert(std::numeric_limits<unsigned long long>::min() == 0u);
106 static_assert(std::numeric_limits<unsigned long long>::max() ==
107 18446744073709551615ull);
108}
109
110TEST(New, Basic) {
111 unsigned char value[4];
112 new (value) int(1234);
113
114 int int_value;
115 std::memcpy(&int_value, value, sizeof(int_value));
116 EXPECT_EQ(1234, int_value);
117}
118
119TEST(StringView, Basic) {
120 constexpr std::string_view value("1234567890");
121 static_assert(value.size() == 10);
122 static_assert(value[1] == '2');
123
124 char buffer[] = "!!!!!";
125 constexpr size_t buffer_size = sizeof(buffer) - 1; // always keep the \0
126
127 value.copy(buffer, buffer_size, 10);
128 EXPECT_STREQ(buffer, "!!!!!");
129
130 value.copy(buffer, buffer_size, 9);
131 EXPECT_STREQ(buffer, "0!!!!");
132
133 value.copy(buffer, buffer_size, 2);
134 EXPECT_STREQ(buffer, "34567");
135
136 value.copy(buffer, buffer_size);
137 EXPECT_STREQ(buffer, "12345");
138}
139
140TEST(TypeTraits, Basic) {
141 static_assert(std::is_integral_v<bool>);
142 static_assert(!std::is_integral_v<float>);
143
144 static_assert(std::is_floating_point_v<float>);
145 static_assert(!std::is_floating_point_v<bool>);
146
147 static_assert(std::is_same_v<float, float>);
148 static_assert(!std::is_same_v<char, unsigned char>);
149}
150
151} // namespace