|
nsz |
1be84a |
#include <stdio.h>
|
|
nsz |
1be84a |
#include <stdlib.h>
|
|
nsz |
1be84a |
#include <string.h>
|
|
nsz |
1be84a |
#include <errno.h>
|
|
nsz |
1be84a |
#include "test.h"
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
/* r = place to store result
|
|
nsz |
1be84a |
* f = function call to test (or any expression)
|
|
nsz |
1be84a |
* x = expected result
|
|
nsz |
1be84a |
* m = message to print on failure (with formats for r & x)
|
|
nsz |
1be84a |
**/
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
#define TEST(r, f, x, m) ( \
|
|
nsz |
1be84a |
errno = 0, msg = #f, ((r) = (f)) == (x) || \
|
|
Szabolcs Nagy |
cfa23c |
(t_error("%s failed (" m ")\n", #f, r, x), 0) )
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
#define TEST2(r, f, x, m) ( \
|
|
nsz |
1be84a |
((r) = (f)) == (x) || \
|
|
Szabolcs Nagy |
cfa23c |
(t_error("%s failed (" m ")\n", msg, r, x), 0) )
|
|
nsz |
1be84a |
|
|
nsz |
462b4f |
int main(void)
|
|
nsz |
462b4f |
{
|
|
nsz |
1be84a |
int i;
|
|
nsz |
1be84a |
long l;
|
|
nsz |
1be84a |
unsigned long ul;
|
|
Szabolcs Nagy |
ad7d56 |
long long ll;
|
|
Szabolcs Nagy |
ad7d56 |
unsigned long long ull;
|
|
nsz |
1be84a |
char *msg="";
|
|
nsz |
1be84a |
char *s, *c;
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
TEST(l, atol("2147483647"), 2147483647L, "max 32bit signed %ld != %ld");
|
|
nsz |
1be84a |
TEST(l, strtol("2147483647", 0, 0), 2147483647L, "max 32bit signed %ld != %ld");
|
|
nsz |
1be84a |
TEST(ul, strtoul("4294967295", 0, 0), 4294967295UL, "max 32bit unsigned %lu != %lu");
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
if (sizeof(long) == 4) {
|
|
nsz |
1be84a |
TEST(l, strtol(s="2147483648", &c, 0), 2147483647L, "uncaught overflow %ld != %ld");
|
|
nsz |
1be84a |
TEST2(i, c-s, 10, "wrong final position %d != %d");
|
|
nsz |
1be84a |
TEST2(i, errno, ERANGE, "missing errno %d != %d");
|
|
nsz |
1be84a |
TEST(l, strtol(s="-2147483649", &c, 0), -2147483647L-1, "uncaught overflow %ld != %ld");
|
|
nsz |
1be84a |
TEST2(i, c-s, 11, "wrong final position %d != %d");
|
|
nsz |
1be84a |
TEST2(i, errno, ERANGE, "missing errno %d != %d");
|
|
nsz |
1be84a |
TEST(ul, strtoul(s="4294967296", &c, 0), 4294967295UL, "uncaught overflow %lu != %lu");
|
|
nsz |
1be84a |
TEST2(i, c-s, 10, "wrong final position %d != %d");
|
|
nsz |
1be84a |
TEST2(i, errno, ERANGE, "missing errno %d != %d");
|
|
nsz |
8f27a3 |
TEST(ul, strtoul(s="-1", &c, 0), -1UL, "rejected negative %lu != %lu");
|
|
nsz |
1be84a |
TEST2(i, c-s, 2, "wrong final position %d != %d");
|
|
nsz |
8f27a3 |
TEST2(i, errno, 0, "spurious errno %d != %d");
|
|
nsz |
8f27a3 |
TEST(ul, strtoul(s="-2", &c, 0), -2UL, "rejected negative %lu != %lu");
|
|
nsz |
1be84a |
TEST2(i, c-s, 2, "wrong final position %d != %d");
|
|
nsz |
8f27a3 |
TEST2(i, errno, 0, "spurious errno %d != %d");
|
|
nsz |
8f27a3 |
TEST(ul, strtoul(s="-2147483648", &c, 0), -2147483648UL, "rejected negative %lu != %lu");
|
|
nsz |
1be84a |
TEST2(i, c-s, 11, "wrong final position %d != %d");
|
|
nsz |
8f27a3 |
TEST2(i, errno, 0, "spurious errno %d != %d");
|
|
nsz |
8f27a3 |
TEST(ul, strtoul(s="-2147483649", &c, 0), -2147483649UL, "rejected negative %lu != %lu");
|
|
nsz |
086c6a |
TEST2(i, c-s, 11, "wrong final position %d != %d");
|
|
nsz |
8f27a3 |
TEST2(i, errno, 0, "spurious errno %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST(ul, strtoul(s="-4294967296", &c, 0), 4294967295UL, "uncaught negative overflow %lu != %lu");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, c-s, 11, "wrong final position %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, errno, ERANGE, "spurious errno %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
} else if (sizeof(long) == 8) {
|
|
Szabolcs Nagy |
ad7d56 |
TEST(l, strtol(s="9223372036854775808", &c, 0), 9223372036854775807L, "uncaught overflow %ld != %ld");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, c-s, 19, "wrong final position %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, errno, ERANGE, "missing errno %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST(l, strtol(s="-9223372036854775809", &c, 0), -9223372036854775807L-1, "uncaught overflow %ld != %ld");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, c-s, 20, "wrong final position %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, errno, ERANGE, "missing errno %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST(ul, strtoul(s="18446744073709551616", &c, 0), 18446744073709551615UL, "uncaught overflow %lu != %lu");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, c-s, 20, "wrong final position %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, errno, ERANGE, "missing errno %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST(ul, strtoul(s="-1", &c, 0), -1UL, "rejected negative %lu != %lu");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, c-s, 2, "wrong final position %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, errno, 0, "spurious errno %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST(ul, strtoul(s="-2", &c, 0), -2UL, "rejected negative %lu != %lu");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, c-s, 2, "wrong final position %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, errno, 0, "spurious errno %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST(ul, strtoul(s="-9223372036854775808", &c, 0), -9223372036854775808UL, "rejected negative %lu != %lu");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, c-s, 20, "wrong final position %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, errno, 0, "spurious errno %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST(ul, strtoul(s="-9223372036854775809", &c, 0), -9223372036854775809UL, "rejected negative %lu != %lu");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, c-s, 20, "wrong final position %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, errno, 0, "spurious errno %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST(ul, strtoul(s="-18446744073709551616", &c, 0), 18446744073709551615UL, "uncaught negative overflow %lu != %lu");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, c-s, 21, "wrong final position %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, errno, ERANGE, "spurious errno %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
} else {
|
|
Szabolcs Nagy |
ad7d56 |
t_error("sizeof(long) == %d, not implemented\n", (int)sizeof(long));
|
|
Szabolcs Nagy |
ad7d56 |
}
|
|
Szabolcs Nagy |
ad7d56 |
|
|
Szabolcs Nagy |
ad7d56 |
if (sizeof(long long) == 8) {
|
|
Szabolcs Nagy |
ad7d56 |
TEST(ll, strtoll(s="9223372036854775808", &c, 0), 9223372036854775807LL, "uncaught overflow %lld != %lld");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, c-s, 19, "wrong final position %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, errno, ERANGE, "missing errno %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST(ll, strtoll(s="-9223372036854775809", &c, 0), -9223372036854775807LL-1, "uncaught overflow %lld != %lld");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, c-s, 20, "wrong final position %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, errno, ERANGE, "missing errno %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST(ull, strtoull(s="18446744073709551616", &c, 0), 18446744073709551615ULL, "uncaught overflow %llu != %llu");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, c-s, 20, "wrong final position %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, errno, ERANGE, "missing errno %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST(ull, strtoull(s="-1", &c, 0), -1ULL, "rejected negative %llu != %llu");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, c-s, 2, "wrong final position %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, errno, 0, "spurious errno %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST(ull, strtoull(s="-2", &c, 0), -2ULL, "rejected negative %llu != %llu");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, c-s, 2, "wrong final position %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, errno, 0, "spurious errno %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST(ull, strtoull(s="-9223372036854775808", &c, 0), -9223372036854775808ULL, "rejected negative %llu != %llu");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, c-s, 20, "wrong final position %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, errno, 0, "spurious errno %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST(ull, strtoull(s="-9223372036854775809", &c, 0), -9223372036854775809ULL, "rejected negative %llu != %llu");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, c-s, 20, "wrong final position %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, errno, 0, "spurious errno %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST(ull, strtoull(s="-18446744073709551616", &c, 0), 18446744073709551615ULL, "uncaught negative overflow %llu != %llu");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, c-s, 21, "wrong final position %d != %d");
|
|
Szabolcs Nagy |
ad7d56 |
TEST2(i, errno, ERANGE, "spurious errno %d != %d");
|
|
nsz |
1be84a |
} else {
|
|
Szabolcs Nagy |
ad7d56 |
t_error("sizeof(long long) == %d, not implemented\n", (int)sizeof(long long));
|
|
nsz |
1be84a |
}
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
TEST(l, strtol("z", 0, 36), 35, "%ld != %ld");
|
|
nsz |
1be84a |
TEST(l, strtol("00010010001101000101011001111000", 0, 2), 0x12345678, "%ld != %ld");
|
|
nsz |
1be84a |
TEST(l, strtol(s="0F5F", &c, 16), 0x0f5f, "%ld != %ld");
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
TEST(l, strtol(s="0xz", &c, 16), 0, "%ld != %ld");
|
|
nsz |
1be84a |
TEST2(i, c-s, 1, "wrong final position %ld != %ld");
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
TEST(l, strtol(s="0x1234", &c, 16), 0x1234, "%ld != %ld");
|
|
nsz |
1be84a |
TEST2(i, c-s, 6, "wrong final position %ld != %ld");
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
c = NULL;
|
|
nsz |
1be84a |
TEST(l, strtol(s="123", &c, 37), 0, "%ld != %ld");
|
|
nsz |
1be84a |
TEST2(i, c-s, 0, "wrong final position %d != %d");
|
|
nsz |
1be84a |
TEST2(i, errno, EINVAL, "%d != %d");
|
|
Szabolcs Nagy |
0c5fb5 |
|
|
Szabolcs Nagy |
0c5fb5 |
TEST(l, strtol(s=" 15437", &c, 8), 015437, "%ld != %ld");
|
|
Szabolcs Nagy |
0c5fb5 |
TEST2(i, c-s, 7, "wrong final position %d != %d");
|
|
Szabolcs Nagy |
0c5fb5 |
|
|
Szabolcs Nagy |
0c5fb5 |
TEST(l, strtol(s=" 1", &c, 0), 1, "%ld != %ld");
|
|
Szabolcs Nagy |
0c5fb5 |
TEST2(i, c-s, 3, "wrong final position %d != %d");
|
|
Szabolcs Nagy |
cfa23c |
return t_status;
|
|
nsz |
1be84a |
}
|