xref: /aosp_15_r20/external/pigweed/pw_preprocessor/boolean_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2019 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 // All of these tests are static asserts. If the test compiles, it has already
16 // passed. The TEST functions are used for organization only.
17 #include "pw_preprocessor/boolean.h"
18 
19 #include "pw_unit_test/framework.h"
20 
21 namespace pw {
22 namespace {
23 
24 #define ONE 1
25 #define ZERO() 0
26 
TEST(BooleanMacros,And)27 TEST(BooleanMacros, And) {
28   static_assert(PW_AND(ZERO(), 0) == 0);
29   static_assert(PW_AND(ZERO(), ONE) == 0);
30   static_assert(PW_AND(1, 0) == 0);
31   static_assert(PW_AND(ONE, PW_NOT(ZERO())) == 1);
32 }
33 
TEST(BooleanMacros,Or)34 TEST(BooleanMacros, Or) {
35   static_assert(PW_OR(ZERO(), 0) == 0);
36   static_assert(PW_OR(ZERO(), ONE) == 1);
37   static_assert(PW_OR(1, 0) == 1);
38   static_assert(PW_OR(ONE, PW_NOT(ZERO())) == 1);
39 }
40 
TEST(BooleanMacros,Not)41 TEST(BooleanMacros, Not) {
42   static_assert(PW_NOT(0) == 1);
43   static_assert(PW_NOT(1) == 0);
44   static_assert(PW_NOT(ONE) == 0);
45   static_assert(PW_NOT(ZERO()) == 1);
46 }
47 
TEST(BooleanMacros,Xor)48 TEST(BooleanMacros, Xor) {
49   static_assert(PW_XOR(ZERO(), 0) == 0);
50   static_assert(PW_XOR(ZERO(), ONE) == 1);
51   static_assert(PW_XOR(1, 0) == 1);
52   static_assert(PW_XOR(ONE, PW_NOT(ZERO())) == 0);
53 }
54 
TEST(BooleanMacros,Nand)55 TEST(BooleanMacros, Nand) {
56   static_assert(PW_NAND(ZERO(), 0) == 1);
57   static_assert(PW_NAND(ZERO(), ONE) == 1);
58   static_assert(PW_NAND(1, 0) == 1);
59   static_assert(PW_NAND(ONE, PW_NOT(ZERO())) == 0);
60 }
61 
TEST(BooleanMacros,Nor)62 TEST(BooleanMacros, Nor) {
63   static_assert(PW_NOR(ZERO(), 0) == 1);
64   static_assert(PW_NOR(ZERO(), ONE) == 0);
65   static_assert(PW_NOR(1, 0) == 0);
66   static_assert(PW_NOR(ONE, PW_NOT(ZERO())) == 0);
67 }
68 
TEST(BooleanMacros,Xnor)69 TEST(BooleanMacros, Xnor) {
70   static_assert(PW_XNOR(ZERO(), 0) == 1);
71   static_assert(PW_XNOR(ZERO(), ONE) == 0);
72   static_assert(PW_XNOR(1, 0) == 0);
73   static_assert(PW_XNOR(ONE, PW_NOT(ZERO())) == 1);
74 }
75 
TEST(BooleanMacros,Nested)76 TEST(BooleanMacros, Nested) {
77   static_assert(PW_AND(1, PW_AND(PW_OR(ZERO(), ONE), PW_XOR(ONE, 0))) == 1);
78 }
79 
80 }  // namespace
81 }  // namespace pw
82