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