xref: /aosp_15_r20/external/pigweed/pw_assert_trap/trap_handler_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2024 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_trap/assert_trap.h"
16 #include "pw_assert_trap/check_trap.h"
17 #include "pw_assert_trap/message.h"
18 #include "pw_unit_test/framework.h"
19 
20 namespace {
21 
TestSetUp()22 void TestSetUp() { pw_assert_trap_clear_message(); }
23 
24 // TODO: https://pwbug.dev/351889230 - add unittests and enable by default
25 // TEST(TrapHandler, AssertHandleFailure) {
26 //   TestSetUp();
27 //   PW_ASSERT_HANDLE_FAILURE(false);
28 //   auto actual_msg = pw_assert_trap_get_message();
29 //   ASSERT_STREQ("PW_ASSERT() or PW_DASSERT() failure", actual_msg.data());
30 // }
31 
TEST(TrapHandler,Crash)32 TEST(TrapHandler, Crash) {
33   TestSetUp();
34   PW_HANDLE_CRASH("crash message: %d", 7);
35   auto actual_msg = pw_assert_trap_get_message();
36   ASSERT_STREQ("crash message: 7", actual_msg.data());
37 }
38 
TEST(TrapHandler,HandleAssertFailure)39 TEST(TrapHandler, HandleAssertFailure) {
40   TestSetUp();
41   PW_HANDLE_ASSERT_FAILURE(false, "assert: %d", 1);
42   auto actual_msg = pw_assert_trap_get_message();
43   ASSERT_STREQ("assert: 1", actual_msg.data());
44 }
45 
TEST(TrapHandler,HandleAssertBinaryCompareFailure)46 TEST(TrapHandler, HandleAssertBinaryCompareFailure) {
47   TestSetUp();
48   PW_HANDLE_ASSERT_BINARY_COMPARE_FAILURE(
49       "expected", 1, "==", "actual", 2, "%d", "fail");
50   auto actual_msg = pw_assert_trap_get_message();
51   ASSERT_STREQ("Check failed: expected (=1) == actual (=2). fail",
52                actual_msg.data());
53 }
54 
55 }  // namespace
56