]> pd.if.org Git - pdclib/blob - functions/stdlib/strtoll.c
Removed the $Name$ tags (not supported by SVN). Added $Id$ to Makefile / text files.
[pdclib] / functions / stdlib / strtoll.c
1 /* $Id$ */
2
3 /* strtoll( 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 <stdlib.h>
11
12 #ifndef REGTEST
13
14 #include <stdint.h>
15
16 long long int strtoll( const char * s, char ** endptr, int base )
17 {
18     long long int rc;
19     char sign = '+';
20     const char * p = _PDCLIB_strtox_prelim( s, &sign, &base );
21     if ( base < 2 || base > 36 ) return 0;
22     if ( sign == '+' )
23     {
24         rc = _PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LLONG_MAX, (uintmax_t)( LLONG_MAX / base ), (uintmax_t)( LLONG_MAX % base ), &sign );
25     }
26     else
27     {
28         /* FIXME: This breaks on some machines that round negatives wrongly */
29         rc = _PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LLONG_MIN, (uintmax_t)( LLONG_MIN / -base ), (uintmax_t)( -( LLONG_MIN % base ) ), &sign );
30     }
31     if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s;
32     return ( sign == '+' ) ? rc : -rc;
33 }
34
35 #endif
36
37 #ifdef TEST
38 #include <_PDCLIB_test.h>
39
40 #ifndef _PDCLIB_INT_H
41 #define _PDCLIB_INT_H
42 #include <_PDCLIB_int.h>
43 #endif
44
45 #include <errno.h>
46
47 int main( void )
48 {
49     char * endptr;
50     /* this, to base 36, overflows even a 256 bit integer */
51     char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
52     errno = 0;
53     /* basic functionality */
54     TESTCASE( strtoll( "123", NULL, 10 ) == 123 );
55     /* proper detecting of default base 10 */
56     TESTCASE( strtoll( "123", NULL, 0 ) == 123 );
57     /* proper functioning to smaller base */
58     TESTCASE( strtoll( "14", NULL, 8 ) == 12 );
59     /* proper autodetecting of octal */
60     TESTCASE( strtoll( "014", NULL, 0 ) == 12 );
61     /* proper autodetecting of hexadecimal, lowercase 'x' */
62     TESTCASE( strtoll( "0xFF", NULL, 0 ) == 255 );
63     /* proper autodetecting of hexadecimal, uppercase 'X' */
64     TESTCASE( strtoll( "0XFF", NULL, 0 ) == 255 );
65     /* errno should still be 0 */
66     TESTCASE( errno == 0 );
67     /* overflowing subject sequence must still return proper endptr */
68     TESTCASE( strtoll( overflow, &endptr, 36 ) == LLONG_MIN );
69     TESTCASE( errno == ERANGE );
70     TESTCASE( ( endptr - overflow ) == 53 );
71     /* same for positive */
72     errno = 0;
73     TESTCASE( strtoll( overflow + 1, &endptr, 36 ) == LLONG_MAX );
74     TESTCASE( errno == ERANGE );
75     TESTCASE( ( endptr - overflow ) == 53 );
76     /* testing skipping of leading whitespace */
77     TESTCASE( strtoll( " \n\v\t\f123", NULL, 0 ) == 123 );
78     /* testing conversion failure */
79     TESTCASE( strtoll( overflow, &endptr, 10 ) == 0 );
80     TESTCASE( endptr == overflow );
81     endptr = NULL;
82     TESTCASE( strtoll( 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 _PDCLIB_LLONG_BYTES == 8
89     /* testing "even" overflow, i.e. base is power of two */
90     TESTCASE( strtoll( "0x7FFFFFFFFFFFFFFF", NULL, 0 ) == 0x7fffffffffffffff );
91     TESTCASE( errno == 0 );
92     TESTCASE( strtoll( "0x8000000000000000", NULL, 0 ) == LLONG_MAX );
93     TESTCASE( errno == ERANGE );
94     errno = 0;
95     TESTCASE( strtoll( "-0x7FFFFFFFFFFFFFFF", NULL, 0 ) == 0x8000000000000001 );
96     TESTCASE( errno == 0 );
97     TESTCASE( strtoll( "-0x8000000000000000", NULL, 0 ) == LLONG_MIN );
98     TESTCASE( errno == 0 );
99     TESTCASE( strtoll( "-0x8000000000000001", NULL, 0 ) == LLONG_MIN );
100     TESTCASE( errno == ERANGE );
101     /* TODO: test "odd" overflow, i.e. base is not power of two */
102 #elif _PDCLIB_LONG_BYTES == 16
103     /* testing "even" overflow, i.e. base is power of two */
104     TESTCASE( strtoll( "0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", NULL, 0 ) == 0x7fffffffffffffffffffffffffffffff );
105     TESTCASE( errno == 0 );
106     TESTCASE( strtoll( "0x80000000000000000000000000000000", NULL, 0 ) == LLONG_MAX );
107     TESTCASE( errno == ERANGE );
108     errno = 0;
109     TESTCASE( strtoll( "-0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", NULL, 0 ) == -0x80000000000000000000000000000001 );
110     TESTCASE( errno == 0 );
111     TESTCASE( strtoll( "-0x80000000000000000000000000000000", NULL, 0 ) == LLONG_MIN );
112     TESTCASE( errno == 0 );
113     TESTCASE( strtoll( "-0x80000000000000000000000000000001", NULL, 0 ) == LLONG_MIN );
114     TESTCASE( errno == ERANGE );
115     /* TODO: test "odd" overflow, i.e. base is not power of two */
116 #else
117 #error Unsupported width of 'long long' (neither 64 nor 128 bit).
118 #endif
119     return TEST_RESULTS;
120 }
121 #endif