]> pd.if.org Git - pdclib/blob - functions/inttypes/strtoimax.c
Extended test drivers to cover sign error.
[pdclib] / functions / inttypes / strtoimax.c
1 /* $Id$ */
2
3 /* strtoimax( const char *, char * *, int )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <limits.h>
10 #include <inttypes.h>
11
12 #ifndef REGTEST
13
14 #include <stddef.h>
15
16 intmax_t strtoimax( const char * _PDCLIB_restrict nptr, char ** _PDCLIB_restrict endptr, int base )
17 {
18     intmax_t rc;
19     char sign = '+';
20     const char * p = _PDCLIB_strtox_prelim( nptr, &sign, &base );
21     if ( base < 2 || base > 36 ) return 0;
22     if ( sign == '+' )
23     {
24         rc = (intmax_t)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)INTMAX_MAX, (uintmax_t)( INTMAX_MAX / base ), (int)( INTMAX_MAX % base ), &sign );
25     }
26     else
27     {
28         /* FIXME: This breaks on some machines that round negatives wrongly */
29         rc = (intmax_t)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)INTMAX_MIN, (uintmax_t)( INTMAX_MIN / -base ), (int)( -( INTMAX_MIN % base ) ), &sign );
30     }
31     if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) nptr;
32     return ( sign == '+' ) ? rc : -rc;
33 }
34
35 #endif
36
37 #ifdef TEST
38 #include <_PDCLIB_test.h>
39
40 #include <errno.h>
41
42 int main( void )
43 {
44     char * endptr;
45     /* this, to base 36, overflows even a 256 bit integer */
46     char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
47     /* tricky border case */
48     char tricky[] = "+0xz";
49     errno = 0;
50     /* basic functionality */
51     TESTCASE( strtoimax( "123", NULL, 10 ) == 123 );
52     /* proper detecting of default base 10 */
53     TESTCASE( strtoimax( "456", NULL, 0 ) == 456 );
54     /* proper functioning to smaller base */
55     TESTCASE( strtoimax( "14", NULL, 8 ) == 12 );
56     /* proper autodetecting of octal */
57     TESTCASE( strtoimax( "016", NULL, 0 ) == 14 );
58     /* proper autodetecting of hexadecimal, lowercase 'x' */
59     TESTCASE( strtoimax( "0xFF", NULL, 0 ) == 255 );
60     /* proper autodetecting of hexadecimal, uppercase 'X' */
61     TESTCASE( strtoimax( "0Xa1", NULL, 0 ) == 161 );
62     /* proper handling of border case: 0x followed by non-hexdigit */
63     TESTCASE( strtoimax( tricky, &endptr, 0 ) == 0 );
64     TESTCASE( endptr == tricky + 2 );
65     /* proper handling of border case: 0 followed by non-octdigit */
66     TESTCASE( strtoimax( tricky, &endptr, 8 ) == 0 );
67     TESTCASE( endptr == tricky + 2 );
68     /* errno should still be 0 */
69     TESTCASE( errno == 0 );
70     /* overflowing subject sequence must still return proper endptr */
71     TESTCASE( strtoimax( overflow, &endptr, 36 ) == INTMAX_MIN );
72     TESTCASE( errno == ERANGE );
73     TESTCASE( ( endptr - overflow ) == 53 );
74     /* same for positive */
75     errno = 0;
76     TESTCASE( strtoimax( overflow + 1, &endptr, 36 ) == INTMAX_MAX );
77     TESTCASE( errno == ERANGE );
78     TESTCASE( ( endptr - overflow ) == 53 );
79     /* testing skipping of leading whitespace */
80     TESTCASE( strtoimax( " \n\v\t\f789", NULL, 0 ) == 789 );
81     /* testing conversion failure */
82     TESTCASE( strtoimax( overflow, &endptr, 10 ) == 0 );
83     TESTCASE( endptr == overflow );
84     endptr = NULL;
85     TESTCASE( strtoimax( overflow, &endptr, 0 ) == 0 );
86     TESTCASE( endptr == overflow );
87     /* These tests assume two-complement, but conversion should work for   */
88     /* one-complement and signed magnitude just as well. Anyone having a   */
89     /* platform to test this on?                                           */
90     errno = 0;
91 #if INTMAX_MAX == 0x7fffffffffffffffLL
92     /* testing "even" overflow, i.e. base is power of two */
93     TESTCASE( strtoimax( "9223372036854775807", NULL, 0 ) == INTMAX_MAX );
94     TESTCASE( errno == 0 );
95     TESTCASE( strtoimax( "9223372036854775808", NULL, 0 ) == INTMAX_MAX );
96     TESTCASE( errno == ERANGE );
97     errno = 0;
98     TESTCASE( strtoimax( "-9223372036854775807", NULL, 0 ) == (INTMAX_MIN + 1) );
99     TESTCASE( errno == 0 );
100     TESTCASE( strtoimax( "-9223372036854775808", NULL, 0 ) == INTMAX_MIN );
101     TESTCASE( errno == 0 );
102     TESTCASE( strtoimax( "-9223372036854775809", NULL, 0 ) == INTMAX_MIN );
103     TESTCASE( errno == ERANGE );
104     /* TODO: test "odd" overflow, i.e. base is not power of two */
105 #elif LLONG_MAX == 0x7fffffffffffffffffffffffffffffffLL
106     /* testing "even" overflow, i.e. base is power of two */
107     TESTCASE( strtoimax( "170141183460469231731687303715884105728", NULL, 0 ) == INTMAX_MAX );
108     TESTCASE( errno == 0 );
109     TESTCASE( strtoimax( "170141183460469231731687303715884105729", NULL, 0 ) == INTMAX_MAX );
110     TESTCASE( errno == ERANGE );
111     errno = 0;
112     TESTCASE( strtoimax( "-170141183460469231731687303715884105728", NULL, 0 ) == (INTMAX_MIN + 1) );
113     TESTCASE( errno == 0 );
114     TESTCASE( strtoimax( "-170141183460469231731687303715884105729", NULL, 0 ) == INTMAX_MIN );
115     TESTCASE( errno == 0 );
116     TESTCASE( strtoimax( "-170141183460469231731687303715884105730", NULL, 0 ) == INTMAX_MIN );
117     TESTCASE( errno == ERANGE );
118     /* TODO: test "odd" overflow, i.e. base is not power of two */
119 #else
120 #error Unsupported width of 'intmax_t' (neither 64 nor 128 bit).
121 #endif
122     return TEST_RESULTS;
123 }
124
125 #endif