Timer: add testing base

This commit is contained in:
o9000 2017-12-27 22:18:02 +01:00
parent a7ca1b739b
commit 40e01e2abf
5 changed files with 65 additions and 2 deletions

View file

@ -23,6 +23,9 @@ set(SOURCES ../util/common.c
../util/strnatcmp.c
../util/cache.c
../util/timer.c
../util/test.c
../util/print.c
../util/signals.c
../config.c
../util/server.c
../util/strlcat.c

View file

@ -1,9 +1,11 @@
#include <errno.h>
#include <fcntl.h>
#include <glib.h>
#ifndef TINT2CONF
#ifdef HAVE_SN
#include <libsn/sn.h>
#endif
#endif
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
@ -34,6 +36,7 @@ void reset_signals()
sigprocmask(SIG_SETMASK, &signal_set, NULL);
}
#ifndef TINT2CONF
void init_signals()
{
// Set signal handlers
@ -164,3 +167,4 @@ int get_signal_pending()
{
return signal_pending;
}
#endif

View file

@ -146,14 +146,16 @@ void run_all_tests()
fprintf(stdout, BLUE "tint2: " RED "%lu" BLUE " out of %lu tests " RED "failed." RESET "\n", failed, count);
}
#if 0
TEST(dummy) {
int x = 2;
int y = 2;
ASSERT_EQUAL(x, y);
}
TEST(dummyBad) {
TEST(dummy_bad) {
int x = 2;
int y = 3;
ASSERT_EQUAL(x, y);
}
#endif

View file

@ -21,6 +21,7 @@
#include <stdio.h>
#include "timer.h"
#include "test.h"
GSList *timeout_list;
struct timeval next_timeout;
@ -85,8 +86,26 @@ void cleanup_timeout()
}
}
static struct timespec mock_time = { 0, 0 };
void set_mock_time(struct timespec *tp)
{
mock_time = *tp;
}
void set_mock_time_ms(u_int64_t ms)
{
struct timespec t;
t.tv_sec = ms / 1000;
t.tv_nsec = (ms % 1000) * 1000 * 1000;
set_mock_time(&t);
}
int gettime(struct timespec *tp)
{
if (mock_time.tv_sec || mock_time.tv_nsec) {
*tp = mock_time;
return 0;
}
// CLOCK_BOOTTIME under Linux is the same as CLOCK_MONOTONIC under *BSD.
#ifdef CLOCK_BOOTTIME
return clock_gettime(CLOCK_BOOTTIME, tp);
@ -471,3 +490,39 @@ double profiling_get_time()
profiling_get_time_old_time = t;
return delta;
}
TEST(mock_time) {
struct timespec t1 = {1000, 2};
struct timespec t2 = {0, 0};
struct timespec t3 = {2000, 3};
int ret;
set_mock_time(&t1);
ret = gettime(&t2);
ASSERT_EQUAL(ret, 0);
ASSERT_EQUAL(t1.tv_sec, t2.tv_sec);
ASSERT_EQUAL(t1.tv_nsec, t2.tv_nsec);
set_mock_time(&t3);
ret = gettime(&t2);
ASSERT_EQUAL(ret, 0);
ASSERT_EQUAL(t3.tv_sec, t2.tv_sec);
ASSERT_EQUAL(t3.tv_nsec, t2.tv_nsec);
}
TEST(mock_time_ms) {
struct timespec t1 = {1000, 2 * 1000 * 1000};
struct timespec t2 = {0, 0};
struct timespec t3 = {2000, 3 * 1000 * 1000};
int ret;
set_mock_time_ms(1000 * 1000 + 2);
ret = gettime(&t2);
ASSERT_EQUAL(ret, 0);
ASSERT_EQUAL(t1.tv_sec, t2.tv_sec);
ASSERT_EQUAL(t1.tv_nsec, t2.tv_nsec);
set_mock_time_ms(2000 * 1000 + 3);
ret = gettime(&t2);
ASSERT_EQUAL(ret, 0);
ASSERT_EQUAL(t3.tv_sec, t2.tv_sec);
ASSERT_EQUAL(t3.tv_nsec, t2.tv_nsec);
}

View file

@ -10,7 +10,6 @@
#define INSTALL_PREFIX "/usr/local"
#define LOCALEDIR "/usr/local/share/locale"
#define SN_API_NOT_YET_FROZEN
#define TINT2CONF 1
#define _BSD_SOURCE
#define _DEFAULT_SOURCE
#define _POSIX_C_SOURCE=200809L