3 /* strtoull( const char *, char * *, int )
5 This file is part of the Public Domain C Library (PDCLib).
6 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 if ( base < 2 || base > 36 ) return 0;
22 rc = _PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)ULLONG_MAX, (uintmax_t)( ULLONG_MAX / base ), (int)( ULLONG_MAX % base ), &sign );
23 if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s;
24 return ( sign == '+' ) ? rc : -rc;
30 #include <_PDCLIB_test.h>
36 /* this, to base 36, overflows even a 256 bit integer */
37 char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
38 /* tricky border case */
39 char tricky[] = "+0xz";
41 /* basic functionality */
42 TESTCASE( strtoull( "123", NULL, 10 ) == 123 );
43 /* proper detecting of default base 10 */
44 TESTCASE( strtoull( "456", NULL, 0 ) == 456 );
45 /* proper functioning to smaller base */
46 TESTCASE( strtoull( "14", NULL, 8 ) == 12 );
47 /* proper autodetecting of octal */
48 TESTCASE( strtoull( "016", NULL, 0 ) == 14 );
49 /* proper autodetecting of hexadecimal, lowercase 'x' */
50 TESTCASE( strtoull( "0xFF", NULL, 0 ) == 255 );
51 /* proper autodetecting of hexadecimal, uppercase 'X' */
52 TESTCASE( strtoull( "0Xa1", NULL, 0 ) == 161 );
53 /* proper handling of border case: 0x followed by non-hexdigit */
54 TESTCASE( strtoull( tricky, &endptr, 0 ) == 0 );
55 TESTCASE( endptr == tricky + 2 );
56 /* proper handling of border case: 0 followed by non-octdigit */
57 TESTCASE( strtoull( 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( strtoull( overflow, &endptr, 36 ) == ULLONG_MAX );
63 TESTCASE( errno == ERANGE );
64 TESTCASE( ( endptr - overflow ) == 53 );
65 /* same for positive */
67 TESTCASE( strtoull( overflow + 1, &endptr, 36 ) == ULLONG_MAX );
68 TESTCASE( errno == ERANGE );
69 TESTCASE( ( endptr - overflow ) == 53 );
70 /* testing skipping of leading whitespace */
71 TESTCASE( strtoull( " \n\v\t\f789", NULL, 0 ) == 789 );
72 /* testing conversion failure */
73 TESTCASE( strtoull( overflow, &endptr, 10 ) == 0 );
74 TESTCASE( endptr == overflow );
76 TESTCASE( strtoull( overflow, &endptr, 0 ) == 0 );
77 TESTCASE( endptr == overflow );
79 #if ULLONG_MAX == 0xffffffffffffffffLL
80 /* testing "even" overflow, i.e. base is power of two */
81 TESTCASE( strtoull( "18446744073709551615", NULL, 0 ) == ULLONG_MAX );
82 TESTCASE( errno == 0 );
83 TESTCASE( strtoull( "18446744073709551616", NULL, 0 ) == ULLONG_MAX );
84 TESTCASE( errno == ERANGE );
85 /* TODO: test "odd" overflow, i.e. base is not power of two */
86 #elif ULLONG_MAX == 0xffffffffffffffffffffffffffffffffLL
87 /* testing "even" overflow, i.e. base is power of two */
88 TESTCASE( strtoull( "340282366920938463463374607431768211455", NULL, 0 ) == ULLONG_MAX );
89 TESTCASE( errno == 0 );
90 TESTCASE( strtoull( "340282366920938463463374607431768211456", NULL, 0 ) == ULLONG_MAX );
91 TESTCASE( errno == ERANGE );
92 /* TODO: test "odd" overflow, i.e. base is not power of two */
94 #error Unsupported width of 'long long' (neither 64 nor 128 bit).