Browse Source

faux.testc_helpers: Function to compare files faux_testc_file_cmp()

Serj Kalichev 4 years ago
parent
commit
397073bd57

+ 8 - 6
faux/ini/testc_ini.c

@@ -22,6 +22,7 @@ int testc_faux_ini_bad(void) {
 	return -1;
 }
 
+
 int testc_faux_ini_signal(void) {
 
 	char *p = NULL;
@@ -30,6 +31,7 @@ int testc_faux_ini_signal(void) {
 	return -1;
 }
 
+
 int testc_faux_ini_parse(void) {
 
 	// Source INI file
@@ -75,7 +77,6 @@ int testc_faux_ini_parse(void) {
 	char *src_fn = NULL;
 	char *dst_fn = NULL;
 	char *etalon_fn = NULL;
-	unsigned num_entries = 0;
 
 	// Prepare files
 	src_fn = faux_testc_tmpfile_deploy(src_file);
@@ -90,13 +91,8 @@ int testc_faux_ini_parse(void) {
 
 	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);
-		goto parse_error;
-	}
 
 	faux_ini_set(ini, "test space", "lk lk lk ");
 	if (faux_ini_write_file(ini, dst_fn) < 0) {
@@ -104,6 +100,12 @@ int testc_faux_ini_parse(void) {
 		goto parse_error;
 	}
 
+	if (faux_testc_file_cmp(dst_fn, etalon_fn) != 0) {
+		fprintf(stderr, "Generated file %s is not equal to etalon %s\n",
+		dst_fn, etalon_fn);
+		goto parse_error;
+	}
+
 	ret = 0; // success
 
 parse_error:

+ 1 - 0
faux/testc_helpers.h

@@ -15,6 +15,7 @@ C_DECL_BEGIN
 
 ssize_t faux_testc_file_deploy(const char *fn, const char *str);
 char *faux_testc_tmpfile_deploy(const char *str);
+int faux_testc_file_cmp(const char *first_file, const char *second_file);
 
 C_DECL_END
 

+ 44 - 0
faux/testc_helpers/testc_helpers.c

@@ -75,3 +75,47 @@ char *faux_testc_tmpfile_deploy(const char *str) {
 
 	return template;
 }
+
+#define CHUNK_SIZE 1024
+
+int faux_testc_file_cmp(const char *first_file, const char *second_file) {
+
+	int ret = -1; // Pessimistic retval
+	faux_file_t *f = NULL;
+	faux_file_t *s = NULL;
+	char buf_f[CHUNK_SIZE];
+	char buf_s[CHUNK_SIZE];
+	ssize_t readed_f = 0;
+	ssize_t readed_s = 0;
+
+	assert(first_file);
+	assert(second_file);
+	if (!first_file || !second_file)
+		return -1;
+
+	f = faux_file_open(first_file, O_RDONLY, 0);
+	s = faux_file_open(second_file, O_RDONLY, 0);
+	if (!f || !s)
+		goto cmp_error;
+
+	do {
+		readed_f = faux_file_read_block(f, buf_f, CHUNK_SIZE);
+		readed_s = faux_file_read_block(s, buf_s, CHUNK_SIZE);
+		if (readed_f != readed_s)
+			goto cmp_error;
+		if (readed_f < 0)
+			goto cmp_error;
+		if (0 == readed_f)
+			break; // EOF
+		if (memcmp(buf_f, buf_s, readed_f) != 0)
+			goto cmp_error;
+	} while (CHUNK_SIZE == readed_f); // Not full chunk so EOF is near
+
+	ret = 0; // Equal
+
+cmp_error:
+	faux_file_close(f);
+	faux_file_close(s);
+
+	return ret;
+}

+ 3 - 3
faux/testc_module/testc_module.c

@@ -4,9 +4,9 @@ const unsigned char testc_version_major = 1;
 const unsigned char testc_version_minor = 0;
 
 const char *testc_module[][2] = {
-	{"testc_faux_ini_bad", "INI bad"},
-	{"testc_faux_ini_signal", "Interrupted by signal"},
-	{"testc_faux_ini_good", "INI subsystem good"},
+//	{"testc_faux_ini_bad", "INI bad"}, // demo
+//	{"testc_faux_ini_signal", "Interrupted by signal"}, // demo
+//	{"testc_faux_ini_good", "INI subsystem good"}, // demo
 	{"testc_faux_ini_parse", "Complex test of INI file parsing"},
 	{NULL, NULL}
 	};