1 // 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 "pw_unit_test/framework.h"
16
17 #include <cstring>
18
19 namespace pw {
20 namespace {
21
TEST(PigweedTest,ExpectBool)22 TEST(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
TEST(PigweedTest,ExpectBasicComparisons)36 TEST(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
TEST(PigweedTest,ExpectStringEquality)59 TEST(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
TEST(PigweedTest,SucceedAndFailMacros)70 TEST(PigweedTest, SucceedAndFailMacros) {
71 SUCCEED();
72
73 // 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.
75 if (false) {
76 ADD_FAILURE();
77 FAIL();
78 }
79 }
80
TEST(PigweedTest,SkipMacro)81 TEST(PigweedTest, SkipMacro) {
82 GTEST_SKIP();
83 // This code should not run.
84 EXPECT_TRUE(false);
85 }
86
87 class SkipOnSetUpTest : public ::testing::Test {
88 public:
SetUp()89 void SetUp() override { GTEST_SKIP(); }
90 };
91
TEST_F(SkipOnSetUpTest,FailTest)92 TEST_F(SkipOnSetUpTest, FailTest) {
93 // This code should not run because the test was skipped in SetUp().
94 EXPECT_TRUE(false);
95 }
96
97 class NonCopyable {
98 public:
NonCopyable(int value)99 NonCopyable(int value) : value_(value) {}
100
101 NonCopyable(const NonCopyable&) = delete;
102 NonCopyable& operator=(const NonCopyable&) = delete;
103
operator ==(const NonCopyable & rhs) const104 bool operator==(const NonCopyable& rhs) const { return value_ == rhs.value_; }
operator !=(const NonCopyable & rhs) const105 bool operator!=(const NonCopyable& rhs) const { return value_ != rhs.value_; }
106
operator bool() const107 operator bool() const { return value_ > 0; }
108
109 private:
110 const int value_;
111 };
112
TEST(PigweedTest,NonCopyableType)113 TEST(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
Increment(int * i)125 bool Increment(int* i) {
126 (*i)++;
127 return true;
128 }
129
TEST(PigweedTest,MacroArgumentsOnlyAreEvaluatedOnce)130 TEST(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
146 class FixtureTest : public ::testing::Test {
147 public:
FixtureTest()148 FixtureTest() : string_("hello world") {}
149
ReturnTrue()150 bool ReturnTrue() { return true; }
StringLength()151 int StringLength() { return std::strlen(string_); }
152
153 protected:
154 const char* string_;
155 };
156
TEST_F(FixtureTest,CustomFixture)157 TEST_F(FixtureTest, CustomFixture) {
158 EXPECT_TRUE(ReturnTrue());
159 EXPECT_EQ(StringLength(), 11);
160 }
161
162 class PigweedTestFixture : public ::testing::Test {
163 protected:
PigweedTestFixture()164 PigweedTestFixture() : cool_number_(35) {}
165
166 int cool_number_;
167 };
168
TEST_F(PigweedTestFixture,TheNumberIs35)169 TEST_F(PigweedTestFixture, TheNumberIs35) {
170 EXPECT_EQ(cool_number_, 35);
171 cool_number_ += 1;
172 EXPECT_EQ(cool_number_, 36);
173 }
174
TEST_F(PigweedTestFixture,YupTheNumberIs35)175 TEST_F(PigweedTestFixture, YupTheNumberIs35) {
176 EXPECT_EQ(cool_number_, 35);
177 cool_number_ *= 100;
178 EXPECT_EQ(cool_number_, 3500);
179 }
180
181 class Expectations : public ::testing::Test {
182 protected:
Expectations()183 Expectations() : cool_number_(3) { ASSERT_EQ(cool_number_, 3); }
184
~Expectations()185 ~Expectations() { ASSERT_EQ(cool_number_, 14159); }
186
187 int cool_number_;
188 };
189
TEST_F(Expectations,SetCoolNumber)190 TEST_F(Expectations, SetCoolNumber) { cool_number_ = 14159; }
191
192 class SetUpAndTearDown : public ::testing::Test {
193 protected:
SetUpAndTearDown()194 SetUpAndTearDown() : value_(0) { EXPECT_EQ(value_, 0); }
195
~SetUpAndTearDown()196 ~SetUpAndTearDown() { EXPECT_EQ(value_, 1); }
197
SetUp()198 void SetUp() override { value_ = 1337; }
199
TearDown()200 void TearDown() override { value_ = 1; }
201
202 int value_;
203 };
204
TEST_F(SetUpAndTearDown,MakeSureItIsSet)205 TEST_F(SetUpAndTearDown, MakeSureItIsSet) {
206 EXPECT_EQ(value_, 1337);
207 value_ = 3210;
208 }
209
210 } // namespace
211 } // namespace pw
212