Module Name:    src
Committed By:   jruoho
Date:           Sat Jun  4 14:56:58 UTC 2011

Modified Files:
        src/distrib/sets/lists/tests: mi
        src/tests/lib/libc/stdlib: Makefile
Added Files:
        src/tests/lib/libc/stdlib: t_strtol.c

Log Message:
Add some tests for strtol(3).


To generate a diff of this commit:
cvs rdiff -u -r1.347 -r1.348 src/distrib/sets/lists/tests/mi
cvs rdiff -u -r1.13 -r1.14 src/tests/lib/libc/stdlib/Makefile
cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/stdlib/t_strtol.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.347 src/distrib/sets/lists/tests/mi:1.348
--- src/distrib/sets/lists/tests/mi:1.347	Sat Jun  4 09:29:43 2011
+++ src/distrib/sets/lists/tests/mi	Sat Jun  4 14:56:57 2011
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.347 2011/06/04 09:29:43 jruoho Exp $
+# $NetBSD: mi,v 1.348 2011/06/04 14:56:57 jruoho Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -442,6 +442,7 @@
 ./usr/libdata/debug/usr/tests/lib/libc/stdlib/t_mi_vector_hash.debug	tests-lib-debug		debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/stdlib/t_posix_memalign.debug	tests-lib-debug		debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/stdlib/t_strtod.debug		tests-lib-debug		debug,atf
+./usr/libdata/debug/usr/tests/lib/libc/stdlib/t_strtol.debug		tests-lib-debug		debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/stdlib/t_strtox.debug		tests-obsolete		obsolete
 ./usr/libdata/debug/usr/tests/lib/libc/string				tests-lib-debug
 ./usr/libdata/debug/usr/tests/lib/libc/string/t_memcpy.debug		tests-lib-debug		debug,atf
@@ -1957,6 +1958,7 @@
 ./usr/tests/lib/libc/stdlib/t_mi_vector_hash	tests-lib-tests		atf
 ./usr/tests/lib/libc/stdlib/t_posix_memalign	tests-lib-tests		atf
 ./usr/tests/lib/libc/stdlib/t_strtod		tests-lib-tests		atf
+./usr/tests/lib/libc/stdlib/t_strtol		tests-lib-tests		atf
 ./usr/tests/lib/libc/stdlib/t_strtox		tests-obsolete		obsolete
 ./usr/tests/lib/libc/string			tests-lib-tests
 ./usr/tests/lib/libc/string/Atffile		tests-lib-tests		atf

Index: src/tests/lib/libc/stdlib/Makefile
diff -u src/tests/lib/libc/stdlib/Makefile:1.13 src/tests/lib/libc/stdlib/Makefile:1.14
--- src/tests/lib/libc/stdlib/Makefile:1.13	Sat Jun  4 11:12:28 2011
+++ src/tests/lib/libc/stdlib/Makefile	Sat Jun  4 14:56:58 2011
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.13 2011/06/04 11:12:28 jruoho Exp $
+# $NetBSD: Makefile,v 1.14 2011/06/04 14:56:58 jruoho Exp $
 
 .include <bsd.own.mk>
 
@@ -12,6 +12,7 @@
 TESTS_C+=	t_mi_vector_hash
 TESTS_C+=	t_posix_memalign
 TESTS_C+=	t_strtod
+TESTS_C+=	t_strtol
 
 TESTS_SH+=	t_atexit
 TESTS_SH+=	t_getopt

Added files:

