Path: blob/master/waterbox/libc/functions/stdlib/strtoul.c
2 views
/* strtoul( const char *, char * *, int )12This file is part of the Public Domain C Library (PDCLib).3Permission is granted to use, modify, and / or redistribute at will.4*/56#include <limits.h>7#include <stdlib.h>89#ifndef REGTEST1011#include <stdint.h>1213unsigned long int strtoul( const char * s, char ** endptr, int base )14{15unsigned long int rc;16char sign = '+';17const char * p = _PDCLIB_strtox_prelim( s, &sign, &base );18if ( base < 2 || base > 36 ) return 0;19rc = (unsigned long int)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)ULONG_MAX, (uintmax_t)( ULONG_MAX / base ), (int)( ULONG_MAX % base ), &sign );20if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s;21return ( sign == '+' ) ? rc : -rc;22}2324#endif2526#ifdef TEST27#include "_PDCLIB_test.h"28#include <errno.h>2930int main( void )31{32char * endptr;33/* this, to base 36, overflows even a 256 bit integer */34char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";35/* tricky border case */36char tricky[] = "+0xz";37errno = 0;38/* basic functionality */39TESTCASE( strtoul( "123", NULL, 10 ) == 123 );40/* proper detecting of default base 10 */41TESTCASE( strtoul( "456", NULL, 0 ) == 456 );42/* proper functioning to smaller base */43TESTCASE( strtoul( "14", NULL, 8 ) == 12 );44/* proper autodetecting of octal */45TESTCASE( strtoul( "016", NULL, 0 ) == 14 );46/* proper autodetecting of hexadecimal, lowercase 'x' */47TESTCASE( strtoul( "0xFF", NULL, 0 ) == 255 );48/* proper autodetecting of hexadecimal, uppercase 'X' */49TESTCASE( strtoul( "0Xa1", NULL, 0 ) == 161 );50/* proper handling of border case: 0x followed by non-hexdigit */51TESTCASE( strtoul( tricky, &endptr, 0 ) == 0 );52TESTCASE( endptr == tricky + 2 );53/* proper handling of border case: 0 followed by non-octdigit */54TESTCASE( strtoul( tricky, &endptr, 8 ) == 0 );55TESTCASE( endptr == tricky + 2 );56/* errno should still be 0 */57TESTCASE( errno == 0 );58/* overflowing subject sequence must still return proper endptr */59TESTCASE( strtoul( overflow, &endptr, 36 ) == ULONG_MAX );60TESTCASE( errno == ERANGE );61TESTCASE( ( endptr - overflow ) == 53 );62/* same for positive */63errno = 0;64TESTCASE( strtoul( overflow + 1, &endptr, 36 ) == ULONG_MAX );65TESTCASE( errno == ERANGE );66TESTCASE( ( endptr - overflow ) == 53 );67/* testing skipping of leading whitespace */68TESTCASE( strtoul( " \n\v\t\f789", NULL, 0 ) == 789 );69/* testing conversion failure */70TESTCASE( strtoul( overflow, &endptr, 10 ) == 0 );71TESTCASE( endptr == overflow );72endptr = NULL;73TESTCASE( strtoul( overflow, &endptr, 0 ) == 0 );74TESTCASE( endptr == overflow );75/* TODO: These tests assume two-complement, but conversion should work */76/* for one-complement and signed magnitude just as well. Anyone having */77/* a platform to test this on? */78errno = 0;79/* long -> 32 bit */80#if ULONG_MAX >> 31 == 181/* testing "even" overflow, i.e. base is power of two */82TESTCASE( strtoul( "4294967295", NULL, 0 ) == ULONG_MAX );83TESTCASE( errno == 0 );84errno = 0;85TESTCASE( strtoul( "4294967296", NULL, 0 ) == ULONG_MAX );86TESTCASE( errno == ERANGE );87/* TODO: test "odd" overflow, i.e. base is not power of two */88/* long -> 64 bit */89#elif ULONG_MAX >> 63 == 190/* testing "even" overflow, i.e. base is power of two */91TESTCASE( strtoul( "18446744073709551615", NULL, 0 ) == ULONG_MAX );92TESTCASE( errno == 0 );93errno = 0;94TESTCASE( strtoul( "18446744073709551616", NULL, 0 ) == ULONG_MAX );95TESTCASE( errno == ERANGE );96/* TODO: test "odd" overflow, i.e. base is not power of two */97#else98#error Unsupported width of 'long' (neither 32 nor 64 bit).99#endif100return TEST_RESULTS;101}102103#endif104105106