1 /* strtoul( const char *, char * *, int )
3 This file is part of the Public Domain C Library (PDCLib).
4 Permission is granted to use, modify, and / or redistribute at will.
14 unsigned long int strtoul( const char * s, char ** endptr, int base )
18 const char * p = _PDCLIB_strtox_prelim( s, &sign, &base );
19 if ( base < 2 || base > 36 ) return 0;
20 rc = (unsigned long int)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)ULONG_MAX, (uintmax_t)( ULONG_MAX / base ), (int)( ULONG_MAX % base ), &sign );
21 if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s;
22 return ( sign == '+' ) ? rc : -rc;
28 #include "_PDCLIB_test.h"
34 /* this, to base 36, overflows even a 256 bit integer */
35 char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
36 /* tricky border case */
37 char tricky[] = "+0xz";
39 /* basic functionality */
40 TESTCASE( strtoul( "123", NULL, 10 ) == 123 );
41 /* proper detecting of default base 10 */
42 TESTCASE( strtoul( "456", NULL, 0 ) == 456 );
43 /* proper functioning to smaller base */
44 TESTCASE( strtoul( "14", NULL, 8 ) == 12 );
45 /* proper autodetecting of octal */
46 TESTCASE( strtoul( "016", NULL, 0 ) == 14 );
47 /* proper autodetecting of hexadecimal, lowercase 'x' */
48 TESTCASE( strtoul( "0xFF", NULL, 0 ) == 255 );
49 /* proper autodetecting of hexadecimal, uppercase 'X' */
50 TESTCASE( strtoul( "0Xa1", NULL, 0 ) == 161 );
51 /* proper handling of border case: 0x followed by non-hexdigit */
52 TESTCASE( strtoul( tricky, &endptr, 0 ) == 0 );
53 TESTCASE( endptr == tricky + 2 );
54 /* proper handling of border case: 0 followed by non-octdigit */
55 TESTCASE( strtoul( tricky, &endptr, 8 ) == 0 );
56 TESTCASE( endptr == tricky + 2 );
57 /* errno should still be 0 */
58 TESTCASE( errno == 0 );
59 /* overflowing subject sequence must still return proper endptr */
60 TESTCASE( strtoul( overflow, &endptr, 36 ) == ULONG_MAX );
61 TESTCASE( errno == ERANGE );
62 TESTCASE( ( endptr - overflow ) == 53 );
63 /* same for positive */
65 TESTCASE( strtoul( overflow + 1, &endptr, 36 ) == ULONG_MAX );
66 TESTCASE( errno == ERANGE );
67 TESTCASE( ( endptr - overflow ) == 53 );
68 /* testing skipping of leading whitespace */
69 TESTCASE( strtoul( " \n\v\t\f789", NULL, 0 ) == 789 );
70 /* testing conversion failure */
71 TESTCASE( strtoul( overflow, &endptr, 10 ) == 0 );
72 TESTCASE( endptr == overflow );
74 TESTCASE( strtoul( overflow, &endptr, 0 ) == 0 );
75 TESTCASE( endptr == overflow );
76 /* TODO: These tests assume two-complement, but conversion should work */
77 /* for one-complement and signed magnitude just as well. Anyone having */
78 /* a platform to test this on? */
81 #if ULONG_MAX >> 31 == 1
82 /* testing "even" overflow, i.e. base is power of two */
83 TESTCASE( strtoul( "4294967295", NULL, 0 ) == ULONG_MAX );
84 TESTCASE( errno == 0 );
86 TESTCASE( strtoul( "4294967296", NULL, 0 ) == ULONG_MAX );
87 TESTCASE( errno == ERANGE );
88 /* TODO: test "odd" overflow, i.e. base is not power of two */
90 #elif ULONG_MAX >> 63 == 1
91 /* testing "even" overflow, i.e. base is power of two */
92 TESTCASE( strtoul( "18446744073709551615", NULL, 0 ) == ULONG_MAX );
93 TESTCASE( 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 */
99 #error Unsupported width of 'long' (neither 32 nor 64 bit).