]> pd.if.org Git - pdclib/blob - functions/stdlib/strtoull.c
Added size_t to stdlib.h, added redefinition guards, adjusted includes.
[pdclib] / functions / stdlib / strtoull.c
1 /* $Id$ */
2
3 /* Release $Name$ */
4
5 /* strtoull( const char *, char * *, int )
6
7    This file is part of the Public Domain C Library (PDCLib).
8    Permission is granted to use, modify, and / or redistribute at will.
9 */
10
11 #include <limits.h>
12 #include <stdlib.h>
13
14 #ifndef REGTEST
15
16 unsigned long long int strtoull( const char * s, char ** endptr, int base )
17 {
18     unsigned long long int rc;
19     char sign = '+';
20     const char * p = _PDCLIB_strtox_prelim( s, &sign, &base );
21     rc = _PDCLIB_strtox_main( &p, base, ULLONG_MAX, ULLONG_MAX / base, ULLONG_MAX % base, &sign );
22     if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s;
23     return ( sign == '+' ) ? rc : -rc;
24 }
25
26 #endif
27
28 #ifdef TEST
29 #include <_PDCLIB_test.h>
30 #include <errno.h>
31
32 int main()
33 {
34     char * endptr;
35     /* this, to base 36, overflows even a 256 bit integer */
36     char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
37     BEGIN_TESTS;
38     errno = 0;
39     /* basic functionality */
40     TESTCASE( strtoull( "123", NULL, 10 ) == 123 );
41     /* proper detecting of default base 10 */
42     TESTCASE( strtoull( "123", NULL, 0 ) == 123 );
43     /* proper functioning to smaller base */
44     TESTCASE( strtoull( "14", NULL, 8 ) == 12 );
45     /* proper autodetecting of octal */
46     TESTCASE( strtoull( "014", NULL, 0 ) == 12 );
47     /* proper autodetecting of hexadecimal, lowercase 'x' */
48     TESTCASE( strtoull( "0xFF", NULL, 0 ) == 255 );
49     /* proper autodetecting of hexadecimal, uppercase 'X' */
50     TESTCASE( strtoull( "0XFF", NULL, 0 ) == 255 );
51     /* errno should still be 0 */
52     TESTCASE( errno == 0 );
53     /* overflowing subject sequence must still return proper endptr */
54     TESTCASE( strtoull( overflow, &endptr, 36 ) == ULLONG_MAX );
55     TESTCASE( errno == ERANGE );
56     TESTCASE( ( endptr - overflow ) == 53 );
57     /* same for positive */
58     errno = 0;
59     TESTCASE( strtoull( overflow + 1, &endptr, 36 ) == ULLONG_MAX );
60     TESTCASE( errno == ERANGE );
61     TESTCASE( ( endptr - overflow ) == 53 );
62     /* testing skipping of leading whitespace */
63     TESTCASE( strtoull( " \n\v\t\f123", NULL, 0 ) == 123 );
64     /* testing conversion failure */
65     TESTCASE( strtoull( overflow, &endptr, 10 ) == 0 );
66     TESTCASE( endptr == overflow );
67     endptr = NULL;
68     TESTCASE( strtoull( overflow, &endptr, 0 ) == 0 );
69     TESTCASE( endptr == overflow );
70     return TEST_RESULTS;
71 }
72 #endif