]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
regress: Add unittest for bdelete_and_free() macro
authorEric Bollengier <eric@baculasystems.com>
Thu, 18 Mar 2021 09:27:03 +0000 (10:27 +0100)
committerEric Bollengier <eric@baculasystems.com>
Fri, 26 Mar 2021 13:57:58 +0000 (14:57 +0100)
bacula/src/tools/Makefile.in
bacula/src/tools/test-cpp.c [new file with mode: 0644]

index 82f1b1d127c722108cff1a2903d333be135e1ee0..24225509b3fb11f3b2c59b1f13d90a74c8b1c15d 100644 (file)
@@ -61,6 +61,9 @@ all: Makefile $(TOOLS) gigaslam grow
        @echo "==== Make of tools is good ===="
        @echo " "
 
+test-cpp: test-cpp.o ../lib/unittests.o
+       $(LIBTOOL_LINK) $(CXX) $(LDFLAGS)  -L../lib -o $@ test-cpp.o ../lib/unittests.o -lbac $(DLIB) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)
+
 fs-io-error: fs-io-error.c
        $(CXX) -Wall fs-io-error.c -D_FILE_OFFSET_BITS=64 -lfuse  -o fs-io-error
 
diff --git a/bacula/src/tools/test-cpp.c b/bacula/src/tools/test-cpp.c
new file mode 100644 (file)
index 0000000..49dfaba
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+   Bacula(R) - The Network Backup Solution
+
+   Copyright (C) 2000-2020 Kern Sibbald
+
+   The original author of Bacula is Kern Sibbald, with contributions
+   from many others, a complete list can be found in the file AUTHORS.
+
+   You may use this file and others of this release according to the
+   license defined in the LICENSE file, which includes the Affero General
+   Public License, v3.0 ("AGPLv3") and some additional permissions and
+   terms pursuant to its AGPLv3 Section 7.
+
+   This notice must be preserved when any source code is
+   conveyed and/or propagated.
+
+   Bacula(R) is a registered trademark of Kern Sibbald.
+*/
+
+/* Written by Eric Bollengier March 2021 */
+
+#include "bacula.h"
+#include "lib/unittests.h"
+
+/* These macro are defined in src/baconfig.h */
+#if __cplusplus >= 201103L
+# define bdelete_and_null_auto(a) do{if(a){auto b__ = a; (a)=NULL; delete b__;}} while(0)
+#else
+# define do{ok(1, "auto not available")
+#endif
+
+# ifdef HAVE_TYPEOF
+#  define bdelete_and_null_typeof(a) do{if(a){typeof(a) b__ = a; (a)=NULL; delete b__;}} while(0)
+# else
+# define ok(1, "typeof not available")
+#endif
+
+class obj
+{
+public:
+   obj(){};
+   ~obj(){ok(1, "delete was called properly");}
+};
+
+int main()
+{
+   Unittests alist_test("test-cpp");
+   log("Test C++ Features ...");
+   obj *a = new obj();
+   obj *b = new obj();
+   char *c = (char *)malloc(10);
+   bdelete_and_null_auto(a);
+   bdelete_and_null_typeof(b);
+   bfree_and_null(c);
+   is(unittest_get_nb_tests(), 2, "Test if the two delete were done");
+   ok(c == NULL, "bfree_and_null()");
+   ok(a == NULL, "bdelete_and_null_auto()");
+   ok(b == NULL, "bdelete_and_null_typeof()");
+   return report();
+}