#include "tinytest.h"
#include "tinytest_macros.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
void
test_strcmp(void *data)
{
(void)data;
if (strcmp("","")) {
tt_abort_msg("The empty string was not equal to itself");
}
tt_assert(strcmp("testcase", "testcase") == 0);
tt_want(strcmp("tinytest", "testcase") > 0);
tt_int_op(strcmp("abc", "abc"), ==, 0);
tt_int_op(strcmp("abc", "abcd"), < , 0);
tt_str_op("abc", <, "abcd");
end:
;
}
struct data_buffer {
char buffer1[512];
char buffer2[512];
};
void *
setup_data_buffer(const struct testcase_t *testcase)
{
struct data_buffer *db = malloc(sizeof(struct data_buffer));
return db;
}
int
clean_data_buffer(const struct testcase_t *testcase, void *ptr)
{
struct data_buffer *db = ptr;
if (db) {
free(db);
return 1;
}
return 0;
}
struct testcase_setup_t data_buffer_setup = {
setup_data_buffer, clean_data_buffer
};
void
test_memcpy(void *ptr)
{
struct data_buffer *db = ptr;
char *mem = NULL;
strcpy(db->buffer1, "String 0");
memcpy(db->buffer2, db->buffer1, sizeof(db->buffer1));
tt_str_op(db->buffer1, ==, db->buffer2);
tt_mem_op(db->buffer1, <, db->buffer2, sizeof(db->buffer1));
mem = strdup("Hello world.");
tt_assert(mem);
tt_str_op(db->buffer1, !=, mem);
end:
if (mem)
free(mem);
}
void
test_timeout(void *ptr)
{
time_t t1, t2;
(void)ptr;
t1 = time(NULL);
#ifdef _WIN32
Sleep(5000);
#else
sleep(5);
#endif
t2 = time(NULL);
tt_int_op(t2-t1, >=, 4);
tt_int_op(t2-t1, <=, 6);
end:
;
}
void
test_timeout_retry(void *ptr)
{
static int i = 0;
++i;
tt_int_op(i, !=, 1);
time_t t1, t2;
(void)ptr;
t1 = time(NULL);
#ifdef _WIN32
Sleep(5000);
#else
sleep(5);
#endif
t2 = time(NULL);
tt_int_op(t2-t1, >=, 4);
tt_int_op(t2-t1, <=, 6);
end:
;
}
struct testcase_t demo_tests[] = {
{ "strcmp", test_strcmp, },
{ "memcpy", test_memcpy, TT_FORK, &data_buffer_setup },
{ "timeout", test_timeout, TT_OFF_BY_DEFAULT },
{ "timeout_retry", test_timeout_retry, TT_RETRIABLE },
END_OF_TESTCASES
};
struct testgroup_t groups[] = {
{ "demo/", demo_tests },
END_OF_GROUPS
};
const char *alltests[] = { "+..", NULL };
const char *slowtests[] = { "+demo/timeout", NULL };
struct testlist_alias_t aliases[] = {
{ "ALL", alltests },
{ "SLOW", slowtests },
END_OF_ALIASES
};
int
main(int c, const char **v)
{
tinytest_set_aliases(aliases);
return tinytest_main(c, v, groups);
}