HelenOS sources
This source file includes following definitions.
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
#include <pcut/pcut.h>
#include <stdlib.h>
#include <str.h>
#include <limits.h>
PCUT_INIT;
PCUT_TEST_SUITE(strtol);
PCUT_TEST(atoi)
{
int i;
i = atoi(" \t42");
PCUT_ASSERT_TRUE(i == 42);
}
PCUT_TEST(atol)
{
long li;
li = atol(" \t42");
PCUT_ASSERT_TRUE(li == 42);
}
PCUT_TEST(atoll)
{
long long lli;
lli = atoll(" \t42");
PCUT_ASSERT_TRUE(lli == 42);
}
PCUT_TEST(strtol)
{
long li;
char *ep;
li = strtol(" \t42x", &ep, 10);
PCUT_ASSERT_TRUE(li == 42);
PCUT_ASSERT_TRUE(*ep == 'x');
}
PCUT_TEST(strtol_dec_auto)
{
long li;
char *ep;
li = strtol(" \t42x", &ep, 0);
PCUT_ASSERT_TRUE(li == 42);
PCUT_ASSERT_TRUE(*ep == 'x');
}
PCUT_TEST(strtol_oct)
{
long li;
char *ep;
li = strtol(" \t052x", &ep, 8);
PCUT_ASSERT_TRUE(li == 052);
PCUT_ASSERT_TRUE(*ep == 'x');
}
#include <stdio.h>
PCUT_TEST(strtol_oct_prefix)
{
long li;
char *ep;
li = strtol(" \t052x", &ep, 0);
printf("li=%ld (0%lo)\n", li, li);
PCUT_ASSERT_TRUE(li == 052);
PCUT_ASSERT_TRUE(*ep == 'x');
}
PCUT_TEST(strtol_hex)
{
long li;
char *ep;
li = strtol(" \t2ax", &ep, 16);
PCUT_ASSERT_TRUE(li == 0x2a);
PCUT_ASSERT_TRUE(*ep == 'x');
}
PCUT_TEST(strtol_hex_prefixed)
{
long li;
char *ep;
li = strtol(" \t0x2ax", &ep, 0);
PCUT_ASSERT_TRUE(li == 0x2a);
PCUT_ASSERT_TRUE(*ep == 'x');
}
PCUT_TEST(strtol_base16_prefix)
{
long li;
char *ep;
li = strtol(" \t0x1y", &ep, 16);
printf("li=%ld\n", li);
PCUT_ASSERT_TRUE(li == 1);
PCUT_ASSERT_TRUE(*ep == 'y');
}
PCUT_TEST(strtol_base36)
{
long li;
char *ep;
li = strtol(" \tz1.", &ep, 36);
PCUT_ASSERT_TRUE(li == 35 * 36 + 1);
PCUT_ASSERT_TRUE(*ep == '.');
}
PCUT_TEST(str_uint_hex)
{
int base;
bool strict;
const char *endp;
uint32_t result;
errno_t rc;
const char *input;
rc = str_uint32_t(input = "0x10", &endp, base = 0, strict = false, &result);
PCUT_ASSERT_INT_EQUALS(EOK, rc);
PCUT_ASSERT_INT_EQUALS(0x10, result);
PCUT_ASSERT_PTR_EQUALS(input + 4, endp);
rc = str_uint32_t(input = "0x10", &endp, base = 16, strict = false, &result);
PCUT_ASSERT_INT_EQUALS(EOK, rc);
PCUT_ASSERT_INT_EQUALS(0, result);
PCUT_ASSERT_PTR_EQUALS(input + 1, endp);
rc = str_uint32_t(input = " \t0x10", &endp, base = 0, strict = false, &result);
PCUT_ASSERT_INT_EQUALS(EOK, rc);
PCUT_ASSERT_INT_EQUALS(0x10, result);
PCUT_ASSERT_PTR_EQUALS(input + 7, endp);
rc = str_uint32_t(input = " \t0x10", &endp, base = 16, strict = false, &result);
PCUT_ASSERT_INT_EQUALS(EOK, rc);
PCUT_ASSERT_INT_EQUALS(0, result);
PCUT_ASSERT_PTR_EQUALS(input + 4, endp);
}
PCUT_TEST(str_uint_overflow)
{
int base;
bool strict;
uint64_t result;
errno_t rc;
const char *input;
rc = str_uint64_t(input = "0xffffffffffffffffffffffffffffffff", NULL, base = 0, strict = false, &result);
#if 0
PCUT_ASSERT_INT_EQUALS(EOK, rc);
PCUT_ASSERT_UINT_EQUALS(0xffffffffffffffff, result);
#else
PCUT_ASSERT_INT_EQUALS(EOVERFLOW, rc);
#endif
rc = str_uint64_t(input = "1" "0000000000" "0000000000" "0000000000" "0000000000", NULL, base = 3, strict = false, &result);
PCUT_ASSERT_INT_EQUALS(EOK, rc);
PCUT_ASSERT_UINT_EQUALS(0xa8b8b452291fe821, result);
rc = str_uint64_t(input = "10" "0000000000" "0000000000" "0000000000" "0000000000", NULL, base = 3, strict = false, &result);
#if 0
PCUT_ASSERT_INT_EQUALS(EOK, rc);
PCUT_ASSERT_UINT_EQUALS(0xfa2a1cf67b5fb863, result);
#else
PCUT_ASSERT_INT_EQUALS(EOVERFLOW, rc);
#endif
}
PCUT_TEST(strtoul_negative_wraparound)
{
long output;
char *endp;
char *endp_unchanged = (char *) "endp_unchanged unique pointer";
int errno_unchanged = -1;
const char *input;
int base;
endp = endp_unchanged;
errno = errno_unchanged;
output = strtoul(input = "-10", &endp, base = 0);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input + 3, endp);
PCUT_ASSERT_UINT_EQUALS(-(10ul), output);
}
PCUT_TEST(strtol_fringe)
{
long output;
char *endp;
char *endp_unchanged = (char *) "endp_unchanged unique pointer";
int errno_unchanged = -1;
const char *input;
int base;
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = "0", &endp, base = 0);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input + 1, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = "0", &endp, base = -10);
PCUT_ASSERT_INT_EQUALS(EINVAL, errno);
PCUT_ASSERT_PTR_EQUALS(endp_unchanged, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = "0", &endp, base = 1);
PCUT_ASSERT_INT_EQUALS(EINVAL, errno);
PCUT_ASSERT_PTR_EQUALS(endp_unchanged, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
for (base = 2; base <= 36; base++) {
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = "0", &endp, base);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input + 1, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = "1", &endp, base);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input + 1, endp);
PCUT_ASSERT_INT_EQUALS(1, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = "10", &endp, base);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input + 2, endp);
PCUT_ASSERT_INT_EQUALS(base, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = "100", &endp, base);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input + 3, endp);
PCUT_ASSERT_INT_EQUALS(base * base, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = "1000", &endp, base);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input + 4, endp);
PCUT_ASSERT_INT_EQUALS(base * base * base, output);
}
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = "0", &endp, base = 8);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input + 1, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = "0", &endp, base = 10);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input + 1, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = "0", &endp, base = 16);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input + 1, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = "0", &endp, base = 36);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input + 1, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = "0", &endp, base = 37);
PCUT_ASSERT_INT_EQUALS(EINVAL, errno);
PCUT_ASSERT_PTR_EQUALS(endp_unchanged, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = "0", &endp, base = 37);
PCUT_ASSERT_INT_EQUALS(EINVAL, errno);
PCUT_ASSERT_PTR_EQUALS(endp_unchanged, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = "", &endp, base = 0);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = " ", &endp, base = 0);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = " ", &endp, base = 10);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = " x", &endp, base = 0);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = " x0", &endp, base = 0);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = " 0x", &endp, base = 0);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input + 5, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = " 0xg", &endp, base = 0);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input + 5, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = " 0x1", &endp, base = 0);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input + 7, endp);
PCUT_ASSERT_INT_EQUALS(1, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = " 0x", &endp, base = 16);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input + 5, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = " 0xg", &endp, base = 16);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input + 5, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = " g", &endp, base = 16);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = " 0x1", &endp, base = 16);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input + 7, endp);
PCUT_ASSERT_INT_EQUALS(1, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = " +", &endp, base = 0);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = " -", &endp, base = 0);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = " +", &endp, base = 10);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = " -", &endp, base = 10);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = "+", &endp, base = 0);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = "-", &endp, base = 0);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = "+", &endp, base = 10);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
endp = endp_unchanged;
errno = errno_unchanged;
output = strtol(input = "-", &endp, base = 10);
PCUT_ASSERT_INT_EQUALS(errno_unchanged, errno);
PCUT_ASSERT_PTR_EQUALS(input, endp);
PCUT_ASSERT_INT_EQUALS(0, output);
}
PCUT_EXPORT(strtol);
HelenOS homepage, sources at GitHub