]>
Commit | Line | Data |
---|---|---|
37be9888 | 1 | /* |
77b1029d | 2 | * Copyright (C) 1996-2020 The Squid Software Foundation and contributors |
37be9888 AJ |
3 | * |
4 | * Squid software is distributed under GPLv2+ license and includes | |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
7 | */ | |
8 | ||
f7f3304a | 9 | #include "squid.h" |
22aa015b | 10 | #include "testPreCompiler.h" |
7f861c77 | 11 | #include "unitTestMain.h" |
19ce1bfe | 12 | |
074d6a40 | 13 | #include <cassert> |
19ce1bfe | 14 | |
19ce1bfe AJ |
15 | CPPUNIT_TEST_SUITE_REGISTRATION( testPreCompiler ); |
16 | ||
17 | /** | |
18 | * Test several ways of defining pre-compiler directives. | |
19 | * Squid-3 uses #if FOO syntax for precompiler directives. | |
20 | * These tests ensure that the inputs will work as expected. | |
21 | */ | |
22 | void | |
23 | testPreCompiler::testIfDef() | |
24 | { | |
25 | /* Defined to explicit value 1 should be true */ | |
26 | #define ONE_FOO 1 | |
27 | #if ONE_FOO | |
28 | bool oneTrue = true; | |
29 | #else | |
30 | bool oneTrue = false; | |
31 | #endif | |
32 | #if !ONE_FOO | |
33 | bool oneFalse = true; | |
34 | #else | |
35 | bool oneFalse = false; | |
36 | #endif | |
37 | CPPUNIT_ASSERT(oneTrue); | |
38 | CPPUNIT_ASSERT(!oneFalse); | |
39 | ||
40 | /* Defined to explicit value 0 should be false */ | |
41 | #define ZERO_FOO 0 | |
42 | #if ZERO_FOO | |
43 | bool zeroTrue = true; | |
44 | #else | |
45 | bool zeroTrue = false; | |
46 | #endif | |
47 | #if !ZERO_FOO | |
48 | bool zeroFalse = true; | |
49 | #else | |
50 | bool zeroFalse = false; | |
51 | #endif | |
52 | CPPUNIT_ASSERT(zeroFalse); | |
53 | CPPUNIT_ASSERT(!zeroTrue); | |
54 | ||
55 | /* Defined to exist without a value generates pre-compiler errors when used in #if . */ | |
56 | ||
d57434f1 | 57 | /* Not Defined to exist at all == false */ |
19ce1bfe AJ |
58 | #undef UNDEFINED_FOO |
59 | #if UNDEFINED_FOO | |
60 | bool undefinedTrue = true; | |
61 | #else | |
62 | bool undefinedTrue = false; | |
63 | #endif | |
64 | #if !UNDEFINED_FOO | |
65 | bool undefinedFalse = true; | |
66 | #else | |
67 | bool undefinedFalse = false; | |
68 | #endif | |
69 | CPPUNIT_ASSERT(undefinedFalse); | |
70 | CPPUNIT_ASSERT(!undefinedTrue); | |
71 | } | |
469620bf AJ |
72 | |
73 | /** | |
74 | * Test several ways of defining pre-compiler directives. | |
75 | * Squid-3 uses #if FOO syntax for precompiler directives. | |
76 | * These tests ensure that the inputs will work as expected | |
77 | * when undefined macros are used in && conditions | |
78 | */ | |
79 | void | |
80 | testPreCompiler::testIfDefAnd() | |
81 | { | |
82 | /* Not Defined to exist at all == false - when used in a compound if */ | |
83 | #undef UNDEFINED_FOO | |
84 | #define ONE_FOO 1 | |
85 | ||
86 | #if UNDEFINED_FOO && ONE_FOO | |
87 | bool undefinedAndTrueA = true; | |
88 | #else | |
89 | bool undefinedAndTrueA = false; | |
90 | #endif | |
91 | #if !UNDEFINED_FOO && ONE_FOO | |
92 | bool undefinedAndFalseA = true; | |
93 | #else | |
94 | bool undefinedAndFalseA = false; | |
95 | #endif | |
96 | CPPUNIT_ASSERT(undefinedAndFalseA); | |
97 | CPPUNIT_ASSERT(!undefinedAndTrueA); | |
98 | ||
99 | #if ONE_FOO && UNDEFINED_FOO | |
100 | bool undefinedAndTrueB = true; | |
101 | #else | |
102 | bool undefinedAndTrueB = false; | |
103 | #endif | |
104 | #if ONE_FOO && !UNDEFINED_FOO | |
105 | bool undefinedAndFalseB = true; | |
106 | #else | |
107 | bool undefinedAndFalseB = false; | |
108 | #endif | |
109 | CPPUNIT_ASSERT(undefinedAndFalseB); | |
110 | CPPUNIT_ASSERT(!undefinedAndTrueB); | |
937f9fb8 AJ |
111 | |
112 | #if UNDEFINED_FOO && UNDEFINED_FOO | |
113 | bool undefinedAndUndefinedC = true; | |
114 | #else | |
115 | bool undefinedAndUndefinedC = false; | |
116 | #endif | |
117 | #if !UNDEFINED_FOO && !UNDEFINED_FOO | |
118 | bool notUndefinedAndNotUndefinedC = true; | |
119 | #else | |
120 | bool notUndefinedAndNotUndefinedC = false; | |
121 | #endif | |
122 | CPPUNIT_ASSERT(!undefinedAndUndefinedC); | |
123 | CPPUNIT_ASSERT(notUndefinedAndNotUndefinedC); | |
469620bf AJ |
124 | } |
125 | ||
126 | /** | |
127 | * Test several ways of defining pre-compiler directives. | |
128 | * Squid-3 uses #if FOO syntax for precompiler directives. | |
129 | * These tests ensure that the inputs will work as expected | |
130 | * when undefined macros are used in || conditions | |
131 | */ | |
132 | void | |
133 | testPreCompiler::testIfDefOr() | |
134 | { | |
135 | /* Not Defined to exist at all == false - when used in a compound if */ | |
136 | #undef UNDEFINED_FOO | |
137 | #define ZERO_FOO 0 | |
138 | ||
139 | #if UNDEFINED_FOO || ZERO_FOO | |
140 | bool undefinedOrTrueA = true; | |
141 | #else | |
142 | bool undefinedOrTrueA = false; | |
143 | #endif | |
144 | #if !UNDEFINED_FOO || ZERO_FOO | |
145 | bool undefinedOrFalseA = true; | |
146 | #else | |
147 | bool undefinedOrFalseA = false; | |
148 | #endif | |
149 | CPPUNIT_ASSERT(undefinedOrFalseA); | |
150 | CPPUNIT_ASSERT(!undefinedOrTrueA); | |
151 | ||
152 | #if ZERO_FOO || UNDEFINED_FOO | |
153 | bool undefinedOrTrueB = true; | |
154 | #else | |
155 | bool undefinedOrTrueB = false; | |
156 | #endif | |
157 | #if ZERO_FOO || !UNDEFINED_FOO | |
158 | bool undefinedOrFalseB = true; | |
159 | #else | |
160 | bool undefinedOrFalseB = false; | |
161 | #endif | |
162 | CPPUNIT_ASSERT(undefinedOrFalseB); | |
163 | CPPUNIT_ASSERT(!undefinedOrTrueB); | |
164 | ||
937f9fb8 AJ |
165 | #if UNDEFINED_FOO || UNDEFINED_FOO |
166 | bool undefinedOrUndefinedC = true; | |
167 | #else | |
168 | bool undefinedOrUndefinedC = false; | |
169 | #endif | |
170 | #if !UNDEFINED_FOO || !UNDEFINED_FOO | |
171 | bool notUndefinedOrNotUndefinedC = true; | |
172 | #else | |
173 | bool notUndefinedOrNotUndefinedC = false; | |
174 | #endif | |
175 | CPPUNIT_ASSERT(notUndefinedOrNotUndefinedC); | |
176 | CPPUNIT_ASSERT(!undefinedOrUndefinedC); | |
469620bf AJ |
177 | } |
178 |