xref: /openbsd-src/regress/lib/libc/strtol/strtoltest.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: strtoltest.c,v 1.1 2012/11/18 04:11:09 jsing Exp $	*/
2 /*
3  * Copyright (c) 2012 Joel Sing <jsing@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <errno.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 
23 struct strtol_test {
24 	char	*input;
25 	long	output;
26 	char	end;
27 	int	base;
28 	int	err;
29 };
30 
31 struct strtol_test strtol_tests[] = {
32 	{"1234567890",	1234567890L,	'\0',	0,	0},
33 	{"0755",	493L,		'\0',	0,	0},
34 	{"0xdeadbeef",	3735928559L,	'\0',	0,	0},
35 	{"1234567890",	0L,		'1',	1,	EINVAL},
36 	{"10101010",	170L,		'\0',	2,	0},
37 	{"755",		493L,		'\0',	8,	0},
38 	{"1234567890",	1234567890L,	'\0',	10,	0},
39 	{"abc",		0L,		'a',	10,	0},
40 	{"123xyz",	123L,		'x',	10,	0},
41 	{"deadbeef",	3735928559L,	'\0',	16,	0},
42 	{"DEADBEEF",	3735928559L,	'\0',	16,	0},
43 	{"deadzbeef",	57005L,		'z',	16,	0},
44 	{"zzz",		46655L,		'\0',	36,	0},
45 	{"1234567890",	0L,		'1',	37, 	EINVAL},
46 	{"1234567890",	0L,		'1',	123,	EINVAL},
47 };
48 
49 int
50 main(int argc, char **argv)
51 {
52 	struct strtol_test *test;
53 	int failure = 0;
54 	char *end;
55 	u_int i;
56 	long n;
57 
58 	for (i = 0; i < (sizeof(strtol_tests) / sizeof(strtol_tests[0])); i++) {
59 		test = &strtol_tests[i];
60 		errno = 0;
61 		n = strtol(test->input, &end, test->base);
62 		if (n != test->output) {
63 			fprintf(stderr, "TEST %i FAILED: %s base %i: %li\n",
64 			    i, test->input, test->base, n);
65 			failure = 1;
66 		} else if (*end != test->end) {
67 			fprintf(stderr, "TEST %i FAILED: end is not %c: %c\n",
68 			    i, test->end, *end);
69 			failure = 1;
70 		} else if (errno != test->err) {
71 			fprintf(stderr, "TEST %i FAILED: errno is not %i: %i\n",
72 			    i, test->err, errno);
73 			failure = 1;
74 		}
75 	}
76 
77 	return failure;
78 }
79