3 /* strtol( const char *, char * *, int )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
16 long int strtol( const char * s, char ** endptr, int base )
20 const char * p = _PDCLIB_strtox_prelim( s, &sign, &base );
21 if ( base < 2 || base > 36 ) return 0;
24 rc = (long int)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LONG_MAX, (uintmax_t)( LONG_MAX / base ), (int)( LONG_MAX % base ), &sign );
28 rc = (long int)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LONG_MIN, (uintmax_t)( LONG_MIN / -base ), (int)( -( LONG_MIN % base ) ), &sign );
30 if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s;
31 return ( sign == '+' ) ? rc : -rc;
37 #include <_PDCLIB_test.h>
44 /* this, to base 36, overflows even a 256 bit integer */
45 char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
46 /* tricky border case */
47 char tricky[] = "+0xz";
49 /* basic functionality */
50 TESTCASE( strtol( "123", NULL, 10 ) == 123 );
51 /* proper detecting of default base 10 */
52 TESTCASE( strtol( "456", NULL, 0 ) == 456 );
53 /* proper functioning to smaller base */
54 TESTCASE( strtol( "14", NULL, 8 ) == 12 );
55 /* proper autodetecting of octal */
56 TESTCASE( strtol( "016", NULL, 0 ) == 14 );
57 /* proper autodetecting of hexadecimal, lowercase 'x' */
58 TESTCASE( strtol( "0xFF", NULL, 0 ) == 255 );
59 /* proper autodetecting of hexadecimal, uppercase 'X' */
60 TESTCASE( strtol( "0Xa1", NULL, 0 ) == 161 );
61 /* proper handling of border case: 0x followed by non-hexdigit */
62 TESTCASE( strtol( tricky, &endptr, 0 ) == 0 );
63 TESTCASE( endptr == tricky + 2 );
64 /* proper handling of border case: 0 followed by non-octdigit */
65 TESTCASE( strtol( tricky, &endptr, 8 ) == 0 );
66 TESTCASE( endptr == tricky + 2 );
67 /* errno should still be 0 */
68 TESTCASE( errno == 0 );
69 /* overflowing subject sequence must still return proper endptr */
70 TESTCASE( strtol( overflow, &endptr, 36 ) == LONG_MIN );
71 TESTCASE( errno == ERANGE );
72 TESTCASE( ( endptr - overflow ) == 53 );
73 /* same for positive */
75 TESTCASE( strtol( overflow + 1, &endptr, 36 ) == LONG_MAX );
76 TESTCASE( errno == ERANGE );
77 TESTCASE( ( endptr - overflow ) == 53 );
78 /* testing skipping of leading whitespace */
79 TESTCASE( strtol( " \n\v\t\f789", NULL, 0 ) == 789 );
80 /* testing conversion failure */
81 TESTCASE( strtol( overflow, &endptr, 10 ) == 0 );
82 TESTCASE( endptr == overflow );
84 TESTCASE( strtol( overflow, &endptr, 0 ) == 0 );
85 TESTCASE( endptr == overflow );
86 /* TODO: These tests assume two-complement, but conversion should work */
87 /* for one-complement and signed magnitude just as well. Anyone having */
88 /* a platform to test this on? */
90 #if LONG_MAX >> 30 == 1
91 /* testing "even" overflow, i.e. base is power of two */
92 TESTCASE( strtol( "2147483647", NULL, 0 ) == 0x7fffffff );
93 TESTCASE( errno == 0 );
95 TESTCASE( strtol( "2147483648", NULL, 0 ) == LONG_MAX );
96 TESTCASE( errno == ERANGE );
98 TESTCASE( strtol( "-2147483647", NULL, 0 ) == (long)0x80000001 );
99 TESTCASE( errno == 0 );
101 TESTCASE( strtol( "-2147483648", NULL, 0 ) == LONG_MIN );
102 TESTCASE( errno == 0 );
104 TESTCASE( strtol( "-2147483649", NULL, 0 ) == LONG_MIN );
105 TESTCASE( errno == ERANGE );
106 /* TODO: test "odd" overflow, i.e. base is not power of two */
107 #elif LONG_MAX >> 62 == 1
108 /* testing "even" overflow, i.e. base is power of two */
109 TESTCASE( strtol( "9223372036854775807", NULL, 0 ) == 0x7fffffffffffffff );
110 TESTCASE( errno == 0 );
112 TESTCASE( strtol( "9223372036854775808", NULL, 0 ) == LONG_MAX );
113 TESTCASE( errno == ERANGE );
115 TESTCASE( strtol( "-9223372036854775807", NULL, 0 ) == (long)0x8000000000000001 );
116 TESTCASE( errno == 0 );
118 TESTCASE( strtol( "-9223372036854775808", NULL, 0 ) == LONG_MIN );
119 TESTCASE( errno == 0 );
121 TESTCASE( strtol( "-9223372036854775809", NULL, 0 ) == LONG_MIN );
122 TESTCASE( errno == ERANGE );
123 /* TODO: test "odd" overflow, i.e. base is not power of two */
125 #error Unsupported width of 'long' (neither 32 nor 64 bit).