]> pd.if.org Git - pdclib/blob - functions/inttypes/strtoimax.c
PDCLib includes with quotes, not <>.
[pdclib] / functions / inttypes / strtoimax.c
1 /* strtoimax( 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 <inttypes.h>
9
10 #ifndef REGTEST
11
12 #include <stddef.h>
13
14 intmax_t strtoimax( const char * _PDCLIB_restrict nptr, char ** _PDCLIB_restrict endptr, int base )
15 {
16     intmax_t rc;
17     char sign = '+';
18     const char * p = _PDCLIB_strtox_prelim( nptr, &sign, &base );
19     if ( base < 2 || base > 36 ) return 0;
20     if ( sign == '+' )
21     {
22         rc = (intmax_t)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)INTMAX_MAX, (uintmax_t)( INTMAX_MAX / base ), (int)( INTMAX_MAX % base ), &sign );
23     }
24     else
25     {
26         rc = (intmax_t)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)INTMAX_MIN, (uintmax_t)( INTMAX_MIN / -base ), (int)( -( INTMAX_MIN % base ) ), &sign );
27     }
28     if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) nptr;
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( strtoimax( "123", NULL, 10 ) == 123 );
49     /* proper detecting of default base 10 */
50     TESTCASE( strtoimax( "456", NULL, 0 ) == 456 );
51     /* proper functioning to smaller base */
52     TESTCASE( strtoimax( "14", NULL, 8 ) == 12 );
53     /* proper autodetecting of octal */
54     TESTCASE( strtoimax( "016", NULL, 0 ) == 14 );
55     /* proper autodetecting of hexadecimal, lowercase 'x' */
56     TESTCASE( strtoimax( "0xFF", NULL, 0 ) == 255 );
57     /* proper autodetecting of hexadecimal, uppercase 'X' */
58     TESTCASE( strtoimax( "0Xa1", NULL, 0 ) == 161 );
59     /* proper handling of border case: 0x followed by non-hexdigit */
60     TESTCASE( strtoimax( tricky, &endptr, 0 ) == 0 );
61     TESTCASE( endptr == tricky + 2 );
62     /* proper handling of border case: 0 followed by non-octdigit */
63     TESTCASE( strtoimax( 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( strtoimax( overflow, &endptr, 36 ) == INTMAX_MIN );
69     TESTCASE( errno == ERANGE );
70     TESTCASE( ( endptr - overflow ) == 53 );
71     /* same for positive */
72     errno = 0;
73     TESTCASE( strtoimax( overflow + 1, &endptr, 36 ) == INTMAX_MAX );
74     TESTCASE( errno == ERANGE );
75     TESTCASE( ( endptr - overflow ) == 53 );
76     /* testing skipping of leading whitespace */
77     TESTCASE( strtoimax( " \n\v\t\f789", NULL, 0 ) == 789 );
78     /* testing conversion failure */
79     TESTCASE( strtoimax( overflow, &endptr, 10 ) == 0 );
80     TESTCASE( endptr == overflow );
81     endptr = NULL;
82     TESTCASE( strtoimax( 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?                                           */
87     errno = 0;
88 #if INTMAX_MAX >> 62 == 1
89     /* testing "odd" overflow, i.e. base is not a power of two */
90     TESTCASE( strtoimax( "9223372036854775807", NULL, 0 ) == INTMAX_MAX );
91     TESTCASE( errno == 0 );
92     TESTCASE( strtoimax( "9223372036854775808", NULL, 0 ) == INTMAX_MAX );
93     TESTCASE( errno == ERANGE );
94     errno = 0;
95     TESTCASE( strtoimax( "-9223372036854775807", NULL, 0 ) == (INTMAX_MIN + 1) );
96     TESTCASE( errno == 0 );
97     TESTCASE( strtoimax( "-9223372036854775808", NULL, 0 ) == INTMAX_MIN );
98     TESTCASE( errno == 0 );
99     TESTCASE( strtoimax( "-9223372036854775809", NULL, 0 ) == INTMAX_MIN );
100     TESTCASE( errno == ERANGE );
101     /* testing "even" overflow, i.e. base is power of two */
102     errno = 0;
103     TESTCASE( strtoimax( "0x7fffffffffffffff", NULL, 0 ) == INTMAX_MAX );
104     TESTCASE( errno == 0 );
105     TESTCASE( strtoimax( "0x8000000000000000", NULL, 0 ) == INTMAX_MAX );
106     TESTCASE( errno == ERANGE );
107     errno = 0;
108     TESTCASE( strtoimax( "-0x7fffffffffffffff", NULL, 0 ) == (INTMAX_MIN + 1) );
109     TESTCASE( errno == 0 );
110     TESTCASE( strtoimax( "-0x8000000000000000", NULL, 0 ) == INTMAX_MIN );
111     TESTCASE( errno == 0 );
112     TESTCASE( strtoimax( "-0x8000000000000001", NULL, 0 ) == INTMAX_MIN );
113     TESTCASE( errno == ERANGE );
114 #elif LLONG_MAX >> 126 == 1
115     /* testing "odd" overflow, i.e. base is not a power of two */
116     TESTCASE( strtoimax( "170141183460469231731687303715884105728", NULL, 0 ) == INTMAX_MAX );
117     TESTCASE( errno == 0 );
118     TESTCASE( strtoimax( "170141183460469231731687303715884105729", NULL, 0 ) == INTMAX_MAX );
119     TESTCASE( errno == ERANGE );
120     errno = 0;
121     TESTCASE( strtoimax( "-170141183460469231731687303715884105728", NULL, 0 ) == (INTMAX_MIN + 1) );
122     TESTCASE( errno == 0 );
123     TESTCASE( strtoimax( "-170141183460469231731687303715884105729", NULL, 0 ) == INTMAX_MIN );
124     TESTCASE( errno == 0 );
125     TESTCASE( strtoimax( "-170141183460469231731687303715884105730", NULL, 0 ) == INTMAX_MIN );
126     TESTCASE( errno == ERANGE );
127     /* testing "even" overflow, i.e. base is power of two */
128     errno = 0;
129     TESTCASE( strtoimax( "0x7fffffffffffffffffffffffffffffff", NULL, 0 ) == INTMAX_MAX );
130     TESTCASE( errno == 0 );
131     TESTCASE( strtoimax( "0x80000000000000000000000000000000", NULL, 0 ) == INTMAX_MAX );
132     TESTCASE( errno == ERANGE );
133     errno = 0;
134     TESTCASE( strtoimax( "-0x7fffffffffffffffffffffffffffffff", NULL, 0 ) == (INTMAX_MIN + 1) );
135     TESTCASE( errno == 0 );
136     TESTCASE( strtoimax( "-0x80000000000000000000000000000000", NULL, 0 ) == INTMAX_MIN );
137     TESTCASE( errno == 0 );
138     TESTCASE( strtoimax( "-0x80000000000000000000000000000001", NULL, 0 ) == INTMAX_MIN );
139     TESTCASE( errno == ERANGE );
140 #else
141 #error Unsupported width of 'intmax_t' (neither 64 nor 128 bit).
142 #endif
143     return TEST_RESULTS;
144 }
145
146 #endif