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.
11 #include <_PDCLIB_int.h>
18 long int strtol( const char * s, char ** endptr, int base )
22 const char * p = _PDCLIB_strtox_prelim( s, &sign, &base );
25 rc = _PDCLIB_strtox_main( &p, base, LONG_MAX, LONG_MAX / base, LONG_MAX % base, &sign );
29 /* FIXME: This breaks on some machines that round negatives wrongly */
30 rc = _PDCLIB_strtox_main( &p, base, LONG_MIN, LONG_MIN / -base, -( LONG_MIN % base ), &sign );
32 if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s;
33 return ( sign == '+' ) ? rc : -rc;
39 #include <_PDCLIB_test.h>
45 /* this, to base 36, overflows even a 256 bit integer */
46 char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
49 /* basic functionality */
50 TESTCASE( strtol( "123", NULL, 10 ) == 123 );
51 /* proper detecting of default base 10 */
52 TESTCASE( strtol( "123", NULL, 0 ) == 123 );
53 /* proper functioning to smaller base */
54 TESTCASE( strtol( "14", NULL, 8 ) == 12 );
55 /* proper autodetecting of octal */
56 TESTCASE( strtol( "014", NULL, 0 ) == 12 );
57 /* proper autodetecting of hexadecimal, lowercase 'x' */
58 TESTCASE( strtol( "0xFF", NULL, 0 ) == 255 );
59 /* proper autodetecting of hexadecimal, uppercase 'X' */
60 TESTCASE( strtol( "0XFF", NULL, 0 ) == 255 );
61 /* errno should still be 0 */
62 TESTCASE( errno == 0 );
63 /* overflowing subject sequence must still return proper endptr */
64 TESTCASE( strtol( overflow, &endptr, 36 ) == LONG_MIN );
65 TESTCASE( errno == ERANGE );
66 TESTCASE( ( endptr - overflow ) == 53 );
67 /* same for positive */
69 TESTCASE( strtol( overflow + 1, &endptr, 36 ) == LONG_MAX );
70 TESTCASE( errno == ERANGE );
71 TESTCASE( ( endptr - overflow ) == 53 );
72 /* testing skipping of leading whitespace */
73 TESTCASE( strtol( " \n\v\t\f123", NULL, 0 ) == 123 );
74 /* testing conversion failure */
75 TESTCASE( strtol( overflow, &endptr, 10 ) == 0 );
76 TESTCASE( endptr == overflow );
78 TESTCASE( strtol( overflow, &endptr, 0 ) == 0 );
79 TESTCASE( endptr == overflow );
80 /* These tests assume two-complement, but conversion should work for */
81 /* one-complement and signed magnitude just as well. Anyone having a */
82 /* platform to test this on? */
84 #if _PDCLIB_LONG_BYTES == 4
85 /* testing "even" overflow, i.e. base is power of two */
86 TESTCASE( strtol( "0x7FFFFFFF", NULL, 0 ) == 0x7fffffff );
87 TESTCASE( errno == 0 );
88 TESTCASE( strtol( "0x80000000", NULL, 0 ) == LONG_MAX );
89 TESTCASE( errno == ERANGE );
91 TESTCASE( strtol( "-0x7FFFFFFF", NULL, 0 ) == 0x80000001 );
92 TESTCASE( errno == 0 );
93 TESTCASE( strtol( "-0x80000000", NULL, 0 ) == LONG_MIN );
94 TESTCASE( errno == 0 );
95 TESTCASE( strtol( "-0x80000001", NULL, 0 ) == LONG_MIN );
96 TESTCASE( errno == ERANGE );
97 /* TODO: test "odd" overflow, i.e. base is not power of two */
98 #elif _PDCLIB_LONG_BYTES == 8
99 /* testing "even" overflow, i.e. base is power of two */
100 TESTCASE( strtol( "0x7FFFFFFFFFFFFFFF", NULL, 0 ) == 0x7fffffffffffffff );
101 TESTCASE( errno == 0 );
102 TESTCASE( strtol( "0x8000000000000000", NULL, 0 ) == LONG_MAX );
103 TESTCASE( errno == ERANGE );
105 TESTCASE( strtol( "-0x7FFFFFFFFFFFFFFF", NULL, 0 ) == -0x8000000000000001 );
106 TESTCASE( errno == 0 );
107 TESTCASE( strtol( "-0x8000000000000000", NULL, 0 ) == LONG_MIN );
108 TESTCASE( errno == 0 );
109 TESTCASE( strtol( "-0x8000000000000001", NULL, 0 ) == LONG_MIN );
110 TESTCASE( errno == ERANGE );
111 /* TODO: test "odd" overflow, i.e. base is not power of two */
113 #error Unsupported width of 'long' (neither 32 nor 64 bit).