Browse Source

faux.testc_helper: Function to create bufs with random numbers

Serj Kalichev 3 years ago
parent
commit
2115d8765b
8 changed files with 190 additions and 144 deletions
  1. 4 2
      faux/buf.h
  2. 3 3
      faux/buf/Makefile.am
  3. 5 0
      faux/buf/buf.c
  4. 116 139
      faux/buf/testc_buf.c
  5. 16 0
      faux/faux.map
  6. 2 0
      faux/testc_helpers.h
  7. 40 0
      faux/testc_helpers/testc_helpers.c
  8. 4 0
      faux/testc_module/testc_module.c

+ 4 - 2
faux/buf.h

@@ -17,11 +17,13 @@ C_DECL_BEGIN
 
 faux_buf_t *faux_buf_new(size_t chunk_size);
 void faux_buf_free(faux_buf_t *buf);
+ssize_t faux_buf_len(const faux_buf_t *buf);
 ssize_t faux_buf_limit(const faux_buf_t *buf);
 bool_t faux_buf_set_limit(faux_buf_t *buf, size_t limit);
+bool_t faux_buf_is_wblocked(const faux_buf_t *buf);
+bool_t faux_buf_is_rblocked(const faux_buf_t *buf);
 ssize_t faux_buf_write(faux_buf_t *buf, const void *data, size_t len);
-ssize_t faux_buf_out(faux_buf_t *buf);
-ssize_t faux_buf_in(faux_buf_t *buf);
+ssize_t faux_buf_read(faux_buf_t *buf, void *data, size_t len);
 
 C_DECL_END
 

+ 3 - 3
faux/buf/Makefile.am

@@ -1,6 +1,6 @@
 libfaux_la_SOURCES += \
 	faux/buf/buf.c
 
-#if TESTC
-#libfaux_la_SOURCES += faux/buf/testc_buf.c
-#endif
+if TESTC
+libfaux_la_SOURCES += faux/buf/testc_buf.c
+endif

+ 5 - 0
faux/buf/buf.c

@@ -357,6 +357,11 @@ ssize_t faux_buf_read(faux_buf_t *buf, void *data, size_t len)
 	assert(buf);
 	if (!buf)
 		return -1;
+	assert(data);
+	if (!data)
+		return -1;
+	if (0 == len)
+		return -1;
 
 	// Don't read from the space reserved for direct read
 	if (faux_buf_is_rblocked(buf))

+ 116 - 139
faux/buf/testc_buf.c

@@ -6,182 +6,159 @@
 #include <unistd.h>
 
 #include "faux/str.h"
-#include "faux/async.h"
+#include "faux/buf.h"
 #include "faux/testc_helpers.h"
 
 
-static bool_t stall_cb(faux_async_t *async, size_t len, void *user_data)
-{
-	bool_t *o_flag = (bool_t *)user_data;
-
-	if (!o_flag)
-		return BOOL_FALSE;
-	*o_flag = BOOL_TRUE;
-
-	async = async; // Happy compiler
-	len = len; // Happy compiler
-
-	return BOOL_TRUE;
-}
+#define CHUNK 100
 
