]> pd.if.org Git - pdclib/blob - functions/stdlib/strtoll.c
Caught "0xz" corner case, and improved testing.
[pdclib] / functions / stdlib / strtoll.c
1 /* $Id$ */
2
3 /* strtoll( const char *, char * *, int )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <limits.h>
10 #include <stdlib.h>
11
12 #ifndef REGTEST
13
14 #include <stdint.h>
15
16 long long int strtoll( const char * s, char ** endptr, int base )
17 {
18     long long int rc;
19     char sign = '+';
20     const char * p = _PDCLIB_strtox_prelim( s, &sign, &base );
21     if ( base < 2 || base > 36 ) return 0;
22     if ( sign == '+' )
23     {
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 );
25     }
26     else
27     {
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 );
31     }
32     if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s;
33     return ( sign == '+' ) ? rc : -rc;
34 }
35
36 #endif
37
38 #ifdef TEST
39 #include <_PDCLIB_test.h>
40
41 #ifndef _PDCLIB_INT_H
42 #define _PDCLIB_INT_H
43 #include <_PDCLIB_int.h>
44 #endif
45
46 #include <errno.h>
47
48 int main( void )
49 {
50     char * endptr;
51     /* this, to base 36, overflows even a 256 bit integer */
52     char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
53     /* tricky border case */
54     char tricky[] = "+0xz";
55     errno = 0;
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 */
81     errno = 0;
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 );
90     endptr = NULL;
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?                                           */
96     errno = 0;
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 );
103     errno = 0;
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 );
117     errno = 0;
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 */
125 #else
126 #error Unsupported width of 'long long' (neither 64 nor 128 bit).
127 #endif
128     return TEST_RESULTS;
129 }
130
131 #endif