Browse Source

testc: Some experimental tests

Serj Kalichev 3 years ago
parent
commit
0ef20602d7
8 changed files with 120 additions and 13 deletions
  1. 5 1
      Makefile.am
  2. 6 6
      configure.ac
  3. 3 3
      faux/Makefile.am
  4. 4 0
      faux/ini/Makefile.am
  5. 11 0
      faux/ini/testc_ini.c
  6. 3 0
      faux/testc_module/Makefile.am
  7. 7 0
      faux/testc_module/testc_module.c
  8. 81 3
      testc/testc.c

+ 5 - 1
Makefile.am

@@ -11,7 +11,11 @@ if DEBUG
 DEBUG_CFLAGS = -DDEBUG
 endif
 
-AM_CFLAGS = -Wall $(DEBUG_CFLAGS) $(LEGACY_CFLAGS)
+if TESTC
+TESTC_CFLAGS = -DTESTC
+endif
+
+AM_CFLAGS = -Wall $(DEBUG_CFLAGS) $(TESTC_CFLAGS)
 
 bin_PROGRAMS =
 lib_LTLIBRARIES =

+ 6 - 6
configure.ac

@@ -78,14 +78,14 @@ AC_ARG_ENABLE(debug,
 AM_CONDITIONAL(DEBUG,test x$enable_debug = xyes)
 
 ################################
-# Deal with legacy klish-2 features
+# Compile in testc tests
 ################################
-AC_ARG_ENABLE(legacy,
-              [AS_HELP_STRING([--enable-legacy],
-                              [Turn on legacy klish-2 features [default=no]])],
+AC_ARG_ENABLE(testc,
+              [AS_HELP_STRING([--enable-testc],
+                              [Enable testc tests compiling [default=no]])],
               [],
-              [enable_debug=no])
-AM_CONDITIONAL(LEGACY,test x$enable_legacy = xyes)
+              [enable_testc=no])
+AM_CONDITIONAL(TESTC,test x$enable_testc = xyes)
 
 ################################
 # Check for Lua support

+ 3 - 3
faux/Makefile.am

@@ -43,6 +43,6 @@ include $(top_srcdir)/faux/list/Makefile.am
 include $(top_srcdir)/faux/ini/Makefile.am
 include $(top_srcdir)/faux/file/Makefile.am
 
-#include $(top_srcdir)/faux/argv/Makefile.am
-#include $(top_srcdir)/faux/dump/Makefile.am
-#include $(top_srcdir)/faux/system/Makefile.am
+if TESTC
+include $(top_srcdir)/faux/testc_module/Makefile.am
+endif

+ 4 - 0
faux/ini/Makefile.am

@@ -2,3 +2,7 @@ libfaux_la_SOURCES += \
 	faux/ini/pair.c \
 	faux/ini/ini.c \
 	faux/ini/private.h
+
+if TESTC
+libfaux_la_SOURCES += faux/ini/testc_ini.c
+endif

+ 11 - 0
faux/ini/testc_ini.c

@@ -0,0 +1,11 @@
+int testc_faux_ini_good(void) {
+
+	return 0;
+}
+
+
+int testc_faux_ini_bad(void) {
+
+	return -1;
+}
+

+ 3 - 0
faux/testc_module/Makefile.am

@@ -0,0 +1,3 @@
+libfaux_la_SOURCES += \
+	faux/testc_module/testc_module.c
+

+ 7 - 0
faux/testc_module/testc_module.c

@@ -0,0 +1,7 @@
+#include "faux/ini.h"
+
+const char *testc_module[] = {
+	"testc_faux_ini_good", "INI subsystem good",
+	"testc_faux_ini_bad", "INI bad",
+	NULL
+	};

+ 81 - 3
testc/testc.c

@@ -6,6 +6,8 @@
 #include <stdio.h>
 #include <assert.h>
 #include <string.h>
+#include <unistd.h>
+#include <dlfcn.h>
 
 #if WITH_INTERNAL_GETOPT
 #include "libc/getopt.h"
@@ -32,6 +34,9 @@
 #define QUOTE(t) #t
 #define version(v) printf("%s\n", v)
 
+#define TESTC_LIST_SYM "testc_module"
+
+
 // Command line options */
 struct opts_s {
 	int debug;
@@ -50,6 +55,12 @@ int main(int argc, char *argv[]) {
 	opts_t *opts = NULL;
 	faux_list_node_t *iter = NULL;
 	char *so = NULL;
+	// Return value will be negative on any error or failed test.
+	// It doesn't mean that any error will break the processing.
+	// The following var is error counter.
+	unsigned int total_errors = 0;
+	unsigned int total_modules = 0;
+	unsigned int total_tests = 0;
 
 #if HAVE_LOCALE_H
 	// Set current locale
@@ -65,13 +76,80 @@ int main(int argc, char *argv[]) {
 
 	iter = faux_list_head(opts->so_list);
 	while ((so = faux_list_each(&iter))) {
-		printf("%s\n", so);
-	}
-	
+		void *so_handle = NULL;
+		const char **test_list = NULL;
+		const char *test_name = NULL;
+		const char *test_desc = NULL;
+		unsigned int module_tests = 0;
+		unsigned int module_errors = 0;
+
+		total_modules++;
+		printf("Processing module \"%s\"...\n", so);
+		if (access(so, R_OK) < 0) {
+			fprintf(stderr, "Error: Can't read module \"%s\"... Skipped\n", so);
+			total_errors++;
+			continue;
+		}
+
+		so_handle = dlopen(so, RTLD_LAZY | RTLD_LOCAL);
+		if (!so_handle) {
+			fprintf(stderr, "Error: Can't open module \"%s\"... Skipped\n", so);
+			total_errors++;
+			continue;
+		}
+
+		test_list = (const char **)dlsym(so_handle, TESTC_LIST_SYM);
+		while (*test_list) {
+			int (*test_sym)(void);
+			int retval = 0;
+			char *result = NULL;
+
+			test_name = *test_list;
+			test_list++;
+			if (!*test_list) // Broken test list structure
+				break;
+			test_desc = *test_list;
+			test_list++;
+			module_tests++;
+
+			test_sym = (int (*)(void))dlsym(so_handle, test_name);
+			if (!test_sym) {
+				fprintf(stderr, "Error: Can't find symbol \"%s\"... Skipped\n", test_name);
+				module_errors++;
+				continue;
+			}
+
+			retval = test_sym();
+			if (0 == retval) {
+				result = "success";
+			} else {
+				result = "fail";
+				module_errors++;
+			}
+			printf("Test #%03u %s() %s: %s\n", module_tests, test_name, test_desc, result);
+		}
 
 
+		dlclose(so_handle);
+		so_handle = NULL;
+
+		printf("Module tests: %u\n", module_tests);
+		printf("Module errors: %u\n", module_errors);
+
+		total_tests += module_tests;
+		total_errors += module_errors;
+
+	}
+
 	opts_free(opts);
 
+	// Total statistics
+	printf("Total modules: %u\n", total_modules);
+	printf("Total tests: %u\n", total_tests);
+	printf("Total errors: %u\n", total_errors);
+
+	if (total_errors > 0)
+		return -1;
 	return 0;
 }