diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/regress/lib/libc/fmemopen/fmemopentest.c | 121 |
1 files changed, 113 insertions, 8 deletions
diff --git a/src/regress/lib/libc/fmemopen/fmemopentest.c b/src/regress/lib/libc/fmemopen/fmemopentest.c index ef7e9ff64f..57063aeb0a 100644 --- a/src/regress/lib/libc/fmemopen/fmemopentest.c +++ b/src/regress/lib/libc/fmemopen/fmemopentest.c | |||
| @@ -1,4 +1,5 @@ | |||
| 1 | /* $OpenBSD: fmemopentest.c,v 1.1 2013/01/01 17:43:07 mpi Exp $ */ | 1 | /* $OpenBSD: fmemopentest.c,v 1.2 2013/03/27 15:08:13 mpi Exp $ */ |
| 2 | |||
| 2 | /* | 3 | /* |
| 3 | * Copyright (c) 2011 Martin Pieuchot <mpi@openbsd.org> | 4 | * Copyright (c) 2011 Martin Pieuchot <mpi@openbsd.org> |
| 4 | * | 5 | * |
| @@ -22,15 +23,15 @@ | |||
| 22 | #include <unistd.h> | 23 | #include <unistd.h> |
| 23 | 24 | ||
| 24 | int | 25 | int |
| 25 | main(void) | 26 | simpletest(void) |
| 26 | { | 27 | { |
| 27 | FILE *s1, *s2; | 28 | FILE *s1, *s2; |
| 28 | char c, string[] = "fmemopen test string!"; | 29 | char string[] = "fmemopen test string!"; |
| 29 | char buffer[1024], *buf = NULL; | 30 | char buffer[1024], *buf = NULL; |
| 30 | size_t len; | 31 | size_t len; |
| 31 | int failures = 0; | 32 | int c, failures = 0; |
| 32 | 33 | ||
| 33 | s1 = fmemopen(string, strlen(string), "r"); | 34 | s1 = fmemopen(string, strlen(string) + 1, "r"); |
| 34 | if (s1 == NULL) { | 35 | if (s1 == NULL) { |
| 35 | warn("unable to open a stream s1"); | 36 | warn("unable to open a stream s1"); |
| 36 | return (1); | 37 | return (1); |
| @@ -46,7 +47,7 @@ main(void) | |||
| 46 | while ((c = fgetc(s1)) != EOF) | 47 | while ((c = fgetc(s1)) != EOF) |
| 47 | fputc(c, s2); | 48 | fputc(c, s2); |
| 48 | 49 | ||
| 49 | if (ftell(s2) != strlen(string)) { | 50 | if (ftell(s2) != strlen(string) + 1) { |
| 50 | warnx("failed copy test (1)"); | 51 | warnx("failed copy test (1)"); |
| 51 | failures++; | 52 | failures++; |
| 52 | } | 53 | } |
| @@ -59,10 +60,114 @@ main(void) | |||
| 59 | } | 60 | } |
| 60 | 61 | ||
| 61 | len = fread(buffer, 1, sizeof(buffer) - 1, s2); | 62 | len = fread(buffer, 1, sizeof(buffer) - 1, s2); |
| 62 | if (len != strlen(string)) { | 63 | if (len != strlen(string) + 1) { |
| 63 | warnx("failed read test (3)"); | 64 | warnx("failed read test (3) %zu != %zu", |
| 65 | len, strlen(string) + 1); | ||
| 66 | failures++; | ||
| 67 | } | ||
| 68 | |||
| 69 | return (failures); | ||
| 70 | } | ||
| 71 | |||
| 72 | int | ||
| 73 | updatetest(void) | ||
| 74 | { | ||
| 75 | FILE *s1; | ||
| 76 | char string[] = "hello\0 test number 2"; | ||
| 77 | char buffer[256]; | ||
| 78 | size_t len; | ||
| 79 | int failures = 0; | ||
| 80 | |||
| 81 | s1 = fmemopen(string, 19, "a+"); | ||
| 82 | if (s1 == NULL) | ||
| 83 | return (1); | ||
| 84 | |||
| 85 | len = fwrite(" world", 1, 6, s1); | ||
| 86 | if (len != 6) { | ||
| 87 | warnx("failed write test (4)"); | ||
| 88 | failures++; | ||
| 89 | } | ||
| 90 | |||
| 91 | fseek(s1, 0, SEEK_SET); | ||
| 92 | if (ftell(s1) != 0) { | ||
| 93 | warnx("failed seek test (5)"); | ||
| 94 | failures++; | ||
| 95 | } | ||
| 96 | |||
| 97 | len = fread(buffer, 1, sizeof(buffer) - 1, s1); | ||
| 98 | if (strncmp(string, buffer, len)) { | ||
| 99 | warnx("failed compare test (6)"); | ||
| 100 | failures++; | ||
| 101 | } | ||
| 102 | |||
| 103 | if (strcmp(string, "hello world")) { | ||
| 104 | warnx("failed compare test (7)"); | ||
| 105 | failures++; | ||
| 106 | } | ||
| 107 | |||
| 108 | if (strcmp(string + strlen(string) + 1, "number 2")) { | ||
| 109 | warnx("failed compare test (8)"); | ||
| 64 | failures++; | 110 | failures++; |
| 65 | } | 111 | } |
| 66 | 112 | ||
| 67 | return (failures); | 113 | return (failures); |
| 68 | } | 114 | } |
| 115 | |||
| 116 | int | ||
| 117 | writetest(void) | ||
| 118 | { | ||
| 119 | FILE *s1; | ||
| 120 | char string[] = "super test number 3"; | ||
| 121 | char buffer[256]; | ||
| 122 | size_t len, slen; | ||
| 123 | int failures = 0; | ||
| 124 | |||
| 125 | slen = strlen(string) + 1; | ||
| 126 | |||
| 127 | s1 = fmemopen(string, slen, "w"); | ||
| 128 | if (s1 == NULL) | ||
| 129 | return (1); | ||
| 130 | |||
| 131 | len = fwrite("short", 1, 5, s1); | ||
| 132 | if (len != strlen("short")) { | ||
| 133 | warnx("failed write test (9)"); | ||
| 134 | failures++; | ||
| 135 | } | ||
| 136 | fclose(s1); | ||
| 137 | |||
| 138 | s1 = fmemopen(string, slen, "r"); | ||
| 139 | if (s1 == NULL) { | ||
| 140 | warnx("failed open test (10)"); | ||
| 141 | failures++; | ||
| 142 | } | ||
| 143 | |||
| 144 | len = fread(buffer, 1, sizeof(buffer) - 1, s1); | ||
| 145 | if (strncmp(string, buffer, len)) { | ||
| 146 | warnx("failed compare test (11)"); | ||
| 147 | failures++; | ||
| 148 | } | ||
| 149 | |||
| 150 | if (strcmp(string, "short")) { | ||
| 151 | warnx("failed compare test (12)"); | ||
| 152 | failures++; | ||
| 153 | } | ||
| 154 | |||
| 155 | if (strcmp(string + strlen(string) + 1, "test number 3")) { | ||
| 156 | warnx("failed compare test (13)"); | ||
| 157 | failures++; | ||
| 158 | } | ||
| 159 | |||
| 160 | return (failures); | ||
| 161 | } | ||
| 162 | |||
| 163 | int | ||
| 164 | main(void) | ||
| 165 | { | ||
| 166 | int failures = 0; | ||
| 167 | |||
| 168 | failures += simpletest(); | ||
| 169 | failures += updatetest(); | ||
| 170 | failures += writetest(); | ||
| 171 | |||
| 172 | return (failures); | ||
| 173 | } | ||
