Module Name: src
Committed By: justin
Date: Wed Oct 15 21:55:34 UTC 2014
Modified Files:
src/distrib/sets/lists/tests: mi
src/tests/lib/libc/stdio: Makefile
Added Files:
src/tests/lib/libc/stdio: t_open_memstream.c
Log Message:
PR standards/49279 add tests for open_memstream, ported from OpenBSD
To generate a diff of this commit:
cvs rdiff -u -r1.593 -r1.594 src/distrib/sets/lists/tests/mi
cvs rdiff -u -r1.11 -r1.12 src/tests/lib/libc/stdio/Makefile
cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/stdio/t_open_memstream.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/distrib/sets/lists/tests/mi
diff -u src/distrib/sets/lists/tests/mi:1.593 src/distrib/sets/lists/tests/mi:1.594
--- src/distrib/sets/lists/tests/mi:1.593 Mon Oct 13 13:55:31 2014
+++ src/distrib/sets/lists/tests/mi Wed Oct 15 21:55:34 2014
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.593 2014/10/13 13:55:31 uebayasi Exp $
+# $NetBSD: mi,v 1.594 2014/10/15 21:55:34 justin Exp $
#
# Note: don't delete entries from here - mark them as "obsolete" instead.
#
@@ -2576,6 +2576,7 @@
./usr/tests/lib/libc/stdio/t_format tests-obsolete obsolete
./usr/tests/lib/libc/stdio/t_fputc tests-lib-tests atf
./usr/tests/lib/libc/stdio/t_mktemp tests-lib-tests atf
+./usr/tests/lib/libc/stdio/t_open_memstream tests-lib-tests atf
./usr/tests/lib/libc/stdio/t_popen tests-lib-tests atf
./usr/tests/lib/libc/stdio/t_printf tests-lib-tests atf
./usr/tests/lib/libc/stdio/t_scanf tests-lib-tests atf
Index: src/tests/lib/libc/stdio/Makefile
diff -u src/tests/lib/libc/stdio/Makefile:1.11 src/tests/lib/libc/stdio/Makefile:1.12
--- src/tests/lib/libc/stdio/Makefile:1.11 Thu Apr 25 13:34:09 2013
+++ src/tests/lib/libc/stdio/Makefile Wed Oct 15 21:55:34 2014
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.11 2013/04/25 13:34:09 joerg Exp $
+# $NetBSD: Makefile,v 1.12 2014/10/15 21:55:34 justin Exp $
.include <bsd.own.mk>
@@ -8,6 +8,7 @@ TESTS_C+= t_clearerr
TESTS_C+= t_fflush
TESTS_C+= t_fmemopen
TESTS_C+= t_fopen
+TESTS_C+= t_open_memstream
TESTS_C+= t_fputc
TESTS_C+= t_mktemp
TESTS_C+= t_popen
Added files:
Index: src/tests/lib/libc/stdio/t_open_memstream.c
diff -u /dev/null src/tests/lib/libc/stdio/t_open_memstream.c:1.1
--- /dev/null Wed Oct 15 21:55:34 2014
+++ src/tests/lib/libc/stdio/t_open_memstream.c Wed Oct 15 21:55:34 2014
@@ -0,0 +1,95 @@
+/*
+ * Based on the OpenBSD test
+ * Copyright (c) 2011 Martin Pieuchot <[email protected]>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_open_memstream.c,v 1.1 2014/10/15 21:55:34 justin Exp $");
+
+#include <atf-c.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+ATF_TC(test_open_memstream);
+ATF_TC_HEAD(test_open_memstream, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test open_memstream functionality");
+}
+
+#define OFFSET 16384
+
+const char start[] = "start";
+const char hello[] = "hello";
+
+ATF_TC_BODY(test_open_memstream, tc)
+{
+ FILE *fp;
+ char *buf = (char *)0xff;
+ size_t size = 0;
+ off_t off;
+ int i;
+
+ fp = open_memstream(&buf, &size);
+ ATF_REQUIRE(fp != NULL);
+
+ off = ftello(fp);
+ ATF_CHECK(off == 0);
+
+ ATF_CHECK(fflush(fp) == 0);
+ ATF_CHECK(size == 0);
+ ATF_CHECK(buf != (char *)0xff);
+ ATF_CHECK(fseek(fp, OFFSET, SEEK_SET) == 0);
+ ATF_CHECK(fprintf(fp, hello) != EOF);
+ ATF_CHECK(fflush(fp) != EOF);
+ ATF_CHECK(size == OFFSET + sizeof(hello)-1);
+ ATF_CHECK(fseek(fp, 0, SEEK_SET) == 0);
+ ATF_CHECK(fprintf(fp, start) != EOF);
+ ATF_CHECK(fflush(fp) != EOF);
+ ATF_CHECK(size == sizeof(start)-1);
+
+ /* Needed for sparse files */
+ ATF_CHECK(strncmp(buf, start, sizeof(start)-1) == 0);
+ for (i = sizeof(start)-1; i < OFFSET; i++)
+ ATF_CHECK(buf[i] == '\0');
+
+ ATF_CHECK(memcmp(buf + OFFSET, hello, sizeof(hello)-1) == 0);
+
+ /* verify that simply seeking past the end doesn't increase the size */
+ ATF_CHECK(fseek(fp, 100, SEEK_END) == 0);
+ ATF_CHECK(fflush(fp) != EOF);
+ ATF_CHECK(size == OFFSET + sizeof(hello)-1);
+ ATF_CHECK(fseek(fp, 8, SEEK_SET) == 0);
+ ATF_CHECK(ftell(fp) == 8);
+
+ /* Try to seek backward */
+ ATF_CHECK(fseek(fp, -1, SEEK_CUR) == 0);
+ ATF_CHECK(ftell(fp) == 7);
+ ATF_CHECK(fseek(fp, 5, SEEK_CUR) == 0);
+ ATF_CHECK(fclose(fp) != EOF);
+ ATF_CHECK(size == 12);
+
+ free(buf);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, test_open_memstream);
+
+ return atf_no_error();
+}