]> pd.if.org Git - pdclib/blob - functions/stdlib/strtol.c
PDCLib includes with quotes, not <>.
[pdclib] / functions / stdlib / strtol.c
1 /* strtol( 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 int strtol( const char * s, char ** endptr, int base )
15 {
16     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 int)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LONG_MAX, (uintmax_t)( LONG_MAX / base ), (int)( LONG_MAX % base ), &sign );
23     }
24     else
25     {
26         rc = (long int)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LONG_MIN, (uintmax_t)( LONG_MIN / -base ), (int)( -( LONG_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( strtol( "123", NULL, 10 ) == 123 );
49     /* proper detecting of default base 10 */
50     TESTCASE( strtol( "456", NULL, 0 ) == 456 );
51     /* proper functioning to smaller base */
52     TESTCASE( strtol( "14", NULL, 8 ) == 12 );
53     /* proper autodetecting of octal */
54     TESTCASE( strtol( "016", NULL, 0 ) == 14 );
55     /* proper autodetecting of hexadecimal, lowercase 'x' */
56     TESTCASE( strtol( "0xFF", NULL, 0 ) == 255 );
57     /* proper autodetecting of hexadecimal, uppercase 'X' */
58     TESTCASE( strtol( "0Xa1", NULL, 0 ) == 161 );
59     /* proper handling of border case: 0x followed by non-hexdigit */
60     TESTCASE( strtol( tricky, &endptr, 0 ) == 0 );
61     TESTCASE( endptr == tricky + 2 );
62     /* proper handling of border case: 0 followed by non-octdigit */
63     TESTCASE( strtol( 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( strtol( overflow, &endptr, 36 ) == LONG_MIN );
69     TESTCASE( errno == ERANGE );
70     TESTCASE( ( endptr - overflow ) == 53 );
71     /* same for positive */
72     errno = 0;
73     TESTCASE( strtol( overflow + 1, &endptr, 36 ) == LONG_MAX );
74     TESTCASE( errno == ERANGE );
75     TESTCASE( ( endptr - overflow ) == 53 );
76     /* testing skipping of leading whitespace */
77     TESTCASE( strtol( " \n\v\t\f789", NULL, 0 ) == 789 );
78     /* testing conversion failure */
79     TESTCASE( strtol( overflow, &endptr, 10 ) == 0 );
80     TESTCASE( endptr == overflow );
81     endptr = NULL;
82     TESTCASE( strtol( 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 LONG_MAX >> 30 == 1
89     /* testing "even" overflow, i.e. base is power of two */
90     TESTCASE( strtol( "2147483647", NULL, 0 ) == 0x7fffffff );
91     TESTCASE( errno == 0 );
92     errno = 0;
93     TESTCASE( strtol( "2147483648", NULL, 0 ) == LONG_MAX );
94     TESTCASE( errno == ERANGE );
95     errno = 0;
96     TESTCASE( strtol( "-2147483647", NULL, 0 ) == (long)0x80000001 );
97     TESTCASE( errno == 0 );
98     errno = 0;
99     TESTCASE( strtol( "-2147483648", NULL, 0 ) == LONG_MIN );
100     TESTCASE( errno == 0 );
101     errno = 0;
102     TESTCASE( strtol( "-2147483649", NULL, 0 ) == LONG_MIN );
103     TESTCASE( errno == ERANGE );
104     /* TODO: test "odd" overflow, i.e. base is not power of two */
105 #elif LONG_MAX >> 62 == 1
106     /* testing "even" overflow, i.e. base is power of two */
107     TESTCASE( strtol( "9223372036854775807", NULL, 0 ) == 0x7fffffffffffffff );
108     TESTCASE( errno == 0 );
109     errno = 0;
110     TESTCASE( strtol( "9223372036854775808", NULL, 0 ) == LONG_MAX );
111     TESTCASE( errno == ERANGE );
112     errno = 0;
113     TESTCASE( strtol( "-9223372036854775807", NULL, 0 ) == (long)0x8000000000000001 );
114     TESTCASE( errno == 0 );
115     errno = 0;
116     TESTCASE( strtol( "-9223372036854775808", NULL, 0 ) == LONG_MIN );
117     TESTCASE( errno == 0 );
118     errno = 0;
119     TESTCASE( strtol( "-9223372036854775809", NULL, 0 ) == LONG_MIN );
120     TESTCASE( errno == ERANGE );
121     /* TODO: test "odd" overflow, i.e. base is not power of two */
122 #else
123 #error Unsupported width of 'long' (neither 32 nor 64 bit).
124 #endif
125     return TEST_RESULTS;
126 }
127
128 #endif