Path: blob/master/waterbox/libc/functions/stdlib/strtol.c
2 views
/* strtol( 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>1213long int strtol( const char * s, char ** endptr, int base )14{15long int rc;16char sign = '+';17const char * p = _PDCLIB_strtox_prelim( s, &sign, &base );18if ( base < 2 || base > 36 ) return 0;19if ( sign == '+' )20{21rc = (long int)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LONG_MAX, (uintmax_t)( LONG_MAX / base ), (int)( LONG_MAX % base ), &sign );22}23else24{25rc = (long int)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LONG_MIN, (uintmax_t)( LONG_MIN / -base ), (int)( -( LONG_MIN % base ) ), &sign );26}27if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s;28return ( sign == '+' ) ? rc : -rc;29}3031#endif3233#ifdef TEST34#include "_PDCLIB_test.h"3536#include <errno.h>3738int main( void )39{40char * endptr;41/* this, to base 36, overflows even a 256 bit integer */42char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";43/* tricky border case */44char tricky[] = "+0xz";45errno = 0;46/* basic functionality */47TESTCASE( strtol( "123", NULL, 10 ) == 123 );48/* proper detecting of default base 10 */49TESTCASE( strtol( "456", NULL, 0 ) == 456 );50/* proper functioning to smaller base */51TESTCASE( strtol( "14", NULL, 8 ) == 12 );52/* proper autodetecting of octal */53TESTCASE( strtol( "016", NULL, 0 ) == 14 );54/* proper autodetecting of hexadecimal, lowercase 'x' */55TESTCASE( strtol( "0xFF", NULL, 0 ) == 255 );56/* proper autodetecting of hexadecimal, uppercase 'X' */57TESTCASE( strtol( "0Xa1", NULL, 0 ) == 161 );58/* proper handling of border case: 0x followed by non-hexdigit */59TESTCASE( strtol( tricky, &endptr, 0 ) == 0 );60TESTCASE( endptr == tricky + 2 );61/* proper handling of border case: 0 followed by non-octdigit */62TESTCASE( strtol( tricky, &endptr, 8 ) == 0 );63TESTCASE( endptr == tricky + 2 );64/* errno should still be 0 */65TESTCASE( errno == 0 );66/* overflowing subject sequence must still return proper endptr */67TESTCASE( strtol( overflow, &endptr, 36 ) == LONG_MIN );68TESTCASE( errno == ERANGE );69TESTCASE( ( endptr - overflow ) == 53 );70/* same for positive */71errno = 0;72TESTCASE( strtol( overflow + 1, &endptr, 36 ) == LONG_MAX );73TESTCASE( errno == ERANGE );74TESTCASE( ( endptr - overflow ) == 53 );75/* testing skipping of leading whitespace */76TESTCASE( strtol( " \n\v\t\f789", NULL, 0 ) == 789 );77/* testing conversion failure */78TESTCASE( strtol( overflow, &endptr, 10 ) == 0 );79TESTCASE( endptr == overflow );80endptr = NULL;81TESTCASE( strtol( overflow, &endptr, 0 ) == 0 );82TESTCASE( endptr == overflow );83/* TODO: These tests assume two-complement, but conversion should work */84/* for one-complement and signed magnitude just as well. Anyone having */85/* a platform to test this on? */86errno = 0;87#if LONG_MAX >> 30 == 188/* testing "even" overflow, i.e. base is power of two */89TESTCASE( strtol( "2147483647", NULL, 0 ) == 0x7fffffff );90TESTCASE( errno == 0 );91errno = 0;92TESTCASE( strtol( "2147483648", NULL, 0 ) == LONG_MAX );93TESTCASE( errno == ERANGE );94errno = 0;95TESTCASE( strtol( "-2147483647", NULL, 0 ) == (long)0x80000001 );96TESTCASE( errno == 0 );97errno = 0;98TESTCASE( strtol( "-2147483648", NULL, 0 ) == LONG_MIN );99TESTCASE( errno == 0 );100errno = 0;101TESTCASE( strtol( "-2147483649", NULL, 0 ) == LONG_MIN );102TESTCASE( errno == ERANGE );103/* TODO: test "odd" overflow, i.e. base is not power of two */104#elif LONG_MAX >> 62 == 1105/* testing "even" overflow, i.e. base is power of two */106TESTCASE( strtol( "9223372036854775807", NULL, 0 ) == 0x7fffffffffffffff );107TESTCASE( errno == 0 );108errno = 0;109TESTCASE( strtol( "9223372036854775808", NULL, 0 ) == LONG_MAX );110TESTCASE( errno == ERANGE );111errno = 0;112TESTCASE( strtol( "-9223372036854775807", NULL, 0 ) == (long)0x8000000000000001 );113TESTCASE( errno == 0 );114errno = 0;115TESTCASE( strtol( "-9223372036854775808", NULL, 0 ) == LONG_MIN );116TESTCASE( errno == 0 );117errno = 0;118TESTCASE( strtol( "-9223372036854775809", NULL, 0 ) == LONG_MIN );119TESTCASE( errno == ERANGE );120/* TODO: test "odd" overflow, i.e. base is not power of two */121#else122#error Unsupported width of 'long' (neither 32 nor 64 bit).123#endif124return TEST_RESULTS;125}126127#endif128129130