]> pd.if.org Git - pdclib/blob - functions/stdlib/strtol.c
Whitespace cleanups.
[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
36 #include "_PDCLIB_test.h"
37
38 #include <errno.h>
39
40 int main( void )
41 {
42     char * endptr;
43     /* this, to base 36, overflows even a 256 bit integer */
44     char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
45     /* tricky border case */
46     char tricky[] = "+0xz";
47     errno = 0;
48     /* basic functionality */
49     TESTCASE( strtol( "123", NULL, 10 ) == 123 );
50     /* proper detecting of default base 10 */
51     TESTCASE( strtol( "456", NULL, 0 ) == 456 );
52     /* proper functioning to smaller base */
53     TESTCASE( strtol( "14", NULL, 8 ) == 12 );
54     /* proper autodetecting of octal */
55     TESTCASE( strtol( "016", NULL, 0 ) == 14 );
56     /* proper autodetecting of hexadecimal, lowercase 'x' */
57     TESTCASE( strtol( "0xFF", NULL, 0 ) == 255 );
58     /* proper autodetecting of hexadecimal, uppercase 'X' */
59     TESTCASE( strtol( "0Xa1", NULL, 0 ) == 161 );
60     /* proper handling of border case: 0x followed by non-hexdigit */
61     TESTCASE( strtol( tricky, &endptr, 0 ) == 0 );
62     TESTCASE( endptr == tricky + 2 );
63     /* proper handling of border case: 0 followed by non-octdigit */
64     TESTCASE( strtol( tricky, &endptr, 8 ) == 0 );
65     TESTCASE( endptr == tricky + 2 );
66     /* errno should still be 0 */
67     TESTCASE( errno == 0 );
68     /* overflowing subject sequence must still return proper endptr */
69     TESTCASE( strtol( overflow, &endptr, 36 ) == LONG_MIN );
70     TESTCASE( errno == ERANGE );
71     TESTCASE( ( endptr - overflow ) == 53 );
72     /* same for positive */
73     errno = 0;
74     TESTCASE( strtol( overflow + 1, &endptr, 36 ) == LONG_MAX );
75     TESTCASE( errno == ERANGE );
76     TESTCASE( ( endptr - overflow ) == 53 );
77     /* testing skipping of leading whitespace */
78     TESTCASE( strtol( " \n\v\t\f789", NULL, 0 ) == 789 );
79     /* testing conversion failure */
80     TESTCASE( strtol( overflow, &endptr, 10 ) == 0 );
81     TESTCASE( endptr == overflow );
82     endptr = NULL;
83     TESTCASE( strtol( overflow, &endptr, 0 ) == 0 );
84     TESTCASE( endptr == overflow );
85     /* TODO: These tests assume two-complement, but conversion should work */
86     /* for one-complement and signed magnitude just as well. Anyone having */
87     /* a platform to test this on?                                         */
88     errno = 0;
89 #if LONG_MAX >> 30 == 1
90     /* testing "even" overflow, i.e. base is power of two */
91     TESTCASE( strtol( "2147483647", NULL, 0 ) == 0x7fffffff );
92     TESTCASE( errno == 0 );
93     errno = 0;
94     TESTCASE( strtol( "2147483648", NULL, 0 ) == LONG_MAX );
95     TESTCASE( errno == ERANGE );
96     errno = 0;
97     TESTCASE( strtol( "-2147483647", NULL, 0 ) == (long)0x80000001 );
98     TESTCASE( errno == 0 );
99     errno = 0;
100     TESTCASE( strtol( "-2147483648", NULL, 0 ) == LONG_MIN );
101     TESTCASE( errno == 0 );
102     errno = 0;
103     TESTCASE( strtol( "-2147483649", NULL, 0 ) == LONG_MIN );
104     TESTCASE( errno == ERANGE );
105     /* TODO: test "odd" overflow, i.e. base is not power of two */
106 #elif LONG_MAX >> 62 == 1
107     /* testing "even" overflow, i.e. base is power of two */
108     TESTCASE( strtol( "9223372036854775807", NULL, 0 ) == 0x7fffffffffffffff );
109     TESTCASE( errno == 0 );
110     errno = 0;
111     TESTCASE( strtol( "9223372036854775808", NULL, 0 ) == LONG_MAX );
112     TESTCASE( errno == ERANGE );
113     errno = 0;
114     TESTCASE( strtol( "-9223372036854775807", NULL, 0 ) == (long)0x8000000000000001 );
115     TESTCASE( errno == 0 );
116     errno = 0;
117     TESTCASE( strtol( "-9223372036854775808", NULL, 0 ) == LONG_MIN );
118     TESTCASE( errno == 0 );
119     errno = 0;
120     TESTCASE( strtol( "-9223372036854775809", NULL, 0 ) == LONG_MIN );
121     TESTCASE( errno == ERANGE );
122     /* TODO: test "odd" overflow, i.e. base is not power of two */
123 #else
124 #error Unsupported width of 'long' (neither 32 nor 64 bit).
125 #endif
126     return TEST_RESULTS;
127 }
128
129 #endif