Files

114 lines
4.3 KiB
C

// SPDX-License-Identifier: GPL-3.0-or-later
#ifdef CMOC_HEADER_FIRST
#include <cmoc.h>
#include <stdlib.h>
#else
#include <stdlib.h>
#include <cmoc.h>
#endif
#include <errno.h>
#include <limits.h>
// The upstream cmoc.h has decimal-only macros. Both include orders must
// expose the base-aware calls and their addresses when compatibility is on.
static long (*signedParser)(const char *, char **, int) = strtol;
static unsigned long (*unsignedParser)(const char *, char **, int) = strtoul;
#define UNCHANGED 73
struct Conversion {
const char *text;
int base;
unsigned consumed;
long signedValue;
unsigned long unsignedValue;
int signedError;
int unsignedError;
};
static const struct Conversion cases[] = {
{ "", 10, 0, 0, 0, UNCHANGED, UNCHANGED },
{ " ", 0, 0, 0, 0, UNCHANGED, UNCHANGED },
{ " +!", 10, 0, 0, 0, UNCHANGED, UNCHANGED },
{ "-", 10, 0, 0, 0, UNCHANGED, UNCHANGED },
{ "MARS", 10, 0, 0, 0, UNCHANGED, UNCHANGED },
{ "0", 0, 1, 0, 0, UNCHANGED, UNCHANGED },
{ " -0", 0, 4, 0, 0, UNCHANGED, UNCHANGED },
{ "+0779", 0, 4, 63, 63, UNCHANGED, UNCHANGED },
{ "09", 10, 2, 9, 9, UNCHANGED, UNCHANGED },
{ "0x", 0, 1, 0, 0, UNCHANGED, UNCHANGED },
{ "0XG", 16, 1, 0, 0, UNCHANGED, UNCHANGED },
{ "0x12", 10, 1, 0, 0, UNCHANGED, UNCHANGED },
{ "0b11", 0, 1, 0, 0, UNCHANGED, UNCHANGED },
{ "0X010f!", 0, 6, 271, 271, UNCHANGED, UNCHANGED },
{ "2X", 0, 1, 2, 2, UNCHANGED, UNCHANGED },
{ "\t\n\v\f\r +1012", 2, 10, 5, 5, UNCHANGED, UNCHANGED },
{ " -aB", 16, 4, -171, 4294967125UL, UNCHANGED, UNCHANGED },
{ "zZ.", 36, 2, 1295, 1295, UNCHANGED, UNCHANGED },
{ "2147483647", 10, 10, LONG_MAX, 2147483647UL, UNCHANGED, UNCHANGED },
{ "2147483648", 10, 10, LONG_MAX, 2147483648UL, ERANGE, UNCHANGED },
{ "-2147483648", 10, 11, -2147483648L, 2147483648UL, UNCHANGED, UNCHANGED },
{ "-2147483649", 10, 11, -2147483648L, 2147483647UL, ERANGE, UNCHANGED },
{ "4294967295!", 10, 10, LONG_MAX, ULONG_MAX, ERANGE, UNCHANGED },
{ "4294967296", 10, 10, LONG_MAX, ULONG_MAX, ERANGE, ERANGE },
{ "-4294967295", 10, 11, -2147483648L, 1, ERANGE, UNCHANGED },
{ "-4294967296", 10, 11, -2147483648L, ULONG_MAX, ERANGE, ERANGE },
{ "0xffffffff!", 0, 10, LONG_MAX, ULONG_MAX, ERANGE, UNCHANGED },
{ "-0x80000000", 0, 11, -2147483648L, 2147483648UL, UNCHANGED, UNCHANGED },
{ "100000000000000000000000000000!", 16, 30, LONG_MAX, ULONG_MAX, ERANGE, ERANGE },
{ "37777777777!", 8, 11, LONG_MAX, ULONG_MAX, ERANGE, UNCHANGED },
{ "40000000000!", 8, 11, LONG_MAX, ULONG_MAX, ERANGE, ERANGE },
{ " +42", -2, 0, 0, 0, EINVAL, EINVAL },
{ " +42", 1, 0, 0, 0, EINVAL, EINVAL },
{ " +42", 37, 0, 0, 0, EINVAL, EINVAL }
};
static int check(void)
{
unsigned i;
for (i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) {
const struct Conversion *c = &cases[i];
char *end = NULL;
errno = UNCHANGED;
long s = signedParser(c->text, &end, c->base);
if (s != c->signedValue || errno != c->signedError || end != c->text + c->consumed) {
printf("SIGNED CASE %u\n", i);
return __LINE__;
}
end = NULL;
errno = UNCHANGED;
unsigned long u = unsignedParser(c->text, &end, c->base);
if (u != c->unsignedValue || errno != c->unsignedError || end != c->text + c->consumed) {
printf("UNSIGNED CASE %u\n", i);
return __LINE__;
}
}
// One more than the highest digit is a terminator in each radix.
const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz!";
for (i = 2; i <= 36; ++i) {
char text[4], *end;
text[0] = '1';
text[1] = digits[i - 1];
text[2] = digits[i];
text[3] = 0;
if (strtol(text, &end, i) != (long)(2 * i - 1) || end != text + 2)
return __LINE__;
if (strtoul(text, &end, i) != (unsigned long)(2 * i - 1) || end != text + 2)
return __LINE__;
}
if (strtoul("ff", NULL, 16) != 255 || strtol("-10", NULL, 2) != -2)
return __LINE__;
char highByte[] = { (char) 0xff, '3', 0 };
char *end;
if (strtol(highByte, &end, 10) != 0 || end != highByte)
return __LINE__;
return 0;
}
int main(void)
{
int line = check();
if (line) printf("FAIL LINE %d\n", line);
else printf("NUMBER CONVERSIONS PASSED\n");
return line != 0;
}