]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - compat/testPreCompiler.cc
Source Format Enforcement (#1234)
[thirdparty/squid.git] / compat / testPreCompiler.cc
index f00d38bb7b792211358bbad14bc122ef5ef1e989..a5b807bd50c3dbe861ee936da5b407b2580b6ab9 100644 (file)
@@ -1,11 +1,16 @@
-#define SQUID_UNIT_TEST 1
-#include "config.h"
-
-#if HAVE_ASSERT_H
-#include <assert.h>
-#endif
+/*
+ * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
 
+#include "squid.h"
 #include "testPreCompiler.h"
+#include "unitTestMain.h"
+
+#include <cassert>
 
 CPPUNIT_TEST_SUITE_REGISTRATION( testPreCompiler );
 
@@ -64,3 +69,110 @@ testPreCompiler::testIfDef()
     CPPUNIT_ASSERT(undefinedFalse);
     CPPUNIT_ASSERT(!undefinedTrue);
 }
+
+/**
+ * Test several ways of defining pre-compiler directives.
+ * Squid-3 uses #if FOO syntax for precompiler directives.
+ * These tests ensure that the inputs will work as expected
+ * when undefined macros are used in && conditions
+ */
+void
+testPreCompiler::testIfDefAnd()
+{
+    /* Not Defined to exist at all == false - when used in a compound if */
+#undef UNDEFINED_FOO
+#define ONE_FOO 1
+
+#if UNDEFINED_FOO && ONE_FOO
+    bool undefinedAndTrueA = true;
+#else
+    bool undefinedAndTrueA = false;
+#endif
+#if !UNDEFINED_FOO && ONE_FOO
+    bool undefinedAndFalseA = true;
+#else
+    bool undefinedAndFalseA = false;
+#endif
+    CPPUNIT_ASSERT(undefinedAndFalseA);
+    CPPUNIT_ASSERT(!undefinedAndTrueA);
+
+#if ONE_FOO && UNDEFINED_FOO
+    bool undefinedAndTrueB = true;
+#else
+    bool undefinedAndTrueB = false;
+#endif
+#if ONE_FOO && !UNDEFINED_FOO
+    bool undefinedAndFalseB = true;
+#else
+    bool undefinedAndFalseB = false;
+#endif
+    CPPUNIT_ASSERT(undefinedAndFalseB);
+    CPPUNIT_ASSERT(!undefinedAndTrueB);
+
+#if UNDEFINED_FOO && UNDEFINED_FOO
+    bool undefinedAndUndefinedC = true;
+#else
+    bool undefinedAndUndefinedC = false;
+#endif
+#if !UNDEFINED_FOO && !UNDEFINED_FOO
+    bool notUndefinedAndNotUndefinedC = true;
+#else
+    bool notUndefinedAndNotUndefinedC = false;
+#endif
+    CPPUNIT_ASSERT(!undefinedAndUndefinedC);
+    CPPUNIT_ASSERT(notUndefinedAndNotUndefinedC);
+}
+
+/**
+ * Test several ways of defining pre-compiler directives.
+ * Squid-3 uses #if FOO syntax for precompiler directives.
+ * These tests ensure that the inputs will work as expected
+ * when undefined macros are used in || conditions
+ */
+void
+testPreCompiler::testIfDefOr()
+{
+    /* Not Defined to exist at all == false - when used in a compound if */
+#undef UNDEFINED_FOO
+#define ZERO_FOO 0
+
+#if UNDEFINED_FOO || ZERO_FOO
+    bool undefinedOrTrueA = true;
+#else
+    bool undefinedOrTrueA = false;
+#endif
+#if !UNDEFINED_FOO || ZERO_FOO
+    bool undefinedOrFalseA = true;
+#else
+    bool undefinedOrFalseA = false;
+#endif
+    CPPUNIT_ASSERT(undefinedOrFalseA);
+    CPPUNIT_ASSERT(!undefinedOrTrueA);
+
+#if ZERO_FOO || UNDEFINED_FOO
+    bool undefinedOrTrueB = true;
+#else
+    bool undefinedOrTrueB = false;
+#endif
+#if ZERO_FOO || !UNDEFINED_FOO
+    bool undefinedOrFalseB = true;
+#else
+    bool undefinedOrFalseB = false;
+#endif
+    CPPUNIT_ASSERT(undefinedOrFalseB);
+    CPPUNIT_ASSERT(!undefinedOrTrueB);
+
+#if UNDEFINED_FOO || UNDEFINED_FOO
+    bool undefinedOrUndefinedC = true;
+#else
+    bool undefinedOrUndefinedC = false;
+#endif
+#if !UNDEFINED_FOO || !UNDEFINED_FOO
+    bool notUndefinedOrNotUndefinedC = true;
+#else
+    bool notUndefinedOrNotUndefinedC = false;
+#endif
+    CPPUNIT_ASSERT(notUndefinedOrNotUndefinedC);
+    CPPUNIT_ASSERT(!undefinedOrUndefinedC);
+}
+