aboutsummaryrefslogtreecommitdiff
path: root/pw_unit_test/framework_test.cc
blob: 211c665ce8f2e12ad536461582d7ad821b2c92cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright 2019 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

#include <cstring>

#include "gtest/gtest.h"
#include "pw_assert/check.h"

namespace pw {
namespace {

TEST(PigweedTest, ExpectBool) {
  EXPECT_TRUE(true);
  EXPECT_FALSE(false);

  EXPECT_TRUE(1);
  EXPECT_TRUE(1203492);
  EXPECT_TRUE(-1);
  EXPECT_TRUE(0.1f);

  EXPECT_FALSE(0);
  EXPECT_FALSE(0.0f);
  EXPECT_FALSE(-0.0f);
}

TEST(PigweedTest, ExpectBasicComparisons) {
  EXPECT_EQ(1, 1 + 0);
  ASSERT_EQ(1, 1 + 0);

  EXPECT_EQ(0.0f, -0.0f);
  ASSERT_EQ(0.0f, -0.0f);

  EXPECT_NE(-1, 0);
  ASSERT_NE(-1, 0);

  EXPECT_GT(2, 1);
  ASSERT_GT(3, 0);

  EXPECT_GE(1, 1);
  ASSERT_GE(3, 0);

  EXPECT_LT(0, 1);
  ASSERT_LT(-2, 1209);

  EXPECT_LE(-1, 0);
  ASSERT_LE(-2, -2);
}

TEST(PigweedTest, ExpectNearComparisons) {
  EXPECT_NEAR(1, 2, 1);
  ASSERT_NEAR(1, 2, 1);

  EXPECT_NEAR(-5, 5, 10);
  ASSERT_NEAR(-5, 5, 10);

  int x = 17;
  int epsilon = 5;

  EXPECT_NEAR(x, 15, epsilon);
  ASSERT_NEAR(x, 15, epsilon);
}

TEST(PigweedTest, ExpectFloatComparisons) {
  EXPECT_FLOAT_EQ(5.0f, 10.0f / 2);
  ASSERT_FLOAT_EQ(5.0f, 10.0f / 2);

  EXPECT_FLOAT_EQ(-0.5f, -5.0f / 10);
  ASSERT_FLOAT_EQ(-0.5f, -5.0f / 10);

  float x = 17.0f / 20.0f;

  EXPECT_FLOAT_EQ(x, 17.0f / 20.0f);
  ASSERT_FLOAT_EQ(x, 17.0f / 20.0f);
}

TEST(PigweedTest, ExpectDoubleComparisons) {
  EXPECT_DOUBLE_EQ(5.0, 10.0 / 2);
  ASSERT_DOUBLE_EQ(5.0, 10.0 / 2);

  EXPECT_DOUBLE_EQ(-0.5, -5.0 / 10);
  ASSERT_DOUBLE_EQ(-0.5, -5.0 / 10);

  double x = 17.0 / 20.0;

  EXPECT_DOUBLE_EQ(x, 17.0 / 20.0);
  ASSERT_DOUBLE_EQ(x, 17.0 / 20.0);
}

TEST(PigweedTest, ExpectStringEquality) {
  EXPECT_STREQ("", "");
  EXPECT_STREQ("Yes", "Yes");

  char no[] = {'N', 'o', '\0'};
  ASSERT_STREQ("No", no);

  EXPECT_STRNE("NO", "no");
  ASSERT_STRNE("yes", no);

  EXPECT_STREQ(nullptr, nullptr);
  EXPECT_STRNE("abc", nullptr);
}

TEST(PigweedTest, SucceedAndFailMacros) {
  SUCCEED();

  // The ADD_FAILURE() and FAIL() macros cause a test to fail if they are
  // reached. Use them, but don't let them run so that this test still passes.
  if (false) {
    ADD_FAILURE();
    FAIL();
  }
}

TEST(PigweedTest, SkipMacro) {
  GTEST_SKIP();
  // This code should not run.
  EXPECT_TRUE(false);
}

TEST(PigweedTest, Logs) {
  EXPECT_TRUE(true) << "This message is ignored";
  EXPECT_FALSE(false) << "This message is ignored";
  EXPECT_EQ(0, 0) << "This message is ignored";
  EXPECT_NE(0, 1) << "This message is ignored";
  EXPECT_GT(1, 0) << "This message is ignored";
  EXPECT_GE(0, 0) << "This message is ignored";
  EXPECT_LT(0, 1) << "This message is ignored";
  EXPECT_LE(0, 0) << "This message is ignored";
  EXPECT_STREQ("", "") << "This message is ignored";
  EXPECT_STRNE("", "?") << "This message is ignored";

  ASSERT_TRUE(true) << "This message is ignored";
  ASSERT_FALSE(false) << "This message is ignored";
  ASSERT_EQ(0, 0) << "This message is ignored";
  ASSERT_NE(0, 1) << "This message is ignored";
  ASSERT_GT(1, 0) << "This message is ignored";
  ASSERT_GE(0, 0) << "This message is ignored";
  ASSERT_LT(0, 1) << "This message is ignored";
  ASSERT_LE(0, 0) << "This message is ignored";
  ASSERT_STREQ("", "") << "This message is ignored";
  ASSERT_STRNE("", "?") << "This message is ignored";

  if (false) {
    ADD_FAILURE() << "This failed!" << 123;
    GTEST_FAIL() << "This failed!" << 123 << '?';
    GTEST_SKIP() << 1.0f << " skips!";
  }
  GTEST_SUCCEED() << "This message is ignored";
}

class SkipOnSetUpTest : public ::testing::Test {
 public:
  void SetUp() override { GTEST_SKIP(); }
};

TEST_F(SkipOnSetUpTest, FailTest) {
  // This code should not run because the test was skipped in SetUp().
  EXPECT_TRUE(false);
}

class NonCopyable {
 public:
  NonCopyable(int value) : value_(value) {}

