1f374ba41SEd Maste /* $OpenBSD: test_ptimeout.c,v 1.1 2023/01/06 02:59:50 djm Exp $ */
2f374ba41SEd Maste /*
3f374ba41SEd Maste * Regress test for misc poll/ppoll timeout helpers.
4f374ba41SEd Maste *
5f374ba41SEd Maste * Placed in the public domain.
6f374ba41SEd Maste */
7f374ba41SEd Maste
84d3fc8b0SEd Maste #include "includes.h"
94d3fc8b0SEd Maste
10f374ba41SEd Maste #include <sys/types.h>
11f374ba41SEd Maste #include <stdio.h>
124d3fc8b0SEd Maste #ifdef HAVE_STDINT_H
13f374ba41SEd Maste # include <stdint.h>
144d3fc8b0SEd Maste #endif
15f374ba41SEd Maste #include <stdlib.h>
16f374ba41SEd Maste #include <string.h>
17*535af610SEd Maste #ifdef HAVE_POLL_H
18f374ba41SEd Maste # include <poll.h>
19*535af610SEd Maste #endif
20f374ba41SEd Maste #include <time.h>
21f374ba41SEd Maste
22f374ba41SEd Maste #include "../test_helper/test_helper.h"
23f374ba41SEd Maste
24f374ba41SEd Maste #include "log.h"
25f374ba41SEd Maste #include "misc.h"
26f374ba41SEd Maste
27f374ba41SEd Maste void test_ptimeout(void);
28f374ba41SEd Maste
29f374ba41SEd Maste void
test_ptimeout(void)30f374ba41SEd Maste test_ptimeout(void)
31f374ba41SEd Maste {
32f374ba41SEd Maste struct timespec pt, *ts;
33f374ba41SEd Maste
34f374ba41SEd Maste TEST_START("ptimeout_init");
35f374ba41SEd Maste ptimeout_init(&pt);
36f374ba41SEd Maste ASSERT_PTR_EQ(ptimeout_get_tsp(&pt), NULL);
37f374ba41SEd Maste ASSERT_INT_EQ(ptimeout_get_ms(&pt), -1);
38f374ba41SEd Maste TEST_DONE();
39f374ba41SEd Maste
40f374ba41SEd Maste TEST_START("ptimeout_deadline_sec");
41f374ba41SEd Maste ptimeout_deadline_sec(&pt, 100);
42f374ba41SEd Maste ptimeout_deadline_sec(&pt, 200);
43f374ba41SEd Maste ASSERT_INT_EQ(ptimeout_get_ms(&pt), 100 * 1000);
44f374ba41SEd Maste ts = ptimeout_get_tsp(&pt);
45f374ba41SEd Maste ASSERT_PTR_NE(ts, NULL);
46f374ba41SEd Maste ASSERT_LONG_EQ(ts->tv_nsec, 0);
47f374ba41SEd Maste ASSERT_LONG_EQ(ts->tv_sec, 100);
48f374ba41SEd Maste TEST_DONE();
49f374ba41SEd Maste
50f374ba41SEd Maste TEST_START("ptimeout_deadline_ms");
51f374ba41SEd Maste ptimeout_deadline_ms(&pt, 50123);
52f374ba41SEd Maste ptimeout_deadline_ms(&pt, 50500);
53f374ba41SEd Maste ASSERT_INT_EQ(ptimeout_get_ms(&pt), 50123);
54f374ba41SEd Maste ts = ptimeout_get_tsp(&pt);
55f374ba41SEd Maste ASSERT_PTR_NE(ts, NULL);
56f374ba41SEd Maste ASSERT_LONG_EQ(ts->tv_nsec, 123 * 1000000);
57f374ba41SEd Maste ASSERT_LONG_EQ(ts->tv_sec, 50);
58f374ba41SEd Maste TEST_DONE();
59f374ba41SEd Maste
60f374ba41SEd Maste TEST_START("ptimeout zero");
61f374ba41SEd Maste ptimeout_init(&pt);
62f374ba41SEd Maste ptimeout_deadline_ms(&pt, 0);
63f374ba41SEd Maste ASSERT_INT_EQ(ptimeout_get_ms(&pt), 0);
64f374ba41SEd Maste ts = ptimeout_get_tsp(&pt);
65f374ba41SEd Maste ASSERT_PTR_NE(ts, NULL);
66f374ba41SEd Maste ASSERT_LONG_EQ(ts->tv_nsec, 0);
67f374ba41SEd Maste ASSERT_LONG_EQ(ts->tv_sec, 0);
68f374ba41SEd Maste TEST_DONE();
69f374ba41SEd Maste
70f374ba41SEd Maste TEST_START("ptimeout_deadline_monotime");
71f374ba41SEd Maste ptimeout_init(&pt);
72f374ba41SEd Maste ptimeout_deadline_monotime(&pt, monotime() + 100);
73f374ba41SEd Maste ASSERT_INT_GT(ptimeout_get_ms(&pt), 50000);
74f374ba41SEd Maste ASSERT_INT_LT(ptimeout_get_ms(&pt), 200000);
75f374ba41SEd Maste ts = ptimeout_get_tsp(&pt);
76f374ba41SEd Maste ASSERT_PTR_NE(ts, NULL);
77f374ba41SEd Maste ASSERT_LONG_GT(ts->tv_sec, 50);
78f374ba41SEd Maste ASSERT_LONG_LT(ts->tv_sec, 200);
79f374ba41SEd Maste TEST_DONE();
80f374ba41SEd Maste
81f374ba41SEd Maste TEST_START("ptimeout_deadline_monotime past");
82f374ba41SEd Maste ptimeout_init(&pt);
83f374ba41SEd Maste ptimeout_deadline_monotime(&pt, monotime() + 100);
84f374ba41SEd Maste ptimeout_deadline_monotime(&pt, monotime() - 100);
85f374ba41SEd Maste ASSERT_INT_EQ(ptimeout_get_ms(&pt), 0);
86f374ba41SEd Maste ts = ptimeout_get_tsp(&pt);
87f374ba41SEd Maste ASSERT_PTR_NE(ts, NULL);
88f374ba41SEd Maste ASSERT_LONG_EQ(ts->tv_nsec, 0);
89f374ba41SEd Maste ASSERT_LONG_EQ(ts->tv_sec, 0);
90f374ba41SEd Maste TEST_DONE();
91f374ba41SEd Maste }
92