testPreCompiler.cc
Go to the documentation of this file.
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
19class TestPreCompiler : public CPPUNIT_NS::TestFixture
20{
26
27protected:
28 void testIfDef();
29 void testIfDefAnd();
30 void testIfDefOr();
31};
33
39void
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
96void
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
149void
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
196int
197main(int argc, char *argv[])
198{
199 return TestProgram().run(argc, argv);
200}
201
CPPUNIT_TEST(testIfDef)
CPPUNIT_TEST(testIfDefAnd)
CPPUNIT_TEST(testIfDefOr)
CPPUNIT_TEST_SUITE(TestPreCompiler)
implements test program's main() function while enabling customization
Definition: unitTestMain.h:26
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
int main(int argc, char *argv[])
CPPUNIT_TEST_SUITE_REGISTRATION(TestPreCompiler)

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors