diff options
author | Martin Pieuchot <mpi@cvs.openbsd.org> | 2013-03-27 15:08:14 +0000 |
---|---|---|
committer | Martin Pieuchot <mpi@cvs.openbsd.org> | 2013-03-27 15:08:14 +0000 |
commit | 50f3db5b45f0e86952183c52d247fd302db6448e (patch) | |
tree | 51898d5a350a8719c69f1775d8df2f6b936dac63 /regress | |
parent | 9ff3ab3504dfe7e7d285e3e0766479704249542f (diff) |
More test cases, especially when opening the stream in append an write
modes.
Diffstat (limited to 'regress')
-rw-r--r-- | regress/lib/libc/fmemopen/fmemopentest.c | 121 |
1 files changed, 113 insertions, 8 deletions
diff --git a/regress/lib/libc/fmemopen/fmemopentest.c b/regress/lib/libc/fmemopen/fmemopentest.c index ef7e9ff64f1..57063aeb0a8 100644 --- a/regress/lib/libc/fmemopen/fmemopentest.c +++ b/regress/lib/libc/fmemopen/fmemopentest.c @@ -1,4 +1,5 @@ -/* $OpenBSD: fmemopentest.c,v 1.1 2013/01/01 17:43:07 mpi Exp $ */ +/* $OpenBSD: fmemopentest.c,v 1.2 2013/03/27 15:08:13 mpi Exp $ */ + /* * Copyright (c) 2011 Martin Pieuchot <mpi@openbsd.org> * @@ -22,15 +23,15 @@ #include <unistd.h> int -main(void) +simpletest(void) { FILE *s1, *s2; - char c, string[] = "fmemopen test string!"; + char string[] = "fmemopen test string!"; char buffer[1024], *buf = NULL; size_t len; - int failures = 0; + int c, failures = 0; - s1 = fmemopen(string, strlen(string), "r"); + s1 = fmemopen(string, strlen(string) + 1, "r"); if (s1 == NULL) { warn("unable to open a stream s1"); return (1); @@ -46,7 +47,7 @@ main(void) while ((c = fgetc(s1)) != EOF) fputc(c, s2); - if (ftell(s2) != strlen(string)) { + if (ftell(s2) != strlen(string) + 1) { warnx("failed copy test (1)"); failures++; } @@ -59,10 +60,114 @@ main(void) } len = fread(buffer, 1, sizeof(buffer) - 1, s2); - if (len != strlen(string)) { - warnx("failed read test (3)"); + if (len != strlen(string) + 1) { + warnx("failed read test (3) %zu != %zu", + len, strlen(string) + 1); + failures++; + } + + return (failures); +} + +int +updatetest(void) +{ + FILE *s1; + char string[] = "hello\0 test number 2"; + char buffer[256]; + size_t len; + int failures = 0; + + s1 = fmemopen(string, 19, "a+"); + if (s1 == NULL) + return (1); + + len = fwrite(" world", 1, 6, s1); + if (len != 6) { + warnx("failed write test (4)"); + failures++; + } + + fseek(s1, 0, SEEK_SET); + if (ftell(s1) != 0) { + warnx("failed seek test (5)"); + failures++; + } + + len = fread(buffer, 1, sizeof(buffer) - 1, s1); + if (strncmp(string, buffer, len)) { + warnx("failed compare test (6)"); + failures++; + } + + if (strcmp(string, "hello world")) { + warnx("failed compare test (7)"); + failures++; + } + + if (strcmp(string + strlen(string) + 1, "number 2")) { + warnx("failed compare test (8)"); failures++; } return (failures); } + +int +writetest(void) +{ + FILE *s1; + char string[] = "super test number 3"; + char buffer[256]; + size_t len, slen; + int failures = 0; + + slen = strlen(string) + 1; + + s1 = fmemopen(string, slen, "w"); + if (s1 == NULL) + return (1); + + len = fwrite("short", 1, 5, s1); + if (len != strlen("short")) { + warnx("failed write test (9)"); + failures++; + } + fclose(s1); + + s1 = fmemopen(string, slen, "r"); + if (s1 == NULL) { + warnx("failed open test (10)"); + failures++; + } + + len = fread(buffer, 1, sizeof(buffer) - 1, s1); + if (strncmp(string, buffer, len)) { + warnx("failed compare test (11)"); + failures++; + } + + if (strcmp(string, "short")) { + warnx("failed compare test (12)"); + failures++; + } + + if (strcmp(string + strlen(string) + 1, "test number 3")) { + warnx("failed compare test (13)"); + failures++; + } + + return (failures); +} + +int +main(void) +{ + int failures = 0; + + failures += simpletest(); + failures += updatetest(); + failures += writetest(); + + return (failures); +} |