2 * Copyright (c) 2013 Hudson River Trading LLC
3 * Written by: John H. Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
46 assert_stream(const wchar_t *contents)
48 if (wcslen(contents) != len)
49 printf("bad length %zd for \"%ls\"\n", len, contents);
50 else if (wcsncmp(buf, contents, wcslen(contents)) != 0)
51 printf("bad buffer \"%ls\" for \"%ls\"\n", buf, contents);
54 ATF_TC_WITHOUT_HEAD(open_group_test);
55 ATF_TC_BODY(open_group_test, tc)
60 fp = open_wmemstream(&buf, &len);
61 ATF_REQUIRE_MSG(fp != NULL, "open_wmemstream failed");
63 fwprintf(fp, L"hello my world");
65 assert_stream(L"hello my world");
68 fwprintf(fp, L"good-bye");
69 fseeko(fp, eob, SEEK_SET);
71 assert_stream(L"good-bye world");
75 ATF_TC_WITHOUT_HEAD(simple_tests);
76 ATF_TC_BODY(simple_tests, tc)
78 static const wchar_t zerobuf[] =
79 { L'f', L'o', L'o', 0, 0, 0, 0, L'b', L'a', L'r', 0 };
83 fp = open_wmemstream(&buf, NULL);
84 ATF_REQUIRE_MSG(fp == NULL, "open_wmemstream did not fail");
85 ATF_REQUIRE_MSG(errno == EINVAL,
86 "open_wmemstream didn't fail with EINVAL");
87 fp = open_wmemstream(NULL, &len);
88 ATF_REQUIRE_MSG(fp == NULL, "open_wmemstream did not fail");
89 ATF_REQUIRE_MSG(errno == EINVAL,
90 "open_wmemstream didn't fail with EINVAL");
91 fp = open_wmemstream(&buf, &len);
92 ATF_REQUIRE_MSG(fp != NULL, "open_memstream failed; errno=%d", errno);
95 if (fwide(fp, 0) <= 0)
96 printf("stream is not wide-oriented\n");
100 assert_stream(L"fo");
103 assert_stream(L"foo");
107 fseek(fp, 0, SEEK_END);
109 assert_stream(L"foo");
112 * Test seeking out past the current end. Should zero-fill the
115 fseek(fp, 4, SEEK_END);
116 fwprintf(fp, L"bar");
120 * Can't use assert_stream() here since this should contain
121 * embedded null characters.
124 printf("bad length %zd for zero-fill test\n", len);
125 else if (memcmp(buf, zerobuf, sizeof(zerobuf)) != 0)
126 printf("bad buffer for zero-fill test\n");
128 fseek(fp, 3, SEEK_SET);
129 fwprintf(fp, L" in ");
131 assert_stream(L"foo in ");
132 fseek(fp, 0, SEEK_END);
134 assert_stream(L"foo in bar");
137 if (fread(&c, sizeof(c), 1, fp) != 0)
138 printf("fread did not fail\n");
139 else if (!ferror(fp))
140 printf("error indicator not set after fread\n");
144 fseek(fp, 4, SEEK_SET);
145 fwprintf(fp, L"bar baz");
147 assert_stream(L"foo bar baz");
151 ATF_TC_WITHOUT_HEAD(seek_tests);
152 ATF_TC_BODY(seek_tests, tc)
156 fp = open_wmemstream(&buf, &len);
157 ATF_REQUIRE_MSG(fp != NULL, "open_wmemstream failed; errno=%d", errno);
159 #define SEEK_FAIL(offset, whence, error) do { \
161 ATF_REQUIRE_MSG(fseeko(fp, (offset), (whence)) != 0, \
162 "fseeko(%s, %s) did not fail, set pos to %jd\n", \
163 __STRING(offset), __STRING(whence), \
164 (intmax_t)ftello(fp)); \
165 ATF_REQUIRE_MSG(errno == (error), \
166 "fseeko(%s, %s) failed with %d rather than %s\n", \
167 __STRING(offset), __STRING(whence), errno, \
171 #define SEEK_OK(offset, whence, result) do { \
172 ATF_REQUIRE_MSG(fseeko(fp, (offset), (whence)) == 0, \
173 "fseeko(%s, %s) failed: %s", \
174 __STRING(offset), __STRING(whence), strerror(errno)); \
175 ATF_REQUIRE_MSG(ftello(fp) == (result), \
176 "fseeko(%s, %s) seeked to %jd rather than %s\n", \
177 __STRING(offset), __STRING(whence), \
178 (intmax_t)ftello(fp), __STRING(result)); \
181 SEEK_FAIL(-1, SEEK_SET, EINVAL);
182 SEEK_FAIL(-1, SEEK_CUR, EINVAL);
183 SEEK_FAIL(-1, SEEK_END, EINVAL);
184 fwprintf(fp, L"foo");
185 SEEK_OK(-1, SEEK_CUR, 2);
186 SEEK_OK(0, SEEK_SET, 0);
187 SEEK_OK(-1, SEEK_END, 2);
188 SEEK_OK(OFF_MAX - 1, SEEK_SET, OFF_MAX - 1);
189 SEEK_FAIL(2, SEEK_CUR, EOVERFLOW);
196 ATF_TP_ADD_TC(tp, open_group_test);
197 ATF_TP_ADD_TC(tp, simple_tests);
198 ATF_TP_ADD_TC(tp, seek_tests);
200 return (atf_no_error());