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 = _PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LLONG_MAX, (uintmax_t)( LLONG_MAX / base ), (uintmax_t)( LLONG_MAX % base ), &sign );
28 /* FIXME: This breaks on some machines that round negatives wrongly */
29 rc = _PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LLONG_MIN, (uintmax_t)( LLONG_MIN / -base ), (uintmax_t)( -( LLONG_MIN % base ) ), &sign );
31 if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s;
32 return ( sign == '+' ) ? rc : -rc;
38 #include <_PDCLIB_test.h>
42 #include <_PDCLIB_int.h>
50 /* this, to base 36, overflows even a 256 bit integer */
51 char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
53 /* basic functionality */
54 TESTCASE( strtoll( "123", NULL, 10 ) == 123 );
55 /* proper detecting of default base 10 */
56 TESTCASE( strtoll( "123", NULL, 0 ) == 123 );
57 /* proper functioning to smaller base */
58 TESTCASE( strtoll( "14", NULL, 8 ) == 12 );
59 /* proper autodetecting of octal */
60 TESTCASE( strtoll( "014", NULL, 0 ) == 12 );
61 /* proper autodetecting of hexadecimal, lowercase 'x' */
62 TESTCASE( strtoll( "0xFF", NULL, 0 ) == 255 );
63 /* proper autodetecting of hexadecimal, uppercase 'X' */
64 TESTCASE( strtoll( "0XFF", NULL, 0 ) == 255 );
65 /* errno should still be 0 */
66 TESTCASE( errno == 0 );
67 /* overflowing subject sequence must still return proper endptr */
68 TESTCASE( strtoll( overflow, &endptr, 36 ) == LLONG_MIN );
69 TESTCASE( errno == ERANGE );
70 TESTCASE( ( endptr - overflow ) == 53 );
71 /* same for positive */
73 TESTCASE( strtoll( overflow + 1, &endptr, 36 ) == LLONG_MAX );
74 TESTCASE( errno == ERANGE );
75 TESTCASE( ( endptr - overflow ) == 53 );
76 /* testing skipping of leading whitespace */
77 TESTCASE( strtoll( " \n\v\t\f123", NULL, 0 ) == 123 );
78 /* testing conversion failure */
79 TESTCASE( strtoll( overflow, &endptr, 10 ) == 0 );
80 TESTCASE( endptr == overflow );
82 TESTCASE( strtoll( overflow, &endptr, 0 ) == 0 );
83 TESTCASE( endptr == overflow );
84 /* These tests assume two-complement, but conversion should work for */
85 /* one-complement and signed magnitude just as well. Anyone having a */
86 /* platform to test this on? */
88 #if _PDCLIB_LLONG_BYTES == 8
89 /* testing "even" overflow, i.e. base is power of two */
90 TESTCASE( strtoll( "0x7FFFFFFFFFFFFFFF", NULL, 0 ) == 0x7fffffffffffffff );
91 TESTCASE( errno == 0 );
92 TESTCASE( strtoll( "0x8000000000000000", NULL, 0 ) == LLONG_MAX );
93 TESTCASE( errno == ERANGE );
95 TESTCASE( strtoll( "-0x7FFFFFFFFFFFFFFF", NULL, 0 ) == 0x8000000000000001 );
96 TESTCASE( errno == 0 );
97 TESTCASE( strtoll( "-0x8000000000000000", NULL, 0 ) == LLONG_MIN );
98 TESTCASE( errno == 0 );
99 TESTCASE( strtoll( "-0x8000000000000001", NULL, 0 ) == LLONG_MIN );
100 TESTCASE( errno == ERANGE );
101 /* TODO: test "odd" overflow, i.e. base is not power of two */
102 #elif _PDCLIB_LONG_BYTES == 16
103 /* testing "even" overflow, i.e. base is power of two */
104 TESTCASE( strtoll( "0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", NULL, 0 ) == 0x7fffffffffffffffffffffffffffffff );
105 TESTCASE( errno == 0 );
106 TESTCASE( strtoll( "0x80000000000000000000000000000000", NULL, 0 ) == LLONG_MAX );
107 TESTCASE( errno == ERANGE );
109 TESTCASE( strtoll( "-0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", NULL, 0 ) == -0x80000000000000000000000000000001 );
110 TESTCASE( errno == 0 );
111 TESTCASE( strtoll( "-0x80000000000000000000000000000000", NULL, 0 ) == LLONG_MIN );
112 TESTCASE( errno == 0 );
113 TESTCASE( strtoll( "-0x80000000000000000000000000000001", NULL, 0 ) == LLONG_MIN );
114 TESTCASE( errno == ERANGE );
115 /* TODO: test "odd" overflow, i.e. base is not power of two */
117 #error Unsupported width of 'long long' (neither 64 nor 128 bit).