git » libfiu » commit d4239ee

tests: Make random tests more robust

author Alberto Bertogli
2015-08-02 14:33:00 UTC
committer Alberto Bertogli
2015-08-02 22:46:09 UTC
parent 3a43154e54a11f4db7b47c08eb38228ec0127049

tests: Make random tests more robust

Some of the tests which check the random operations depend on /bin/cat doing
I/O operations a certain way, which is not portable and not very accurate
either. This results in these tests occasionally failing without good reason.

To fix that, we introduce a small "cat" implementation with very simple and
predictable I/O, which we can rely upon in the tests.

.gitignore +1 -0
tests/Makefile +4 -2
tests/small-cat.c +36 -0
tests/test-fiu_ctrl.py +3 -1

diff --git a/.gitignore b/.gitignore
index 55e90a6..b365210 100644
--- a/.gitignore
+++ b/.gitignore
@@ -29,6 +29,7 @@ preload/run/build-needlibdl
 tests/*.o
 tests/build-flags
 tests/test-?
+tests/small-cat
 tests/libs/
 tests/generated/build-flags
 tests/generated/tests/*.[oc]
diff --git a/tests/Makefile b/tests/Makefile
index 7d57070..c6efc9a 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -81,9 +81,11 @@ PY_TESTS := $(wildcard test-*.py)
 
 py-tests: $(patsubst %.py,py-run-%,$(PY_TESTS))
 
-py-run-%: %.py lnlibs
+py-run-%: %.py lnlibs small-cat
 	$(NICE_PY) ./$<
 
+small-cat: small-cat.c
+	$(NICE_CC) $(ALL_CFLAGS) $< -o $@
 
 #
 # Sub-directory tests
@@ -104,7 +106,7 @@ utils-tests:
 # also remove them when cleaning just in case.
 clean:
 	rm -f $(C_OBJS) $(C_BINS)
-	rm -rf libs/
+	rm -rf libs/ small-cat
 	rm -f *.bb *.bbg *.da *.gcov *.gcda *.gcno gmon.out build-flags
 	$(MAKE) -C generated clean
 
diff --git a/tests/small-cat.c b/tests/small-cat.c
new file mode 100644
index 0000000..4fa42f1
--- /dev/null
+++ b/tests/small-cat.c
@@ -0,0 +1,36 @@
+// A small "cat" utility that copies stdin to stdout.
+// It does only one 4K read from stdin, and writes it to stdout in as few
+// write()s as possible.
+// This gives a controlled number of operations, which makes testing the
+// random operations more robust.
+
+#include <stdio.h>  // printf(), perror()
+#include <unistd.h> // read(), write()
+
+const size_t BUFSIZE = 4092;
+
+int main(void)
+{
+	char buf[BUFSIZE];
+	ssize_t r, w, pos;
+
+	r = read(0, buf, BUFSIZE);
+	if (r < 0) {
+		perror("Read error in small-cat");
+		return 1;
+	}
+
+	pos = 0;
+	while (r > 0) {
+		w = write(1, buf + pos, r);
+		if (w <= 0) {
+			perror("Write error in small-cat");
+			return 2;
+		}
+
+		pos += w;
+		r -= w;
+	}
+
+	return 0;
+}
diff --git a/tests/test-fiu_ctrl.py b/tests/test-fiu_ctrl.py
index 104ca9d..04e2fbc 100644
--- a/tests/test-fiu_ctrl.py
+++ b/tests/test-fiu_ctrl.py
@@ -13,7 +13,7 @@ import time
 fiu_ctrl.PLIBPATH = "./libs/"
 
 def run_cat(**kwargs):
-    return fiu_ctrl.Subprocess(["/bin/cat"],
+    return fiu_ctrl.Subprocess(["./small-cat"],
         stdin = subprocess.PIPE, stdout = subprocess.PIPE,
         stderr = subprocess.PIPE, **kwargs)
 
@@ -49,6 +49,8 @@ out, err = p.communicate('test\n')
 assert out == 'test\n', (out, err)
 
 # Enable random.
+# This relies on cat doing a reasonably small number of read and writes, which
+# our small-cat does.
 result = { True: 0, False: 0 }
 for i in range(50):
     cmd = run_cat(fiu_enable_posix = True)