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>
39 #include <darwintest.h>
45 assert_stream(const wchar_t *contents
)
47 if (wcslen(contents
) != len
)
48 printf("bad length %zd for \"%ls\"\n", len
, contents
);
49 else if (wcsncmp(buf
, contents
, wcslen(contents
)) != 0)
50 printf("bad buffer \"%ls\" for \"%ls\"\n", buf
, contents
);
53 T_DECL(freebsd_open_wmemstream_open_group_test
, "")
58 fp
= open_wmemstream(&buf
, &len
);
59 T_ASSERT_NOTNULL(fp
, "open_wmemstream failed");
61 fwprintf(fp
, L
"hello my world");
63 assert_stream(L
"hello my world");
66 fwprintf(fp
, L
"good-bye");
67 fseeko(fp
, eob
, SEEK_SET
);
69 assert_stream(L
"good-bye world");
73 T_DECL(freebsd_open_wmemstream_simple_tests
, "")
75 static const wchar_t zerobuf
[] =
76 { L
'f', L
'o', L
'o', 0, 0, 0, 0, L
'b', L
'a', L
'r', 0 };
80 fp
= open_wmemstream(&buf
, NULL
);
81 T_ASSERT_NULL(fp
, "open_wmemstream did not fail");
82 T_ASSERT_EQ(errno
, EINVAL
, "open_wmemstream didn't fail with EINVAL");
83 fp
= open_wmemstream(NULL
, &len
);
84 T_ASSERT_NULL(fp
, "open_wmemstream did not fail");
85 T_ASSERT_EQ(errno
, EINVAL
, "open_wmemstream didn't fail with EINVAL");
86 fp
= open_wmemstream(&buf
, &len
);
87 T_ASSERT_NOTNULL(fp
, "open_memstream failed, errno=%d", errno
);
90 if (fwide(fp
, 0) <= 0)
91 printf("stream is not wide-oriented\n");
98 assert_stream(L
"foo");
102 fseek(fp
, 0, SEEK_END
);
104 assert_stream(L
"foo");
107 * Test seeking out past the current end. Should zero-fill the
110 fseek(fp
, 4, SEEK_END
);
111 fwprintf(fp
, L
"bar");
115 * Can't use assert_stream() here since this should contain
116 * embedded null characters.
119 printf("bad length %zd for zero-fill test\n", len
);
120 else if (memcmp(buf
, zerobuf
, sizeof(zerobuf
)) != 0)
121 printf("bad buffer for zero-fill test\n");
123 fseek(fp
, 3, SEEK_SET
);
124 fwprintf(fp
, L
" in ");
126 assert_stream(L
"foo in ");
127 fseek(fp
, 0, SEEK_END
);
129 assert_stream(L
"foo in bar");
132 if (fread(&c
, sizeof(c
), 1, fp
) != 0)
133 printf("fread did not fail\n");
134 else if (!ferror(fp
))
135 printf("error indicator not set after fread\n");
139 fseek(fp
, 4, SEEK_SET
);
140 fwprintf(fp
, L
"bar baz");
142 assert_stream(L
"foo bar baz");
146 T_DECL(freebsd_open_wmemstream_seek_tests
, "")
150 fp
= open_wmemstream(&buf
, &len
);
151 T_ASSERT_NOTNULL(fp
, "open_wmemstream failed, errno=%d", errno
);
153 #define SEEK_FAIL(offset, whence, error) do { \
155 T_ASSERT_NE(fseeko(fp, (offset), (whence)), 0, \
156 "fseeko(%s, %s) did not fail, set pos to %jd", \
157 __STRING(offset), __STRING(whence), \
158 (intmax_t)ftello(fp)); \
159 T_ASSERT_EQ(errno, (error), \
160 "fseeko(%s, %s) failed with %d rather than %s", \
161 __STRING(offset), __STRING(whence), errno, \
165 #define SEEK_OK(offset, whence, result) do { \
166 T_ASSERT_EQ(fseeko(fp, (offset), (whence)), 0, \
167 "fseeko(%s, %s) failed: %s", \
168 __STRING(offset), __STRING(whence), strerror(errno)); \
169 T_ASSERT_EQ(ftello(fp), (off_t)(result), \
170 "fseeko(%s, %s) seeked to %jd rather than %s", \
171 __STRING(offset), __STRING(whence), \
172 (intmax_t)ftello(fp), __STRING(result)); \
175 SEEK_FAIL(-1, SEEK_SET
, EINVAL
);
176 SEEK_FAIL(-1, SEEK_CUR
, EINVAL
);
177 SEEK_FAIL(-1, SEEK_END
, EINVAL
);
178 fwprintf(fp
, L
"foo");
179 SEEK_OK(-1, SEEK_CUR
, 2);
180 SEEK_OK(0, SEEK_SET
, 0);
181 SEEK_OK(-1, SEEK_END
, 2);
182 SEEK_OK(OFF_MAX
- 1, SEEK_SET
, OFF_MAX
- 1);
183 SEEK_FAIL(2, SEEK_CUR
, EOVERFLOW
);