]> pd.if.org Git - pdclib/blob - functions/inttypes/strtoumax.c
c54cfce481ff087c0e234df85a121b37daab6f1e
[pdclib] / functions / inttypes / strtoumax.c
1 /* $Id$ */
2
3 /* strtoumax( 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 uintmax_t strtoumax( const char * _PDCLIB_restrict nptr, char ** _PDCLIB_restrict endptr, int base )
17 {
18     uintmax_t rc;
19     char sign = '+';
20     const char * p = _PDCLIB_strtox_prelim( nptr, &sign, &base );
21     if ( base < 2 || base > 36 ) return 0;
22     rc = _PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)UINTMAX_MAX, (uintmax_t)( UINTMAX_MAX / base ), (int)( UINTMAX_MAX % base ), &sign );
23     if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) nptr;
24     return ( sign == '+' ) ? rc : -rc;
25 }
26
27 #endif
28
29 #ifdef TEST
30 #include <_PDCLIB_test.h>
31 #include <errno.h>
32
33 int main( void )
34 {
35     char * endptr;
36     /* this, to base 36, overflows even a 256 bit integer */
37     char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
38     /* tricky border case */
39     char tricky[] = "+0xz";
40     errno = 0;
41     /* basic functionality */
42     TESTCASE( strtoumax( "123", NULL, 10 ) == 123 );
43     /* proper detecting of default base 10 */
44     TESTCASE( strtoumax( "456", NULL, 0 ) == 456 );
45     /* proper functioning to smaller base */
46     TESTCASE( strtoumax( "14", NULL, 8 ) == 12 );
47     /* proper autodetecting of octal */
48     TESTCASE( strtoumax( "016", NULL, 0 ) == 14 );
49     /* proper autodetecting of hexadecimal, lowercase 'x' */
50     TESTCASE( strtoumax( "0xFF", NULL, 0 ) == 255 );
51     /* proper autodetecting of hexadecimal, uppercase 'X' */
52     TESTCASE( strtoumax( "0Xa1", NULL, 0 ) == 161 );
53     /* proper handling of border case: 0x followed by non-hexdigit */
54     TESTCASE( strtoumax( tricky, &endptr, 0 ) == 0 );
55     TESTCASE( endptr == tricky + 2 );
56     /* proper handling of border case: 0 followed by non-octdigit */
57     TESTCASE( strtoumax( tricky, &endptr, 8 ) == 0 );
58     TESTCASE( endptr == tricky + 2 );
59     /* errno should still be 0 */
60     TESTCASE( errno == 0 );
61     /* overflowing subject sequence must still return proper endptr */
62     TESTCASE( strtoumax( overflow, &endptr, 36 ) == UINTMAX_MAX );
63     TESTCASE( errno == ERANGE );
64     TESTCASE( ( endptr - overflow ) == 53 );
65     /* same for positive */
66     errno = 0;
67     TESTCASE( strtoumax( overflow + 1, &endptr, 36 ) == UINTMAX_MAX );
68     TESTCASE( errno == ERANGE );
69     TESTCASE( ( endptr - overflow ) == 53 );
70     /* testing skipping of leading whitespace */
71     TESTCASE( strtoumax( " \n\v\t\f789", NULL, 0 ) == 789 );
72     /* testing conversion failure */
73     TESTCASE( strtoumax( overflow, &endptr, 10 ) == 0 );
74     TESTCASE( endptr == overflow );
75     endptr = NULL;
76     TESTCASE( strtoumax( overflow, &endptr, 0 ) == 0 );
77     TESTCASE( endptr == overflow );
78     errno = 0;
79 /* uintmax_t -> long long -> 64 bit */
80 #if UINTMAX_MAX >> 63 == 1
81     /* testing "odd" overflow, i.e. base is not power of two */
82     TESTCASE( strtoumax( "18446744073709551615", NULL, 0 ) == UINTMAX_MAX );
83     TESTCASE( errno == 0 );
84     TESTCASE( strtoumax( "18446744073709551616", NULL, 0 ) == UINTMAX_MAX );
85     TESTCASE( errno == ERANGE );
86     /* testing "even" overflow, i.e. base is power of two */
87     errno = 0;
88     TESTCASE( strtoumax( "0xFFFFFFFFFFFFFFFF", NULL, 0 ) == UINTMAX_MAX );
89     TESTCASE( errno == 0 );
90     TESTCASE( strtoumax( "0x10000000000000000", NULL, 0 ) == UINTMAX_MAX );
91     TESTCASE( errno == ERANGE );
92 /* uintmax_t -> long long -> 128 bit */
93 #elif UINTMAX_MAX >> 127 == 1
94     /* testing "odd" overflow, i.e. base is not power of two */
95     TESTCASE( strtoumax( "340282366920938463463374607431768211455", NULL, 0 ) == UINTMAX_MAX );
96     TESTCASE( errno == 0 );
97     TESTCASE( strtoumax( "340282366920938463463374607431768211456", NULL, 0 ) == UINTMAX_MAX );
98     TESTCASE( errno == ERANGE );
99     /* testing "even" everflow, i.e. base is power of two */
100     errno = 0;
101     TESTCASE( strtoumax( "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", NULL, 0 ) == UINTMAX_MAX );
102     TESTCASE( errno == 0 );
103     TESTCASE( strtoumax( "0x100000000000000000000000000000000", NULL, 0 ) == UINTMAX_MAX );
104     TESTCASE( errno == ERANGE );
105 #else
106 #error Unsupported width of 'uintmax_t' (neither 64 nor 128 bit).
107 #endif
108     return TEST_RESULTS;
109 }
110
111 #endif