]> pd.if.org Git - pdclib/blob - functions/stdlib/strtoll.c
Initial checkin.
[pdclib] / functions / stdlib / strtoll.c
1 /* $Id$ */
2
3 /* Release $Name$ */
4
5 /* strtoll( const char *, char * *, int )
6
7    This file is part of the Public Domain C Library (PDCLib).
8    Permission is granted to use, modify, and / or redistribute at will.
9 */
10
11 #include <_PDCLIB_int.h>
12 #include <limits.h>
13 #include <stdlib.h>
14
15 #ifndef REGTEST
16
17 long long int strtoll( const char * s, char ** endptr, int base )
18 {
19     long long int rc;
20     char sign = '+';
21     const char * p = _PDCLIB_strtox_prelim( s, &sign, &base );
22     if ( sign == '+' )
23     {
24         rc = _PDCLIB_strtox_main( &p, base, LLONG_MAX, LLONG_MAX / base, LLONG_MAX % base, &sign );
25     }
26     else
27     {
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 );
30     }
31     if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s;
32     return ( sign == '+' ) ? rc : -rc;
33 }
34
35 #endif
36
37 #ifdef TEST
38 #include <_PDCLIB_test.h>
39 #include <errno.h>
40
41 int main()
42 {
43     char * endptr;
44     /* this, to base 36, overflows even a 256 bit integer */
45     char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
46     BEGIN_TESTS;
47     errno = 0;
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 */
67     errno = 0;
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 );
76     endptr = NULL;
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?                                           */
82     errno = 0;
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 );
89     errno = 0;
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 );
103     errno = 0;
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 */
111 #else
112 #error Unsupported width of 'long long' (neither 64 nor 128 bit).
113 #endif
114     return TEST_RESULTS;
115 }
116 #endif