5 /* strtoull( 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 long int strtoull( const char * s, char ** endptr, int base )
18 unsigned long long int rc;
20 const char * p = _PDCLIB_strtox_prelim( s, &sign, &base );
21 rc = _PDCLIB_strtox_main( &p, base, ULLONG_MAX, ULLONG_MAX / base, ULLONG_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( strtoull( "123", NULL, 10 ) == 123 );
41 /* proper detecting of default base 10 */
42 TESTCASE( strtoull( "123", NULL, 0 ) == 123 );
43 /* proper functioning to smaller base */
44 TESTCASE( strtoull( "14", NULL, 8 ) == 12 );
45 /* proper autodetecting of octal */
46 TESTCASE( strtoull( "014", NULL, 0 ) == 12 );
47 /* proper autodetecting of hexadecimal, lowercase 'x' */
48 TESTCASE( strtoull( "0xFF", NULL, 0 ) == 255 );
49 /* proper autodetecting of hexadecimal, uppercase 'X' */
50 TESTCASE( strtoull( "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( strtoull( overflow, &endptr, 36 ) == ULLONG_MAX );
55 TESTCASE( errno == ERANGE );
56 TESTCASE( ( endptr - overflow ) == 53 );
57 /* same for positive */
59 TESTCASE( strtoull( overflow + 1, &endptr, 36 ) == ULLONG_MAX );
60 TESTCASE( errno == ERANGE );
61 TESTCASE( ( endptr - overflow ) == 53 );
62 /* testing skipping of leading whitespace */
63 TESTCASE( strtoull( " \n\v\t\f123", NULL, 0 ) == 123 );
64 /* testing conversion failure */
65 TESTCASE( strtoull( overflow, &endptr, 10 ) == 0 );
66 TESTCASE( endptr == overflow );
68 TESTCASE( strtoull( overflow, &endptr, 0 ) == 0 );
69 TESTCASE( endptr == overflow );