Home | History | Annotate | Line # | Download | only in stdlib
t_strtol.c revision 1.3
      1 /*	$NetBSD: t_strtol.c,v 1.3 2011/06/05 13:51:46 jruoho Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jukka Ruohonen.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __RCSID("$NetBSD: t_strtol.c,v 1.3 2011/06/05 13:51:46 jruoho Exp $");
     34 
     35 #include <atf-c.h>
     36 #include <errno.h>
     37 #include <stdlib.h>
     38 #include <string.h>
     39 #include <limits.h>
     40 
     41 struct test {
     42 	const char	*str;
     43 	long long int	 res;
     44 	int		 base;
     45 	const char	*end;
     46 };
     47 
     48 static void	check(struct test *, long int, long long int, char *);
     49 
     50 static void
     51 check(struct test *t, long int li, long long int lli, char *end)
     52 {
     53 
     54 	if (li != -1 && li != t->res)
     55 		atf_tc_fail_nonfatal("strtol(%s, &end, %d) failed "
     56 		    "(rv = %ld)", t->str, t->base, li);
     57 
     58 	if (lli != -1 && lli != t->res)
     59 		atf_tc_fail_nonfatal("strtoll(%s, NULL, %d) failed "
     60 		    "(rv = %lld)", t->str, t->base, lli);
     61 
     62 	if (t->end != NULL && strcmp(t->end, end) != 0)
     63 		atf_tc_fail_nonfatal("invalid end pointer ('%s') from "
     64 		    "strtol(%s, &end, %d)", end, t->str, t->base);
     65 }
     66 
     67 ATF_TC(strtol_base);
     68 ATF_TC_HEAD(strtol_base, tc)
     69 {
     70 	atf_tc_set_md_var(tc, "descr", "Test strtol(3) with different bases");
     71 }
     72 
     73 ATF_TC_BODY(strtol_base, tc)
     74 {
     75 	struct test t[] = {
     76 		{ "123456789",			 123456789,  0, NULL	},
     77 		{ "111010110111100110100010101", 123456789,  2, NULL	},
     78 		{ "22121022020212200",		 123456789,  3, NULL	},
     79 		{ "13112330310111",		 123456789,  4, NULL	},
     80 		{ "223101104124",		 123456789,  5, NULL	},
     81 		{ "20130035113",		 123456789,  6, NULL	},
     82 		{ "3026236221",			 123456789,  7, NULL	},
     83 		{ "726746425",			 123456789,  8, NULL	},
     84 		{ "277266780",			 123456789,  9, NULL	},
     85 		{ "123456789",			 123456789, 10, NULL	},
     86 		{ "63762A05",			 123456789, 11, NULL	},
     87 		{ "35418A99",			 123456789, 12, NULL	},
     88 		{ "1C767471",			 123456789, 13, NULL	},
     89 		{ "12579781",			 123456789, 14, NULL	},
     90 		{ "AC89BC9",			 123456789, 15, NULL	},
     91 		{ "75BCD15",			 123456789, 16, NULL	},
     92 		{ "123456789",			    342391,  8, NULL	},
     93 		{ "0123456789",			    342391,  0, NULL	},
     94 		{ "0123456789",			 123456789, 10, NULL	},
     95 		{ "0123456789",		       0x123456789, 16, NULL	},
     96 		{ "0x123456789",	       0x123456789,  0, NULL	},
     97 		{ "0x75bcd15",		         123456789,  0, NULL	},
     98 	};
     99 
    100 	long long int lli;
    101 	long int li;
    102 	char *end;
    103 	size_t i;
    104 
    105 	for (i = 0; i < __arraycount(t); i++) {
    106 
    107 		li = strtol(t[i].str, &end, t[i].base);
    108 		lli = strtoll(t[i].str, NULL, t[i].base);
    109 
    110 		check(&t[i], li, lli, end);
    111 	}
    112 }
    113 
    114 ATF_TC(strtol_case);
    115 ATF_TC_HEAD(strtol_case, tc)
    116 {
    117 	atf_tc_set_md_var(tc, "descr", "Case insensitivity with strtol(3)");
    118 }
    119 
    120 ATF_TC_BODY(strtol_case, tc)
    121 {
    122 	struct test t[] = {
    123 		{ "abcd",	0xabcd, 16, NULL	},
    124 		{ "     dcba",	0xdcba, 16, NULL	},
    125 		{ "abcd dcba",	0xabcd, 16, " dcba"	},
    126 		{ "abc0x123",	0xabc0, 16, NULL	},
    127 		{ "abcd\0x123",	0xabcd, 16, "\0x123"	},
    128 		{ "ABCD",	0xabcd, 16, NULL	},
    129 		{ "aBcD",	0xabcd, 16, NULL	},
    130 		{ "0xABCD",	0xabcd, 16, NULL	},
    131 		{ "0xABCDX",	0xabcd, 16, "X"		},
    132 	};
    133 
    134 	long long int lli;
    135 	long int li;
    136 	char *end;
    137 	size_t i;
    138 
    139 	for (i = 0; i < __arraycount(t); i++) {
    140 
    141 		li = strtol(t[i].str, &end, t[i].base);
    142 		lli = strtoll(t[i].str, NULL, t[i].base);
    143 
    144 		check(&t[i], li, lli, end);
    145 	}
    146 }
    147 
    148 ATF_TC(strtol_range);
    149 ATF_TC_HEAD(strtol_range, tc)
    150 {
    151 	atf_tc_set_md_var(tc, "descr", "Test ERANGE from strtol(3)");
    152 }
    153 
    154 ATF_TC_BODY(strtol_range, tc)
    155 {
    156 
    157 #if LONG_MAX == 0x7fffffff	/* XXX: Is this portable? */
    158 
    159 	struct test t[] = {
    160 		{ "20000000000", 2147483647, 8, NULL },
    161 		{ "2147483648",  2147483647, 10, NULL },
    162 		{ "80000000",	 2147483647, 16, NULL },
    163 	};
    164 #else
    165 	struct test t[] = {
    166 		{ "1000000000000000000000", 9223372036854775807, 8, NULL },
    167 		{ "9223372036854775808",    9223372036854775807, 10, NULL },
    168 		{ "8000000000000000",       9223372036854775807, 16, NULL },
    169 	};
    170 #endif
    171 
    172 	long int li;
    173 	char *end;
    174 	size_t i;
    175 
    176 	for (i = 0; i < __arraycount(t); i++) {
    177 
    178 		errno = 0;
    179 		li = strtol(t[i].str, &end, t[i].base);
    180 
    181 		if (errno != ERANGE)
    182 			atf_tc_fail("strtol(3) did not catch ERANGE");
    183 
    184 		check(&t[i], li, -1, end);
    185 	}
    186 }
    187 
    188 ATF_TC(strtol_signed);
    189 ATF_TC_HEAD(strtol_signed, tc)
    190 {
    191 	atf_tc_set_md_var(tc, "descr", "A basic test of strtol(3)");
    192 }
    193 
    194 ATF_TC_BODY(strtol_signed, tc)
    195 {
    196 	struct test t[] = {
    197 		{ "1",		 1, 0, NULL	},
    198 		{ " 2",		 2, 0, NULL	},
    199 		{ "  3",	 3, 0, NULL	},
    200 		{ " -3",	-3, 0, NULL	},
    201 		{ "--1",	 0, 0, "--1"	},
    202 		{ "+-2",	 0, 0, "+-2"	},
    203 		{ "++3",	 0, 0, "++3"	},
    204 		{ "+9",		 9, 0, NULL	},
    205 		{ "+123",      123, 0, NULL	},
    206 		{ "-1 3",       -1, 0, " 3"	},
    207 		{ "-1.3",       -1, 0, ".3"	},
    208 		{ "-  3",        0, 0, "-  3"	},
    209 		{ "+33.",       33, 0, "."	},
    210 		{ "30x0",       30, 0, "x0"	},
    211 	};
    212 
    213 	long long int lli;
    214 	long int li;
    215 	char *end;
    216 	size_t i;
    217 
    218 	for (i = 0; i < __arraycount(t); i++) {
    219 
    220 		li = strtol(t[i].str, &end, t[i].base);
    221 		lli = strtoll(t[i].str, NULL, t[i].base);
    222 
    223 		check(&t[i], li, lli, end);
    224 	}
    225 }
    226 
    227 ATF_TP_ADD_TCS(tp)
    228 {
    229 
    230 	ATF_TP_ADD_TC(tp, strtol_base);
    231 	ATF_TP_ADD_TC(tp, strtol_case);
    232 	ATF_TP_ADD_TC(tp, strtol_range);
    233 	ATF_TP_ADD_TC(tp, strtol_signed);
    234 
    235 	return atf_no_error();
    236 }
    237