5 /* strtoul( const char *, char * *, int )
7 This file is part of the Public Domain C Library (PDCLib).
8 Permission is granted to use, modify, and / or redistribute at will.
16 unsigned long int strtoul( const char * s, char ** endptr, int base )
20 const char * p = _PDCLIB_strtox_prelim( s, &sign, &base );
21 rc = _PDCLIB_strtox_main( &p, base, ULONG_MAX, ULONG_MAX / base, ULONG_MAX % base, &sign );
22 if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s;
23 return ( sign == '+' ) ? rc : -rc;
29 #include <_PDCLIB_test.h>
35 /* this, to base 36, overflows even a 256 bit integer */
36 char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
39 /* basic functionality */
40 TESTCASE( strtoul( "123", NULL, 10 ) == 123 );
41 /* proper detecting of default base 10 */
42 TESTCASE( strtoul( "123", NULL, 0 ) == 123 );
43 /* proper functioning to smaller base */
44 TESTCASE( strtoul( "14", NULL, 8 ) == 12 );
45 /* proper autodetecting of octal */
46 TESTCASE( strtoul( "014", NULL, 0 ) == 12 );
47 /* proper autodetecting of hexadecimal, lowercase 'x' */
48 TESTCASE( strtoul( "0xFF", NULL, 0 ) == 255 );
49 /* proper autodetecting of hexadecimal, uppercase 'X' */
50 TESTCASE( strtoul( "0XFF", NULL, 0 ) == 255 );
51 /* errno should still be 0 */
52 TESTCASE( errno == 0 );
53 /* overflowing subject sequence must still return proper endptr */
54 TESTCASE( strtoul( overflow, &endptr, 36 ) == ULONG_MAX );
55 TESTCASE( errno == ERANGE );
56 TESTCASE( ( endptr - overflow ) == 53 );
57 /* same for positive */
59 TESTCASE( strtoul( overflow + 1, &endptr, 36 ) == ULONG_MAX );
60 TESTCASE( errno == ERANGE );
61 TESTCASE( ( endptr - overflow ) == 53 );
62 /* testing skipping of leading whitespace */
63 TESTCASE( strtoul( " \n\v\t\f123", NULL, 0 ) == 123 );
64 /* testing conversion failure */
65 TESTCASE( strtoul( overflow, &endptr, 10 ) == 0 );
66 TESTCASE( endptr == overflow );
68 TESTCASE( strtoul( overflow, &endptr, 0 ) == 0 );
69 TESTCASE( endptr == overflow );