From 18b8a00cae733446a68cfb159309791e9053d85f Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 21 Jan 2011 14:08:35 +1030 Subject: [PATCH] lca2011: enhance test/run to try multiple inputs. --- ccan/oserver/test/run.c | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/ccan/oserver/test/run.c b/ccan/oserver/test/run.c index 3a93be8..f6c5b45 100644 --- a/ccan/oserver/test/run.c +++ b/ccan/oserver/test/run.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -10,31 +11,41 @@ int main(void) { - int fd; + int fd, i; int status; char buf[200]; + const char *input[] = { "This is a test\n", + "This is a test\r", + "This is a test\r\n", + "This is a test\nWith extra", + "This is a test\rWith extra", + "This is a test\r\nWith extra" }; /* This is how many tests you plan to run */ - plan_tests(3); + plan_tests(4 * ARRAY_SIZE(input)); - fd = open("run-fd", O_RDWR|O_CREAT|O_TRUNC, 0600); - write(fd, "This is a test\n", sizeof("This is a test\n")); - lseek(fd, 0, SEEK_SET); + for (i = 0; i < ARRAY_SIZE(input); i++) { + fd = open("run-fd", O_RDWR|O_CREAT|O_TRUNC, 0600); - if (fork() == 0) - oserver_serve(fd); + write(fd, input[i], strlen(input[i])); + lseek(fd, 0, SEEK_SET); - wait(&status); + if (fork() == 0) + oserver_serve(fd); - ok1(WIFEXITED(status)); - ok1(WEXITSTATUS(status) == 0); + wait(&status); - lseek(fd, 0, SEEK_SET); - buf[read(fd, buf, sizeof(buf)-1)] = '\0'; + ok1(WIFEXITED(status)); + ok1(WEXITSTATUS(status) == 0); - ok1(streq(buf, "This is a test\n" - "Louder, like this: 'THIS IS A TEST'\r\n")); + lseek(fd, 0, SEEK_SET); + buf[read(fd, buf, sizeof(buf)-1)] = '\0'; + ok1(strncmp(buf, input[i], strlen("This is a test")) == 0); + ok1(streq(buf + strlen("This is a test") + 1, + "Louder, like this: 'THIS IS A TEST'\r\n")); + } + /* This exits depending on whether all tests passed */ return exit_status(); } -- 2.39.2