1 /* $NetBSD: t_fmemopen.c,v 1.4 2013/10/19 17:45:00 christos Exp $ */
4 * Copyright (c)2010 Takehiko NOZAKI,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #if defined(__FreeBSD__) || defined(__NetBSD__)
33 #if defined(__linux__)
39 #define ATF_TC(arg0) static void arg0##_head(void)
40 #define ATF_TC_HEAD(arg0, arg1) static void arg0##_head()
41 #define atf_tc_set_md_var(arg0, arg1, ...) do { \
42 printf(__VA_ARGS__); \
44 } while (/*CONSTCOND*/0)
45 #define ATF_TC_BODY(arg0, arg1) static void arg0##_body()
46 #define ATF_CHECK(arg0) assert(arg0)
47 #define ATF_TP_ADD_TCS(arg0) int main(void)
48 #define ATF_TP_ADD_TC(arg0, arg1) arg1##_head(); arg1##_body()
49 #define atf_no_error() 0
59 const char *mode_rwa[] = {
60 "r", "rb", "r+", "rb+", "r+b",
61 "w", "wb", "w+", "wb+", "w+b",
62 "a", "ab", "a+", "ab+", "a+b",
66 const char *mode_r[] = { "r", "rb", "r+", "rb+", "r+b", NULL };
67 const char *mode_w[] = { "w", "wb", "w+", "wb+", "w+b", NULL };
68 const char *mode_a[] = { "a", "ab", "a+", "ab+", "a+b", NULL };
74 #define TESTSTR(s) { s, sizeof(s)-1 }
75 TESTSTR("\0he quick brown fox jumps over the lazy dog"),
76 TESTSTR("T\0e quick brown fox jumps over the lazy dog"),
77 TESTSTR("Th\0 quick brown fox jumps over the lazy dog"),
78 TESTSTR("The\0quick brown fox jumps over the lazy dog"),
79 TESTSTR("The \0uick brown fox jumps over the lazy dog"),
80 TESTSTR("The q\0ick brown fox jumps over the lazy dog"),
81 TESTSTR("The qu\0ck brown fox jumps over the lazy dog"),
82 TESTSTR("The qui\0k brown fox jumps over the lazy dog"),
83 TESTSTR("The quic\0 brown fox jumps over the lazy dog"),
84 TESTSTR("The quick\0brown fox jumps over the lazy dog"),
85 TESTSTR("The quick \0rown fox jumps over the lazy dog"),
86 TESTSTR("The quick b\0own fox jumps over the lazy dog"),
87 TESTSTR("The quick br\0wn fox jumps over the lazy dog"),
88 TESTSTR("The quick bro\0n fox jumps over the lazy dog"),
89 TESTSTR("The quick brow\0 fox jumps over the lazy dog"),
90 TESTSTR("The quick brown\0fox jumps over the lazy dog"),
91 TESTSTR("The quick brown \0ox jumps over the lazy dog"),
92 TESTSTR("The quick brown f\0x jumps over the lazy dog"),
93 TESTSTR("The quick brown fo\0 jumps over the lazy dog"),
94 TESTSTR("The quick brown fox\0jumps over the lazy dog"),
95 TESTSTR("The quick brown fox \0umps over the lazy dog"),
96 TESTSTR("The quick brown fox j\0mps over the lazy dog"),
97 TESTSTR("The quick brown fox ju\0ps over the lazy dog"),
98 TESTSTR("The quick brown fox jum\0s over the lazy dog"),
99 TESTSTR("The quick brown fox jump\0 over the lazy dog"),
100 TESTSTR("The quick brown fox jumps\0over the lazy dog"),
101 TESTSTR("The quick brown fox jumps \0ver the lazy dog"),
102 TESTSTR("The quick brown fox jumps o\0er the lazy dog"),
103 TESTSTR("The quick brown fox jumps ov\0r the lazy dog"),
104 TESTSTR("The quick brown fox jumps ove\0 the lazy dog"),
105 TESTSTR("The quick brown fox jumps over\0the lazy dog"),
106 TESTSTR("The quick brown fox jumps over \0he lazy dog"),
107 TESTSTR("The quick brown fox jumps over t\0e lazy dog"),
108 TESTSTR("The quick brown fox jumps over th\0 lazy dog"),
109 TESTSTR("The quick brown fox jumps over the\0lazy dog"),
110 TESTSTR("The quick brown fox jumps over the \0azy dog"),
111 TESTSTR("The quick brown fox jumps over the l\0zy dog"),
112 TESTSTR("The quick brown fox jumps over the la\0y dog"),
113 TESTSTR("The quick brown fox jumps over the laz\0 dog"),
114 TESTSTR("The quick brown fox jumps over the lazy\0dog"),
115 TESTSTR("The quick brown fox jumps over the lazy \0og"),
116 TESTSTR("The quick brown fox jumps over the lazy d\0g"),
117 TESTSTR("The quick brown fox jumps over the lazy do\0"),
118 TESTSTR("The quick brown fox jumps over the lazy dog"),
123 ATF_TC_HEAD(test00, tc)
125 atf_tc_set_md_var(tc, "descr", "test00");
127 ATF_TC_BODY(test00, tc)
133 for (p = &mode_rwa[0]; *p != NULL; ++p) {
134 fp = fmemopen(&buf[0], sizeof(buf), *p);
136 * Upon successful completion, fmemopen() shall return a pointer to the
137 * object controlling the stream.
139 ATF_CHECK(fp != NULL);
141 ATF_CHECK(fclose(fp) == 0);
146 ATF_TC_HEAD(test01, tc)
148 atf_tc_set_md_var(tc, "descr", "test01");
150 ATF_TC_BODY(test01, tc)
153 const char *mode[] = {
161 for (p = &mode[0]; *p != NULL; ++p) {
163 * If a null pointer is specified as the buf argument, fmemopen() shall
164 * allocate size bytes of memory as if by a call to malloc().
166 fp = fmemopen(NULL, BUFSIZ, *p);
167 ATF_CHECK(fp != NULL);
170 * If buf is a null pointer, the initial position shall always be set
171 * to the beginning of the buffer.
173 ATF_CHECK(ftello(fp) == (off_t)0);
175 ATF_CHECK(fclose(fp) == 0);
180 ATF_TC_HEAD(test02, tc)
182 atf_tc_set_md_var(tc, "descr", "test02");
184 ATF_TC_BODY(test02, tc)
190 for (p = &mode_r[0]; *p != NULL; ++p) {
192 memset(&buf[0], 0x1, sizeof(buf));
193 fp = fmemopen(&buf[0], sizeof(buf), *p);
194 ATF_CHECK(fp != NULL);
197 * This position is initially set to either the beginning of the buffer
198 * (for r and w modes)
200 ATF_CHECK((unsigned char)buf[0] == 0x1);
201 ATF_CHECK(ftello(fp) == (off_t)0);
204 * The stream also maintains the size of the current buffer contents.
205 * For modes r and r+ the size is set to the value given by the size argument.
207 #if !defined(__GLIBC__)
208 ATF_CHECK(fseeko(fp, (off_t)0, SEEK_END) == 0);
209 ATF_CHECK(ftello(fp) == (off_t)sizeof(buf));
211 ATF_CHECK(fclose(fp) == 0);
216 ATF_TC_HEAD(test03, tc)
218 atf_tc_set_md_var(tc, "descr", "test03");
220 ATF_TC_BODY(test03, tc)
226 for (p = &mode_w[0]; *p != NULL; ++p) {
228 memset(&buf[0], 0x1, sizeof(buf));
229 fp = fmemopen(&buf[0], sizeof(buf), *p);
230 ATF_CHECK(fp != NULL);
233 * This position is initially set to either the beginning of the buffer
234 * (for r and w modes)
236 ATF_CHECK(buf[0] == '\0');
237 ATF_CHECK(ftello(fp) == (off_t)0);
240 * For modes w and w+ the initial size is zero
242 ATF_CHECK(fseeko(fp, (off_t)0, SEEK_END) == 0);
243 ATF_CHECK(ftello(fp) == (off_t)0);
245 ATF_CHECK(fclose(fp) == 0);
250 ATF_TC_HEAD(test04, tc)
252 atf_tc_set_md_var(tc, "descr", "test04");
254 ATF_TC_BODY(test04, tc)
261 * or to the first null byte in the buffer (for a modes)
263 for (p = &mode_a[0]; *p != NULL; ++p) {
265 memset(&buf[0], 0x1, sizeof(buf));
266 fp = fmemopen(&buf[0], sizeof(buf), *p);
267 ATF_CHECK(fp != NULL);
269 ATF_CHECK((unsigned char)buf[0] == 0x1);
271 /* If no null byte is found in append mode,
272 * the initial position is set to one byte after the end of the buffer.
274 #if !defined(__GLIBC__)
275 ATF_CHECK(ftello(fp) == (off_t)sizeof(buf));
279 * and for modes a and a+ the initial size is either the position of the
280 * first null byte in the buffer or the value of the size argument
281 * if no null byte is found.
283 #if !defined(__GLIBC__)
284 ATF_CHECK(fseeko(fp, (off_t)0, SEEK_END) == 0);
285 ATF_CHECK(ftello(fp) == (off_t)sizeof(buf));
288 ATF_CHECK(fclose(fp) == 0);
293 ATF_TC_HEAD(test05, tc)
295 atf_tc_set_md_var(tc, "descr", "test05");
297 ATF_TC_BODY(test05, tc)
303 for (p = &mode_rwa[0]; *p != NULL; ++p) {
305 * Otherwise, a null pointer shall be returned, and errno shall be set
306 * to indicate the error.
309 fp = fmemopen(NULL, (size_t)0, *p);
310 ATF_CHECK(fp == NULL);
311 ATF_CHECK(errno == EINVAL);
314 fp = fmemopen((void *)&buf[0], 0, *p);
315 ATF_CHECK(fp == NULL);
316 ATF_CHECK(errno == EINVAL);
321 ATF_TC_HEAD(test06, tc)
323 atf_tc_set_md_var(tc, "descr", "test06");
325 ATF_TC_BODY(test06, tc)
328 const char *mode[] = { "", " ", "???", NULL };
331 for (p = &mode[0]; *p != NULL; ++p) {
333 * The value of the mode argument is not valid.
335 fp = fmemopen(NULL, 1, *p);
336 ATF_CHECK(fp == NULL);
337 ATF_CHECK(errno == EINVAL);
342 ATF_TC_HEAD(test07, tc)
344 atf_tc_set_md_var(tc, "descr", "test07");
346 ATF_TC_BODY(test07, tc)
348 #if !defined(__GLIBC__)
350 const char *mode[] = {
358 for (p = &mode[0]; *p != NULL; ++p) {
360 * Because this feature is only useful when the stream is opened for updating
361 * (because there is no way to get a pointer to the buffer) the fmemopen()
362 * call may fail if the mode argument does not include a '+' .
365 fp = fmemopen(NULL, 1, *p);
366 ATF_CHECK(fp == NULL);
367 ATF_CHECK(errno == EINVAL);
373 ATF_TC_HEAD(test08, tc)
375 atf_tc_set_md_var(tc, "descr", "test08");
377 ATF_TC_BODY(test08, tc)
379 #if !defined(__GLIBC__)
381 const char *mode[] = {
389 for (p = &mode[0]; *p != NULL; ++p) {
391 * The buf argument is a null pointer and the allocation of a buffer of
392 * length size has failed.
394 fp = fmemopen(NULL, SIZE_MAX, *p);
395 ATF_CHECK(fp == NULL);
396 ATF_CHECK(errno == ENOMEM);
403 * An attempt to seek a memory buffer stream to a negative position or to a
404 * position larger than the buffer size given in the size argument shall fail.
408 ATF_TC_HEAD(test09, tc)
410 atf_tc_set_md_var(tc, "descr", "test09");
412 ATF_TC_BODY(test09, tc)
420 for (t = &testcases[0]; t->s != NULL; ++t) {
421 for (p = &mode_rwa[0]; *p != NULL; ++p) {
423 memcpy(&buf[0], t->s, t->n);
424 fp = fmemopen(&buf[0], t->n, *p);
425 ATF_CHECK(fp != NULL);
428 * test fmemopen_seek(SEEK_SET)
431 ATF_CHECK(fseeko(fp, (off_t)0, SEEK_SET) == 0);
432 ATF_CHECK(ftello(fp) == (off_t)0);
435 for (i = (off_t)1; i <= (off_t)t->n; ++i) {
436 ATF_CHECK(fseeko(fp, i, SEEK_SET) == 0);
437 ATF_CHECK(ftello(fp) == i);
440 ATF_CHECK(fseeko(fp, t->n + 1, SEEK_SET) == -1);
441 ATF_CHECK(ftello(fp) == t->n);
444 ATF_CHECK(fseeko(fp, (off_t)-1, SEEK_SET) == -1);
445 ATF_CHECK(ftello(fp) == t->n);
447 ATF_CHECK(fclose(fp) == 0);
452 const char *mode_rw[] = {
453 "r", "rb", "r+", "rb+", "r+b",
454 "w", "wb", "w+", "wb+", "w+b",
459 ATF_TC_HEAD(test10, tc)
461 atf_tc_set_md_var(tc, "descr", "test10");
463 ATF_TC_BODY(test10, tc)
471 for (t = &testcases[0]; t->s != NULL; ++t) {
472 for (p = &mode_rw[0]; *p != NULL; ++p) {
474 memcpy(&buf[0], t->s, t->n);
475 fp = fmemopen(&buf[0], t->n, *p);
476 ATF_CHECK(fp != NULL);
479 * test fmemopen_seek(SEEK_CUR)
481 ATF_CHECK(ftello(fp) == (off_t)0);
484 ATF_CHECK(fseeko(fp, (off_t)0, SEEK_CUR) == 0);
485 ATF_CHECK(ftello(fp) == (off_t)0);
488 ATF_CHECK(fseeko(fp, (off_t)-1, SEEK_CUR) == -1);
489 ATF_CHECK(ftello(fp) == (off_t)0);
492 for (i = 0; i < (off_t)t->n; ++i) {
493 ATF_CHECK(fseeko(fp, (off_t)1, SEEK_CUR) == 0);
494 ATF_CHECK(ftello(fp) == i + 1);
498 ATF_CHECK(fseeko(fp, (off_t)1, SEEK_CUR) == -1);
499 ATF_CHECK(ftello(fp) == (off_t)t->n);
501 ATF_CHECK(fclose(fp) == 0);
507 ATF_TC_HEAD(test11, tc)
509 atf_tc_set_md_var(tc, "descr", "test11");
511 ATF_TC_BODY(test11, tc)
519 /* test fmemopen_seek(SEEK_CUR) */
520 for (t = &testcases[0]; t->s != NULL; ++t) {
521 len = (off_t)strnlen(t->s, t->n);
522 rest = (off_t)t->n - len;
523 for (p = &mode_a[0]; *p != NULL; ++p) {
525 memcpy(&buf[0], t->s, t->n);
526 fp = fmemopen(&buf[0], t->n, *p);
527 ATF_CHECK(fp != NULL);
529 * test fmemopen_seek(SEEK_CUR)
531 #if defined(__GLIBC__)
532 if (i < (off_t)t->n) {
535 ATF_CHECK(fseeko(fp, (off_t)0, SEEK_CUR) == 0);
536 ATF_CHECK(ftello(fp) == len);
539 for (i = (off_t)1; i <= rest; ++i) {
540 ATF_CHECK(fseeko(fp, (off_t)1, SEEK_CUR) == 0);
541 ATF_CHECK(ftello(fp) == len + i);
545 ATF_CHECK(fseeko(fp, (off_t)1, SEEK_CUR) == -1);
546 ATF_CHECK(ftello(fp) == (off_t)t->n);
549 for (i = (off_t)1; i <= (off_t)t->n; ++i) {
550 ATF_CHECK(fseeko(fp, (off_t)-1, SEEK_CUR) == 0);
551 ATF_CHECK(ftello(fp) == (off_t)t->n - i);
555 ATF_CHECK(fseeko(fp, (off_t)-1, SEEK_CUR) == -1);
556 ATF_CHECK(ftello(fp) == (off_t)0);
558 #if defined(__GLIBC__)
561 ATF_CHECK(fclose(fp) == 0);
568 ATF_TC_HEAD(test12, tc)
570 atf_tc_set_md_var(tc, "descr", "test12");
572 ATF_TC_BODY(test12, tc)
580 /* test fmemopen_seek(SEEK_END) */
581 for (t = &testcases[0]; t->s != NULL; ++t) {
582 len = (off_t)strnlen(t->s, t->n);
584 for (p = &mode_r[0]; *p != NULL; ++p) {
586 memcpy(buf, t->s, t->n);
587 fp = fmemopen(&buf[0], t->n, *p);
588 ATF_CHECK(fp != NULL);
591 * test fmemopen_seek(SEEK_END)
593 #if !defined(__GLIBC__)
594 ATF_CHECK(ftello(fp) == (off_t)0);
597 ATF_CHECK(fseeko(fp, (off_t)0, SEEK_END) == 0);
598 ATF_CHECK(ftello(fp) == len);
601 ATF_CHECK(fseeko(fp, rest + 1, SEEK_END) == -1);
602 ATF_CHECK(ftello(fp) == len);
605 ATF_CHECK(fseeko(fp, -(len + 1), SEEK_END) == -1);
606 ATF_CHECK(ftello(fp) == len);
609 for (i = 1; i <= rest; ++i) {
610 ATF_CHECK(fseeko(fp, i, SEEK_END) == 0);
611 ATF_CHECK(ftello(fp) == len + i);
615 for (i = 1; i < len; ++i) {
616 ATF_CHECK(fseeko(fp, -i, SEEK_END) == 0);
617 ATF_CHECK(ftello(fp) == len - i);
620 ATF_CHECK(fclose(fp) == 0);
627 ATF_TC_HEAD(test13, tc)
629 atf_tc_set_md_var(tc, "descr", "test13");
631 ATF_TC_BODY(test13, tc)
641 /* test fmemopen_seek(SEEK_END) */
642 for (t = &testcases[0]; t->s != NULL; ++t) {
643 for (p = &mode_w[0]; *p != NULL; ++p) {
645 memcpy(buf, t->s, t->n);
646 fp = fmemopen(&buf[0], t->n, *p);
647 ATF_CHECK(fp != NULL);
649 * test fmemopen_seek(SEEK_END)
651 #if !defined(__GLIBC__)
652 ATF_CHECK(ftello(fp) == (off_t)0);
653 ATF_CHECK(buf[0] == '\0');
656 ATF_CHECK(fseeko(fp, (off_t)0, SEEK_END) == 0);
657 ATF_CHECK(ftello(fp) == (off_t)0);
660 ATF_CHECK(fseeko(fp, (off_t)t->n + 1, SEEK_END) == -1);
661 ATF_CHECK(ftello(fp) == (off_t)0);
664 ATF_CHECK(fseeko(fp, -1, SEEK_END) == -1);
665 ATF_CHECK(ftello(fp) == (off_t)0);
670 for (i = 1; i <= t->n; ++i) {
671 ATF_CHECK(fseeko(fp, i, SEEK_END) == 0);
672 ATF_CHECK(ftello(fp) == i);
675 ATF_CHECK(fclose(fp) == 0);
681 ATF_TC_HEAD(test14, tc)
683 atf_tc_set_md_var(tc, "descr", "test14");
685 ATF_TC_BODY(test14, tc)
693 /* test fmemopen_seek(SEEK_END) */
694 for (t = &testcases[0]; t->s != NULL; ++t) {
695 len = (off_t)strnlen(t->s, t->n);
696 rest = (off_t)t->n - len;
697 for (p = &mode_a[0]; *p != NULL; ++p) {
699 memcpy(buf, t->s, t->n);
700 fp = fmemopen(&buf[0], t->n, *p);
701 ATF_CHECK(fp != NULL);
703 * test fmemopen_seek(SEEK_END)
705 #if !defined(__GLIBC__)
706 ATF_CHECK(ftello(fp) == len);
709 ATF_CHECK(fseeko(fp, 0, SEEK_END) == 0);
710 ATF_CHECK(ftello(fp) == len);
713 ATF_CHECK(fseeko(fp, rest + 1, SEEK_END) == -1);
714 ATF_CHECK(ftello(fp) == len);
717 ATF_CHECK(fseeko(fp, -(len + 1), SEEK_END) == -1);
718 ATF_CHECK(ftello(fp) == len);
722 for (i = 1; i <= rest; ++i) {
723 ATF_CHECK(fseeko(fp, i, SEEK_END) == 0);
724 ATF_CHECK(ftello(fp) == len + i);
729 for (i = 1; i < len; ++i) {
730 ATF_CHECK(fseeko(fp, -i, SEEK_END) == 0);
731 ATF_CHECK(ftello(fp) == len - i);
734 ATF_CHECK(fclose(fp) == 0);
739 const char *mode_rw1[] = {
740 "r", "rb", "r+", "rb+", "r+b",
748 * When a stream open for writing is flushed or closed, a null byte is written
749 * at the current position or at the end of the buffer, depending on the size
754 ATF_TC_HEAD(test15, tc)
756 atf_tc_set_md_var(tc, "descr", "test15");
758 ATF_TC_BODY(test15, tc)
766 for (t = &testcases[0]; t->s != NULL; ++t) {
767 for (p = &mode_rw1[0]; *p != NULL; ++p) {
769 memcpy(&buf0[0], t->s, t->n);
770 fp = fmemopen(&buf0[0], t->n, *p);
771 ATF_CHECK(fp != NULL);
773 * test fmemopen_read + fgetc(3)
775 for (i = 0; i < t->n; ++i) {
776 ATF_CHECK(ftello(fp) == (off_t)i);
777 ATF_CHECK(fgetc(fp) == buf0[i]);
778 ATF_CHECK(feof(fp) == 0);
779 ATF_CHECK(ftello(fp) == (off_t)i + 1);
781 ATF_CHECK(fgetc(fp) == EOF);
782 ATF_CHECK(feof(fp) != 0);
783 ATF_CHECK(ftello(fp) == (off_t)t->n);
784 ATF_CHECK(fclose(fp) == 0);
790 ATF_TC_HEAD(test16, tc)
792 atf_tc_set_md_var(tc, "descr", "test16");
794 ATF_TC_BODY(test16, tc)
798 char buf0[BUFSIZ], buf1[BUFSIZ];
801 for (t = &testcases[0]; t->s != NULL; ++t) {
802 for (p = &mode_rw1[0]; *p != NULL; ++p) {
804 memcpy(&buf0[0], t->s, t->n);
806 fp = fmemopen(&buf0[0], t->n, *p);
807 ATF_CHECK(fp != NULL);
809 * test fmemopen_read + fread(4)
811 ATF_CHECK(ftello(fp) == (off_t)0);
812 ATF_CHECK(fread(&buf1[0], 1, sizeof(buf1), fp) == (size_t)t->n);
813 ATF_CHECK(feof(fp) != 0);
814 ATF_CHECK(memcmp(&buf0[0], &buf1[0], t->n) == 0);
815 ATF_CHECK((unsigned char)buf1[t->n] == 0x1);
817 ATF_CHECK(fclose(fp) == 0);
822 const char *mode_a1[] = { "a+", "ab+", NULL };
825 ATF_TC_HEAD(test17, tc)
827 atf_tc_set_md_var(tc, "descr", "test17");
829 ATF_TC_BODY(test17, tc)
838 for (t = &testcases[0]; t->s != NULL; ++t) {
839 len = strnlen(t->s, t->n);
840 for (p = &mode_a1[0]; *p != NULL; ++p) {
842 memcpy(&buf[0], t->s, t->n);
843 fp = fmemopen(&buf[0], t->n, *p);
844 ATF_CHECK(fp != NULL);
846 * test fmemopen_read + fgetc(3)
848 #if defined(__GLIBC__)
851 for (i = len; i < t->n; ++i) {
852 ATF_CHECK(ftello(fp) == (off_t)i);
853 ATF_CHECK(fgetc(fp) == buf[i]);
854 ATF_CHECK(feof(fp) == 0);
855 ATF_CHECK(ftello(fp) == (off_t)i + 1);
857 ATF_CHECK(fgetc(fp) == EOF);
858 ATF_CHECK(feof(fp) != 0);
859 ATF_CHECK(ftello(fp) == (off_t)t->n);
861 for (i = 0; i < t->n; ++i) {
862 ATF_CHECK(ftello(fp) == (off_t)i);
863 ATF_CHECK(fgetc(fp) == buf[i]);
864 ATF_CHECK(feof(fp) == 0);
865 ATF_CHECK(ftello(fp) == (off_t)i + 1);
867 ATF_CHECK(fgetc(fp) == EOF);
868 ATF_CHECK(feof(fp) != 0);
869 ATF_CHECK(ftello(fp) == (off_t)t->n);
870 #if defined(__GLIBC__)
873 ATF_CHECK(fclose(fp) == 0);
879 ATF_TC_HEAD(test18, tc)
881 atf_tc_set_md_var(tc, "descr", "test18");
883 ATF_TC_BODY(test18, tc)
888 char buf0[BUFSIZ], buf1[BUFSIZ];
891 for (t = &testcases[0]; t->s != NULL; ++t) {
892 len = strnlen(t->s, t->n);
893 for (p = &mode_a1[0]; *p != NULL; ++p) {
895 memcpy(&buf0[0], t->s, t->n);
896 buf1[t->n - len] = 0x1;
897 fp = fmemopen(&buf0[0], t->n, *p);
898 ATF_CHECK(fp != NULL);
900 * test fmemopen_read + fread(3)
902 #if defined(__GLIBC__)
905 ATF_CHECK(ftello(fp) == (off_t)len);
906 ATF_CHECK(fread(&buf1[0], 1, sizeof(buf1), fp)
908 ATF_CHECK(feof(fp) != 0);
909 ATF_CHECK(!memcmp(&buf0[len], &buf1[0], t->n - len));
910 ATF_CHECK((unsigned char)buf1[t->n - len] == 0x1);
913 ATF_CHECK(ftello(fp) == (off_t)0);
914 ATF_CHECK(fread(&buf1[0], 1, sizeof(buf1), fp)
916 ATF_CHECK(feof(fp) != 0);
917 ATF_CHECK(!memcmp(&buf0[0], &buf1[0], t->n));
918 ATF_CHECK((unsigned char)buf1[t->n] == 0x1);
919 #if defined(__GLIBC__)
922 ATF_CHECK(fclose(fp) == 0);
929 * If a stream open for update is flushed or closed and the last write has
930 * advanced the current buffer size, a null byte is written at the end of the
934 const char *mode_rw2[] = {
936 "w", "wb", "w+", "wb+", "w+b",
941 ATF_TC_HEAD(test19, tc)
943 atf_tc_set_md_var(tc, "descr", "test19");
945 ATF_TC_BODY(test19, tc)
953 for (t = &testcases[0]; t->s != NULL; ++t) {
954 for (p = &mode_rw2[0]; *p != NULL; ++p) {
956 memcpy(&buf[0], t->s, t->n);
958 fp = fmemopen(&buf[0], t->n + 1, *p);
959 ATF_CHECK(fp != NULL);
962 * test fmemopen_write + fputc(3)
964 for (i = 0; i < t->n; ++i) {
965 ATF_CHECK(ftello(fp) == (off_t)i);
966 ATF_CHECK(fputc(t->s[i], fp) == t->s[i]);
967 ATF_CHECK(buf[i] == t->s[i]);
968 ATF_CHECK(ftello(fp) == (off_t)i + 1);
969 ATF_CHECK(buf[i] == t->s[i]);
970 #if !defined(__GLIBC__)
971 ATF_CHECK(buf[i + 1] == '\0');
975 /* don't accept non nul character at end of buffer */
976 ATF_CHECK(fputc(0x1, fp) == EOF);
977 ATF_CHECK(ftello(fp) == (off_t)t->n);
978 ATF_CHECK(feof(fp) == 0);
980 /* accept nul character at end of buffer */
981 ATF_CHECK(fputc('\0', fp) == '\0');
982 ATF_CHECK(ftello(fp) == (off_t)t->n + 1);
983 ATF_CHECK(feof(fp) == 0);
986 ATF_CHECK(fputc('\0', fp) == EOF);
987 ATF_CHECK(ftello(fp) == (off_t)t->n + 1);
990 ATF_CHECK(memcmp(&buf[0], t->s, t->n) == 0);
991 ATF_CHECK(buf[t->n] == '\0');
993 ATF_CHECK(fclose(fp) == 0);
999 ATF_TC_HEAD(test20, tc)
1001 atf_tc_set_md_var(tc, "descr", "test20");
1003 ATF_TC_BODY(test20, tc)
1010 for (t = &testcases[0]; t->s != NULL; ++t) {
1011 for (p = &mode_rw2[0]; *p != NULL; ++p) {
1013 memcpy(&buf[0], t->s, t->n);
1015 fp = fmemopen(&buf[0], t->n + 1, *p);
1016 ATF_CHECK(fp != NULL);
1018 ATF_CHECK(fwrite(t->s, 1, t->n, fp) == (size_t)t->n);
1020 * test fmemopen_write + fwrite(3)
1022 #if !defined(__GLIBC__)
1023 ATF_CHECK(buf[t->n] == '\0');
1025 /* don't accept non nul character at end of buffer */
1026 ATF_CHECK(fwrite("\x1", 1, 1, fp) == 0);
1027 ATF_CHECK(ftello(fp) == (off_t)t->n);
1028 ATF_CHECK(feof(fp) == 0);
1031 /* accept nul character at end of buffer */
1032 ATF_CHECK(fwrite("\x0", 1, 1, fp) == 1);
1033 ATF_CHECK(ftello(fp) == (off_t)t->n + 1);
1034 ATF_CHECK(feof(fp) == 0);
1037 ATF_CHECK(fputc('\0', fp) == EOF);
1038 ATF_CHECK(ftello(fp) == (off_t)t->n + 1);
1041 ATF_CHECK(memcmp(&buf[0], t->s, t->n) == 0);
1042 ATF_CHECK(buf[t->n] == '\0');
1044 ATF_CHECK(fclose(fp) == 0);
1050 ATF_TC_HEAD(test21, tc)
1052 atf_tc_set_md_var(tc, "descr", "test21");
1054 ATF_TC_BODY(test21, tc)
1062 for (t = &testcases[0]; t->s != NULL; ++t) {
1063 len = strnlen(t->s, t->n);
1064 for (p = &mode_a[0]; *p != NULL; ++p) {
1065 memcpy(&buf[0], t->s, t->n);
1066 fp = fmemopen(&buf[0], t->n, *p);
1067 ATF_CHECK(fp != NULL);
1070 * test fmemopen_write + fputc(3)
1073 for (i = len; i < t->n - 1; ++i) {
1074 ATF_CHECK(ftello(fp) == (off_t)i);
1075 ATF_CHECK(fputc(t->s[i - len], fp)
1077 ATF_CHECK(buf[i] == t->s[i - len]);
1078 ATF_CHECK(ftello(fp) == (off_t)i + 1);
1079 #if !defined(__GLIBC__)
1080 ATF_CHECK(buf[i + 1] == '\0');
1084 /* don't accept non nul character at end of buffer */
1085 ATF_CHECK(ftello(fp) == (off_t)t->n - 1);
1086 ATF_CHECK(fputc(0x1, fp) == EOF);
1087 ATF_CHECK(ftello(fp) == (off_t)t->n - 1);
1089 /* accept nul character at end of buffer */
1090 ATF_CHECK(ftello(fp) == (off_t)t->n - 1);
1091 ATF_CHECK(fputc('\0', fp) == '\0');
1092 ATF_CHECK(ftello(fp) == (off_t)t->n);
1096 ATF_CHECK(ftello(fp) == (off_t)t->n);
1097 ATF_CHECK(fputc('\0', fp) == EOF);
1098 ATF_CHECK(ftello(fp) == (off_t)t->n);
1100 ATF_CHECK(fclose(fp) == 0);
1106 ATF_TC_HEAD(test22, tc)
1108 atf_tc_set_md_var(tc, "descr", "test22");
1110 ATF_TC_BODY(test22, tc)
1112 struct testcase *t0, *t1;
1113 size_t len0, len1, nleft;
1118 for (t0 = &testcases[0]; t0->s != NULL; ++t0) {
1119 len0 = strnlen(t0->s, t0->n);
1120 for (t1 = &testcases[0]; t1->s != NULL; ++t1) {
1121 len1 = strnlen(t1->s, t1->n);
1122 for (p = &mode_a[0]; *p != NULL; ++p) {
1124 memcpy(&buf[0], t0->s, t0->n);
1125 fp = fmemopen(&buf[0], t0->n, *p);
1126 ATF_CHECK(fp != NULL);
1129 * test fmemopen_write + fwrite(3)
1131 nleft = t0->n - len0;
1132 #if !defined(__GLIBC__)
1133 if (nleft == 0 || len1 == nleft - 1) {
1134 ATF_CHECK(fwrite(t1->s, 1, t1->n, fp)
1136 ATF_CHECK(ftell(fp) == t1->n);
1138 ATF_CHECK(fwrite(t1->s, 1, t1->n, fp)
1140 ATF_CHECK(ftell(fp) == t1->n - 1);
1143 ATF_CHECK(fclose(fp) == 0);
1152 ATF_TP_ADD_TC(tp, test00);
1153 ATF_TP_ADD_TC(tp, test01);
1154 ATF_TP_ADD_TC(tp, test02);
1155 ATF_TP_ADD_TC(tp, test03);
1156 ATF_TP_ADD_TC(tp, test04);
1157 ATF_TP_ADD_TC(tp, test05);
1158 ATF_TP_ADD_TC(tp, test06);
1159 ATF_TP_ADD_TC(tp, test07);
1160 ATF_TP_ADD_TC(tp, test08);
1161 ATF_TP_ADD_TC(tp, test09);
1162 ATF_TP_ADD_TC(tp, test10);
1163 ATF_TP_ADD_TC(tp, test11);
1165 ATF_TP_ADD_TC(tp, test12);
1167 ATF_TP_ADD_TC(tp, test13);
1168 ATF_TP_ADD_TC(tp, test14);
1170 ATF_TP_ADD_TC(tp, test15);
1171 ATF_TP_ADD_TC(tp, test16);
1172 ATF_TP_ADD_TC(tp, test17);
1173 ATF_TP_ADD_TC(tp, test18);
1174 ATF_TP_ADD_TC(tp, test19);
1175 ATF_TP_ADD_TC(tp, test20);
1176 ATF_TP_ADD_TC(tp, test21);
1177 ATF_TP_ADD_TC(tp, test22);
1180 return atf_no_error();