Index: src/tests/lib/libc/stdlib/t_strtol.c
diff -u /dev/null src/tests/lib/libc/stdlib/t_strtol.c:1.1
--- /dev/null	Sat Jun  4 14:56:58 2011
+++ src/tests/lib/libc/stdlib/t_strtol.c	Sat Jun  4 14:56:58 2011
@@ -0,0 +1,236 @@
+/*	$NetBSD: t_strtol.c,v 1.1 2011/06/04 14:56:58 jruoho Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jukka Ruohonen.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_strtol.c,v 1.1 2011/06/04 14:56:58 jruoho Exp $");
+
+#include <atf-c.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+
+struct test {
+	const char	*str;
+	long int	 res;
+	int		 base;
+	const char	*end;
+};
+
+static void	check(struct test *, long int, long long int, char *);
+
+static void
+check(struct test *t, long int li, long long int lli, char *end)
+{
+
+	if (li != -1 && li != t->res)
+		atf_tc_fail_nonfatal("strtol(%s, &end, %d) failed "
+		    "(rv = %ld)", t->str, t->base, li);
+
+	if (lli != -1 && lli != (long long int)t->res)
+		atf_tc_fail_nonfatal("strtoll(%s, NULL, %d) failed "
+		    "(rv = %lld)", t->str, t->base, lli);
+
+	if (t->end != NULL && strcmp(t->end, end) != 0)
+		atf_tc_fail_nonfatal("invalid end pointer ('%s') from "
+		    "strtol(%s, &end, %d)", end, t->str, t->base);
+}
+
+ATF_TC(strtol_base);
+ATF_TC_HEAD(strtol_base, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test strtol(3) with different bases");
+}
+
+ATF_TC_BODY(strtol_base, tc)
+{
+	struct test t[] = {
+		{ "123456789",			 123456789,  0, NULL	},
+		{ "111010110111100110100010101", 123456789,  2, NULL	},
+		{ "22121022020212200",		 123456789,  3, NULL	},
+		{ "13112330310111",		 123456789,  4, NULL	},
+		{ "223101104124",		 123456789,  5, NULL	},
+		{ "20130035113",		 123456789,  6, NULL	},
+		{ "3026236221",			 123456789,  7, NULL	},
+		{ "726746425",			 123456789,  8, NULL	},
+		{ "277266780",			 123456789,  9, NULL	},
+		{ "123456789",			 123456789, 10, NULL	},
+		{ "63762A05",			 123456789, 11, NULL	},
+		{ "35418A99",			 123456789, 12, NULL	},
+		{ "1C767471",			 123456789, 13, NULL	},
+		{ "12579781",			 123456789, 14, NULL	},
+		{ "AC89BC9",			 123456789, 15, NULL	},
+		{ "75BCD15",			 123456789, 16, NULL	},
+		{ "123456789",			    342391,  8, NULL	},
+		{ "0123456789",			    342391,  0, NULL	},
+		{ "0123456789",			 123456789, 10, NULL	},
+		{ "0123456789",		       0x123456789, 16, NULL	},
+		{ "0x123456789",	       0x123456789,  0, NULL	},
+		{ "0x75bcd15",		         123456789,  0, NULL	},
+	};
+
+	long long int lli;
+	long int li;
+	char *end;
+	size_t i;
+
+	for (i = 0; i < __arraycount(t); i++) {
+
+		li = strtol(t[i].str, &end, t[i].base);
+		lli = strtoll(t[i].str, NULL, t[i].base);
+
+		check(&t[i], li, lli, end);
+	}
+}
+
+ATF_TC(strtol_case);
+ATF_TC_HEAD(strtol_case, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Case insensitivity with strtol(3)");
+}
+
+ATF_TC_BODY(strtol_case, tc)
+{
+	struct test t[] = {
+		{ "abcd",	0xabcd, 16, NULL	},
+		{ "     dcba",	0xdcba, 16, NULL	},
+		{ "abcd dcba",	0xabcd, 16, " dcba"	},
+		{ "abc0x123",	0xabc0, 16, NULL	},
+		{ "abcd\0x123",	0xabcd, 16, "\0x123"	},
+		{ "ABCD",	0xabcd, 16, NULL	},
+		{ "aBcD",	0xabcd, 16, NULL	},
+		{ "0xABCD",	0xabcd, 16, NULL	},
+		{ "0xABCDX",	0xabcd, 16, "X"		},
+	};
+
+	long long int lli;
+	long int li;
+	char *end;
+	size_t i;
+
+	for (i = 0; i < __arraycount(t); i++) {
+
+		li = strtol(t[i].str, &end, t[i].base);
+		lli = strtoll(t[i].str, NULL, t[i].base);
+
+		check(&t[i], li, lli, end);
+	}
+}
+
+ATF_TC(strtol_range);
+ATF_TC_HEAD(strtol_range, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test ERANGE from strtol(3)");
+}
+
+ATF_TC_BODY(strtol_range, tc)
+{
+
+#if LONG_MAX == 0x7fffffff	/* XXX: Is this portable? */
+
+	struct test t[] = {
+		{ "20000000000", 2147483647, 8, NULL },
+		{ "2147483648",  2147483647, 10, NULL },
+		{ "80000000",	 2147483647, 16, NULL },
+	};
+#else
+	struct test t[] = {
+		{ "1000000000000000000000", 9223372036854775807, 8, NULL },
+		{ "9223372036854775808",    9223372036854775807, 10, NULL },
+		{ "8000000000000000",       9223372036854775807, 16, NULL },
+	};
+#endif
+
+	long int li;
+	char *end;
+	size_t i;
+
+	for (i = 0; i < __arraycount(t); i++) {
+
+		errno = 0;
+		li = strtol(t[i].str, &end, t[i].base);
+
+		if (errno != ERANGE)
+			atf_tc_fail("strtol(3) did not catch ERANGE");
+
+		check(&t[i], li, -1, end);
+	}
+}
+
+ATF_TC(strtol_signed);
+ATF_TC_HEAD(strtol_signed, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "A basic test of strtol(3)");
+}
+
+ATF_TC_BODY(strtol_signed, tc)
+{
+	struct test t[] = {
+		{ "1",		 1, 0, NULL	},
+		{ " 2",		 2, 0, NULL	},
+		{ "  3",	 3, 0, NULL	},
+		{ " -3",	-3, 0, NULL	},
+		{ "--1",	 0, 0, "--1"	},
+		{ "+-2",	 0, 0, "+-2"	},
+		{ "++3",	 0, 0, "++3"	},
+		{ "+9",		 9, 0, NULL	},
+		{ "+123",      123, 0, NULL	},
+		{ "-1 3",       -1, 0, " 3"	},
+		{ "-1.3",       -1, 0, ".3"	},
+		{ "-  3",        0, 0, "-  3"	},
+		{ "+33.",       33, 0, "."	},
+		{ "30x0",       30, 0, "x0"	},
+	};
+
+	long long int lli;
+	long int li;
+	char *end;
+	size_t i;
+
+	for (i = 0; i < __arraycount(t); i++) {
+
+		li = strtol(t[i].str, &end, t[i].base);
+		lli = strtoll(t[i].str, NULL, t[i].base);
+
+		check(&t[i], li, lli, end);
+	}
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+	ATF_TP_ADD_TC(tp, strtol_base);
+	ATF_TP_ADD_TC(tp, strtol_case);
+	ATF_TP_ADD_TC(tp, strtol_range);
+	ATF_TP_ADD_TC(tp, strtol_signed);
+
+	return atf_no_error();
+}

Reply via email to