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
81 class NonCopyable {
82 public:
NonCopyable(int value)83 NonCopyable(int value) : value_(value) {}
84
85 NonCopyable(const NonCopyable&) = delete;
86 NonCopyable& operator=(const NonCopyable&) = delete;
87
operator ==(const NonCopyable & rhs) const88 bool operator==(const NonCopyable& rhs) const { return value_ == rhs.value_; }
operator !=(const NonCopyable & rhs) const89 bool operator!=(const NonCopyable& rhs) const { return value_ != rhs.value_; }
90
operator bool() const91 operator bool() const { return value_ > 0; }
92
93 private:
94 const int value_;
95 };
96
TEST(PigweedTest,NonCopyableType)97 TEST(PigweedTest, NonCopyableType) {
98 EXPECT_TRUE(NonCopyable(6));
99 EXPECT_FALSE(NonCopyable(-1));
100
101 const NonCopyable this_one(100);
102 EXPECT_EQ(this_one, this_one);
103 EXPECT_TRUE(this_one);
104
105 EXPECT_EQ(NonCopyable(5), NonCopyable(5));
106 EXPECT_NE(NonCopyable(5), NonCopyable(6));
107 }
108
Increment(int * i)109 bool Increment(int* i) {
110 (*i)++;
111 return true;
112 }
113
TEST(PigweedTest,MacroArgumentsOnlyAreEvaluatedOnce)114 TEST(PigweedTest, MacroArgumentsOnlyAreEvaluatedOnce) {
115 int i = 1;
116
117 EXPECT_TRUE(Increment(&i));
118 EXPECT_EQ(i, 2);
119 ASSERT_TRUE(Increment(&i));
120 EXPECT_EQ(i, 3);
121
122 EXPECT_EQ(0x600dbeef, [&i]() {
123 i += 1;
124 return 0x600dbeef;
125 }());
126
127 EXPECT_EQ(i, 4);
128 }
129
130 class FixtureTest : public ::testing::Test {
131 public:
FixtureTest()132 FixtureTest() : string_("hello world") {}
133
ReturnTrue()134 bool ReturnTrue() { return true; }
StringLength()135 int StringLength() { return std::strlen(string_); }
136
137 protected:
138 const char* string_;
139 };
140
TEST_F(FixtureTest,CustomFixture)141 TEST_F(FixtureTest, CustomFixture) {
142 EXPECT_TRUE(ReturnTrue());
143 EXPECT_EQ(StringLength(), 11);
144 }
145
146 class PigweedTestFixture : public ::testing::Test {
147 protected:
PigweedTestFixture()148 PigweedTestFixture() : cool_number_(35) {}
149
150 int cool_number_;
151 };
152
TEST_F(PigweedTestFixture,TheNumberIs35)153 TEST_F(PigweedTestFixture, TheNumberIs35) {
154 EXPECT_EQ(cool_number_, 35);
155 cool_number_ += 1;
156 EXPECT_EQ(cool_number_, 36);
157 }
158
TEST_F(PigweedTestFixture,YupTheNumberIs35)159 TEST_F(PigweedTestFixture, YupTheNumberIs35) {
160 EXPECT_EQ(cool_number_, 35);
161 cool_number_ *= 100;
162 EXPECT_EQ(cool_number_, 3500);
163 }
164
165 class Expectations : public ::testing::Test {
166 protected:
Expectations()167 Expectations() : cool_number_(3) { ASSERT_EQ(cool_number_, 3); }
168
~Expectations()169 ~Expectations() { ASSERT_EQ(cool_number_, 14159); }
170
171 int cool_number_;
172 };
173
TEST_F(Expectations,SetCoolNumber)174 TEST_F(Expectations, SetCoolNumber) { cool_number_ = 14159; }
175
176 class SetUpAndTearDown : public ::testing::Test {
177 protected:
SetUpAndTearDown()178 SetUpAndTearDown() : value_(0) { EXPECT_EQ(value_, 0); }
179
~SetUpAndTearDown()180 ~SetUpAndTearDown() { EXPECT_EQ(value_, 1); }
181
SetUp()182 void SetUp() override { value_ = 1337; }
183
TearDown()184 void TearDown() override { value_ = 1; }
185
186 int value_;
187 };
188
TEST_F(SetUpAndTearDown,MakeSureItIsSet)189 TEST_F(SetUpAndTearDown, MakeSureItIsSet) {
190 EXPECT_EQ(value_, 1337);
191 value_ = 3210;
192 }
193
194 } // namespace
195 } // namespace pw
196