]> pd.if.org Git - pdclib/blob - functions/stdlib/strtoll.c
Cosmetic comment fixes.
[pdclib] / functions / stdlib / strtoll.c
1 /* strtoll( const char *, char * *, int )
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <limits.h>
8 #include <stdlib.h>
9
10 #ifndef REGTEST
11
12 #include <stdint.h>
13
14 long long int strtoll( const char * s, char ** endptr, int base )
15 {
16     long long int rc;
17     char sign = '+';
18     const char * p = _PDCLIB_strtox_prelim( s, &sign, &base );
19     if ( base < 2 || base > 36 ) return 0;
20     if ( sign == '+' )
21     {
22         rc = (long long int)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LLONG_MAX, (uintmax_t)( LLONG_MAX / base ), (int)( LLONG_MAX % base ), &sign );
23     }
24     else
25     {
26         rc = (long long int)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LLONG_MIN, (uintmax_t)( LLONG_MIN / -base ), (int)( -( LLONG_MIN % base ) ), &sign );
27     }
28     if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s;
29     return ( sign == '+' ) ? rc : -rc;
30 }
31
32 #endif
33
34 #ifdef TEST
35 #include "_PDCLIB_test.h"
36
37 #include <errno.h>
38
39 int main( void )
40 {
41     char * endptr;
42     /* this, to base 36, overflows even a 256 bit integer */
43     char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
44     /* tricky border case */
45     char tricky[] = "+0xz";
46     errno = 0;
47     /* basic functionality */
48     TESTCASE( strtoll( "123", NULL, 10 ) == 123 );
49     /* proper detecting of default base 10 */
50     TESTCASE( strtoll( "456", NULL, 0 ) == 456 );
51     /* proper functioning to smaller base */
52     TESTCASE( strtoll( "14", NULL, 8 ) == 12 );
53     /* proper autodetecting of octal */
54     TESTCASE( strtoll( "016", NULL, 0 ) == 14 );
55     /* proper autodetecting of hexadecimal, lowercase 'x' */
56     TESTCASE( strtoll( "0xFF", NULL, 0 ) == 255 );
57     /* proper autodetecting of hexadecimal, uppercase 'X' */
58     TESTCASE( strtoll( "0Xa1", NULL, 0 ) == 161 );
59     /* proper handling of border case: 0x followed by non-hexdigit */
60     TESTCASE( strtoll( tricky, &endptr, 0 ) == 0 );
61     TESTCASE( endptr == tricky + 2 );
62     /* proper handling of border case: 0 followed by non-octdigit */
63     TESTCASE( strtoll( tricky, &endptr, 8 ) == 0 );
64     TESTCASE( endptr == tricky + 2 );
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 */
72     errno = 0;
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\f789", NULL, 0 ) == 789 );
78     /* testing conversion failure */
79     TESTCASE( strtoll( overflow, &endptr, 10 ) == 0 );
80     TESTCASE( endptr == overflow );
81     endptr = NULL;
82     TESTCASE( strtoll( overflow, &endptr, 0 ) == 0 );
83     TESTCASE( endptr == overflow );
84     /* TODO: These tests assume two-complement, but conversion should work */
85     /* for one-complement and signed magnitude just as well. Anyone having */
86     /* a platform to test this on?                                         */
87     errno = 0;
88 #if LLONG_MAX >> 62 == 1
89     /* testing "even" overflow, i.e. base is power of two */
90     TESTCASE( strtoll( "9223372036854775807", NULL, 0 ) == 0x7fffffffffffffff );
91     TESTCASE( errno == 0 );
92     TESTCASE( strtoll( "9223372036854775808", NULL, 0 ) == LLONG_MAX );
93     TESTCASE( errno == ERANGE );
94     errno = 0;
95     TESTCASE( strtoll( "-9223372036854775807", NULL, 0 ) == (long long)0x8000000000000001 );
96     TESTCASE( errno == 0 );
97     TESTCASE( strtoll( "-9223372036854775808", NULL, 0 ) == LLONG_MIN );
98     TESTCASE( errno == 0 );
99     TESTCASE( strtoll( "-9223372036854775809", NULL, 0 ) == LLONG_MIN );
100     TESTCASE( errno == ERANGE );
101     /* TODO: test "odd" overflow, i.e. base is not power of two */
102 #elif LLONG_MAX >> 126 == 1
103     /* testing "even" overflow, i.e. base is power of two */
104     TESTCASE( strtoll( "170141183460469231731687303715884105728", NULL, 0 ) == 0x7fffffffffffffffffffffffffffffff );
105     TESTCASE( errno == 0 );
106     TESTCASE( strtoll( "170141183460469231731687303715884105729", NULL, 0 ) == LLONG_MAX );
107     TESTCASE( errno == ERANGE );
108     errno = 0;
109     TESTCASE( strtoll( "-170141183460469231731687303715884105728", NULL, 0 ) == -0x80000000000000000000000000000001 );
110     TESTCASE( errno == 0 );
111     TESTCASE( strtoll( "-170141183460469231731687303715884105729", NULL, 0 ) == LLONG_MIN );
112     TESTCASE( errno == 0 );
113     TESTCASE( strtoll( "-170141183460469231731687303715884105730", NULL, 0 ) == LLONG_MIN );
114     TESTCASE( errno == ERANGE );
115     /* TODO: test "odd" overflow, i.e. base is not power of two */
116 #else
117 #error Unsupported width of 'long long' (neither 64 nor 128 bit).
118 #endif
119     return TEST_RESULTS;
120 }
121
122 #endif