• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "pw_assert/check.h"
20 #include "pw_status/status.h"
21 
22 namespace pw {
23 namespace {
24 
TEST(PigweedTest,ExpectBool)25 TEST(PigweedTest, ExpectBool) {
26   EXPECT_TRUE(true);
27   EXPECT_FALSE(false);
28 
29   EXPECT_TRUE(1);
30   EXPECT_TRUE(1203492);
31   EXPECT_TRUE(-1);
32   EXPECT_TRUE(0.1f);
33 
34   EXPECT_FALSE(0);
35   EXPECT_FALSE(0.0f);
36   EXPECT_FALSE(-0.0f);
37 }
38 
TEST(PigweedTest,ExpectBasicComparisons)39 TEST(PigweedTest, ExpectBasicComparisons) {
40   EXPECT_EQ(1, 1 + 0);
41   ASSERT_EQ(1, 1 + 0);
42 
43   EXPECT_EQ(0.0f, -0.0f);
44   ASSERT_EQ(0.0f, -0.0f);
45 
46   EXPECT_NE(-1, 0);
47   ASSERT_NE(-1, 0);
48 
49   EXPECT_GT(2, 1);
50   ASSERT_GT(3, 0);
51 
52   EXPECT_GE(1, 1);
53   ASSERT_GE(3, 0);
54 
55   EXPECT_LT(0, 1);
56   ASSERT_LT(-2, 1209);
57 
58   EXPECT_LE(-1, 0);
59   ASSERT_LE(-2, -2);
60 }
61 
TEST(PigweedTest,ExpectNearComparisons)62 TEST(PigweedTest, ExpectNearComparisons) {
63   EXPECT_NEAR(1, 2, 1);
64   ASSERT_NEAR(1, 2, 1);
65 
66   EXPECT_NEAR(-5, 5, 10);
67   ASSERT_NEAR(-5, 5, 10);
68 
69   int x = 17;
70   int epsilon = 5;
71 
72   EXPECT_NEAR(x, 15, epsilon);
73   ASSERT_NEAR(x, 15, epsilon);
74 }
75 
TEST(PigweedTest,ExpectFloatComparisons)76 TEST(PigweedTest, ExpectFloatComparisons) {
77   EXPECT_FLOAT_EQ(5.0f, 10.0f / 2);
78   ASSERT_FLOAT_EQ(5.0f, 10.0f / 2);
79 
80   EXPECT_FLOAT_EQ(-0.5f, -5.0f / 10);
81   ASSERT_FLOAT_EQ(-0.5f, -5.0f / 10);
82 
83   float x = 17.0f / 20.0f;
84 
85   EXPECT_FLOAT_EQ(x, 17.0f / 20.0f);
86   ASSERT_FLOAT_EQ(x, 17.0f / 20.0f);
87 }
88 
TEST(PigweedTest,ExpectDoubleComparisons)89 TEST(PigweedTest, ExpectDoubleComparisons) {
90   EXPECT_DOUBLE_EQ(5.0, 10.0 / 2);
91   ASSERT_DOUBLE_EQ(5.0, 10.0 / 2);
92 
93   EXPECT_DOUBLE_EQ(-0.5, -5.0 / 10);
94   ASSERT_DOUBLE_EQ(-0.5, -5.0 / 10);
95 
96   double x = 17.0 / 20.0;
97 
98   EXPECT_DOUBLE_EQ(x, 17.0 / 20.0);
99   ASSERT_DOUBLE_EQ(x, 17.0 / 20.0);
100 }
101 
TEST(PigweedTest,ExpectStringEquality)102 TEST(PigweedTest, ExpectStringEquality) {
103   EXPECT_STREQ("", "");
104   EXPECT_STREQ("Yes", "Yes");
105 
106   char no[] = {'N', 'o', '\0'};
107   ASSERT_STREQ("No", no);
108 
109   EXPECT_STRNE("NO", "no");
110   ASSERT_STRNE("yes", no);
111 
112   const char* invalid_string = nullptr;
113   EXPECT_STREQ(invalid_string, nullptr);
114   EXPECT_STRNE("abc", nullptr);
115 }
116 
TEST(PigweedTest,SucceedAndFailMacros)117 TEST(PigweedTest, SucceedAndFailMacros) {
118   SUCCEED();
119 
120   // The ADD_FAILURE() and FAIL() macros cause a test to fail if they are
121   // reached. Use them, but don't let them run so that this test still passes.
122   if (false) {
123     ADD_FAILURE();
124     FAIL();
125   }
126 }
127 
TEST(PigweedTest,SkipMacro)128 TEST(PigweedTest, SkipMacro) {
129   GTEST_SKIP();
130   // This code should not run.
131   EXPECT_TRUE(false);
132 }
133 
TEST(PigweedTest,Logs)134 TEST(PigweedTest, Logs) {
135   EXPECT_TRUE(true) << "This message is ignored";
136   EXPECT_FALSE(false) << "This message is ignored";
137   EXPECT_EQ(0, 0) << "This message is ignored";
138   EXPECT_NE(0, 1) << "This message is ignored";
139   EXPECT_GT(1, 0) << "This message is ignored";
140   EXPECT_GE(0, 0) << "This message is ignored";
141   EXPECT_LT(0, 1) << "This message is ignored";
142   EXPECT_LE(0, 0) << "This message is ignored";
143   EXPECT_STREQ("", "") << "This message is ignored";
144   EXPECT_STRNE("", "?") << "This message is ignored";
145 
146   ASSERT_TRUE(true) << "This message is ignored";
147   ASSERT_FALSE(false) << "This message is ignored";
148   ASSERT_EQ(0, 0) << "This message is ignored";
149   ASSERT_NE(0, 1) << "This message is ignored";
150   ASSERT_GT(1, 0) << "This message is ignored";
151   ASSERT_GE(0, 0) << "This message is ignored";
152   ASSERT_LT(0, 1) << "This message is ignored";
153   ASSERT_LE(0, 0) << "This message is ignored";
154   ASSERT_STREQ("", "") << "This message is ignored";
155   ASSERT_STRNE("", "?") << "This message is ignored";
156 
157   if (false) {
158     ADD_FAILURE() << "This failed!" << 123;
159     GTEST_FAIL() << "This failed!" << 123 << '?';
160     GTEST_SKIP() << 1.0f << " skips!";
161   }
162   GTEST_SUCCEED() << "This message is ignored";
163 }
164 
165 class SkipOnSetUpTest : public ::testing::Test {
166  public:
SetUp()167   void SetUp() override { GTEST_SKIP(); }
168 };
169 
TEST_F(SkipOnSetUpTest,FailTest)170 TEST_F(SkipOnSetUpTest, FailTest) {
171   // This code should not run because the test was skipped in SetUp().
172   EXPECT_TRUE(false);
173 }
174 
175 class NonCopyable {
176  public:
NonCopyable(int value)177   NonCopyable(int value) : value_(value) {}
178 
179   NonCopyable(const NonCopyable&) = delete;
180   NonCopyable& operator=(const NonCopyable&) = delete;
181 
operator ==(const NonCopyable & rhs) const182   bool operator==(const NonCopyable& rhs) const { return value_ == rhs.value_; }
operator !=(const NonCopyable & rhs) const183   bool operator!=(const NonCopyable& rhs) const { return value_ != rhs.value_; }
184 
operator bool() const185   operator bool() const { return value_ > 0; }
186 
187  private:
188   const int value_;
189 };
190 
TEST(PigweedTest,NonCopyableType)191 TEST(PigweedTest, NonCopyableType) {
192   EXPECT_TRUE(NonCopyable(6));
193   EXPECT_FALSE(NonCopyable(-1));
194 
195   const NonCopyable this_one(100);
196   EXPECT_EQ(this_one, this_one);
197   EXPECT_TRUE(this_one);
198 
199   EXPECT_EQ(NonCopyable(5), NonCopyable(5));
200   EXPECT_NE(NonCopyable(5), NonCopyable(6));
201 }
202 
Increment(int * i)203 bool Increment(int* i) {
204   (*i)++;
205   return true;
206 }
207 
TEST(PigweedTest,MacroArgumentsOnlyAreEvaluatedOnce)208 TEST(PigweedTest, MacroArgumentsOnlyAreEvaluatedOnce) {
209   int i = 1;
210 
211   EXPECT_TRUE(Increment(&i));
212   EXPECT_EQ(i, 2);
213   ASSERT_TRUE(Increment(&i));
214   EXPECT_EQ(i, 3);
215 
216   EXPECT_EQ(0x600dbeef, [&i]() {
217     i += 1;
218     return 0x600dbeef;
219   }());
220 
221   EXPECT_EQ(i, 4);
222 }
223 
224 class ClassWithPrivateMethod {
225   FRIEND_TEST(FixtureTest, FriendClass);
226 
227  private:
Return314()228   int Return314() { return 314; }
229 };
230 
231 class FixtureTest : public ::testing::Test {
232  public:
FixtureTest()233   FixtureTest() : string_("hello world") {}
234 
ReturnTrue()235   bool ReturnTrue() { return true; }
StringLength()236   int StringLength() { return std::strlen(string_); }
237 
238  protected:
239   const char* string_;
240 };
241 
TEST_F(FixtureTest,CustomFixture)242 TEST_F(FixtureTest, CustomFixture) {
243   EXPECT_TRUE(ReturnTrue());
244   EXPECT_EQ(StringLength(), 11);
245 }
246 
TEST_F(FixtureTest,FriendClass)247 TEST_F(FixtureTest, FriendClass) {
248   EXPECT_EQ(ClassWithPrivateMethod().Return314(), 314);
249 }
250 
251 class PigweedTestFixture : public ::testing::Test {
252  protected:
PigweedTestFixture()253   PigweedTestFixture() : cool_number_(35) {}
254 
255   int cool_number_;
256 };
257 
TEST_F(PigweedTestFixture,TheNumberIs35)258 TEST_F(PigweedTestFixture, TheNumberIs35) {
259   EXPECT_EQ(cool_number_, 35);
260   cool_number_ += 1;
261   EXPECT_EQ(cool_number_, 36);
262 }
263 
TEST_F(PigweedTestFixture,YupTheNumberIs35)264 TEST_F(PigweedTestFixture, YupTheNumberIs35) {
265   EXPECT_EQ(cool_number_, 35);
266   cool_number_ *= 100;
267   EXPECT_EQ(cool_number_, 3500);
268 }
269 
270 class Expectations : public ::testing::Test {
271  protected:
Expectations()272   Expectations() : cool_number_(3) { PW_CHECK_INT_EQ(cool_number_, 3); }
273 
~Expectations()274   ~Expectations() override { PW_CHECK_INT_EQ(cool_number_, 14159); }
275 
276   int cool_number_;
277 };
278 
TEST_F(Expectations,SetCoolNumber)279 TEST_F(Expectations, SetCoolNumber) { cool_number_ = 14159; }
280 
281 class SetUpAndTearDown : public ::testing::Test {
282  public:
283   static int value;
284 
SetUpTestSuite()285   static void SetUpTestSuite() {
286     value = 1;
287     EXPECT_EQ(value, 1);
288     value++;
289   }
290 
TearDownTestSuite()291   static void TearDownTestSuite() {
292     EXPECT_EQ(value, 7);
293     value++;
294   }
295 
296  protected:
SetUpAndTearDown()297   SetUpAndTearDown() {
298     EXPECT_EQ(value, 2);
299     value++;
300   }
301 
~SetUpAndTearDown()302   ~SetUpAndTearDown() override {
303     EXPECT_EQ(value, 6);
304     value++;
305   }
306 
SetUp()307   void SetUp() override {
308     EXPECT_EQ(value, 3);
309     value++;
310   }
311 
TearDown()312   void TearDown() override {
313     EXPECT_EQ(value, 5);
314     value++;
315   }
316 };
317 
318 int SetUpAndTearDown::value = 1;
319 
TEST_F(SetUpAndTearDown,MakeSureItIsSet)320 TEST_F(SetUpAndTearDown, MakeSureItIsSet) {
321   EXPECT_EQ(value, 4);
322   value++;
323 }
324 
TEST(TestSuiteTearDown,MakeSureItRan)325 TEST(TestSuiteTearDown, MakeSureItRan) {
326   EXPECT_EQ(SetUpAndTearDown::value, 8);
327 }
328 
329 class Interleaved : public ::testing::Test {
330  public:
SetUpTestSuite()331   static void SetUpTestSuite() { suites_running++; }
TearDownTestSuite()332   static void TearDownTestSuite() { suites_running--; }
333 
334  protected:
335   static int suites_running;
336 };
337 
338 int Interleaved::suites_running = 0;
339 
340 class InterleavedA : public Interleaved {};
341 class InterleavedB : public Interleaved {};
342 
TEST_F(InterleavedA,Test1)343 TEST_F(InterleavedA, Test1) { ASSERT_EQ(suites_running, 1); }
TEST_F(InterleavedB,Test1)344 TEST_F(InterleavedB, Test1) { ASSERT_EQ(suites_running, 1); }
TEST_F(Interleaved,Test12)345 TEST_F(Interleaved, Test12) { ASSERT_EQ(suites_running, 1); }
TEST_F(InterleavedB,Test2)346 TEST_F(InterleavedB, Test2) { ASSERT_EQ(suites_running, 1); }
TEST_F(InterleavedA,Test2)347 TEST_F(InterleavedA, Test2) { ASSERT_EQ(suites_running, 1); }
348 
349 }  // namespace
350 }  // namespace pw
351