-
-int testc_faux_async_write(void)
+int testc_faux_buf(void)
 {
-	const size_t len = 9000000l;
-	const size_t read_chunk = 1000;
-	char *read_buf = NULL;
-	ssize_t readed = 0;
-	char *src_file = NULL;
-	int ret = -1; // Pessimistic return value
 	char *src_fn = NULL;
 	char *dst_fn = NULL;
-	unsigned int i = 0;
-	unsigned char counter = 0;
-	int fd = -1;
-	faux_async_t *out = NULL;
-	bool_t o_flag = BOOL_FALSE;
-	int pipefd[2] = {-1, -1};
+	ssize_t len = 0;
+	char *rnd = NULL;
+	char *dst = NULL;
+	faux_buf_t *buf = NULL;
 
 	// Prepare files
-	src_file = faux_zmalloc(len);
-	for (i = 0; i < len; i++) {
-		src_file[i] = counter;
-		counter++;
-	}
-	src_fn = faux_testc_tmpfile_deploy(src_file, len);
-
-	if (pipe(pipefd) < 0)
-		goto parse_error;
-	read_buf = faux_malloc(read_chunk);
-
-	dst_fn = faux_str_sprintf("%s/dst", getenv(FAUX_TESTC_TMPDIR_ENV));
-	fd = open(dst_fn, O_WRONLY | O_CREAT | O_TRUNC, 0600);
-
-	out = faux_async_new(pipefd[1]);
-	faux_async_set_stall_cb(out, stall_cb, &o_flag);
-	faux_async_set_write_overflow(out, len + 1);
-	if (faux_async_write(out, src_file, len) < 0) {
-		fprintf(stderr, "faux_async_write() error\n");
-		goto parse_error;
-	}
-
-	// "Async" pipe write and sync pipe read
-	while (o_flag) {
-		o_flag = BOOL_FALSE;
-		faux_async_out(out);
-		readed = read(pipefd[0], read_buf, read_chunk);
-		if (readed < 0)
-			continue;
-		if (write(fd, read_buf, readed) < 0)
-			continue;
-	}
-
-	// Read the rest data
-	close(pipefd[1]);
-	pipefd[1] = -1;
-	while ((readed = read(pipefd[0], read_buf, read_chunk)) > 0)
-		if (write(fd, read_buf, readed) < 0)
-			continue;
-
-	// Compare etalon file and generated file
-	if (faux_testc_file_cmp(dst_fn, src_fn) != 0) {
-		fprintf(stderr, "Destination file %s is not equal to source %s\n",
-			dst_fn, src_fn);
-		goto parse_error;
+	len = CHUNK * 3 + 15;
+	rnd = faux_testc_rnd_buf(len);
+	src_fn = faux_testc_tmpfile_deploy(rnd, len);
+
+	// Create buf
+	buf = faux_buf_new(CHUNK);
+	if (!buf) {
+		fprintf(stderr, "faux_buf_new() error\n");
+		return -1;
 	}
 
-	ret = 0; // success
-
-parse_error:
-	if (pipefd[0] >= 0)
-		close(pipefd[0]);
-	if (pipefd[1] >= 0)
-		close(pipefd[1]);
-	faux_async_free(out);
-	faux_str_free(dst_fn);
-	faux_str_free(src_fn);
+	// Write to buffer
+	if (faux_buf_write(buf, rnd, len - 5) != (len - 5)) {
+		fprintf(stderr, "faux_buf_write() error\n");
+		return -1;
+	}
+	if (faux_buf_write(buf, rnd + len - 5, 5) != 5) {
+		fprintf(stderr, "faux_buf_write() the rest error\n");
+		return -1;
+	}
 
-	return ret;
-}
+	// Buf length
+	if (faux_buf_len(buf) != len) {
+		fprintf(stderr, "faux_buf_len() error\n");
+		return -1;
+	}
 
+	// Buf read
+	dst = faux_malloc(len);
+	if (!dst) {
+		fprintf(stderr, "faux_malloc() error\n");
+		return -1;
+	}
+	if (faux_buf_read(buf, dst, len) != len) {
+		fprintf(stderr, "faux_buf_read() error\n");
+		return -1;
+	}
+	dst_fn = faux_testc_tmpfile_deploy(dst, len);
 
-static bool_t read_cb(faux_async_t *async, void *data, size_t len, void *user_data)
-{
-	int fd = *((int *)user_data);
+	// Buf length == 0
+	if (faux_buf_len(buf) != 0) {
+		fprintf(stderr, "faux_buf_len() is not 0: error\n");
+		return -1;
+	}
 
-	faux_write_block(fd, data, len);
-	faux_free(data);
+	// Compare files
+	if (faux_testc_file_cmp(dst_fn, src_fn) != 0) {
+		fprintf(stderr, "Destination file %s is not equal to source %s\n",
+			dst_fn, src_fn);
+		return -1;
+	}
 
-	async = async; // Happy compiler
+	faux_free(dst);
+	faux_buf_free(buf);
 
-	return BOOL_TRUE;
+	return 0;
 }
 
 
-int testc_faux_async_read(void)
+int testc_faux_buf_boundaries(void)
 {
-	const size_t len = 9000000l;
-	const size_t write_chunk = 2000;
-	const size_t read_chunk = 5000;
-	size_t left = 0;
-	char *src_file = NULL;
-	int ret = -1; // Pessimistic return value
 	char *src_fn = NULL;
 	char *dst_fn = NULL;
-	unsigned int i = 0;
-	unsigned char counter = 0;
-	faux_async_t *out = NULL;
-	int pipefd[2] = {-1, -1};
-	int fd = -1;
+	ssize_t len = 0;
+	char *rnd = NULL;
+	char *dst = NULL;
+	faux_buf_t *buf = NULL;
 
 	// Prepare files
-	src_file = faux_zmalloc(len);
-	for (i = 0; i < len; i++) {
-		src_file[i] = counter;
-		counter++;
+	len = CHUNK * 3;
+	rnd = faux_testc_rnd_buf(len);
+	src_fn = faux_testc_tmpfile_deploy(rnd, len);
+
+	// Create buf
+	buf = faux_buf_new(CHUNK);
+	if (!buf) {
+		fprintf(stderr, "faux_buf_new() error\n");
+		return -1;
 	}
-	src_fn = faux_testc_tmpfile_deploy(src_file, len);
 
-	if (pipe(pipefd) < 0)
-		goto parse_error;
-
-	dst_fn = faux_str_sprintf("%s/dst", getenv(FAUX_TESTC_TMPDIR_ENV));
-	fd = open(dst_fn, O_WRONLY | O_CREAT | O_TRUNC, 0600);
+	// Write to buffer
+	if (faux_buf_write(buf, rnd, len) != len) {
+		fprintf(stderr, "faux_buf_write() error\n");
+		return -1;
+	}
 
-	out = faux_async_new(pipefd[0]);
-	faux_async_set_read_cb(out, read_cb, &fd);
-	faux_async_set_read_limits(out, read_chunk, read_chunk);
+	// Buf length
+	if (faux_buf_len(buf) != len) {
+		fprintf(stderr, "faux_buf_len() error\n");
+		return -1;
+	}
 
-	// Sync pipe write and async pipe read
-	left = len;
-	while (left > 0) {
-		ssize_t bytes_written = 0;
+	// Buf read
+	dst = faux_malloc(len);
+	if (!dst) {
+		fprintf(stderr, "faux_malloc() error\n");
+		return -1;
+	}
+	if (faux_buf_read(buf, dst, len) != len) {
+		fprintf(stderr, "faux_buf_read() error\n");
+		return -1;
+	}
+	dst_fn = faux_testc_tmpfile_deploy(dst, len);
 
-		bytes_written = write(pipefd[1], src_file + len - left,
-			left < write_chunk ? left : write_chunk);
-		if (bytes_written < 0)
-			continue;
-		left -= bytes_written;
-		faux_async_in(out);
+	// Buf length == 0
+	if (faux_buf_len(buf) != 0) {
+		fprintf(stderr, "faux_buf_len() is not 0: error\n");
+		return -1;
 	}
 
-	// Compare etalon file and generated file
+	// Compare files
 	if (faux_testc_file_cmp(dst_fn, src_fn) != 0) {
 		fprintf(stderr, "Destination file %s is not equal to source %s\n",
 			dst_fn, src_fn);
-		goto parse_error;
+		return -1;
+	}
+
+	// Write to buffer anoter time
+	if (faux_buf_write(buf, rnd, len) != len) {
+		fprintf(stderr, "another faux_buf_write() error\n");
+		return -1;
+	}
+	if (faux_buf_read(buf, dst, len) != len) {
+		fprintf(stderr, "another faux_buf_read() error\n");
+		return -1;
 	}
+	dst_fn = faux_testc_tmpfile_deploy(dst, len);
 
-	ret = 0; // success
+	// Compare files another time
+	if (faux_testc_file_cmp(dst_fn, src_fn) != 0) {
+		fprintf(stderr, "Destination file %s is not equal to source %s\n",
+			dst_fn, src_fn);
+		return -1;
+	}
 
-parse_error:
-	if (pipefd[0] >= 0)
-		close(pipefd[0]);
-	if (pipefd[1] >= 0)
-		close(pipefd[1]);
-	faux_async_free(out);
-	faux_str_free(dst_fn);
-	faux_str_free(src_fn);
+	faux_free(dst);
+	faux_buf_free(buf);
 
-	return ret;
+	return 0;
 }

+ 16 - 0
faux/faux.map

@@ -320,5 +320,21 @@ FAUX_2.0 {
 		faux_vec_find_fn;
 		faux_vec_find;
 
+		faux_buf_new;
+		faux_buf_free;
+		faux_buf_len;
+		faux_buf_limit;
+		faux_buf_set_limit;
+		faux_buf_is_wblocked;
+		faux_buf_is_rblocked;
+		faux_buf_write;
+		faux_buf_read;
+
+		testc_version_major;
+		testc_version_minor;
+		testc_module;
+		testc_faux_*;
+
+
 	local: *;
 };

+ 2 - 0
faux/testc_helpers.h

@@ -18,6 +18,8 @@ ssize_t faux_testc_file_deploy_str(const char *fn, const char *str);
 char *faux_testc_tmpfile_deploy(const void *buf, size_t len);
 char *faux_testc_tmpfile_deploy_str(const char *str);
 int faux_testc_file_cmp(const char *first_file, const char *second_file);
+bool_t faux_testc_fill_rnd(void *buf, size_t len);
+char *faux_testc_rnd_buf(size_t len);
 
 C_DECL_END
 

+ 40 - 0
faux/testc_helpers/testc_helpers.c

@@ -140,3 +140,43 @@ cmp_error:
 
 	return ret;
 }
+
+
+bool_t faux_testc_fill_rnd(void *buf, size_t len)
+{
+	char *b = (char *)buf;
+	size_t pos = 0;
+
+	assert(buf);
+	if (!buf)
+		return BOOL_FALSE;
+	if (0 == len)
+		return BOOL_FALSE;
+
+	for (pos = 0; pos < len; pos++) {
+		b[pos] = (char)random();
+	}
+
+	return BOOL_TRUE;
+}
+
+
+char *faux_testc_rnd_buf(size_t len)
+{
+	char *buf = NULL;
+
+	if (0 == len)
+		return NULL;
+
+	buf = faux_malloc(len);
+	assert(buf);
+	if (!buf)
+		return NULL;
+
+	if (!faux_testc_fill_rnd(buf, len)) {
+		faux_free(buf);
+		return NULL;
+	}
+
+	return buf;
+}

+ 4 - 0
faux/testc_module/testc_module.c

@@ -46,6 +46,10 @@ const char *testc_module[][2] = {
 	{"testc_faux_async_write", "Async write operations"},
 	{"testc_faux_async_read", "Async read operations"},
 
+	// buf
+	{"testc_faux_buf", "Dynamic buffer"},
+	{"testc_faux_buf_boundaries", "Dynamic buffer. Check boundaries case"},
+
 	// End of list
 	{NULL, NULL}
 	};