blob: 1c0a1ce4dfef34de5e00bbacbd81f9502c290966 [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
Alexei Frolovc10c8122019-11-01 16:31:19 -070081class NonCopyable {
82 public:
83 NonCopyable(int value) : value_(value) {}
84
85 NonCopyable(const NonCopyable&) = delete;
86 NonCopyable& operator=(const NonCopyable&) = delete;
87
88 bool operator==(const NonCopyable& rhs) const { return value_ == rhs.value_; }
89 bool operator!=(const NonCopyable& rhs) const { return value_ != rhs.value_; }
90
91 operator bool() const { return value_ > 0; }
92
93 private:
94 const int value_;
95};
96
97TEST(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
109bool Increment(int* i) {
110 (*i)++;
111 return true;
112}
113
114TEST(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
130class FixtureTest : public ::testing::Test {
131 public:
132 FixtureTest() : string_("hello world") {}
133
134 bool ReturnTrue() { return true; }
135 int StringLength() { return std::strlen(string_); }
136
137 protected:
138 const char* string_;
139};
140
141TEST_F(FixtureTest, CustomFixture) {
142 EXPECT_TRUE(ReturnTrue());
143 EXPECT_EQ(StringLength(), 11);
144}
145
146class PigweedTestFixture : public ::testing::Test {
147 protected:
148 PigweedTestFixture() : cool_number_(35) {}
149
150 int cool_number_;
151};
152
153TEST_F(PigweedTestFixture, TheNumberIs35) {
154 EXPECT_EQ(cool_number_, 35);
155 cool_number_ += 1;
156 EXPECT_EQ(cool_number_, 36);
157}
158
159TEST_F(PigweedTestFixture, YupTheNumberIs35) {
160 EXPECT_EQ(cool_number_, 35);
161 cool_number_ *= 100;
162 EXPECT_EQ(cool_number_, 3500);
163}
164
Wyatt Heplercad29b42020-02-03 17:21:48 -0800165class Expectations : public ::testing::Test {
166 protected:
167 Expectations() : cool_number_(3) { ASSERT_EQ(cool_number_, 3); }
168
169 ~Expectations() { ASSERT_EQ(cool_number_, 14159); }
170
171 int cool_number_;
172};
173
174TEST_F(Expectations, SetCoolNumber) { cool_number_ = 14159; }
175
Wyatt Heplerfc80d922020-02-11 20:19:26 -0800176class SetUpAndTearDown : public ::testing::Test {
177 protected:
178 SetUpAndTearDown() : value_(0) { EXPECT_EQ(value_, 0); }
179
180 ~SetUpAndTearDown() { EXPECT_EQ(value_, 1); }
181
182 void SetUp() override { value_ = 1337; }
183
184 void TearDown() override { value_ = 1; }
185
186 int value_;
187};
188
189TEST_F(SetUpAndTearDown, MakeSureItIsSet) {
190 EXPECT_EQ(value_, 1337);
191 value_ = 3210;
192}
193
Alexei Frolovc10c8122019-11-01 16:31:19 -0700194} // namespace
195} // namespace pw