test-strnum.c revision 814bf67459ad405a157af0b8940602024d7fadfe
/* Copyright (c) 2014-2015 Dovecot authors, see the included COPYING file */
#include "test-lib.h"
#define INVALID(n) { #n, -1, 0 }
#define VALID(n) { #n, 0, n }
/* always pads with leading zeros to a size of 9 digits */
{
#define BIGBASE 1000000000ull
#define STRINGIFY(s) #s
#define STRINGIFY2(s) STRINGIFY(s)
int len = 0;
}
}
static void test_str_to_uintmax(void)
{
unsigned int i=0;
test_begin("str_to_uintmax in range");
const char *endp;
if (value >= 64)
test_assert_idx(ret == 0, i);
/* test with trailing noise */
value_back = 0x1234567890123456;
test_assert_idx(ret < 0, i);
test_assert_idx(ret == 0, i);
i++;
}
test_end();
/* not knowing exactly how large a uintmax_t is, we have to construct
the troublesome near-10/9*MAX strings manually by appending digits
to a MAX/9 string which we can easily create. Do a wider range
of 30 rather than the obvious 10, just in case - all are too large.*/
test_begin("str_to_uintmax overflow corner case");
for(i = 0; i <= 30; ++i) {
int j = len + 1;
while (buff[--j] == '9')
buff[j] = '0';
buff[j]++;
}
test_end();
}
/* always pads with leading zeros to a size of 9 digits */
{
#define BIGBASE 0x1000000000ull
#define STRINGIFY(s) #s
#define STRINGIFY2(s) STRINGIFY(s)
int len = 0;
}
}
static void test_str_to_uintmax_hex(void)
{
unsigned int i=0;
test_begin("str_to_uintmax_hex in range");
const char *endp;
if (value >= 64)
test_assert_idx(ret == 0, i);
/* test with trailing noise */
value_back = 0x1234567890123456;
test_assert_idx(ret < 0, i);
test_assert_idx(ret == 0, i);
i++;
}
test_end();
/* not knowing exactly how large a uintmax_t is, we have to construct
the troublesome near-0x10/0x0F*MAX strings manually by appending digits
to a MAX/0x0f string which we can easily create. Do a wider range
of 0x30 rather than the obvious 0x10, just in case - all are too large.*/
test_begin("str_to_uintmax_hex overflow corner case");
for(i = 0; i <= 0x30; ++i) {
int j = len + 1;
while (buff[--j] == 'f')
buff[j] = '0';
if (buff[j] == '9')
buff[j] = 'a';
else
buff[j]++;
}
test_end();
}
/* always pads with leading zeros to a size of 9 digits */
{
#define BIGBASE 01000000000ull
#define STRINGIFY(s) #s
#define STRINGIFY2(s) STRINGIFY(s)
int len = 0;
}
}
static void test_str_to_uintmax_oct(void)
{
unsigned int i=0;
test_begin("str_to_uintmax_oct in range");
const char *endp;
if (value >= 64)
test_assert_idx(ret == 0, i);
/* test with trailing noise */
value_back = 0x1234567890123456;
test_assert_idx(ret < 0, i);
test_assert_idx(ret == 0, i);
i++;
}
test_end();
/* not knowing exactly how large a uintmax_t is, we have to construct
the troublesome near-010/007*MAX strings manually by appending digits
to a MAX/007 string which we can easily create. Do a wider range
of 030 rather than the obvious 010, just in case - all are too large.*/
test_begin("str_to_uintmax_oct overflow corner case");
for(i = 0; i <= 030; ++i) {
int j = len + 1;
while (buff[--j] == '7')
buff[j] = '0';
buff[j]++;
}
test_end();
}
static void test_str_to_u64(void)
{
unsigned int i;
const struct {
const char *input;
int ret;
} u64tests[] = {
INVALID(-1),
VALID(0),
VALID(000000000000000000000000000000000000000000000000000000000000000),
{ "000000000000000000000000000000000000000000000000000001000000001", 0, 1000000001 },
{ "18446744073709551615", 0, 18446744073709551615ULL },
INVALID(18446744073709551616),
};
test_begin("str_to_uint64");
for (i = 0; i < N_ELEMENTS(u64tests); ++i) {
if (ret == 0)
else
if (ret == 0)
T_BEGIN {
test_assert_idx(ret < 0, i);
} T_END;
}
test_end();
}
static void test_str_to_u32(void)
{
unsigned int i;
const struct {
const char *input;
int ret;
} u32tests[] = {
VALID(0),
INVALID(-0),
VALID(4294967295),
INVALID(4294967296),
INVALID(4772185880),
INVALID(4772185884),
INVALID(4772185890),
};
test_begin("str_to_uint32");
for (i = 0; i < N_ELEMENTS(u32tests); ++i) {
if (ret == 0)
else
}
test_end();
}
/* Assumes long long is 64 bit, 2's complement */
static void test_str_to_llong(void)
{
unsigned int i;
const struct {
const char *input;
int ret;
long long val;
} i64tests[] = {
VALID(0),
VALID(-0),
INVALID(--0),
VALID(2147483648),
VALID(-2147483649),
VALID(9223372036854775807),
{ "-9223372036854775808", 0, -9223372036854775807-1 },
INVALID(9223372036854775808),
INVALID(-9223372036854775809),
};
test_begin("str_to_llong");
for (i = 0; i < N_ELEMENTS(i64tests); ++i) {
long long val = 123456789;
if (ret == 0)
else
}
test_end();
}
/* Assumes int is 32 bit, 2's complement */
static void test_str_to_i32(void)
{
unsigned int i;
const struct {
const char *input;
int ret;
int val;
} i32tests[] = {
VALID(0),
VALID(-0),
INVALID(--0),
VALID(2147483647),
VALID(-2147483648),
INVALID(2147483648),
INVALID(-2147483649),
};
test_begin("str_to_int");
for (i = 0; i < N_ELEMENTS(i32tests); ++i) {
int val = 123456789;
if (ret == 0)
else
}
test_end();
}
void test_strnum(void)
{
/* If the above isn't true, then we do expect some failures possibly */
}