1 // Copyright 2020 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_assert/assert.h"
16
17 #include "gtest/gtest.h"
18
19 // PW_ASSERT() should always be enabled, and always evaluate the expression.
TEST(Assert,AssertTrue)20 TEST(Assert, AssertTrue) {
21 int evaluated = 1;
22 PW_ASSERT(++evaluated);
23 EXPECT_EQ(evaluated, 2);
24 }
25
26 // PW_DASSERT() might be disabled sometimes.
TEST(Assert,DebugAssertTrue)27 TEST(Assert, DebugAssertTrue) {
28 int evaluated = 1;
29 PW_DASSERT(++evaluated);
30 if (PW_ASSERT_ENABLE_DEBUG == 1) {
31 EXPECT_EQ(evaluated, 2);
32 } else {
33 EXPECT_EQ(evaluated, 1);
34 }
35 }
36
37 // Unfortunately, we don't have the infrastructure to test failure handling
38 // automatically, since the harness crashes in the process of running this
39 // test. The unsatisfying alternative is to test the functionality manually,
40 // then disable the test.
41
TEST(Assert,AssertFalse)42 TEST(Assert, AssertFalse) {
43 if (false) {
44 PW_ASSERT(false);
45 }
46 }
47
TEST(Assert,DebugAssertFalse)48 TEST(Assert, DebugAssertFalse) {
49 if (false) {
50 PW_DASSERT(false);
51 }
52 }
53