summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/fmemopen/fmemopentest.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regress/lib/libc/fmemopen/fmemopentest.c')
-rw-r--r--src/regress/lib/libc/fmemopen/fmemopentest.c212
1 files changed, 212 insertions, 0 deletions
diff --git a/src/regress/lib/libc/fmemopen/fmemopentest.c b/src/regress/lib/libc/fmemopen/fmemopentest.c
new file mode 100644
index 0000000000..203c93be65
--- /dev/null
+++ b/src/regress/lib/libc/fmemopen/fmemopentest.c
@@ -0,0 +1,212 @@
1/* $OpenBSD: fmemopentest.c,v 1.3 2013/03/28 09:35:58 mpi Exp $ */
2
3/*
4 * Copyright (c) 2011 Martin Pieuchot <mpi@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <err.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
24
25int
26simpletest(void)
27{
28 FILE *s1, *s2;
29 char string[] = "fmemopen test string!";
30 char buffer[1024], *buf = NULL;
31 size_t len;
32 int c, failures = 0;
33
34 s1 = fmemopen(string, strlen(string) + 1, "r");
35 if (s1 == NULL) {
36 warn("unable to open a stream s1");
37 return (1);
38 }
39
40 s2 = fmemopen(buf, 22, "w+");
41 if (s2 == NULL) {
42 warn("unable to create a stream s2");
43 fclose(s1);
44 return (1);
45 }
46
47 while ((c = fgetc(s1)) != EOF)
48 fputc(c, s2);
49
50 if (ftell(s2) != strlen(string) + 1) {
51 warnx("failed copy test (1)");
52 failures++;
53 }
54 fclose(s1);
55
56 fseek(s2, 0, SEEK_SET);
57 if (ftell(s2) != 0) {
58 warnx("failed seek test (2)");
59 failures++;
60 }
61
62 len = fread(buffer, 1, sizeof(buffer) - 1, s2);
63 if (len != strlen(string) + 1) {
64 warnx("failed read test (3) %zu != %zu",
65 len, strlen(string) + 1);
66 failures++;
67 }
68
69 return (failures);
70}
71
72int
73updatetest(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)");
110 failures++;
111 }
112
113 return (failures);
114}
115
116int
117writetest(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
163int
164seektest(void)
165{
166 FILE *s1;
167 char string[] = "long string for testing seek";
168 size_t len, slen;
169 int failures = 0;
170
171 slen = strlen(string) + 1;
172
173 s1 = fmemopen(string, slen, "r");
174 if (s1 == NULL)
175 return (1);
176
177 if (fseek(s1, 8, SEEK_SET) != 0) {
178 warnx("failed to fseek. (14)");
179 failures++;
180 }
181
182 if (ftell(s1) != 8) {
183 warnx("failed seek test. (15)");
184 failures++;
185 }
186
187 /* Try to seek backward */
188 if (fseek(s1, -1, SEEK_CUR) != 0) {
189 warnx("failed to fseek. (16)");
190 failures++;
191 }
192
193 if (ftell(s1) != 7) {
194 warnx("failed seeking backward. (17)");
195 failures++;
196 }
197
198 return (failures);
199}
200
201int
202main(void)
203{
204 int failures = 0;
205
206 failures += simpletest();
207 failures += updatetest();
208 failures += writetest();
209 failures += seektest();
210
211 return (failures);
212}