summaryrefslogtreecommitdiff
path: root/test/stdlib/teststrtol.c
blob: 5b43a9b0c89c5d9ceeab3edba9cef9b75de17cfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

#include <stdlib.h>
#include <stdio.h>


const char *strings[]={
	/* some simple stuff */
	"0", "1", "10",
	"100", "1000", "10000", "100000", "1000000",
	"10000000", "100000000", "1000000000",

	/* negative */
	"-0", "-1", "-10",
	"-100", "-1000", "-10000", "-100000", "-1000000",
	"-10000000", "-100000000", "-1000000000",

	/* test base>10 */
	"a", "b", "f", "g", "z",

	/* test hex */
	"0x0", "0x1", "0xa", "0xf", "0x10",

	/* test octal */
	"00", "01", "07", "08", "0a", "010",

	/* other */
	"0x8000000",

	/* check overflow cases: (for 32 bit) */
	"2147483645",
	"2147483646",
	"2147483647",
	"2147483648",
	"2147483649",
	"-2147483645",
	"-2147483646",
	"-2147483647",
	"-2147483648",
	"-2147483649",
	"4294967293",
	"4294967294",
	"4294967295",
	"4294967296",
	"4294967297",
	"-4294967293",
	"-4294967294",
	"-4294967295",
	"-4294967296",
	"-4294967297",

	/* bad input tests */
	"",
	"00",
	"0x",
	"0x0",
	"-",
	"+",
	" ",
	" -",
	" - 0",
};
int n_tests=sizeof(strings)/sizeof(strings[0]);

void do_test(int base);
void do_utest(int base);

int main(int argc,char *argv[])
{
	do_test(0);
	do_test(8);
	do_test(10);
	do_test(16);
	do_test(36);

	do_utest(0);
	do_utest(8);
	do_utest(10);
	do_utest(16);
	do_utest(36);

	return 0;
}

void do_test(int base)
{
	int i;
	long n;
	char *endptr;

	for(i=0;i<n_tests;i++){
		n=strtol(strings[i],&endptr,base);
		printf("strtol(\"%s\",%d) len=%lu res=%ld\n",
			strings[i],base,(unsigned long)(endptr-strings[i]),n);
	}
}

void do_utest(int base)
{
	int i;
	unsigned long n;
	char *endptr;

	for(i=0;i<n_tests;i++){
		n=strtoul(strings[i],&endptr,base);
		printf("strtoul(\"%s\",%d) len=%lu res=%lu\n",
			strings[i],base,(unsigned long)(endptr-strings[i]),n);
	}
}