1 // Copyright 2019 The Pigweed Authors
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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
15 #include "pw_unit_test/framework.h"
22 TEST(PigweedTest, ExpectBool) {
36 TEST(PigweedTest, ExpectBasicComparisons) {
40 EXPECT_EQ(0.0f, -0.0f);
41 ASSERT_EQ(0.0f, -0.0f);
59 TEST(PigweedTest, ExpectStringEquality) {
61 EXPECT_STREQ("Yes", "Yes");
63 char no[] = {'N', 'o', '\0'};
64 ASSERT_STREQ("No", no);
66 EXPECT_STRNE("NO", "no");
67 ASSERT_STRNE("yes", no);
70 TEST(PigweedTest, SucceedAndFailMacros) {
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.
83 NonCopyable(int value) : value_(value) {}
85 NonCopyable(const NonCopyable&) = delete;
86 NonCopyable& operator=(const NonCopyable&) = delete;
88 bool operator==(const NonCopyable& rhs) const { return value_ == rhs.value_; }
89 bool operator!=(const NonCopyable& rhs) const { return value_ != rhs.value_; }
91 operator bool() const { return value_ > 0; }
97 TEST(PigweedTest, NonCopyableType) {
98 EXPECT_TRUE(NonCopyable(6));
99 EXPECT_FALSE(NonCopyable(-1));
101 const NonCopyable this_one(100);
102 EXPECT_EQ(this_one, this_one);
103 EXPECT_TRUE(this_one);
105 EXPECT_EQ(NonCopyable(5), NonCopyable(5));
106 EXPECT_NE(NonCopyable(5), NonCopyable(6));
109 bool Increment(int* i) {
114 TEST(PigweedTest, MacroArgumentsOnlyAreEvaluatedOnce) {
117 EXPECT_TRUE(Increment(&i));
119 ASSERT_TRUE(Increment(&i));
122 EXPECT_EQ(0x600dbeef, [&i]() {
130 class FixtureTest : public ::testing::Test {
132 FixtureTest() : string_("hello world") {}
134 bool ReturnTrue() { return true; }
135 int StringLength() { return std::strlen(string_); }
141 TEST_F(FixtureTest, CustomFixture) {
142 EXPECT_TRUE(ReturnTrue());
143 EXPECT_EQ(StringLength(), 11);
146 class PigweedTestFixture : public ::testing::Test {
148 PigweedTestFixture() : cool_number_(35) {}
153 TEST_F(PigweedTestFixture, TheNumberIs35) {
154 EXPECT_EQ(cool_number_, 35);
156 EXPECT_EQ(cool_number_, 36);
159 TEST_F(PigweedTestFixture, YupTheNumberIs35) {
160 EXPECT_EQ(cool_number_, 35);
162 EXPECT_EQ(cool_number_, 3500);
165 class Expectations : public ::testing::Test {
167 Expectations() : cool_number_(3) { ASSERT_EQ(cool_number_, 3); }
169 ~Expectations() { ASSERT_EQ(cool_number_, 14159); }
174 TEST_F(Expectations, SetCoolNumber) { cool_number_ = 14159; }
176 class SetUpAndTearDown : public ::testing::Test {
178 SetUpAndTearDown() : value_(0) { EXPECT_EQ(value_, 0); }
180 ~SetUpAndTearDown() { EXPECT_EQ(value_, 1); }
182 void SetUp() override { value_ = 1337; }
184 void TearDown() override { value_ = 1; }
189 TEST_F(SetUpAndTearDown, MakeSureItIsSet) {
190 EXPECT_EQ(value_, 1337);