blob: d287859a52e68493d168aeb4ebeb24a335250454 [file] [log] [blame]
Alexei Frolovc10c8122019-11-01 16:31:19 -07001// Copyright 2019 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
Wyatt Hepler1a960942019-11-26 14:13:38 -08004// use this file except in compliance with the License. You may obtain a copy of
5// the License at
Alexei Frolovc10c8122019-11-01 16:31:19 -07006//
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
Wyatt Hepler1a960942019-11-26 14:13:38 -080012// License for the specific language governing permissions and limitations under
13// the License.
Alexei Frolovc10c8122019-11-01 16:31:19 -070014
15#include "pw_unit_test/framework.h"
16
17#include <cstring>
18
19namespace pw {
20namespace {
21
22TEST(PigweedTest, ExpectBool) {
23 EXPECT_TRUE(true);
24 EXPECT_FALSE(false);
25
26 EXPECT_TRUE(1);
27 EXPECT_TRUE(1203492);
28 EXPECT_TRUE(-1);
29 EXPECT_TRUE(0.1f);
30
31 EXPECT_FALSE(0);
32 EXPECT_FALSE(0.0f);
33 EXPECT_FALSE(-0.0f);
34}
35
36TEST(PigweedTest, ExpectBasicComparisons) {
37 EXPECT_EQ(1, 1 + 0);
38 ASSERT_EQ(1, 1 + 0);
39
40 EXPECT_EQ(0.0f, -0.0f);
41 ASSERT_EQ(0.0f, -0.0f);
42
43 EXPECT_NE(-1, 0);
44 ASSERT_NE(-1, 0);
45
46 EXPECT_GT(2, 1);
47 ASSERT_GT(3, 0);
48
49 EXPECT_GE(1, 1);
50 ASSERT_GE(3, 0);
51
52 EXPECT_LT(0, 1);
53 ASSERT_LT(-2, 1209);
54
55 EXPECT_LE(-1, 0);
56 ASSERT_LE(-2, -2);
57}
58
59TEST(PigweedTest, ExpectStringEquality) {
60 EXPECT_STREQ("", "");
61 EXPECT_STREQ("Yes", "Yes");
62
63 char no[] = {'N', 'o', '\0'};
64 ASSERT_STREQ("No", no);
65
66 EXPECT_STRNE("NO", "no");
67 ASSERT_STRNE("yes", no);
68}
69
Wyatt Heplerf9c64d12020-01-03 17:26:17 -080070TEST(PigweedTest, SucceedAndFailMacros) {
Wyatt Heplerb3fca3a2020-01-03 12:14:00 -080071 SUCCEED();
72
Wyatt Heplerf9c64d12020-01-03 17:26:17 -080073 // The ADD_FAILURE() and FAIL() macros cause a test to fail if they are
74 // reached. Use them, but don't let them run so that this test still passes.
Wyatt Heplerb3fca3a2020-01-03 12:14:00 -080075 if (false) {
76 ADD_FAILURE();
77 FAIL();
78 }
79}
80
Alex Deymo8b4bb782022-02-09 00:27:45 +010081TEST(PigweedTest, SkipMacro) {
82 GTEST_SKIP();
83 // This code should not run.
84 EXPECT_TRUE(false);
85}
86
Alex Deymob0ae0112022-02-16 17:31:21 +010087class SkipOnSetUpTest : public ::testing::Test {
88 public:
89 void SetUp() override { GTEST_SKIP(); }
90};
91
92TEST_F(SkipOnSetUpTest, FailTest) {
93 // This code should not run because the test was skipped in SetUp().
94 EXPECT_TRUE(false);
95}
96
Alexei Frolovc10c8122019-11-01 16:31:19 -070097class NonCopyable {
98 public:
99 NonCopyable(int value) : value_(value) {}
100
101 NonCopyable(const NonCopyable&) = delete;
102 NonCopyable& operator=(const NonCopyable&) = delete;
103
104 bool operator==(const NonCopyable& rhs) const { return value_ == rhs.value_; }
105 bool operator!=(const NonCopyable& rhs) const { return value_ != rhs.value_; }
106
107 operator bool() const { return value_ > 0; }
108
109 private:
110 const int value_;
111};
112
113TEST(PigweedTest, NonCopyableType) {
114 EXPECT_TRUE(NonCopyable(6));
115 EXPECT_FALSE(NonCopyable(-1));
116
117 const NonCopyable this_one(100);
118 EXPECT_EQ(this_one, this_one);
119 EXPECT_TRUE(this_one);
120
121 EXPECT_EQ(NonCopyable(5), NonCopyable(5));
122 EXPECT_NE(NonCopyable(5), NonCopyable(6));
123}
124
125bool Increment(int* i) {
126 (*i)++;
127 return true;
128}
129
130TEST(PigweedTest, MacroArgumentsOnlyAreEvaluatedOnce) {
131 int i = 1;
132
133 EXPECT_TRUE(Increment(&i));
134 EXPECT_EQ(i, 2);
135 ASSERT_TRUE(Increment(&i));
136 EXPECT_EQ(i, 3);
137
138 EXPECT_EQ(0x600dbeef, [&i]() {
139 i += 1;
140 return 0x600dbeef;
141 }());
142
143 EXPECT_EQ(i, 4);
144}
145
146class FixtureTest : public ::testing::Test {
147 public:
148 FixtureTest() : string_("hello world") {}
149
150 bool ReturnTrue() { return true; }
151 int StringLength() { return std::strlen(string_); }
152
153 protected:
154 const char* string_;
155};
156
157TEST_F(FixtureTest, CustomFixture) {
158 EXPECT_TRUE(ReturnTrue());
159 EXPECT_EQ(StringLength(), 11);
160}
161
162class PigweedTestFixture : public ::testing::Test {
163 protected:
164 PigweedTestFixture() : cool_number_(35) {}
165
166 int cool_number_;
167};
168
169TEST_F(PigweedTestFixture, TheNumberIs35) {
170 EXPECT_EQ(cool_number_, 35);
171 cool_number_ += 1;
172 EXPECT_EQ(cool_number_, 36);
173}
174
175TEST_F(PigweedTestFixture, YupTheNumberIs35) {
176 EXPECT_EQ(cool_number_, 35);
177 cool_number_ *= 100;
178 EXPECT_EQ(cool_number_, 3500);
179}
180
Wyatt Heplercad29b42020-02-03 17:21:48 -0800181class Expectations : public ::testing::Test {
182 protected:
183 Expectations() : cool_number_(3) { ASSERT_EQ(cool_number_, 3); }
184
185 ~Expectations() { ASSERT_EQ(cool_number_, 14159); }
186
187 int cool_number_;
188};
189
190TEST_F(Expectations, SetCoolNumber) { cool_number_ = 14159; }
191
Wyatt Heplerfc80d922020-02-11 20:19:26 -0800192class SetUpAndTearDown : public ::testing::Test {
193 protected:
194 SetUpAndTearDown() : value_(0) { EXPECT_EQ(value_, 0); }
195
196 ~SetUpAndTearDown() { EXPECT_EQ(value_, 1); }
197
198 void SetUp() override { value_ = 1337; }
199
200 void TearDown() override { value_ = 1; }
201
202 int value_;
203};
204
205TEST_F(SetUpAndTearDown, MakeSureItIsSet) {
206 EXPECT_EQ(value_, 1337);
207 value_ = 3210;
208}
209
Alexei Frolovc10c8122019-11-01 16:31:19 -0700210} // namespace
211} // namespace pw