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