]> pd.if.org Git - pdclib/blob - functions/stdlib/strtoll.c
Added size_t to stdlib.h, added redefinition guards, adjusted includes.
[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 <limits.h>
12 #include <stdlib.h>
13
14 #ifndef REGTEST
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 ( sign == '+' )
22     {
23         rc = _PDCLIB_strtox_main( &p, base, LLONG_MAX, LLONG_MAX / base, LLONG_MAX % base, &sign );
24     }
25     else
26     {
27         /* FIXME: This breaks on some machines that round negatives wrongly */
28         rc = _PDCLIB_strtox_main( &p, base, LLONG_MIN, LLONG_MIN / -base, -( LLONG_MIN % base ), &sign );
29     }
30     if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s;
31     return ( sign == '+' ) ? rc : -rc;
32 }
33
34 #endif
35
36 #ifdef TEST
37 #include <_PDCLIB_test.h>
38
39 #ifndef _PDCLIB_INT_H
40 #define _PDCLIB_INT_H
41 #include <_PDCLIB_int.h>
42 #endif
43
44 #include <errno.h>
45
46 int main()
47 {
48     char * endptr;
49     /* this, to base 36, overflows even a 256 bit integer */
50     char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
51     BEGIN_TESTS;
52     errno = 0;
53     /* basic functionality */
54     TESTCASE( strtoll( "123", NULL, 10 ) == 123 );
55     /* proper detecting of default base 10 */
56     TESTCASE( strtoll( "123", NULL, 0 ) == 123 );
57     /* proper functioning to smaller base */
58     TESTCASE( strtoll( "14", NULL, 8 ) == 12 );
59     /* proper autodetecting of octal */
60     TESTCASE( strtoll( "014", NULL, 0 ) == 12 );
61     /* proper autodetecting of hexadecimal, lowercase 'x' */
62     TESTCASE( strtoll( "0xFF", NULL, 0 ) == 255 );
63     /* proper autodetecting of hexadecimal, uppercase 'X' */
64     TESTCASE( strtoll( "0XFF", NULL, 0 ) == 255 );
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\f123", NULL, 0 ) == 123 );
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     /* These tests assume two-complement, but conversion should work for   */
85     /* one-complement and signed magnitude just as well. Anyone having a   */
86     /* platform to test this on?                                           */
87     errno = 0;
88 #if _PDCLIB_LLONG_BYTES == 8
89     /* testing "even" overflow, i.e. base is power of two */
90     TESTCASE( strtoll( "0x7FFFFFFFFFFFFFFF", NULL, 0 ) == 0x7fffffffffffffff );
91     TESTCASE( errno == 0 );
92     TESTCASE( strtoll( "0x8000000000000000", NULL, 0 ) == LLONG_MAX );
93     TESTCASE( errno == ERANGE );
94     errno = 0;
95     TESTCASE( strtoll( "-0x7FFFFFFFFFFFFFFF", NULL, 0 ) == 0x8000000000000001 );
96     TESTCASE( errno == 0 );
97     TESTCASE( strtoll( "-0x8000000000000000", NULL, 0 ) == LLONG_MIN );
98     TESTCASE( errno == 0 );
99     TESTCASE( strtoll( "-0x8000000000000001", NULL, 0 ) == LLONG_MIN );
100     TESTCASE( errno == ERANGE );
101     /* TODO: test "odd" overflow, i.e. base is not power of two */
102 #elif _PDCLIB_LONG_BYTES == 16
103     /* testing "even" overflow, i.e. base is power of two */
104     TESTCASE( strtoll( "0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", NULL, 0 ) == 0x7fffffffffffffffffffffffffffffff );
105     TESTCASE( errno == 0 );
106     TESTCASE( strtoll( "0x80000000000000000000000000000000", NULL, 0 ) == LLONG_MAX );
107     TESTCASE( errno == ERANGE );
108     errno = 0;
109     TESTCASE( strtoll( "-0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", NULL, 0 ) == -0x80000000000000000000000000000001 );
110     TESTCASE( errno == 0 );
111     TESTCASE( strtoll( "-0x80000000000000000000000000000000", NULL, 0 ) == LLONG_MIN );
112     TESTCASE( errno == 0 );
113     TESTCASE( strtoll( "-0x80000000000000000000000000000001", 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 #endif