]> pd.if.org Git - pdclib/blob - functions/inttypes/strtoimax.c
Whitespace cleanups.
[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
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( strtoimax( "123", NULL, 10 ) == 123 );
50     /* proper detecting of default base 10 */
51     TESTCASE( strtoimax( "456", NULL, 0 ) == 456 );
52     /* proper functioning to smaller base */
53     TESTCASE( strtoimax( "14", NULL, 8 ) == 12 );
54     /* proper autodetecting of octal */
55     TESTCASE( strtoimax( "016", NULL, 0 ) == 14 );
56     /* proper autodetecting of hexadecimal, lowercase 'x' */
57     TESTCASE( strtoimax( "0xFF", NULL, 0 ) == 255 );
58     /* proper autodetecting of hexadecimal, uppercase 'X' */
59     TESTCASE( strtoimax( "0Xa1", NULL, 0 ) == 161 );
60     /* proper handling of border case: 0x followed by non-hexdigit */
61     TESTCASE( strtoimax( tricky, &endptr, 0 ) == 0 );
62     TESTCASE( endptr == tricky + 2 );
63     /* proper handling of border case: 0 followed by non-octdigit */
64     TESTCASE( strtoimax( 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( strtoimax( overflow, &endptr, 36 ) == INTMAX_MIN );
70     TESTCASE( errno == ERANGE );
71     TESTCASE( ( endptr - overflow ) == 53 );
72     /* same for positive */
73     errno = 0;
74     TESTCASE( strtoimax( overflow + 1, &endptr, 36 ) == INTMAX_MAX );
75     TESTCASE( errno == ERANGE );
76     TESTCASE( ( endptr - overflow ) == 53 );
77     /* testing skipping of leading whitespace */
78     TESTCASE( strtoimax( " \n\v\t\f789", NULL, 0 ) == 789 );
79     /* testing conversion failure */
80     TESTCASE( strtoimax( overflow, &endptr, 10 ) == 0 );
81     TESTCASE( endptr == overflow );
82     endptr = NULL;
83     TESTCASE( strtoimax( overflow, &endptr, 0 ) == 0 );
84     TESTCASE( endptr == overflow );
85     /* These tests assume two-complement, but conversion should work for   */
86     /* one-complement and signed magnitude just as well. Anyone having a   */
87     /* platform to test this on?                                           */
88     errno = 0;
89 #if INTMAX_MAX >> 62 == 1
90     /* testing "odd" overflow, i.e. base is not a power of two */
91     TESTCASE( strtoimax( "9223372036854775807", NULL, 0 ) == INTMAX_MAX );
92     TESTCASE( errno == 0 );
93     TESTCASE( strtoimax( "9223372036854775808", NULL, 0 ) == INTMAX_MAX );
94     TESTCASE( errno == ERANGE );
95     errno = 0;
96     TESTCASE( strtoimax( "-9223372036854775807", NULL, 0 ) == (INTMAX_MIN + 1) );
97     TESTCASE( errno == 0 );
98     TESTCASE( strtoimax( "-9223372036854775808", NULL, 0 ) == INTMAX_MIN );
99     TESTCASE( errno == 0 );
100     TESTCASE( strtoimax( "-9223372036854775809", NULL, 0 ) == INTMAX_MIN );
101     TESTCASE( errno == ERANGE );
102     /* testing "even" overflow, i.e. base is power of two */
103     errno = 0;
104     TESTCASE( strtoimax( "0x7fffffffffffffff", NULL, 0 ) == INTMAX_MAX );
105     TESTCASE( errno == 0 );
106     TESTCASE( strtoimax( "0x8000000000000000", NULL, 0 ) == INTMAX_MAX );
107     TESTCASE( errno == ERANGE );
108     errno = 0;
109     TESTCASE( strtoimax( "-0x7fffffffffffffff", NULL, 0 ) == (INTMAX_MIN + 1) );
110     TESTCASE( errno == 0 );
111     TESTCASE( strtoimax( "-0x8000000000000000", NULL, 0 ) == INTMAX_MIN );
112     TESTCASE( errno == 0 );
113     TESTCASE( strtoimax( "-0x8000000000000001", NULL, 0 ) == INTMAX_MIN );
114     TESTCASE( errno == ERANGE );
115 #elif LLONG_MAX >> 126 == 1
116     /* testing "odd" overflow, i.e. base is not a power of two */
117     TESTCASE( strtoimax( "170141183460469231731687303715884105728", NULL, 0 ) == INTMAX_MAX );
118     TESTCASE( errno == 0 );
119     TESTCASE( strtoimax( "170141183460469231731687303715884105729", NULL, 0 ) == INTMAX_MAX );
120     TESTCASE( errno == ERANGE );
121     errno = 0;
122     TESTCASE( strtoimax( "-170141183460469231731687303715884105728", NULL, 0 ) == (INTMAX_MIN + 1) );
123     TESTCASE( errno == 0 );
124     TESTCASE( strtoimax( "-170141183460469231731687303715884105729", NULL, 0 ) == INTMAX_MIN );
125     TESTCASE( errno == 0 );
126     TESTCASE( strtoimax( "-170141183460469231731687303715884105730", NULL, 0 ) == INTMAX_MIN );
127     TESTCASE( errno == ERANGE );
128     /* testing "even" overflow, i.e. base is power of two */
129     errno = 0;
130     TESTCASE( strtoimax( "0x7fffffffffffffffffffffffffffffff", NULL, 0 ) == INTMAX_MAX );
131     TESTCASE( errno == 0 );
132     TESTCASE( strtoimax( "0x80000000000000000000000000000000", NULL, 0 ) == INTMAX_MAX );
133     TESTCASE( errno == ERANGE );
134     errno = 0;
135     TESTCASE( strtoimax( "-0x7fffffffffffffffffffffffffffffff", NULL, 0 ) == (INTMAX_MIN + 1) );
136     TESTCASE( errno == 0 );
137     TESTCASE( strtoimax( "-0x80000000000000000000000000000000", NULL, 0 ) == INTMAX_MIN );
138     TESTCASE( errno == 0 );
139     TESTCASE( strtoimax( "-0x80000000000000000000000000000001", NULL, 0 ) == INTMAX_MIN );
140     TESTCASE( errno == ERANGE );
141 #else
142 #error Unsupported width of 'intmax_t' (neither 64 nor 128 bit).
143 #endif
144     return TEST_RESULTS;
145 }
146
147 #endif