Home | History | Annotate | Line # | Download | only in stdlib
t_strtoi.c revision 1.1.8.1
      1  1.1.8.1  pgoyette /*	$NetBSD: t_strtoi.c,v 1.1.8.1 2017/05/02 03:19:23 pgoyette Exp $	*/
      2      1.1  christos 
      3      1.1  christos /*-
      4      1.1  christos  * Copyright (c) 2015 The NetBSD Foundation, Inc.
      5      1.1  christos  * All rights reserved.
      6      1.1  christos  *
      7      1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1  christos  * by Jukka Ruohonen.
      9      1.1  christos  *
     10      1.1  christos  * Redistribution and use in source and binary forms, with or without
     11      1.1  christos  * modification, are permitted provided that the following conditions
     12      1.1  christos  * are met:
     13      1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14      1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15      1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17      1.1  christos  *    documentation and/or other materials provided with the distribution.
     18      1.1  christos  *
     19      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1  christos  */
     31      1.1  christos 
     32      1.1  christos /*
     33  1.1.8.1  pgoyette  * Created by Kamil Rytarowski, based on ID:
     34      1.1  christos  * NetBSD: t_strtol.c,v 1.5 2011/06/14 02:45:58 jruoho Exp
     35      1.1  christos  */
     36      1.1  christos 
     37      1.1  christos #include <sys/cdefs.h>
     38  1.1.8.1  pgoyette __RCSID("$NetBSD: t_strtoi.c,v 1.1.8.1 2017/05/02 03:19:23 pgoyette Exp $");
     39      1.1  christos 
     40      1.1  christos #include <atf-c.h>
     41      1.1  christos #include <errno.h>
     42      1.1  christos #include <inttypes.h>
     43      1.1  christos #include <stdlib.h>
     44      1.1  christos #include <string.h>
     45      1.1  christos #include <limits.h>
     46      1.1  christos 
     47      1.1  christos struct test {
     48      1.1  christos 	const char	*str;
     49      1.1  christos 	intmax_t	 res;
     50      1.1  christos 	int		 base;
     51      1.1  christos 	const char	*end;
     52      1.1  christos 	intmax_t	 lo;
     53      1.1  christos 	intmax_t	 hi;
     54      1.1  christos 	int		 rstatus;
     55      1.1  christos };
     56      1.1  christos 
     57      1.1  christos static void	check(struct test *, intmax_t, char *, int);
     58      1.1  christos 
     59      1.1  christos static void
     60      1.1  christos check(struct test *t, intmax_t rv, char *end, int rstatus)
     61      1.1  christos {
     62      1.1  christos 
     63      1.1  christos 	if (rv != t->res)
     64      1.1  christos 		atf_tc_fail_nonfatal("strtoi(%s, &end, %d, %jd, %jd, &rstatus)"
     65      1.1  christos 		    " failed (rv = %jd)", t->str, t->base, t->lo, t->hi, rv);
     66      1.1  christos 
     67      1.1  christos 	if (rstatus != t->rstatus)
     68      1.1  christos 		atf_tc_fail_nonfatal("strtoi(%s, &end, %d, %jd, %jd, &rstatus)"
     69      1.1  christos 		    " failed (rstatus: %d ('%s'))",
     70      1.1  christos 		    t->str, t->base, t->lo, t->hi, rstatus, strerror(rstatus));
     71      1.1  christos 
     72      1.1  christos 	if ((t->end != NULL && strcmp(t->end, end) != 0) ||
     73      1.1  christos 	    (t->end == NULL && *end != '\0'))
     74      1.1  christos 		atf_tc_fail_nonfatal("invalid end pointer ('%s') from "
     75      1.1  christos 		    "strtoi(%s, &end, %d, %jd, %jd, &rstatus)",
     76      1.1  christos 		     end, t->str, t->base, t->lo, t->hi);
     77      1.1  christos }
     78      1.1  christos 
     79      1.1  christos ATF_TC(strtoi_base);
     80      1.1  christos ATF_TC_HEAD(strtoi_base, tc)
     81      1.1  christos {
     82      1.1  christos 	atf_tc_set_md_var(tc, "descr", "Test strtoi(3) with different bases");
     83      1.1  christos }
     84      1.1  christos 
     85      1.1  christos ATF_TC_BODY(strtoi_base, tc)
     86      1.1  christos {
     87      1.1  christos 	struct test t[] = {
     88      1.1  christos 		{ "123456789",                  123456789,	0,	NULL,
     89      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
     90      1.1  christos 		{ "111010110111100110100010101",123456789,	2,	NULL,
     91      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
     92      1.1  christos 		{ "22121022020212200",          123456789,	3,	NULL,
     93      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
     94      1.1  christos 		{ "13112330310111",	        123456789,	4,	NULL,
     95      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
     96      1.1  christos 		{ "223101104124",               123456789,	5,	NULL,
     97      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
     98      1.1  christos 		{ "20130035113",                123456789,	6,	NULL,
     99      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
    100      1.1  christos 		{ "3026236221",	                123456789,	7,	NULL,
    101      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
    102      1.1  christos 		{ "726746425",                  123456789,	8,	NULL,
    103      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
    104      1.1  christos 		{ "277266780",                  123456789,	9,	NULL,
    105      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
    106      1.1  christos 		{ "123456789",                  123456789,	10,	NULL,
    107      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
    108      1.1  christos 		{ "63762A05",                   123456789,	11,	NULL,
    109      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
    110      1.1  christos 		{ "35418A99",                   123456789,	12,	NULL,
    111      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
    112      1.1  christos 		{ "1C767471",                   123456789,	13,	NULL,
    113      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
    114      1.1  christos 		{ "12579781",                   123456789,	14,	NULL,
    115      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
    116      1.1  christos 		{ "AC89BC9",                    123456789,	15,	NULL,
    117      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
    118      1.1  christos 		{ "75BCD15",                    123456789,	16,	NULL,
    119      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
    120      1.1  christos 		{ "1234567",                       342391,	8,	NULL,
    121      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
    122      1.1  christos 		{ "01234567",                      342391,	0,	NULL,
    123      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
    124      1.1  christos 		{ "0123456789",                 123456789,	10,	NULL,
    125      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
    126      1.1  christos 		{ "0x75bcd15",                  123456789,	0,	NULL,
    127      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
    128      1.1  christos 	};
    129      1.1  christos 
    130      1.1  christos 	intmax_t rv;
    131      1.1  christos 	char *end;
    132      1.1  christos 	int e;
    133      1.1  christos 	size_t i;
    134      1.1  christos 
    135      1.1  christos 	for (i = 0; i < __arraycount(t); i++) {
    136      1.1  christos 
    137      1.1  christos 		errno = 0;
    138      1.1  christos 		rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e);
    139      1.1  christos 
    140      1.1  christos 		if (errno != 0)
    141      1.1  christos 			atf_tc_fail("strtoi(3) changed errno to %d ('%s')",
    142      1.1  christos 			            e, strerror(e));
    143      1.1  christos 
    144      1.1  christos 		check(&t[i], rv, end, e);
    145      1.1  christos 	}
    146      1.1  christos }
    147      1.1  christos 
    148      1.1  christos ATF_TC(strtoi_case);
    149      1.1  christos ATF_TC_HEAD(strtoi_case, tc)
    150      1.1  christos {
    151      1.1  christos 	atf_tc_set_md_var(tc, "descr", "Case insensitivity with strtoi(3)");
    152      1.1  christos }
    153      1.1  christos 
    154      1.1  christos ATF_TC_BODY(strtoi_case, tc)
    155      1.1  christos {
    156      1.1  christos 	struct test t[] = {
    157      1.1  christos 		{ "abcd",	0xabcd,	16,	NULL,
    158      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
    159      1.1  christos 		{ "     dcba",	0xdcba,	16,	NULL,
    160      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
    161      1.1  christos 		{ "abcd dcba",	0xabcd,	16,	" dcba",
    162      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	ENOTSUP	},
    163      1.1  christos 		{ "abc0x123",	0xabc0, 16,	"x123",
    164      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	ENOTSUP	},
    165      1.1  christos 		{ "abcd\0x123",	0xabcd, 16,	"\0x123",
    166      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
    167      1.1  christos 		{ "ABCD",	0xabcd, 16,	NULL,
    168      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
    169      1.1  christos 		{ "aBcD",	0xabcd, 16,	NULL,
    170      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
    171      1.1  christos 		{ "0xABCD",	0xabcd, 16,	NULL,
    172      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0	},
    173      1.1  christos 		{ "0xABCDX",	0xabcd, 16,	"X",
    174      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	ENOTSUP},
    175      1.1  christos 	};
    176      1.1  christos 
    177      1.1  christos 	intmax_t rv;
    178      1.1  christos 	char *end;
    179      1.1  christos 	int e;
    180      1.1  christos 	size_t i;
    181      1.1  christos 
    182      1.1  christos 	for (i = 0; i < __arraycount(t); i++) {
    183      1.1  christos 
    184      1.1  christos 		errno = 0;
    185      1.1  christos 		rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e);
    186      1.1  christos 
    187      1.1  christos 		if (errno != 0)
    188      1.1  christos 			atf_tc_fail("strtoi(3) changed errno to %d ('%s')",
    189      1.1  christos 			            e, strerror(e));
    190      1.1  christos 
    191      1.1  christos 		check(&t[i], rv, end, e);
    192      1.1  christos 	}
    193      1.1  christos }
    194      1.1  christos 
    195      1.1  christos ATF_TC(strtoi_range);
    196      1.1  christos ATF_TC_HEAD(strtoi_range, tc)
    197      1.1  christos {
    198      1.1  christos 	atf_tc_set_md_var(tc, "descr", "Test ERANGE from strtoi(3)");
    199      1.1  christos }
    200      1.1  christos 
    201      1.1  christos ATF_TC_BODY(strtoi_range, tc)
    202      1.1  christos {
    203      1.1  christos 	struct test t[] = {
    204      1.1  christos #if INTMAX_MAX == 0x7fffffffffffffff
    205      1.1  christos 		{ "1000000000000000000000", INTMAX_MAX, 8, NULL,
    206      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	ERANGE },
    207      1.1  christos 		{ "9223372036854775808",    INTMAX_MAX, 10, NULL,
    208      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	ERANGE },
    209      1.1  christos 		{ "8000000000000000",       INTMAX_MAX, 16, NULL,
    210      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	ERANGE },
    211      1.1  christos #else
    212      1.1  christos #error extend this test to your platform!
    213      1.1  christos #endif
    214      1.1  christos 		{ "10",	1,	10,	NULL,
    215      1.1  christos 		  -1,	1,	ERANGE	},
    216      1.1  christos 		{ "10",	11,	10,	NULL,
    217      1.1  christos 		  11,	20,	ERANGE	},
    218      1.1  christos 	};
    219      1.1  christos 
    220      1.1  christos 	intmax_t rv;
    221      1.1  christos 	char *end;
    222      1.1  christos 	int e;
    223      1.1  christos 	size_t i;
    224      1.1  christos 
    225      1.1  christos 	for (i = 0; i < __arraycount(t); i++) {
    226      1.1  christos 
    227      1.1  christos 		errno = 0;
    228      1.1  christos 		rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e);
    229      1.1  christos 
    230      1.1  christos 		if (errno != 0)
    231      1.1  christos 			atf_tc_fail("strtoi(3) changed errno to %d ('%s')",
    232      1.1  christos 			            e, strerror(e));
    233      1.1  christos 
    234      1.1  christos 		check(&t[i], rv, end, e);
    235      1.1  christos 	}
    236      1.1  christos }
    237      1.1  christos 
    238      1.1  christos ATF_TC(strtoi_signed);
    239      1.1  christos ATF_TC_HEAD(strtoi_signed, tc)
    240      1.1  christos {
    241      1.1  christos 	atf_tc_set_md_var(tc, "descr", "A basic test of strtoi(3)");
    242      1.1  christos }
    243      1.1  christos 
    244      1.1  christos ATF_TC_BODY(strtoi_signed, tc)
    245      1.1  christos {
    246      1.1  christos 	struct test t[] = {
    247      1.1  christos 		{ "1",		 1, 0, NULL,
    248      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0 },
    249      1.1  christos 		{ " 2",		 2, 0, NULL,
    250      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0 },
    251      1.1  christos 		{ "  3",	 3, 0, NULL,
    252      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0 },
    253      1.1  christos 		{ " -3",	-3, 0, NULL,
    254      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0 },
    255      1.1  christos 		{ "--1",	 0, 0, "--1",
    256      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	ECANCELED },
    257      1.1  christos 		{ "+-2",	 0, 0, "+-2",
    258      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	ECANCELED },
    259      1.1  christos 		{ "++3",	 0, 0, "++3",
    260      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	ECANCELED },
    261      1.1  christos 		{ "+9",		 9, 0, NULL,
    262      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0 },
    263      1.1  christos 		{ "+123",      123, 0, NULL,
    264      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	0 },
    265      1.1  christos 		{ "-1 3",       -1, 0, " 3",
    266      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	ENOTSUP },
    267      1.1  christos 		{ "-1.3",       -1, 0, ".3",
    268      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	ENOTSUP },
    269      1.1  christos 		{ "-  3",        0, 0, "-  3",
    270      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	ECANCELED },
    271      1.1  christos 		{ "+33.",       33, 0, ".",
    272      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	ENOTSUP },
    273      1.1  christos 		{ "30x0",       30, 0, "x0",
    274      1.1  christos 		  INTMAX_MIN,	INTMAX_MAX,	ENOTSUP },
    275      1.1  christos 	};
    276      1.1  christos 
    277      1.1  christos 	intmax_t rv;
    278      1.1  christos 	char *end;
    279      1.1  christos 	int e;
    280      1.1  christos 	size_t i;
    281      1.1  christos 
    282      1.1  christos 	for (i = 0; i < __arraycount(t); i++) {
    283      1.1  christos 
    284      1.1  christos 		errno = 0;
    285      1.1  christos 		rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e);
    286      1.1  christos 
    287      1.1  christos 		if (errno != 0)
    288      1.1  christos 			atf_tc_fail("strtoi(3) changed errno to %d ('%s')",
    289      1.1  christos 			            e, strerror(e));
    290      1.1  christos 
    291      1.1  christos 		check(&t[i], rv, end, e);
    292      1.1  christos 	}
    293      1.1  christos }
    294      1.1  christos 
    295      1.1  christos ATF_TP_ADD_TCS(tp)
    296      1.1  christos {
    297      1.1  christos 
    298      1.1  christos 	ATF_TP_ADD_TC(tp, strtoi_base);
    299      1.1  christos 	ATF_TP_ADD_TC(tp, strtoi_case);
    300      1.1  christos 	ATF_TP_ADD_TC(tp, strtoi_range);
    301      1.1  christos 	ATF_TP_ADD_TC(tp, strtoi_signed);
    302      1.1  christos 
    303      1.1  christos 	return atf_no_error();
    304      1.1  christos }
    305