test-lib.c revision cb63aae771b2cfe51259de09dbff5431739dcc55
/* Copyright (c) 2007-2008 Dovecot authors, see the included COPYING file */
#include "test-lib.h"
#include "array.h"
#include "str.h"
#include "base64.h"
#include "bsearch-insert-pos.h"
#include "aqueue.h"
#include "network.h"
#include "primes.h"
#include "priorityq.h"
#include "seq-range-array.h"
#include "str-sanitize.h"
#include "utc-mktime.h"
#include <stdlib.h>
#include <time.h>
static void test_array(void)
{
ARRAY_DEFINE(intarr, int);
const int *output;
unsigned int i, j;
for (i = 0; i < N_ELEMENTS(input); i++) {
for (j = 0; j < i; j++) {
break;
}
}
}
}
static void test_base64_encode(void)
{
static const char *input[] = {
"hello world",
"foo barits",
"just niin"
};
static const char *output[] = {
"aGVsbG8gd29ybGQ=",
"Zm9vIGJhcml0cw==",
"anVzdCBuaWlu"
};
unsigned int i;
bool success;
for (i = 0; i < N_ELEMENTS(input); i++) {
str_truncate(str, 0);
}
}
struct test_base64_decode_output {
const char *text;
int ret;
unsigned int src_pos;
};
static void test_base64_decode(void)
{
static const char *input[] = {
"\taGVsbG8gd29ybGQ=",
"\nZm9v\n \tIGJh \t\ncml0cw==",
" anVzdCBuaWlu \n",
"aGVsb",
"aGVsb!!!!!",
"aGVs!!!!!"
};
static const struct test_base64_decode_output output[] = {
{ "hello world", 0, -1 },
{ "foo barits", 0, -1 },
{ "just niin", 1, -1 },
{ "hel", 1, 4 },
{ "hel", -1, 4 },
{ "hel", -1, 4 }
};
unsigned int i;
int ret;
bool success;
for (i = 0; i < N_ELEMENTS(input); i++) {
str_truncate(str, 0);
src_pos = 0;
}
}
{
}
static void test_bsearch_insert_pos(void)
{
static const unsigned int input[] = {
1, 5, 9, 15, 16, -1,
1, 5, 9, 15, 16, 17, -1,
-1
};
static const unsigned int max_key = 18;
const unsigned int *cur;
bool success;
for (i = 0; cur[0] != -1U; i++) {
else if (idx == 0)
else {
}
if (!success)
break;
}
success);
}
}
static void test_buffer(void)
{
#define BUF_TEST_COUNT 1000
unsigned int i, shadowbuf_size;
int test = -1;
bool zero;
for (i = 0; i < BUF_TEST_SIZE; i++)
srand(1);
shadowbuf_size = 0;
for (i = 0; i < BUF_TEST_COUNT; i++) {
}
switch (test) {
case 0:
if (!zero) {
} else {
}
break;
case 1:
if (!zero) {
} else {
}
shadowbuf_size += size;
break;
case 2:
if (!zero) {
} else {
}
if (pos < shadowbuf_size)
shadowbuf_size += size;
else
break;
case 3:
if (pos < shadowbuf_size) {
shadowbuf_size -= size;
}
break;
case 4:
if (shadowbuf_size == 0)
break;
break;
case 5:
break;
}
break;
}
if (i == BUF_TEST_COUNT)
else {
}
buffer_free(&buf);
}
{
const unsigned int *p;
unsigned int n, i, count;
for (i = 0, n = 1; i < count; i++, n++) {
if (i == deleted_n)
n++;
if (*p != n)
return FALSE;
}
return TRUE;
}
static const char *test_aqueue2(unsigned int initial_size)
{
ARRAY_DEFINE(aqueue_array, unsigned int);
unsigned int i, j, k;
for (i = 0; i < N_ELEMENTS(aqueue_input); i++) {
for (k = 0; k < N_ELEMENTS(aqueue_input); k++) {
for (j = 0; j < k; j++) {
return t_strdup_printf("Wrong count after append %u vs %u)",
}
return "Invalid data after append";
}
if (k != 0 && i < k) {
aqueue_delete(aqueue, i);
return "Wrong count after delete";
if (!aqueue_is_ok(aqueue, i))
return "Invalid data after delete";
}
}
}
if (aqueue_count(aqueue) != 0)
return "aqueue_clear() broken";
return NULL;
}
static void test_aqueue(void)
{
unsigned int i;
T_BEGIN {
reason = test_aqueue2(i);
} T_END;
}
}
{
unsigned int i;
for (i = 0; i < size; i++) {
if (bytes[i] != b)
return FALSE;
}
return TRUE;
}
static void test_mempool_alloconly(void)
{
#define PMALLOC_MAX_COUNT 128
unsigned int i, j, k;
for (i = 0; i < 64; i++) {
for (j = 1; j <= 128; j++) {
for (k = 1; k <= PMALLOC_MAX_COUNT; k++) {
}
if (!mem_has_bytes(mem[0], j, j))
for (k = 1; k <= PMALLOC_MAX_COUNT; k++) {
if (!mem_has_bytes(mem[k], k, k))
}
pool_unref(&pool);
}
}
}
struct test_net_is_in_network_input {
const char *ip;
const char *net;
unsigned int bits;
bool ret;
};
static void test_net_is_in_network(void)
{
static struct test_net_is_in_network_input input[] = {
#ifdef HAVE_IPV6
,
#endif
};
unsigned int i;
bool success;
for (i = 0; i < N_ELEMENTS(input); i++) {
}
}
struct pq_test_item {
struct priorityq_item item;
int num;
};
{
}
static void test_primes(void)
{
unsigned int i, j, num;
bool success;
success = primes_closest(0) > 0;
}
for (i = 10; i < 32; i++) {
for (j = 0; j < 200; j++, num++) {
}
}
}
static void test_priorityq(void)
{
#define PQ_MAX_ITEMS 100
static const int input[] = {
1, 2, 3, 4, 5, 6, 7, 8, -1,
8, 7, 6, 5, 4, 3, 2, 1, -1,
8, 7, 5, 6, 1, 3, 4, 2, -1,
-1
};
static const int output[] = {
1, 2, 3, 4, 5, 6, 7, 8
};
unsigned int i, j;
int prev;
/* simple tests with popping only */
for (i = 0; input[i] != -1; i++) {
for (j = 0; input[i] != -1; i++, j++) {
if (priorityq_count(pq) != j)
}
for (j = 0; j < N_ELEMENTS(output); j++) {
}
if (priorityq_count(pq) != 0)
}
/* randomized tests, remove elements */
for (i = 0; i < 100; i++) {
for (j = 0; j < PQ_MAX_ITEMS; j++) {
}
for (j = 0; j < PQ_MAX_ITEMS; j++) {
if (rand() % 3 == 0) {
}
}
prev = 0;
while (priorityq_count(pq) > 0) {
}
for (j = 0; j < PQ_MAX_ITEMS; j++) {
}
}
pool_unref(&pool);
}
static void test_seq_range_array_random(void)
{
#define SEQ_RANGE_TEST_BUFSIZE 20
#define SEQ_RANGE_TEST_COUNT 10000
unsigned char shadowbuf[SEQ_RANGE_TEST_BUFSIZE];
int test = -1;
for (i = 0; i < SEQ_RANGE_TEST_COUNT; i++) {
switch (test) {
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
ret2++;
}
}
break;
}
break;
goto fail;
goto fail;
}
goto fail;
}
}
goto fail;
}
}
fail:
if (i == SEQ_RANGE_TEST_COUNT)
else {
}
}
static void test_seq_range_array_invert(void)
{
static const unsigned int input[] = {
1, 2, 3, 4, 5, -1U,
2, 3, 4, -1U,
1, 2, 4, 5, -1U,
1, 3, 5, -1U,
1, -1U,
5, -1U,
-1U
};
bool old_exists, success;
start = i;
for (; input[i] != -1U; i++) {
for (j = start; j < i; j++) {
}
}
break;
}
}
success);
array_free(&range);
}
}
{
unsigned int i;
for (i = 0; i < 8; i++) {
if ((byte & (1 << i)) != 0)
}
}
static void test_seq_range_array_have_common(void)
{
unsigned int i, j;
for (i = 0; i < 256; i++) {
test_seq_range_create(&arr1, i);
for (j = 0; j < 256; j++) {
test_seq_range_create(&arr2, j);
ret2 = (i & j) != 0;
}
}
}
static void test_seq_range_array(void)
{
}
struct str_sanitize_input {
const char *str;
unsigned int max_len;
};
static void test_str_sanitize(void)
{
static struct str_sanitize_input input[] = {
{ NULL, 2 },
{ "", 2 },
{ "a", 2 },
{ "ab", 2 },
{ "abc", 2 },
{ "abcd", 3 },
{ "abcde", 4 }
};
static const char *output[] = {
NULL,
"",
"a",
"ab",
"...",
"...",
"a..."
};
const char *str;
unsigned int i;
bool success;
for (i = 0; i < N_ELEMENTS(input); i++) {
}
}
struct test_message_date_output {
int tz_offset;
bool ret;
};
struct test_utc_mktime_input {
};
static void test_utc_mktime(void)
{
static struct test_utc_mktime_input input[] = {
#ifdef TIME_T_SIGNED
{ 1969, 12, 31, 23, 59, 59 },
{ 1901, 12, 13, 20, 45, 53 },
#endif
{ 2106, 2, 7, 6, 28, 15 },
#endif
{ 2007, 11, 7, 1, 7, 20 },
{ 1970, 1, 1, 0, 0, 0 },
{ 2038, 1, 19, 3, 14, 7 }
};
#ifdef TIME_T_SIGNED
-1,
-2147483647,
#endif
4294967295,
#endif
1194397640,
0,
2147483647
};
unsigned int i;
time_t t;
bool success;
for (i = 0; i < N_ELEMENTS(input); i++) {
t = utc_mktime(&tm);
(long)t, (long)output[i]));
}
}
int main(void)
{
static void (*test_functions[])(void) = {
};
unsigned int i;
test_init();
for (i = 0; i < N_ELEMENTS(test_functions); i++) {
T_BEGIN {
test_functions[i]();
} T_END;
}
return test_deinit();
}