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 char *contents
)
47 T_EXPECT_EQ(strlen(contents
), len
,
48 "bad length %zd for \"%s\"\n", len
, contents
);
49 T_EXPECT_EQ(strncmp(buf
, contents
, strlen(contents
)), 0,
50 "bad buffer \"%s\" for \"%s\"\n", buf
, contents
);
53 T_DECL(freebsd_open_memstream_open_group_test
, "")
58 fp
= open_memstream(&buf
, &len
);
59 T_ASSERT_NOTNULL(fp
, "open_memstream failed");
61 fprintf(fp
, "hello my world");
63 assert_stream("hello my world");
66 fprintf(fp
, "good-bye");
67 fseeko(fp
, eob
, SEEK_SET
);
69 assert_stream("good-bye world");
73 T_DECL(freebsd_open_memstream_simple_tests
, "")
75 static const char zerobuf
[] =
76 { 'f', 'o', 'o', 0, 0, 0, 0, 'b', 'a', 'r', 0 };
80 fp
= open_memstream(&buf
, NULL
);
81 T_ASSERT_NULL(fp
, "open_memstream did not fail");
82 T_ASSERT_EQ(errno
, EINVAL
, "open_memstream didn't fail with EINVAL");
83 fp
= open_memstream(NULL
, &len
);
84 T_ASSERT_NULL(fp
, "open_memstream did not fail");
85 T_ASSERT_EQ(errno
, EINVAL
, "open_memstream didn't fail with EINVAL");
86 fp
= open_memstream(&buf
, &len
);
87 T_ASSERT_NOTNULL(fp
, "open_memstream failed, errno=%d", errno
);
90 if (fwide(fp
, 0) >= 0)
91 printf("stream is not byte-oriented\n");
102 fseek(fp
, 0, SEEK_END
);
104 assert_stream("foo");
107 * Test seeking out past the current end. Should zero-fill the
110 fseek(fp
, 4, SEEK_END
);
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
);
126 assert_stream("foo in ");
127 fseek(fp
, 0, SEEK_END
);
129 assert_stream("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 fprintf(fp
, "bar baz");
142 assert_stream("foo bar baz");
146 T_DECL(freebsd_open_memstream_seek_tests
, "")
150 fp
= open_memstream(&buf
, &len
);
151 T_ASSERT_NOTNULL(fp
, "open_memstream failed: %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
);
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
);