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 /* FIXME: Sign error not caught by testdriver */
30 rc = _PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LLONG_MIN, (uintmax_t)( LLONG_MIN / -base ), (uintmax_t)( -( 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_";
54 /* basic functionality */
55 TESTCASE( strtoll( "123", NULL, 10 ) == 123 );
56 /* proper detecting of default base 10 */
57 TESTCASE( strtoll( "123", NULL, 0 ) == 123 );
58 /* proper functioning to smaller base */
59 TESTCASE( strtoll( "14", NULL, 8 ) == 12 );
60 /* proper autodetecting of octal */
61 TESTCASE( strtoll( "014", NULL, 0 ) == 12 );
62 /* proper autodetecting of hexadecimal, lowercase 'x' */
63 TESTCASE( strtoll( "0xFF", NULL, 0 ) == 255 );
64 /* proper autodetecting of hexadecimal, uppercase 'X' */
65 TESTCASE( strtoll( "0XFF", NULL, 0 ) == 255 );
66 /* errno should still be 0 */
67 TESTCASE( errno == 0 );
68 /* overflowing subject sequence must still return proper endptr */
69 TESTCASE( strtoll( overflow, &endptr, 36 ) == LLONG_MIN );
70 TESTCASE( errno == ERANGE );
71 TESTCASE( ( endptr - overflow ) == 53 );
72 /* same for positive */
74 TESTCASE( strtoll( overflow + 1, &endptr, 36 ) == LLONG_MAX );
75 TESTCASE( errno == ERANGE );
76 TESTCASE( ( endptr - overflow ) == 53 );
77 /* testing skipping of leading whitespace */
78 TESTCASE( strtoll( " \n\v\t\f123", NULL, 0 ) == 123 );
79 /* testing conversion failure */
80 TESTCASE( strtoll( overflow, &endptr, 10 ) == 0 );
81 TESTCASE( endptr == overflow );
83 TESTCASE( strtoll( overflow, &endptr, 0 ) == 0 );
84 TESTCASE( endptr == overflow );
85 /* These tests assume two-complement, but conversion should work for */
86 /* one-complement and signed magnitude just as well. Anyone having a */
87 /* platform to test this on? */
89 #if _PDCLIB_LLONG_BYTES == 8
90 /* testing "even" overflow, i.e. base is power of two */
91 TESTCASE( strtoll( "0x7FFFFFFFFFFFFFFF", NULL, 0 ) == 0x7fffffffffffffff );
92 TESTCASE( errno == 0 );
93 TESTCASE( strtoll( "0x8000000000000000", NULL, 0 ) == LLONG_MAX );
94 TESTCASE( errno == ERANGE );
96 TESTCASE( strtoll( "-0x7FFFFFFFFFFFFFFF", NULL, 0 ) == 0x8000000000000001 );
97 TESTCASE( errno == 0 );
98 TESTCASE( strtoll( "-0x8000000000000000", NULL, 0 ) == LLONG_MIN );
99 TESTCASE( errno == 0 );
100 TESTCASE( strtoll( "-0x8000000000000001", NULL, 0 ) == LLONG_MIN );
101 TESTCASE( errno == ERANGE );
102 /* TODO: test "odd" overflow, i.e. base is not power of two */
103 #elif _PDCLIB_LONG_BYTES == 16
104 /* testing "even" overflow, i.e. base is power of two */
105 TESTCASE( strtoll( "0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", NULL, 0 ) == 0x7fffffffffffffffffffffffffffffff );
106 TESTCASE( errno == 0 );
107 TESTCASE( strtoll( "0x80000000000000000000000000000000", NULL, 0 ) == LLONG_MAX );
108 TESTCASE( errno == ERANGE );
110 TESTCASE( strtoll( "-0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", NULL, 0 ) == -0x80000000000000000000000000000001 );
111 TESTCASE( errno == 0 );
112 TESTCASE( strtoll( "-0x80000000000000000000000000000000", NULL, 0 ) == LLONG_MIN );
113 TESTCASE( errno == 0 );
114 TESTCASE( strtoll( "-0x80000000000000000000000000000001", NULL, 0 ) == LLONG_MIN );
115 TESTCASE( errno == ERANGE );
116 /* TODO: test "odd" overflow, i.e. base is not power of two */
118 #error Unsupported width of 'long long' (neither 64 nor 128 bit).