From 5d09eb685f545febb5d69f80b1a2a5c32b6fda50 Mon Sep 17 00:00:00 2001 From: Eric Bollengier Date: Thu, 18 Mar 2021 10:27:03 +0100 Subject: [PATCH] regress: Add unittest for bdelete_and_free() macro --- bacula/src/tools/Makefile.in | 3 ++ bacula/src/tools/test-cpp.c | 60 ++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 bacula/src/tools/test-cpp.c diff --git a/bacula/src/tools/Makefile.in b/bacula/src/tools/Makefile.in index 82f1b1d12..24225509b 100644 --- a/bacula/src/tools/Makefile.in +++ b/bacula/src/tools/Makefile.in @@ -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 index 000000000..ae11268a0 --- /dev/null +++ b/bacula/src/tools/test-cpp.c @@ -0,0 +1,60 @@ +/* + Bacula(R) - The Network Backup Solution + + Copyright (C) 2000-2022 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(); +} -- 2.47.3