Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/waterbox/libc/functions/stdlib/strtoul.c
2 views
1
/* strtoul( 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 <stdlib.h>
9
10
#ifndef REGTEST
11
12
#include <stdint.h>
13
14
unsigned long int strtoul( const char * s, char ** endptr, int base )
15
{
16
unsigned long int rc;
17
char sign = '+';
18
const char * p = _PDCLIB_strtox_prelim( s, &sign, &base );
19
if ( base < 2 || base > 36 ) return 0;
20
rc = (unsigned long int)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)ULONG_MAX, (uintmax_t)( ULONG_MAX / base ), (int)( ULONG_MAX % base ), &sign );
21
if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s;
22
return ( sign == '+' ) ? rc : -rc;
23
}
24
25
#endif
26
27
#ifdef TEST
28
#include "_PDCLIB_test.h"
29
#include <errno.h>
30
31
int main( void )
32
{
33
char * endptr;
34
/* this, to base 36, overflows even a 256 bit integer */
35
char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
36
/* tricky border case */
37
char tricky[] = "+0xz";
38
errno = 0;
39
/* basic functionality */
40
TESTCASE( strtoul( "123", NULL, 10 ) == 123 );
41
/* proper detecting of default base 10 */
42
TESTCASE( strtoul( "456", NULL, 0 ) == 456 );
43
/* proper functioning to smaller base */
44
TESTCASE( strtoul( "14", NULL, 8 ) == 12 );
45
/* proper autodetecting of octal */
46
TESTCASE( strtoul( "016", NULL, 0 ) == 14 );
47
/* proper autodetecting of hexadecimal, lowercase 'x' */
48
TESTCASE( strtoul( "0xFF", NULL, 0 ) == 255 );
49
/* proper autodetecting of hexadecimal, uppercase 'X' */
50
TESTCASE( strtoul( "0Xa1", NULL, 0 ) == 161 );
51
/* proper handling of border case: 0x followed by non-hexdigit */
52
TESTCASE( strtoul( tricky, &endptr, 0 ) == 0 );
53
TESTCASE( endptr == tricky + 2 );
54
/* proper handling of border case: 0 followed by non-octdigit */
55
TESTCASE( strtoul( tricky, &endptr, 8 ) == 0 );
56
TESTCASE( endptr == tricky + 2 );
57
/* errno should still be 0 */
58
TESTCASE( errno == 0 );
59
/* overflowing subject sequence must still return proper endptr */
60
TESTCASE( strtoul( overflow, &endptr, 36 ) == ULONG_MAX );
61
TESTCASE( errno == ERANGE );
62
TESTCASE( ( endptr - overflow ) == 53 );
63
/* same for positive */
64
errno = 0;
65
TESTCASE( strtoul( overflow + 1, &endptr, 36 ) == ULONG_MAX );
66
TESTCASE( errno == ERANGE );
67
TESTCASE( ( endptr - overflow ) == 53 );
68
/* testing skipping of leading whitespace */
69
TESTCASE( strtoul( " \n\v\t\f789", NULL, 0 ) == 789 );
70
/* testing conversion failure */
71
TESTCASE( strtoul( overflow, &endptr, 10 ) == 0 );
72
TESTCASE( endptr == overflow );
73
endptr = NULL;
74
TESTCASE( strtoul( overflow, &endptr, 0 ) == 0 );
75
TESTCASE( endptr == overflow );
76
/* TODO: These tests assume two-complement, but conversion should work */
77
/* for one-complement and signed magnitude just as well. Anyone having */
78
/* a platform to test this on? */
79
errno = 0;
80
/* long -> 32 bit */
81
#if ULONG_MAX >> 31 == 1
82
/* testing "even" overflow, i.e. base is power of two */
83
TESTCASE( strtoul( "4294967295", NULL, 0 ) == ULONG_MAX );
84
TESTCASE( errno == 0 );
85
errno = 0;
86
TESTCASE( strtoul( "4294967296", NULL, 0 ) == ULONG_MAX );
87
TESTCASE( errno == ERANGE );
88
/* TODO: test "odd" overflow, i.e. base is not power of two */
89
/* long -> 64 bit */
90
#elif ULONG_MAX >> 63 == 1
91
/* testing "even" overflow, i.e. base is power of two */
92
TESTCASE( strtoul( "18446744073709551615", NULL, 0 ) == ULONG_MAX );
93
TESTCASE( errno == 0 );
94
errno = 0;
95
TESTCASE( strtoul( "18446744073709551616", NULL, 0 ) == ULONG_MAX );
96
TESTCASE( errno == ERANGE );
97
/* TODO: test "odd" overflow, i.e. base is not power of two */
98
#else
99
#error Unsupported width of 'long' (neither 32 nor 64 bit).
100
#endif
101
return TEST_RESULTS;
102
}
103
104
#endif
105
106