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