File: test_timer.tcc

package info (click to toggle)
uhub 0.4.1-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,436 kB
  • ctags: 2,270
  • sloc: ansic: 18,123; xml: 575; perl: 568; sh: 368; makefile: 24
file content (119 lines) | stat: -rw-r--r-- 2,981 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <uhub.h>

#define MAX_EVENTS 15
static struct timeout_queue* g_queue;
static time_t g_now;
static size_t g_max;
static struct timeout_evt g_events[MAX_EVENTS];

static size_t g_triggered;

static void timeout_cb(struct timeout_evt* t)
{
	g_triggered++;
}

/*
typedef void (*timeout_evt_cb)(struct timeout_evt*);

struct timeout_evt
{
	time_t timestamp;
	timeout_evt_cb callback;
	void* ptr;
	struct timeout_evt* prev;
	struct timeout_evt* next;
};

void timeout_evt_initialize(struct timeout_evt*, timeout_evt_cb, void* ptr);
void timeout_evt_reset(struct timeout_evt*);
int  timeout_evt_is_scheduled(struct timeout_evt*);


struct timeout_queue
{
	time_t last;
	size_t max;
	struct timeout_evt** events;
};

void timeout_queue_initialize(struct timeout_queue*, time_t now, size_t max);
void timeout_queue_shutdown(struct timeout_queue*);
size_t timeout_queue_process(struct timeout_queue*, time_t now);
void timeout_queue_insert(struct timeout_queue*, struct timeout_evt*, size_t seconds);
void timeout_queue_remove(struct timeout_queue*, struct timeout_evt*);
void timeout_queue_reschedule(struct timeout_queue*, struct timeout_evt*, size_t seconds);

size_t timeout_queue_get_next_timeout(struct timeout_queue*, time_t now);
*/


EXO_TEST(timer_setup,{
	size_t n;
	g_queue = hub_malloc_zero(sizeof(struct timeout_queue));
	g_now = 0;
	g_max = 5;
	g_triggered = 0;
	timeout_queue_initialize(g_queue, g_now, g_max);

	memset(g_events, 0,  sizeof(g_events));
	for (n = 0; n < MAX_EVENTS; n++)
	{
		timeout_evt_initialize(&g_events[n], timeout_cb, &g_events[n]);
	}

	return g_queue != NULL;
});


EXO_TEST(timer_check_timeout_0,{
	return timeout_queue_get_next_timeout(g_queue, g_now) == g_max;
});


EXO_TEST(timer_add_event_1,{
	timeout_queue_insert(g_queue, &g_events[0], 2);
	return g_events[0].prev != NULL;
});

EXO_TEST(timer_check_timeout_1,{
	return timeout_queue_get_next_timeout(g_queue, g_now) == 2;
});

EXO_TEST(timer_remove_event_1,{
	timeout_queue_remove(g_queue, &g_events[0]);
	return g_events[0].prev == NULL;
});

EXO_TEST(timer_check_timeout_2,{
	return timeout_queue_get_next_timeout(g_queue, g_now) == g_max;
});

/* test re-removing an event - should not crash! */
EXO_TEST(timer_remove_event_1_no_crash,{
	timeout_queue_remove(g_queue, &g_events[0]);
	return g_events[0].prev == NULL;
});

EXO_TEST(timer_add_5_events_1,{
	timeout_queue_insert(g_queue, &g_events[0], 0);
	timeout_queue_insert(g_queue, &g_events[1], 1);
	timeout_queue_insert(g_queue, &g_events[2], 2);
	timeout_queue_insert(g_queue, &g_events[3], 3);
	timeout_queue_insert(g_queue, &g_events[4], 4);

	return (g_events[0].prev != NULL &&
			g_events[1].prev != NULL &&
			g_events[2].prev != NULL &&
			g_events[3].prev != NULL &&
			g_events[4].prev != NULL);
});

EXO_TEST(timer_check_5_events_1,{
	return timeout_queue_get_next_timeout(g_queue, g_now) == 1;
});

EXO_TEST(timer_process_5_events_1,{
	g_now = 4;
	return timeout_queue_process(g_queue, g_now) == g_triggered;
});