]> pd.if.org Git - pdclib/blob - functions/stdlib/strtoul.c
Extended test drivers to cover sign error.
[pdclib] / functions / stdlib / strtoul.c
1 /* $Id$ */
2
3 /* strtoul( const char *, char * *, int )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <limits.h>
10 #include <stdlib.h>
11
12 #ifndef REGTEST
13
14 #include <stdint.h>
15
16 unsigned long int strtoul( const char * s, char ** endptr, int base )
17 {
18     unsigned long int rc;
19     char sign = '+';
20     const char * p = _PDCLIB_strtox_prelim( s, &sign, &base );
21     if ( base < 2 || base > 36 ) return 0;
22     rc = (unsigned long int)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)ULONG_MAX, (uintmax_t)( ULONG_MAX / base ), (int)( ULONG_MAX % base ), &sign );
23     if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s;
24     return ( sign == '+' ) ? rc : -rc;
25 }
26
27 #endif
28
29 #ifdef TEST
30 #include <_PDCLIB_test.h>
31 #include <errno.h>
32
33 int main( void )
34 {
35     char * endptr;
36     /* this, to base 36, overflows even a 256 bit integer */
37     char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
38     /* tricky border case */
39     char tricky[] = "+0xz";
40     errno = 0;
41     /* basic functionality */
42     TESTCASE( strtoul( "123", NULL, 10 ) == 123 );
43     /* proper detecting of default base 10 */
44     TESTCASE( strtoul( "456", NULL, 0 ) == 456 );
45     /* proper functioning to smaller base */
46     TESTCASE( strtoul( "14", NULL, 8 ) == 12 );
47     /* proper autodetecting of octal */
48     TESTCASE( strtoul( "016", NULL, 0 ) == 14 );
49     /* proper autodetecting of hexadecimal, lowercase 'x' */
50     TESTCASE( strtoul( "0xFF", NULL, 0 ) == 255 );
51     /* proper autodetecting of hexadecimal, uppercase 'X' */
52     TESTCASE( strtoul( "0Xa1", NULL, 0 ) == 161 );
53     /* proper handling of border case: 0x followed by non-hexdigit */
54     TESTCASE( strtoul( tricky, &endptr, 0 ) == 0 );
55     TESTCASE( endptr == tricky + 2 );
56     /* proper handling of border case: 0 followed by non-octdigit */
57     TESTCASE( strtoul( tricky, &endptr, 8 ) == 0 );
58     TESTCASE( endptr == tricky + 2 );
59     /* errno should still be 0 */
60     TESTCASE( errno == 0 );
61     /* overflowing subject sequence must still return proper endptr */
62     TESTCASE( strtoul( overflow, &endptr, 36 ) == ULONG_MAX );
63     TESTCASE( errno == ERANGE );
64     TESTCASE( ( endptr - overflow ) == 53 );
65     /* same for positive */
66     errno = 0;
67     TESTCASE( strtoul( overflow + 1, &endptr, 36 ) == ULONG_MAX );
68     TESTCASE( errno == ERANGE );
69     TESTCASE( ( endptr - overflow ) == 53 );
70     /* testing skipping of leading whitespace */
71     TESTCASE( strtoul( " \n\v\t\f789", NULL, 0 ) == 789 );
72     /* testing conversion failure */
73     TESTCASE( strtoul( overflow, &endptr, 10 ) == 0 );
74     TESTCASE( endptr == overflow );
75     endptr = NULL;
76     TESTCASE( strtoul( overflow, &endptr, 0 ) == 0 );
77     TESTCASE( endptr == overflow );
78     /* TODO: These tests assume two-complement, but conversion should work */
79     /* for one-complement and signed magnitude just as well. Anyone having */
80     /* a platform to test this on?                                         */
81     errno = 0;
82 #if ULONG_MAX == 0xffffffffL
83     /* testing "even" overflow, i.e. base is power of two */
84     TESTCASE( strtoul( "4294967295", NULL, 0 ) == ULONG_MAX );
85     TESTCASE( errno == 0 );
86     errno = 0;
87     TESTCASE( strtoul( "4294967296", NULL, 0 ) == ULONG_MAX );
88     TESTCASE( errno == ERANGE );
89     /* TODO: test "odd" overflow, i.e. base is not power of two */
90 #elif ULONG_MAX == 0xffffffffffffffffL
91     /* testing "even" overflow, i.e. base is power of two */
92     TESTCASE( strtoul( "18446744073709551615", NULL, 0 ) == ULONG_MAX );
93     TESTCASE( errno == 0 );
94     errno = 0;
95     TESTCASE( strtoul( "18446744073709551616", NULL, 0 ) == ULONG_MAX );
96     TESTCASE( errno == ERANGE );
97     /* TODO: test "odd" overflow, i.e. base is not power of two */
98 #else
99 #error Unsupported width of 'long' (neither 32 nor 64 bit).
100 #endif
101     return TEST_RESULTS;
102 }
103
104 #endif