Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src PR standards/49279 add tests for open_memstream, ported from...
details: https://anonhg.NetBSD.org/src/rev/37da44a909e9
branches: trunk
changeset: 333043:37da44a909e9
user: justin <justin%NetBSD.org@localhost>
date: Wed Oct 15 21:55:34 2014 +0000
description:
PR standards/49279 add tests for open_memstream, ported from OpenBSD
diffstat:
distrib/sets/lists/tests/mi | 3 +-
tests/lib/libc/stdio/Makefile | 3 +-
tests/lib/libc/stdio/t_open_memstream.c | 95 +++++++++++++++++++++++++++++++++
3 files changed, 99 insertions(+), 2 deletions(-)
diffs (133 lines):
diff -r fb61e9998a89 -r 37da44a909e9 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi Wed Oct 15 21:47:48 2014 +0000
+++ b/distrib/sets/lists/tests/mi Wed Oct 15 21:55:34 2014 +0000
@@ -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
diff -r fb61e9998a89 -r 37da44a909e9 tests/lib/libc/stdio/Makefile
--- a/tests/lib/libc/stdio/Makefile Wed Oct 15 21:47:48 2014 +0000
+++ b/tests/lib/libc/stdio/Makefile Wed Oct 15 21:55:34 2014 +0000
@@ -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_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
diff -r fb61e9998a89 -r 37da44a909e9 tests/lib/libc/stdio/t_open_memstream.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/stdio/t_open_memstream.c Wed Oct 15 21:55:34 2014 +0000
@@ -0,0 +1,95 @@
+/*
+ * Based on the OpenBSD test
+ * Copyright (c) 2011 Martin Pieuchot <mpi%openbsd.org@localhost>
+ *
+ * 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();
+}
Home |
Main Index |
Thread Index |
Old Index