5 /* strtol( const char *, char * *, int )
7 This file is part of the Public Domain C Library (PDCLib).
8 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 );
23 rc = _PDCLIB_strtox_main( &p, base, LONG_MAX, LONG_MAX / base, LONG_MAX % base, &sign );
27 /* FIXME: This breaks on some machines that round negatives wrongly */
28 rc = _PDCLIB_strtox_main( &p, base, LONG_MIN, LONG_MIN / -base, -( 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>
41 #include <_PDCLIB_int.h>
49 /* this, to base 36, overflows even a 256 bit integer */
50 char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
53 /* basic functionality */
54 TESTCASE( strtol( "123", NULL, 10 ) == 123 );
55 /* proper detecting of default base 10 */
56 TESTCASE( strtol( "123", NULL, 0 ) == 123 );
57 /* proper functioning to smaller base */
58 TESTCASE( strtol( "14", NULL, 8 ) == 12 );
59 /* proper autodetecting of octal */
60 TESTCASE( strtol( "014", NULL, 0 ) == 12 );
61 /* proper autodetecting of hexadecimal, lowercase 'x' */
62 TESTCASE( strtol( "0xFF", NULL, 0 ) == 255 );
63 /* proper autodetecting of hexadecimal, uppercase 'X' */
64 TESTCASE( strtol( "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( strtol( overflow, &endptr, 36 ) == LONG_MIN );
69 TESTCASE( errno == ERANGE );
70 TESTCASE( ( endptr - overflow ) == 53 );
71 /* same for positive */
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\f123", NULL, 0 ) == 123 );
78 /* testing conversion failure */
79 TESTCASE( strtol( overflow, &endptr, 10 ) == 0 );
80 TESTCASE( endptr == overflow );
82 TESTCASE( strtol( 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? */
88 #if _PDCLIB_LONG_BYTES == 4
89 /* testing "even" overflow, i.e. base is power of two */
90 TESTCASE( strtol( "0x7FFFFFFF", NULL, 0 ) == 0x7fffffff );
91 TESTCASE( errno == 0 );
92 TESTCASE( strtol( "0x80000000", NULL, 0 ) == LONG_MAX );
93 TESTCASE( errno == ERANGE );
95 TESTCASE( strtol( "-0x7FFFFFFF", NULL, 0 ) == 0x80000001 );
96 TESTCASE( errno == 0 );
97 TESTCASE( strtol( "-0x80000000", NULL, 0 ) == LONG_MIN );
98 TESTCASE( errno == 0 );
99 TESTCASE( strtol( "-0x80000001", NULL, 0 ) == LONG_MIN );
100 TESTCASE( errno == ERANGE );
101 /* TODO: test "odd" overflow, i.e. base is not power of two */
102 #elif _PDCLIB_LONG_BYTES == 8
103 /* testing "even" overflow, i.e. base is power of two */
104 TESTCASE( strtol( "0x7FFFFFFFFFFFFFFF", NULL, 0 ) == 0x7fffffffffffffff );
105 TESTCASE( errno == 0 );
106 TESTCASE( strtol( "0x8000000000000000", NULL, 0 ) == LONG_MAX );
107 TESTCASE( errno == ERANGE );
109 TESTCASE( strtol( "-0x7FFFFFFFFFFFFFFF", NULL, 0 ) == -0x8000000000000001 );
110 TESTCASE( errno == 0 );
111 TESTCASE( strtol( "-0x8000000000000000", NULL, 0 ) == LONG_MIN );
112 TESTCASE( errno == 0 );
113 TESTCASE( strtol( "-0x8000000000000001", NULL, 0 ) == LONG_MIN );
114 TESTCASE( errno == ERANGE );
115 /* TODO: test "odd" overflow, i.e. base is not power of two */
117 #error Unsupported width of 'long' (neither 32 nor 64 bit).