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