Browse Source

faux.testc_helpers: Testc helpers

Serj Kalichev 4 years ago
parent
commit
979e6f010b

+ 5 - 2
faux/Makefile.am

@@ -15,7 +15,8 @@ nobase_include_HEADERS += \
 	faux/log.h \
 	faux/list.h \
 	faux/ini.h \
-	faux/file.h
+	faux/file.h \
+	faux/testc_helpers.h
 
 EXTRA_DIST += \
 	faux/base/Makefile.am \
@@ -26,7 +27,8 @@ EXTRA_DIST += \
 	faux/log/Makefile.am \
 	faux/list/Makefile.am \
 	faux/ini/Makefile.am \
-	faux/file/Makefile.am
+	faux/file/Makefile.am \
+	faux/testc_helpers/Makefile.am
 
 include $(top_srcdir)/faux/base/Makefile.am
 include $(top_srcdir)/faux/ctype/Makefile.am
@@ -37,6 +39,7 @@ include $(top_srcdir)/faux/log/Makefile.am
 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/testc_helpers/Makefile.am
 
 if TESTC
 include $(top_srcdir)/faux/testc_module/Makefile.am

+ 79 - 0
faux/ini/testc_ini.c

@@ -1,6 +1,9 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "faux/ini.h"
+#include "faux/testc_helpers.h"
+
 int testc_faux_ini_good(void) {
 
 	char *path = NULL;
@@ -25,3 +28,79 @@ int testc_faux_ini_signal(void) {
 	printf("%s\n", p);
 	return -1;
 }
+
+int testc_faux_ini_parse(void) {
+
+	// Source INI file
+	const char *src_file =
+		"# Comment\n"
+		"DISTRIB_ID=Ubuntu\n"
+		"DISTRIB_RELEASE=18.04\n"
+		"DISTRIB_CODENAME=bionic\n"
+		"DISTRIB_DESCRIPTION=\"Ubuntu 18.04.4 LTS\"\n"
+		"COMPLEX_VAR=\"  Ubuntu\t\t1818 \"\n"
+		"WO_QUOTES_VAR = qwerty\n"
+		"WO_QUOTES_VAR2 = qwerty 98989898\n"
+		"EMPTY_VAR3 = \n"
+		"EMPTY_VAR4 =\n"
+		"     EMPTY_VAR5 = \"\"\t   \n"
+		"     ANOTHER_VAR6 = \"Normal var\"\t   \n"
+		"\tTABBED_VAR = \"Normal tabbed var\"\t   \n"
+		"# Another comment\n"
+		"  # Yet another comment\n"
+		"\t# Tabbed comment\n"
+		"VAR_WITHOUT_EOL=zxcvbnm"
+	;
+
+// Etalon file
+	const char *etalon_file =
+		"ANOTHER_VAR6=\"Normal var\"\n"
+		"COMPLEX_VAR=\"  Ubuntu\t\t1818 \"\n"
+		"DISTRIB_CODENAME=bionic\n"
+		"DISTRIB_DESCRIPTION=\"Ubuntu 18.04.4 LTS\"\n"
+		"DISTRIB_ID=Ubuntu\n"
+		"DISTRIB_RELEASE=18.04\n"
+		"TABBED_VAR=\"Normal tabbed var\"\n"
+		"VAR_WITHOUT_EOL=zxcvbnm\n"
+		"WO_QUOTES_VAR=qwerty\n"
+		"WO_QUOTES_VAR2=qwerty\n"
+		"\"test space\"=\"lk lk lk \"\n"
+	;
+
+	faux_ini_t *ini = NULL;
+	faux_ini_node_t *iter = NULL;
+	const faux_pair_t *pair = NULL;
+	const char *src_fn = "/tmp/src12";
+	const char *dst_fn = "/tmp/dst12";
+	const char *etalon_fn = "/tmp/etalon12";
+	unsigned num_entries = 0;
+	ssize_t r = 0;
+
+	// Prepare files
+	r = faux_testc_file_deploy(src_fn, src_file);
+	if (r < 0) {
+		fprintf(stderr, "Can't create test file %s\n", src_fn);
+	}
+	faux_testc_file_deploy(etalon_fn, etalon_file);
+
+	ini = faux_ini_new();
+	faux_ini_parse_file(ini, src_fn);
+	iter = faux_ini_iter(ini);
+	while ((pair = faux_ini_each(&iter))) {
+		num_entries++;
+		printf("[%s] = [%s]\n", faux_pair_name(pair), faux_pair_value(pair));
+	}
+	if (10 != num_entries) {
+		fprintf(stderr, "Wrong number of entries %u\n", num_entries);
+		faux_ini_free(ini);
+		return -1;
+	}
+
+	faux_ini_set(ini, "test space", "lk lk lk ");
+	faux_ini_write_file(ini, dst_fn);
+
+	faux_ini_free(ini);
+
+	return 0;
+
+}

+ 18 - 0
faux/testc_helpers.h

@@ -0,0 +1,18 @@
+/** @file testc_helpers.h
+ * @brief Testc helper functions.
+ */
+
+#ifndef _faux_testc_helpers_h
+#define _faux_testc_helpers_h
+
+#include <stddef.h>
+
+#include "faux/faux.h"
+
+C_DECL_BEGIN
+
+ssize_t faux_testc_file_deploy(const char *fn, const char *str);
+
+C_DECL_END
+
+#endif				/* _faux_testc_helpers_h */

+ 3 - 0
faux/testc_helpers/Makefile.am

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

+ 38 - 0
faux/testc_helpers/testc_helpers.c

@@ -0,0 +1,38 @@
+/** @file testc_helpers.c
+ * @brief Testc helper functions
+ *
+ * This file implements helpers for writing tests for 'testc' utility.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <errno.h>
+
+#include "faux/ctype.h"
+#include "faux/str.h"
+#include "faux/file.h"
+
+
+ssize_t faux_testc_file_deploy(const char *fn, const char *str) {
+
+	faux_file_t *f = NULL;
+	ssize_t bytes_written = 0;
+
+	assert(fn);
+	assert(str);
+	if (!fn || !str)
+		return -1;
+
+	f = faux_file_open(fn, O_WRONLY | O_CREAT | O_TRUNC, 00644);
+	if (!f)
+		return -1;
+	bytes_written = faux_file_write_block(f, str, strlen(str));
+	faux_file_close(f);
+	if (bytes_written < 0)
+		return -1;
+
+	return bytes_written;
+}

+ 1 - 0
faux/testc_module/testc_module.c

@@ -7,5 +7,6 @@ const char *testc_module[][2] = {
 	{"testc_faux_ini_good", "INI subsystem good"},
 	{"testc_faux_ini_bad", "INI bad"},
 	{"testc_faux_ini_signal", "Interrupted by signal"},
+	{"testc_faux_ini_parse", "Complex test of INI file parsing"},
 	{NULL, NULL}
 	};

+ 5 - 1
testc/testc.c

@@ -47,7 +47,7 @@
 #define SYM_TESTC_MODULE "testc_module"
 
 #define CHUNK_SIZE 1024
-#define TEST_OUTPUT_LIMIT 4096
+#define TEST_OUTPUT_LIMIT 1024 * CHUNK_SIZE
 
 // Command line options */
 struct opts_s {
@@ -247,7 +247,11 @@ int main(int argc, char *argv[]) {
 			if (!WIFEXITED(wstatus) ||
 				WEXITSTATUS(wstatus) != 0 ||
 				opts->debug) {
+				if (faux_list_len(buf_list) > 0)
+					printf("~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ \n");
 				print_test_output(buf_list);
+				if (faux_list_len(buf_list) > 0)
+					printf("~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ \n");
 			}
 
 			faux_list_free(buf_list);

+ 1 - 1
utils/faux-file2c.c

@@ -133,7 +133,7 @@ int main(int argc, char *argv[]) {
 			faux_free(buf);
 			if (0 == total_bytes) // Empty file
 				printf("\t\"\"\n");
-		
+
 		// Text mode
 		} else {
 			bool_t eof = BOOL_FALSE;