3 /* strtoll( 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 long long int strtoll( const char * s, char ** endptr, int base )
20 const char * p = _PDCLIB_strtox_prelim( s, &sign, &base );
21 if ( base < 2 || base > 36 ) return 0;
24 rc = (long long int)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LLONG_MAX, (uintmax_t)( LLONG_MAX / base ), (int)( LLONG_MAX % base ), &sign );
28 /* FIXME: This breaks on some machines that round negatives wrongly */
29 /* FIXME: Sign error not caught by testdriver */
30 rc = (long long int)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LLONG_MIN, (uintmax_t)( LLONG_MIN / -base ), (int)( -( LLONG_MIN % base ) ), &sign );
32 if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s;
33 return ( sign == '+' ) ? rc : -rc;
39 #include <_PDCLIB_test.h>
46 /* this, to base 36, overflows even a 256 bit integer */
47 char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
48 /* tricky border case */
49 char tricky[] = "+0xz";
51 /* basic functionality */
52 TESTCASE( strtoll( "123", NULL, 10 ) == 123 );
53 /* proper detecting of default base 10 */
54 TESTCASE( strtoll( "456", NULL, 0 ) == 456 );
55 /* proper functioning to smaller base */
56 TESTCASE( strtoll( "14", NULL, 8 ) == 12 );
57 /* proper autodetecting of octal */
58 TESTCASE( strtoll( "016", NULL, 0 ) == 14 );
59 /* proper autodetecting of hexadecimal, lowercase 'x' */
60 TESTCASE( strtoll( "0xFF", NULL, 0 ) == 255 );
61 /* proper autodetecting of hexadecimal, uppercase 'X' */
62 TESTCASE( strtoll( "0Xa1", NULL, 0 ) == 161 );
63 /* proper handling of border case: 0x followed by non-hexdigit */
64 TESTCASE( strtoll( tricky, &endptr, 0 ) == 0 );
65 TESTCASE( endptr == tricky + 2 );
66 /* proper handling of border case: 0 followed by non-octdigit */
67 TESTCASE( strtoll( tricky, &endptr, 8 ) == 0 );
68 TESTCASE( endptr == tricky + 2 );
69 /* errno should still be 0 */
70 TESTCASE( errno == 0 );
71 /* overflowing subject sequence must still return proper endptr */
72 TESTCASE( strtoll( overflow, &endptr, 36 ) == LLONG_MIN );
73 TESTCASE( errno == ERANGE );
74 TESTCASE( ( endptr - overflow ) == 53 );
75 /* same for positive */
77 TESTCASE( strtoll( overflow + 1, &endptr, 36 ) == LLONG_MAX );
78 TESTCASE( errno == ERANGE );
79 TESTCASE( ( endptr - overflow ) == 53 );
80 /* testing skipping of leading whitespace */
81 TESTCASE( strtoll( " \n\v\t\f789", NULL, 0 ) == 789 );
82 /* testing conversion failure */
83 TESTCASE( strtoll( overflow, &endptr, 10 ) == 0 );
84 TESTCASE( endptr == overflow );
86 TESTCASE( strtoll( overflow, &endptr, 0 ) == 0 );
87 TESTCASE( endptr == overflow );
88 /* These tests assume two-complement, but conversion should work for */
89 /* one-complement and signed magnitude just as well. Anyone having a */
90 /* platform to test this on? */
92 #if LLONG_MAX == 0x7fffffffffffffffLL
93 /* testing "even" overflow, i.e. base is power of two */
94 TESTCASE( strtoll( "0x7FFFFFFFFFFFFFFF", NULL, 0 ) == 0x7fffffffffffffff );
95 TESTCASE( errno == 0 );
96 TESTCASE( strtoll( "0x8000000000000000", NULL, 0 ) == LLONG_MAX );
97 TESTCASE( errno == ERANGE );
99 TESTCASE( strtoll( "-0x7FFFFFFFFFFFFFFF", NULL, 0 ) == (long long)0x8000000000000001 );
100 TESTCASE( errno == 0 );
101 TESTCASE( strtoll( "-0x8000000000000000", NULL, 0 ) == LLONG_MIN );
102 TESTCASE( errno == 0 );
103 TESTCASE( strtoll( "-0x8000000000000001", NULL, 0 ) == LLONG_MIN );
104 TESTCASE( errno == ERANGE );
105 /* TODO: test "odd" overflow, i.e. base is not power of two */
106 #elif LLONG_MAX == 0x7fffffffffffffffffffffffffffffffLL
107 /* testing "even" overflow, i.e. base is power of two */
108 TESTCASE( strtoll( "0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", NULL, 0 ) == 0x7fffffffffffffffffffffffffffffff );
109 TESTCASE( errno == 0 );
110 TESTCASE( strtoll( "0x80000000000000000000000000000000", NULL, 0 ) == LLONG_MAX );
111 TESTCASE( errno == ERANGE );
113 TESTCASE( strtoll( "-0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", NULL, 0 ) == -0x80000000000000000000000000000001 );
114 TESTCASE( errno == 0 );
115 TESTCASE( strtoll( "-0x80000000000000000000000000000000", NULL, 0 ) == LLONG_MIN );
116 TESTCASE( errno == 0 );
117 TESTCASE( strtoll( "-0x80000000000000000000000000000001", NULL, 0 ) == LLONG_MIN );
118 TESTCASE( errno == ERANGE );
119 /* TODO: test "odd" overflow, i.e. base is not power of two */
121 #error Unsupported width of 'long long' (neither 64 nor 128 bit).