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>
43 #include <_PDCLIB_int.h>
51 /* this, to base 36, overflows even a 256 bit integer */
52 char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
53 /* tricky border case */
54 char tricky[] = "+0xz";
56 /* basic functionality */
57 TESTCASE( strtoll( "123", NULL, 10 ) == 123 );
58 /* proper detecting of default base 10 */
59 TESTCASE( strtoll( "456", NULL, 0 ) == 456 );
60 /* proper functioning to smaller base */
61 TESTCASE( strtoll( "14", NULL, 8 ) == 12 );
62 /* proper autodetecting of octal */
63 TESTCASE( strtoll( "016", NULL, 0 ) == 14 );
64 /* proper autodetecting of hexadecimal, lowercase 'x' */
65 TESTCASE( strtoll( "0xFF", NULL, 0 ) == 255 );
66 /* proper autodetecting of hexadecimal, uppercase 'X' */
67 TESTCASE( strtoll( "0Xa1", NULL, 0 ) == 161 );
68 /* proper handling of border case: 0x followed by non-hexdigit */
69 TESTCASE( strtoll( tricky, &endptr, 0 ) == 0 );
70 TESTCASE( endptr == tricky + 2 );
71 /* proper handling of border case: 0 followed by non-octdigit */
72 TESTCASE( strtoll( tricky, &endptr, 8 ) == 0 );
73 TESTCASE( endptr == tricky + 2 );
74 /* errno should still be 0 */
75 TESTCASE( errno == 0 );
76 /* overflowing subject sequence must still return proper endptr */
77 TESTCASE( strtoll( overflow, &endptr, 36 ) == LLONG_MIN );
78 TESTCASE( errno == ERANGE );
79 TESTCASE( ( endptr - overflow ) == 53 );
80 /* same for positive */
82 TESTCASE( strtoll( overflow + 1, &endptr, 36 ) == LLONG_MAX );
83 TESTCASE( errno == ERANGE );
84 TESTCASE( ( endptr - overflow ) == 53 );
85 /* testing skipping of leading whitespace */
86 TESTCASE( strtoll( " \n\v\t\f789", NULL, 0 ) == 789 );
87 /* testing conversion failure */
88 TESTCASE( strtoll( overflow, &endptr, 10 ) == 0 );
89 TESTCASE( endptr == overflow );
91 TESTCASE( strtoll( overflow, &endptr, 0 ) == 0 );
92 TESTCASE( endptr == overflow );
93 /* These tests assume two-complement, but conversion should work for */
94 /* one-complement and signed magnitude just as well. Anyone having a */
95 /* platform to test this on? */
97 #if _PDCLIB_LLONG_BYTES == 8
98 /* testing "even" overflow, i.e. base is power of two */
99 TESTCASE( strtoll( "0x7FFFFFFFFFFFFFFF", NULL, 0 ) == 0x7fffffffffffffff );
100 TESTCASE( errno == 0 );
101 TESTCASE( strtoll( "0x8000000000000000", NULL, 0 ) == LLONG_MAX );
102 TESTCASE( errno == ERANGE );
104 TESTCASE( strtoll( "-0x7FFFFFFFFFFFFFFF", NULL, 0 ) == (long long)0x8000000000000001 );
105 TESTCASE( errno == 0 );
106 TESTCASE( strtoll( "-0x8000000000000000", NULL, 0 ) == LLONG_MIN );
107 TESTCASE( errno == 0 );
108 TESTCASE( strtoll( "-0x8000000000000001", NULL, 0 ) == LLONG_MIN );
109 TESTCASE( errno == ERANGE );
110 /* TODO: test "odd" overflow, i.e. base is not power of two */
111 #elif _PDCLIB_LONG_BYTES == 16
112 /* testing "even" overflow, i.e. base is power of two */
113 TESTCASE( strtoll( "0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", NULL, 0 ) == 0x7fffffffffffffffffffffffffffffff );
114 TESTCASE( errno == 0 );
115 TESTCASE( strtoll( "0x80000000000000000000000000000000", NULL, 0 ) == LLONG_MAX );
116 TESTCASE( errno == ERANGE );
118 TESTCASE( strtoll( "-0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", NULL, 0 ) == -0x80000000000000000000000000000001 );
119 TESTCASE( errno == 0 );
120 TESTCASE( strtoll( "-0x80000000000000000000000000000000", NULL, 0 ) == LLONG_MIN );
121 TESTCASE( errno == 0 );
122 TESTCASE( strtoll( "-0x80000000000000000000000000000001", NULL, 0 ) == LLONG_MIN );
123 TESTCASE( errno == ERANGE );
124 /* TODO: test "odd" overflow, i.e. base is not power of two */
126 #error Unsupported width of 'long long' (neither 64 nor 128 bit).