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 <cstring>
16
17 #include "gtest/gtest.h"
18 #include "pw_assert/check.h"
19
20 namespace pw {
21 namespace {
22
TEST(PigweedTest,ExpectBool)23 TEST(PigweedTest, ExpectBool) {
24 EXPECT_TRUE(true);
25 EXPECT_FALSE(false);
26
27 EXPECT_TRUE(1);
28 EXPECT_TRUE(1203492);
29 EXPECT_TRUE(-1);
30 EXPECT_TRUE(0.1f);
31
32 EXPECT_FALSE(0);
33 EXPECT_FALSE(0.0f);
34 EXPECT_FALSE(-0.0f);
35 }
36
TEST(PigweedTest,ExpectBasicComparisons)37 TEST(PigweedTest, ExpectBasicComparisons) {
38 EXPECT_EQ(1, 1 + 0);
39 ASSERT_EQ(1, 1 + 0);
40
41 EXPECT_EQ(0.0f, -0.0f);
42 ASSERT_EQ(0.0f, -0.0f);
43
44 EXPECT_NE(-1, 0);
45 ASSERT_NE(-1, 0);
46
47 EXPECT_GT(2, 1);
48 ASSERT_GT(3, 0);
49
50 EXPECT_GE(1, 1);
51 ASSERT_GE(3, 0);
52
53 EXPECT_LT(0, 1);
54 ASSERT_LT(-2, 1209);
55
56 EXPECT_LE(-1, 0);
57 ASSERT_LE(-2, -2);
58 }
59
TEST(PigweedTest,ExpectStringEquality)60 TEST(PigweedTest, ExpectStringEquality) {
61 EXPECT_STREQ("", "");
62 EXPECT_STREQ("Yes", "Yes");
63
64 char no[] = {'N', 'o', '\0'};
65 ASSERT_STREQ("No", no);
66
67 EXPECT_STRNE("NO", "no");
68 ASSERT_STRNE("yes", no);
69 }
70
TEST(PigweedTest,SucceedAndFailMacros)71 TEST(PigweedTest, SucceedAndFailMacros) {
72 SUCCEED();
73
74 // The ADD_FAILURE() and FAIL() macros cause a test to fail if they are
75 // reached. Use them, but don't let them run so that this test still passes.
76 if (false) {
77 ADD_FAILURE();
78 FAIL();
79 }
80 }
81
TEST(PigweedTest,SkipMacro)82 TEST(PigweedTest, SkipMacro) {
83 GTEST_SKIP();
84 // This code should not run.
85 EXPECT_TRUE(false);
86 }
87
88 class SkipOnSetUpTest : public ::testing::Test {
89 public:
SetUp()90 void SetUp() override { GTEST_SKIP(); }
91 };
92
TEST_F(SkipOnSetUpTest,FailTest)93 TEST_F(SkipOnSetUpTest, FailTest) {
94 // This code should not run because the test was skipped in SetUp().
95 EXPECT_TRUE(false);
96 }
97
98 class NonCopyable {
99 public:
NonCopyable(int value)100 NonCopyable(int value) : value_(value) {}
101
102 NonCopyable(const NonCopyable&) = delete;
103 NonCopyable& operator=(const NonCopyable&) = delete;
104
operator ==(const NonCopyable & rhs) const105 bool operator==(const NonCopyable& rhs) const { return value_ == rhs.value_; }
operator !=(const NonCopyable & rhs) const106 bool operator!=(const NonCopyable& rhs) const { return value_ != rhs.value_; }
107
operator bool() const108 operator bool() const { return value_ > 0; }
109
110 private:
111 const int value_;
112 };
113
TEST(PigweedTest,NonCopyableType)114 TEST(PigweedTest, NonCopyableType) {
115 EXPECT_TRUE(NonCopyable(6));
116 EXPECT_FALSE(NonCopyable(-1));
117
118 const NonCopyable this_one(100);
119 EXPECT_EQ(this_one, this_one);
120 EXPECT_TRUE(this_one);
121
122 EXPECT_EQ(NonCopyable(5), NonCopyable(5));
123 EXPECT_NE(NonCopyable(5), NonCopyable(6));
124 }
125
Increment(int * i)126 bool Increment(int* i) {
127 (*i)++;
128 return true;
129 }
130
TEST(PigweedTest,MacroArgumentsOnlyAreEvaluatedOnce)131 TEST(PigweedTest, MacroArgumentsOnlyAreEvaluatedOnce) {
132 int i = 1;
133
134 EXPECT_TRUE(Increment(&i));
135 EXPECT_EQ(i, 2);
136 ASSERT_TRUE(Increment(&i));
137 EXPECT_EQ(i, 3);
138
139 EXPECT_EQ(0x600dbeef, [&i]() {
140 i += 1;
141 return 0x600dbeef;
142 }());
143
144 EXPECT_EQ(i, 4);
145 }
146
147 class FixtureTest : public ::testing::Test {
148 public:
FixtureTest()149 FixtureTest() : string_("hello world") {}
150
ReturnTrue()151 bool ReturnTrue() { return true; }
StringLength()152 int StringLength() { return std::strlen(string_); }
153
154 protected:
155 const char* string_;
156 };
157
TEST_F(FixtureTest,CustomFixture)158 TEST_F(FixtureTest, CustomFixture) {
159 EXPECT_TRUE(ReturnTrue());
160 EXPECT_EQ(StringLength(), 11);
161 }
162
163 class PigweedTestFixture : public ::testing::Test {
164 protected:
PigweedTestFixture()165 PigweedTestFixture() : cool_number_(35) {}
166
167 int cool_number_;
168 };
169
TEST_F(PigweedTestFixture,TheNumberIs35)170 TEST_F(PigweedTestFixture, TheNumberIs35) {
171 EXPECT_EQ(cool_number_, 35);
172 cool_number_ += 1;
173 EXPECT_EQ(cool_number_, 36);
174 }
175
TEST_F(PigweedTestFixture,YupTheNumberIs35)176 TEST_F(PigweedTestFixture, YupTheNumberIs35) {
177 EXPECT_EQ(cool_number_, 35);
178 cool_number_ *= 100;
179 EXPECT_EQ(cool_number_, 3500);
180 }
181
182 class Expectations : public ::testing::Test {
183 protected:
Expectations()184 Expectations() : cool_number_(3) { PW_CHECK_INT_EQ(cool_number_, 3); }
185
~Expectations()186 ~Expectations() override { PW_CHECK_INT_EQ(cool_number_, 14159); }
187
188 int cool_number_;
189 };
190
TEST_F(Expectations,SetCoolNumber)191 TEST_F(Expectations, SetCoolNumber) { cool_number_ = 14159; }
192
193 class SetUpAndTearDown : public ::testing::Test {
194 protected:
SetUpAndTearDown()195 SetUpAndTearDown() : value_(0) { EXPECT_EQ(value_, 0); }
196
~SetUpAndTearDown()197 ~SetUpAndTearDown() override { EXPECT_EQ(value_, 1); }
198
SetUp()199 void SetUp() override { value_ = 1337; }
200
TearDown()201 void TearDown() override { value_ = 1; }
202
203 int value_;
204 };
205
TEST_F(SetUpAndTearDown,MakeSureItIsSet)206 TEST_F(SetUpAndTearDown, MakeSureItIsSet) {
207 EXPECT_EQ(value_, 1337);
208 value_ = 3210;
209 }
210
211 } // namespace
212 } // namespace pw
213