]> git.ipfire.org Git - thirdparty/squid.git/blob - compat/testPreCompiler.cc
Windows: Fix symbol collisions (#1385)
[thirdparty/squid.git] / compat / testPreCompiler.cc
1 /*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
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
9 #include "squid.h"
10 #include "unitTestMain.h"
11
12 #include <cassert>
13 #include <cppunit/extensions/HelperMacros.h>
14
15 /*
16 * Test the pre-compiler directives used within Squid code actually work.
17 */
18
19 class TestPreCompiler : public CPPUNIT_NS::TestFixture
20 {
21 CPPUNIT_TEST_SUITE(TestPreCompiler);
22 CPPUNIT_TEST(testIfDef);
23 CPPUNIT_TEST(testIfDefAnd);
24 CPPUNIT_TEST(testIfDefOr);
25 CPPUNIT_TEST_SUITE_END();
26
27 protected:
28 void testIfDef();
29 void testIfDefAnd();
30 void testIfDefOr();
31 };
32 CPPUNIT_TEST_SUITE_REGISTRATION(TestPreCompiler);
33
34 /**
35 * Test several ways of defining pre-compiler directives.
36 * Squid-3 uses #if FOO syntax for precompiler directives.
37 * These tests ensure that the inputs will work as expected.
38 */
39 void
40 TestPreCompiler::testIfDef()
41 {
42 /* Defined to explicit value 1 should be true */
43 #define ONE_FOO 1
44 #if ONE_FOO
45 bool oneTrue = true;
46 #else
47 bool oneTrue = false;
48 #endif
49 #if !ONE_FOO
50 bool oneFalse = true;
51 #else
52 bool oneFalse = false;
53 #endif
54 CPPUNIT_ASSERT(oneTrue);
55 CPPUNIT_ASSERT(!oneFalse);
56
57 /* Defined to explicit value 0 should be false */
58 #define ZERO_FOO 0
59 #if ZERO_FOO
60 bool zeroTrue = true;
61 #else
62 bool zeroTrue = false;
63 #endif
64 #if !ZERO_FOO
65 bool zeroFalse = true;
66 #else
67 bool zeroFalse = false;
68 #endif
69 CPPUNIT_ASSERT(zeroFalse);
70 CPPUNIT_ASSERT(!zeroTrue);
71
72 /* Defined to exist without a value generates pre-compiler errors when used in #if . */
73
74 /* Not Defined to exist at all == false */
75 #undef UNDEFINED_FOO
76 #if UNDEFINED_FOO
77 bool undefinedTrue = true;
78 #else
79 bool undefinedTrue = false;
80 #endif
81 #if !UNDEFINED_FOO
82 bool undefinedFalse = true;
83 #else
84 bool undefinedFalse = false;
85 #endif
86 CPPUNIT_ASSERT(undefinedFalse);
87 CPPUNIT_ASSERT(!undefinedTrue);
88 }
89
90 /**
91 * Test several ways of defining pre-compiler directives.
92 * Squid-3 uses #if FOO syntax for precompiler directives.
93 * These tests ensure that the inputs will work as expected
94 * when undefined macros are used in && conditions
95 */
96 void
97 TestPreCompiler::testIfDefAnd()
98 {
99 /* Not Defined to exist at all == false - when used in a compound if */
100 #undef UNDEFINED_FOO
101 #define ONE_FOO 1
102
103 #if UNDEFINED_FOO && ONE_FOO
104 bool undefinedAndTrueA = true;
105 #else
106 bool undefinedAndTrueA = false;
107 #endif
108 #if !UNDEFINED_FOO && ONE_FOO
109 bool undefinedAndFalseA = true;
110 #else
111 bool undefinedAndFalseA = false;
112 #endif
113 CPPUNIT_ASSERT(undefinedAndFalseA);
114 CPPUNIT_ASSERT(!undefinedAndTrueA);
115
116 #if ONE_FOO && UNDEFINED_FOO
117 bool undefinedAndTrueB = true;
118 #else
119 bool undefinedAndTrueB = false;
120 #endif
121 #if ONE_FOO && !UNDEFINED_FOO
122 bool undefinedAndFalseB = true;
123 #else
124 bool undefinedAndFalseB = false;
125 #endif
126 CPPUNIT_ASSERT(undefinedAndFalseB);
127 CPPUNIT_ASSERT(!undefinedAndTrueB);
128
129 #if UNDEFINED_FOO && UNDEFINED_FOO
130 bool undefinedAndUndefinedC = true;
131 #else
132 bool undefinedAndUndefinedC = false;
133 #endif
134 #if !UNDEFINED_FOO && !UNDEFINED_FOO
135 bool notUndefinedAndNotUndefinedC = true;
136 #else
137 bool notUndefinedAndNotUndefinedC = false;
138 #endif
139 CPPUNIT_ASSERT(!undefinedAndUndefinedC);
140 CPPUNIT_ASSERT(notUndefinedAndNotUndefinedC);
141 }
142
143 /**
144 * Test several ways of defining pre-compiler directives.
145 * Squid-3 uses #if FOO syntax for precompiler directives.
146 * These tests ensure that the inputs will work as expected
147 * when undefined macros are used in || conditions
148 */
149 void
150 TestPreCompiler::testIfDefOr()
151 {
152 /* Not Defined to exist at all == false - when used in a compound if */
153 #undef UNDEFINED_FOO
154 #define ZERO_FOO 0
155
156 #if UNDEFINED_FOO || ZERO_FOO
157 bool undefinedOrTrueA = true;
158 #else
159 bool undefinedOrTrueA = false;
160 #endif
161 #if !UNDEFINED_FOO || ZERO_FOO
162 bool undefinedOrFalseA = true;
163 #else
164 bool undefinedOrFalseA = false;
165 #endif
166 CPPUNIT_ASSERT(undefinedOrFalseA);
167 CPPUNIT_ASSERT(!undefinedOrTrueA);
168
169 #if ZERO_FOO || UNDEFINED_FOO
170 bool undefinedOrTrueB = true;
171 #else
172 bool undefinedOrTrueB = false;
173 #endif
174 #if ZERO_FOO || !UNDEFINED_FOO
175 bool undefinedOrFalseB = true;
176 #else
177 bool undefinedOrFalseB = false;
178 #endif
179 CPPUNIT_ASSERT(undefinedOrFalseB);
180 CPPUNIT_ASSERT(!undefinedOrTrueB);
181
182 #if UNDEFINED_FOO || UNDEFINED_FOO
183 bool undefinedOrUndefinedC = true;
184 #else
185 bool undefinedOrUndefinedC = false;
186 #endif
187 #if !UNDEFINED_FOO || !UNDEFINED_FOO
188 bool notUndefinedOrNotUndefinedC = true;
189 #else
190 bool notUndefinedOrNotUndefinedC = false;
191 #endif
192 CPPUNIT_ASSERT(notUndefinedOrNotUndefinedC);
193 CPPUNIT_ASSERT(!undefinedOrUndefinedC);
194 }
195
196 int
197 main(int argc, char *argv[])
198 {
199 return TestProgram().run(argc, argv);
200 }
201