From d4239ee4a5f8c4a5e199fb462b25e4415a4c5d95 Mon Sep 17 00:00:00 2001 From: Alberto Bertogli Date: Sun, 2 Aug 2015 15:33:00 +0100 Subject: [PATCH] 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 + tests/Makefile | 6 ++++-- tests/small-cat.c | 36 ++++++++++++++++++++++++++++++++++++ tests/test-fiu_ctrl.py | 4 +++- 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 tests/small-cat.c 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 // printf(), perror() +#include // 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) -- 2.11.4.GIT