aboutsummaryrefslogtreecommitdiffstats
path: root/tests/timer/timer_misc_test.c
blob: 9e604b5a8a35e0f19cb5d9c6acf16f718f062c17 (plain)
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
/*
 * (C) 2018 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
 * All Rights Reserved
 *
 * Authors: Neels Hofmeyr <neels@hofmeyr.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <stdio.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/timer.h>

struct timer_remaining_testcase {
	struct timeval now;
	struct timeval timeout;
	int expect_rc;
	struct timeval expect_diff;
};

struct timer_remaining_testcase timer_remaining_data[] = {
	{
		.now = { .tv_sec = 1000000, .tv_usec = 0 },
		.timeout = { .tv_sec = 1000123, .tv_usec = 1 },
		.expect_rc = 0,
		.expect_diff = { .tv_sec = 123, .tv_usec = 1 },
	},
	{
		.now = { .tv_sec = 1000000, .tv_usec = 0 },
		.timeout = { .tv_sec = 1000000, .tv_usec = 1 },
		.expect_rc = 0,
		.expect_diff = { .tv_sec = 0, .tv_usec = 1 },
	},
	{
		.now = { .tv_sec = 1000000, .tv_usec = 1 },
		.timeout = { .tv_sec = 1000000, .tv_usec = 0 },
		.expect_rc = -1,
		.expect_diff = { .tv_sec = 0 - 1, .tv_usec = 999999 },
	},
	{
		.now = { .tv_sec = 1000001, .tv_usec = 1 },
		.timeout = { .tv_sec = 1000000, .tv_usec = 0 },
		.expect_rc = -1,
		.expect_diff = { .tv_sec = -1 - 1, .tv_usec = 999999 },
	},
	{
		.now = { .tv_sec = 1000123, .tv_usec = 1 },
		.timeout = { .tv_sec = 1000000, .tv_usec = 0 },
		.expect_rc = -1,
		.expect_diff = { .tv_sec = -123 - 1, .tv_usec = 999999 },
	},
};

void test_timer_remaining()
{
	int i;
	bool all_ok = true;
	printf("\n--- start: %s\n", __func__);

	for (i = 0; i < ARRAY_SIZE(timer_remaining_data); i++) {
		struct timer_remaining_testcase *tc = &timer_remaining_data[i];
		struct osmo_timer_list t = {
			.timeout = tc->timeout,
			.active = 1,
		};
		struct timeval diff;
		int rc;
		bool ok = true;

		rc = osmo_timer_remaining(&t, &tc->now, &diff);

		printf("timeout:%ld.%06ld - now:%ld.%06ld = diff:%ld.%06ld; rc=%d\n",
		       t.timeout.tv_sec, t.timeout.tv_usec,
		       tc->now.tv_sec, tc->now.tv_usec,
		       diff.tv_sec, diff.tv_usec,
		       rc);

		if (rc != tc->expect_rc) {
			printf("  ERROR: expected rc = %d\n", tc->expect_rc);
			ok = false;
		}

		if (diff.tv_sec != tc->expect_diff.tv_sec || diff.tv_usec != tc->expect_diff.tv_usec) {
			printf("  ERROR: expected diff = %ld.%06ld\n",
			       tc->expect_diff.tv_sec, tc->expect_diff.tv_usec);
			ok = false;
		}

		if (!ok)
			all_ok = false;
	}

	if (!all_ok) {
		printf("--- FAILURE: %s\n", __func__);
		exit(EXIT_FAILURE);
	}

	printf("--- done: %s\n", __func__);
}

int main(int argc, char *argv[])
{
	test_timer_remaining();
}