  NonCopyable(const NonCopyable&) = delete;
  NonCopyable& operator=(const NonCopyable&) = delete;

  bool operator==(const NonCopyable& rhs) const { return value_ == rhs.value_; }
  bool operator!=(const NonCopyable& rhs) const { return value_ != rhs.value_; }

  operator bool() const { return value_ > 0; }

 private:
  const int value_;
};

TEST(PigweedTest, NonCopyableType) {
  EXPECT_TRUE(NonCopyable(6));
  EXPECT_FALSE(NonCopyable(-1));

  const NonCopyable this_one(100);
  EXPECT_EQ(this_one, this_one);
  EXPECT_TRUE(this_one);

  EXPECT_EQ(NonCopyable(5), NonCopyable(5));
  EXPECT_NE(NonCopyable(5), NonCopyable(6));
}

bool Increment(int* i) {
  (*i)++;
  return true;
}

TEST(PigweedTest, MacroArgumentsOnlyAreEvaluatedOnce) {
  int i = 1;

  EXPECT_TRUE(Increment(&i));
  EXPECT_EQ(i, 2);
  ASSERT_TRUE(Increment(&i));
  EXPECT_EQ(i, 3);

  EXPECT_EQ(0x600dbeef, [&i]() {
    i += 1;
    return 0x600dbeef;
  }());

  EXPECT_EQ(i, 4);
}

class ClassWithPrivateMethod {
  FRIEND_TEST(FixtureTest, FriendClass);

 private:
  int Return314() { return 314; }
};

class FixtureTest : public ::testing::Test {
 public:
  FixtureTest() : string_("hello world") {}

  bool ReturnTrue() { return true; }
  int StringLength() { return std::strlen(string_); }

 protected:
  const char* string_;
};

TEST_F(FixtureTest, CustomFixture) {
  EXPECT_TRUE(ReturnTrue());
  EXPECT_EQ(StringLength(), 11);
}

TEST_F(FixtureTest, FriendClass) {
  EXPECT_EQ(ClassWithPrivateMethod().Return314(), 314);
}

class PigweedTestFixture : public ::testing::Test {
 protected:
  PigweedTestFixture() : cool_number_(35) {}

  int cool_number_;
};

TEST_F(PigweedTestFixture, TheNumberIs35) {
  EXPECT_EQ(cool_number_, 35);
  cool_number_ += 1;
  EXPECT_EQ(cool_number_, 36);
}

TEST_F(PigweedTestFixture, YupTheNumberIs35) {
  EXPECT_EQ(cool_number_, 35);
  cool_number_ *= 100;
  EXPECT_EQ(cool_number_, 3500);
}

class Expectations : public ::testing::Test {
 protected:
  Expectations() : cool_number_(3) { PW_CHECK_INT_EQ(cool_number_, 3); }

  ~Expectations() override { PW_CHECK_INT_EQ(cool_number_, 14159); }

  int cool_number_;
};

TEST_F(Expectations, SetCoolNumber) { cool_number_ = 14159; }

class SetUpAndTearDown : public ::testing::Test {
 public:
  static int value;

  static void SetUpTestSuite() {
    value = 1;
    EXPECT_EQ(value, 1);
    value++;
  }

  static void TearDownTestSuite() {
    EXPECT_EQ(value, 7);
    value++;
  }

 protected:
  SetUpAndTearDown() {
    EXPECT_EQ(value, 2);
    value++;
  }

  ~SetUpAndTearDown() override {
    EXPECT_EQ(value, 6);
    value++;
  }

  void SetUp() override {
    EXPECT_EQ(value, 3);
    value++;
  }

  void TearDown() override {
    EXPECT_EQ(value, 5);
    value++;
  }
};

int SetUpAndTearDown::value = 1;

TEST_F(SetUpAndTearDown, MakeSureItIsSet) {
  EXPECT_EQ(value, 4);
  value++;
}

TEST(TestSuiteTearDown, MakeSureItRan) {
  EXPECT_EQ(SetUpAndTearDown::value, 8);
}

TEST(UnknownTypeToString, SmallObject) {
  struct {
    char a = 0xa1;
  } object;

  StringBuffer<64> expected;
  expected << "<1-byte object at 0x" << &object << '>';
  ASSERT_EQ(OkStatus(), expected.status());

  StringBuffer<64> actual;
  actual << object;
  ASSERT_EQ(OkStatus(), actual.status());
  EXPECT_STREQ(expected.c_str(), actual.c_str());
}

TEST(UnknownTypeToString, NineByteObject) {
  struct {
    char a[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  } object;

  StringBuffer<64> expected;
  expected << "<9-byte object at 0x" << &object << '>';
  ASSERT_EQ(OkStatus(), expected.status());

  StringBuffer<64> actual;
  actual << object;
  ASSERT_EQ(OkStatus(), actual.status());
  EXPECT_STREQ(expected.c_str(), actual.c_str());
}

TEST(UnknownTypeToString, TenByteObject) {
  struct {
    char a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  } object;

  StringBuffer<72> expected;
  expected << "<10-byte object at 0x" << &object << '>';
  ASSERT_EQ(OkStatus(), expected.status());

  StringBuffer<72> actual;
  actual << object;
  ASSERT_EQ(OkStatus(), actual.status());
  EXPECT_STREQ(expected.c_str(), actual.c_str());
}

}  // namespace
}  // namespace pw