blob: a309cf0eec61ba22f0f8e667f983d0fc2d97db26 [file] [log] [blame]
Keir Mierle3cee8792020-01-22 17:08:13 -08001// 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// This is "test" verifies that the assert backend:
16//
17// - Compiles as plain C
18//
19// Unfortunately, this doesn't really test the crashing functionality since
20// that is so backend dependent.
21//
22// NOTE: To run these tests for pw_assert_basic, you must modify two things:
23//
24// (1) Set DISABLE_ASSERT_TEST_EXECUTION 1 in assert_test.cc (this file)
25// (1) Set DISABLE_ASSERT_TEST_EXECUTION 1 in assert_test.c
26// (2) Set PW_ASSERT_BASIC_DISABLE_NORETURN 1 in assert_basic.h
27//
28// This is obviously not a long term solution.
29
30#include "pw_assert/assert.h"
31
32#ifdef __cplusplus
33#error "This file must be compiled as plain C to verify C compilation works."
34#endif // __cplusplus
35
36// This is a global constant to feed into the formatter for tests.
37// Intended to pair with FAIL_IF_DISPLAYED_ARGS or FAIL_IF_HIDDEN_ARGS.
38static const int z = 10;
39
40// At some point in the future when there is a proper test system in place for
41// crashing, the below strings can help indicate pass/fail for a check.
42
43#define FAIL_IF_DISPLAYED "FAIL IF DISPLAYED"
44#define FAIL_IF_DISPLAYED_ARGS "FAIL IF DISPLAYED: %d"
45
46#define FAIL_IF_HIDDEN "FAIL IF HIDDEN"
47#define FAIL_IF_HIDDEN_ARGS "FAIL IF HIDDEN: %d"
48
49// This switch exists to support compiling and/or running the tests.
50#define DISABLE_ASSERT_TEST_EXECUTION 1
51#if DISABLE_ASSERT_TEST_EXECUTION
52#define MAYBE_SKIP_TEST return
53#else
54#define MAYBE_SKIP_TEST ;
55#endif
56
57static int Add3(int a, int b, int c) { return a + b + c; }
58
59void AssertTestsInC() {
60 { // TEST(Crash, WithAndWithoutMessageArguments)
61 MAYBE_SKIP_TEST;
62 PW_CRASH(FAIL_IF_HIDDEN);
63 PW_CRASH(FAIL_IF_HIDDEN_ARGS, z);
64 }
65
66 { // TEST(Check, NoMessage)
67 MAYBE_SKIP_TEST;
68 PW_CHECK(1);
69 PW_CHECK(0);
70 }
71
72 { // TEST(Check, WithMessageAndArgs)
73 MAYBE_SKIP_TEST;
74 PW_CHECK(1, FAIL_IF_DISPLAYED);
75 PW_CHECK(1, FAIL_IF_DISPLAYED_ARGS, z);
76
77 PW_CHECK(0, FAIL_IF_HIDDEN);
78 PW_CHECK(0, FAIL_IF_HIDDEN_ARGS, z);
79 }
80
81 { // TEST(Check, IntComparison)
82 MAYBE_SKIP_TEST;
83 int x_int = 50;
84 int y_int = 66;
85
86 PW_CHECK_INT_LE(x_int, y_int);
87 PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED);
88 PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z);
89
90 PW_CHECK_INT_GE(x_int, y_int);
91 PW_CHECK_INT_GE(x_int, y_int, "INT: " FAIL_IF_HIDDEN);
92 PW_CHECK_INT_GE(x_int, y_int, "INT: " FAIL_IF_HIDDEN_ARGS, z);
93 }
94
95 { // TEST(Check, UintComparison)
96 MAYBE_SKIP_TEST;
97 unsigned int x_uint = 50;
98 unsigned int y_uint = 66;
99
100 PW_CHECK_UINT_LE(x_uint, y_uint);
101 PW_CHECK_UINT_LE(x_uint, y_uint, "UINT: " FAIL_IF_DISPLAYED);
102 PW_CHECK_UINT_LE(x_uint, y_uint, "UINT: " FAIL_IF_DISPLAYED_ARGS, z);
103
104 PW_CHECK_UINT_GE(x_uint, y_uint);
105 PW_CHECK_UINT_GE(x_uint, y_uint, "UINT: " FAIL_IF_HIDDEN);
106 PW_CHECK_UINT_GE(x_uint, y_uint, "UINT: " FAIL_IF_HIDDEN_ARGS, z);
107 }
108
109 { // TEST(Check, FloatComparison)
110 MAYBE_SKIP_TEST;
111 float x_float = 50.5;
112 float y_float = 66.5;
113
114 PW_CHECK_FLOAT_LE(x_float, y_float);
115 PW_CHECK_FLOAT_LE(x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED);
116 PW_CHECK_FLOAT_LE(x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED_ARGS, z);
117
118 PW_CHECK_FLOAT_GE(x_float, y_float);
119 PW_CHECK_FLOAT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN);
120 PW_CHECK_FLOAT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN_ARGS, z);
121 }
122
123 { // TEST(Check, ComparisonArgumentsWithCommas)
124 MAYBE_SKIP_TEST;
125 int x_int = 50;
126 int y_int = 66;
127
128 PW_CHECK_INT_LE(Add3(1, 2, 3), y_int);
129 PW_CHECK_INT_LE(x_int, Add3(1, 2, 3));
130
131 PW_CHECK_INT_LE(Add3(1, 2, 3), y_int, FAIL_IF_DISPLAYED);
132 PW_CHECK_INT_LE(x_int, Add3(1, 2, 3), FAIL_IF_DISPLAYED_ARGS, z);
133
134 PW_CHECK_INT_LE(Add3(1, 2, 3), Add3(1, 2, 3), "INT: " FAIL_IF_DISPLAYED);
135 PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z);
136 }
137}