summaryrefslogtreecommitdiffstats
path: root/regress/unittests/misc/test_ptimeout.c
blob: 7adc590e98d6c04a24db7daf3ec663c7bf5919ec (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
/* 	$OpenBSD: test_ptimeout.c,v 1.1 2023/01/06 02:59:50 djm Exp $ */
/*
 * Regress test for misc poll/ppoll timeout helpers.
 *
 * Placed in the public domain.
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <poll.h>
#include <time.h>

#include "../test_helper/test_helper.h"

#include "log.h"
#include "misc.h"

void test_ptimeout(void);

void
test_ptimeout(void)
{
	struct timespec pt, *ts;

	TEST_START("ptimeout_init");
	ptimeout_init(&pt);
	ASSERT_PTR_EQ(ptimeout_get_tsp(&pt), NULL);
	ASSERT_INT_EQ(ptimeout_get_ms(&pt), -1);
	TEST_DONE();

	TEST_START("ptimeout_deadline_sec");
	ptimeout_deadline_sec(&pt, 100);
	ptimeout_deadline_sec(&pt, 200);
	ASSERT_INT_EQ(ptimeout_get_ms(&pt), 100 * 1000);
	ts = ptimeout_get_tsp(&pt);
	ASSERT_PTR_NE(ts, NULL);
	ASSERT_LONG_EQ(ts->tv_nsec, 0);
	ASSERT_LONG_EQ(ts->tv_sec, 100);
	TEST_DONE();

	TEST_START("ptimeout_deadline_ms");
	ptimeout_deadline_ms(&pt, 50123);
	ptimeout_deadline_ms(&pt, 50500);
	ASSERT_INT_EQ(ptimeout_get_ms(&pt), 50123);
	ts = ptimeout_get_tsp(&pt);
	ASSERT_PTR_NE(ts, NULL);
	ASSERT_LONG_EQ(ts->tv_nsec, 123 * 1000000);
	ASSERT_LONG_EQ(ts->tv_sec, 50);
	TEST_DONE();

	TEST_START("ptimeout zero");
	ptimeout_init(&pt);
	ptimeout_deadline_ms(&pt, 0);
	ASSERT_INT_EQ(ptimeout_get_ms(&pt), 0);
	ts = ptimeout_get_tsp(&pt);
	ASSERT_PTR_NE(ts, NULL);
	ASSERT_LONG_EQ(ts->tv_nsec, 0);
	ASSERT_LONG_EQ(ts->tv_sec, 0);
	TEST_DONE();

	TEST_START("ptimeout_deadline_monotime");
	ptimeout_init(&pt);
	ptimeout_deadline_monotime(&pt, monotime() + 100);
	ASSERT_INT_GT(ptimeout_get_ms(&pt), 50000);
	ASSERT_INT_LT(ptimeout_get_ms(&pt), 200000);
	ts = ptimeout_get_tsp(&pt);
	ASSERT_PTR_NE(ts, NULL);
	ASSERT_LONG_GT(ts->tv_sec, 50);
	ASSERT_LONG_LT(ts->tv_sec, 200);
	TEST_DONE();

	TEST_START("ptimeout_deadline_monotime past");
	ptimeout_init(&pt);
	ptimeout_deadline_monotime(&pt, monotime() + 100);
	ptimeout_deadline_monotime(&pt, monotime() - 100);
	ASSERT_INT_EQ(ptimeout_get_ms(&pt), 0);
	ts = ptimeout_get_tsp(&pt);
	ASSERT_PTR_NE(ts, NULL);
	ASSERT_LONG_EQ(ts->tv_nsec, 0);
	ASSERT_LONG_EQ(ts->tv_sec, 0);
	TEST_DONE();
}