• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "pw_status/status.h"
18 #include "pw_unit_test/framework.h"
19 
20 // PW_ASSERT() should always be enabled, and always evaluate the expression.
TEST(Assert,AssertTrue)21 TEST(Assert, AssertTrue) {
22   int evaluated = 1;
23   PW_ASSERT(++evaluated);
24   EXPECT_EQ(evaluated, 2);
25 }
26 
27 // PW_DASSERT() might be disabled sometimes.
TEST(Assert,DebugAssertTrue)28 TEST(Assert, DebugAssertTrue) {
29   int evaluated = 1;
30   PW_DASSERT(++evaluated);
31   if (PW_ASSERT_ENABLE_DEBUG == 1) {
32     EXPECT_EQ(evaluated, 2);
33   } else {
34     EXPECT_EQ(evaluated, 1);
35   }
36 }
37 
TEST(Assert,AssertOkEvaluatesExpressionAndDoesNotCrashOnOk)38 TEST(Assert, AssertOkEvaluatesExpressionAndDoesNotCrashOnOk) {
39   int evaluated = 1;
40   PW_ASSERT_OK(([&]() {
41     ++evaluated;
42     return pw::OkStatus();
43   })());
44   EXPECT_EQ(evaluated, 2);
45 }
46 
47 // Unfortunately, we don't have the infrastructure to test failure handling
48 // automatically, since the harness crashes in the process of running this
49 // test. The unsatisfying alternative is to test the functionality manually,
50 // then disable the test.
51 
TEST(Assert,AssertFalse)52 TEST(Assert, AssertFalse) {
53   if (false) {
54     PW_ASSERT(false);
55   }
56 }
57 
TEST(Assert,DebugAssertFalse)58 TEST(Assert, DebugAssertFalse) {
59   if (false) {
60     PW_DASSERT(false);
61   }
62 }
63