1 /* $OpenBSD: test_convtime.c,v 1.1 2021/03/19 03:25:01 djm Exp $ */ 2 /* 3 * Regress test for misc time conversion functions. 4 * 5 * Placed in the public domain. 6 */ 7 8 #include <sys/types.h> 9 #include <sys/param.h> 10 #include <stdio.h> 11 #include <stdint.h> 12 #include <stdlib.h> 13 #include <string.h> 14 15 #include "test_helper.h" 16 17 #include "log.h" 18 #include "misc.h" 19 20 void test_convtime(void); 21 22 void 23 test_convtime(void) 24 { 25 char buf[1024]; 26 27 TEST_START("misc_convtime"); 28 ASSERT_INT_EQ(convtime("0"), 0); 29 ASSERT_INT_EQ(convtime("1"), 1); 30 ASSERT_INT_EQ(convtime("2s"), 2); 31 ASSERT_INT_EQ(convtime("3m"), 180); 32 ASSERT_INT_EQ(convtime("1m30"), 90); 33 ASSERT_INT_EQ(convtime("1m30s"), 90); 34 ASSERT_INT_EQ(convtime("1h1s"), 3601); 35 ASSERT_INT_EQ(convtime("1h30m"), 90 * 60); 36 ASSERT_INT_EQ(convtime("1d"), 24 * 60 * 60); 37 ASSERT_INT_EQ(convtime("1w"), 7 * 24 * 60 * 60); 38 ASSERT_INT_EQ(convtime("1w2d3h4m5"), 788645); 39 ASSERT_INT_EQ(convtime("1w2d3h4m5s"), 788645); 40 /* any negative number or error returns -1 */ 41 ASSERT_INT_EQ(convtime("-1"), -1); 42 ASSERT_INT_EQ(convtime(""), -1); 43 ASSERT_INT_EQ(convtime("trout"), -1); 44 ASSERT_INT_EQ(convtime("-77"), -1); 45 /* boundary conditions */ 46 snprintf(buf, sizeof buf, "%llu", (long long unsigned)INT_MAX); 47 ASSERT_INT_EQ(convtime(buf), INT_MAX); 48 snprintf(buf, sizeof buf, "%llu", (long long unsigned)INT_MAX + 1); 49 ASSERT_INT_EQ(convtime(buf), -1); 50 ASSERT_INT_EQ(convtime("3550w5d3h14m7s"), 2147483647); 51 #if INT_MAX == 2147483647 52 ASSERT_INT_EQ(convtime("3550w5d3h14m8s"), -1); 53 #endif 54 TEST_DONE(); 55 } 56