X-Git-Url: https://pd.if.org/git/?p=pdclib;a=blobdiff_plain;f=functions%2Fstdlib%2Fstrtoul.c;h=568029756b69af9267be04b790ad5a086f0ba4bc;hp=4aefc4f08785c6b6dd934f6abfdf9efa0907d8ee;hb=45f394553721285e7bc6187419613c33915cce21;hpb=a381f50d5325748b70096ac5f5d3f63ae4f6e18a diff --git a/functions/stdlib/strtoul.c b/functions/stdlib/strtoul.c index 4aefc4f..5680297 100644 --- a/functions/stdlib/strtoul.c +++ b/functions/stdlib/strtoul.c @@ -75,6 +75,29 @@ int main( void ) endptr = NULL; TESTCASE( strtoul( overflow, &endptr, 0 ) == 0 ); TESTCASE( endptr == overflow ); + /* TODO: These tests assume two-complement, but conversion should work */ + /* for one-complement and signed magnitude just as well. Anyone having */ + /* a platform to test this on? */ + errno = 0; +#if ULONG_MAX == 0xffffffffL + /* testing "even" overflow, i.e. base is power of two */ + TESTCASE( strtoul( "4294967295", NULL, 0 ) == ULONG_MAX ); + TESTCASE( errno == 0 ); + errno = 0; + TESTCASE( strtoul( "4294967296", NULL, 0 ) == ULONG_MAX ); + TESTCASE( errno == ERANGE ); + /* TODO: test "odd" overflow, i.e. base is not power of two */ +#elif ULONG_MAX == 0xffffffffffffffffL + /* testing "even" overflow, i.e. base is power of two */ + TESTCASE( strtoul( "18446744073709551615", NULL, 0 ) == ULONG_MAX ); + TESTCASE( errno == 0 ); + errno = 0; + TESTCASE( strtoul( "18446744073709551616", NULL, 0 ) == ULONG_MAX ); + TESTCASE( errno == ERANGE ); + /* TODO: test "odd" overflow, i.e. base is not power of two */ +#else +#error Unsupported width of 'long' (neither 32 nor 64 bit). +#endif return TEST_RESULTS; }