test_time.c (5692B)
1 /* Copyright (C) 2013-2023, 2025 Vincent Forest (vaplv@free.fr) 2 * 3 * The RSys library is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published 5 * by the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * The RSys library is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #include "clock_time.h" 17 #include <stdlib.h> 18 #include <string.h> 19 20 static struct time 21 build_time 22 (const int64_t days, 23 const int64_t hours, 24 const int64_t mins, 25 const int64_t secs, 26 const int64_t msecs, 27 const int64_t usecs, 28 const int64_t nsecs) 29 { 30 struct time t; 31 32 t.sec = days * 3600 * 24; 33 t.sec += hours * 3600; 34 t.sec += mins * 60; 35 t.sec += secs; 36 37 t.nsec = msecs * 1000000; 38 t.nsec += usecs * 1000; 39 t.nsec += nsecs; 40 41 return t; 42 } 43 44 int 45 main(int argc, char** argv) 46 { 47 struct time start, end, res; 48 char dump[512]; 49 char* tk; 50 int64_t time = 0; 51 int64_t i = 0; 52 size_t dump_len; 53 (void)argc, (void)argv; 54 55 CHK(time_current(&start) == &start); 56 FOR_EACH(i, 0, INT32_MAX / 64) {} /* Active wait */ 57 CHK(time_current(&end) == &end); 58 59 CHK(time_sub(&res, &end, &start) == &res); 60 time = time_val(&res, TIME_NSEC); 61 CHK(time >= 0); 62 CHK(time_val(&res, TIME_USEC) == time / 1000); 63 CHK(time_val(&res, TIME_MSEC) == time / 1000000); 64 CHK(time_val(&res, TIME_SEC) == time / 1000000000); 65 66 time_dump 67 (&res, TIME_SEC|TIME_MSEC|TIME_USEC, &dump_len, dump, sizeof(dump)); 68 CHK(dump_len == strlen(dump)); 69 printf("%s\n", dump); 70 time_dump(&res, TIME_ALL, NULL, dump, sizeof(dump)); 71 printf("%s\n", dump); 72 73 CHK(time_add(&res, &res, &res) == &res); 74 CHK(time_val(&res, TIME_NSEC) == 2*time); 75 time_dump(&res, TIME_ALL, NULL, dump, sizeof(dump)); 76 printf("%s\n", dump); 77 78 time = time_val(&res, TIME_NSEC); 79 CHK(time_zero(&start) == &start); 80 CHK(time_val(&start, TIME_NSEC) == 0); 81 CHK(time_add(&res, &res, &start) == &res); 82 CHK(time_val(&res, TIME_NSEC) == time); 83 CHK(time_add(&start, &res, &start) == &start); 84 CHK(time_val(&start, TIME_NSEC) == time); 85 CHK(time_sub(&res, time_zero(&start), &res) == &res); 86 CHK(time_val(&res, TIME_NSEC) == -time); 87 88 CHK(time_sub(&res, &end, &end) == &res); 89 CHK(time_val(&res, TIME_NSEC) == 0); 90 91 res = build_time(1, 10, 55, 30, 998, 763, 314); 92 dump[0] = '?'; 93 time_dump(&res, TIME_ALL, &dump_len, dump, 0); 94 CHK(dump[0] = '?'); 95 time_dump(&res, TIME_ALL, NULL, dump, sizeof(dump)); 96 CHK(dump_len == strlen(dump)); 97 98 printf("%s\n", dump); 99 100 CHK((tk = strtok(dump, " ")) && !strcmp(tk, "1")); 101 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "day")); 102 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "10")); 103 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "hours")); 104 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "55")); 105 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "mins")); 106 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "30")); 107 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "secs")); 108 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "998")); 109 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "msecs")); 110 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "763")); 111 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "usecs")); 112 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "314")); 113 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "nsecs")); 114 CHK((tk = strtok(NULL, " ")) == NULL); 115 116 /* Check string truncation */ 117 time_dump(&res, TIME_ALL, NULL, dump, dump_len - 27 + 1/*null char*/); 118 CHK((tk = strtok(dump, " ")) && !strcmp(tk, "1")); 119 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "day")); 120 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "10")); 121 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "hours")); 122 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "55")); 123 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "mins")); 124 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "30")); 125 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "secs")); 126 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "99")); 127 CHK((tk = strtok(NULL, " ")) == NULL); 128 129 time_dump(&res, TIME_MIN|TIME_MSEC, NULL, dump, sizeof(dump)); 130 CHK((tk = strtok(dump, " ")) && !strcmp(tk, "2095")); 131 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "mins")); 132 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "30998")); 133 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "msecs")); 134 CHK((tk = strtok(NULL, " ")) == NULL); 135 136 time_dump(&res, 0, &dump_len, dump, sizeof(dump)); 137 CHK(dump_len == 0 && dump[0] == '\0'); 138 139 res = build_time(0, 0, 0, 0, 0, 0, 0); 140 time_dump(&res, TIME_ALL, NULL, dump, sizeof(dump)); 141 CHK((tk = strtok(dump, " ")) && !strcmp(tk, "0")); 142 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "nsec")); 143 CHK((tk = strtok(NULL, " ")) == NULL); 144 145 time_dump(&res, TIME_DAY, NULL, dump, sizeof(dump)); 146 CHK((tk = strtok(dump, " ")) && !strcmp(tk, "0")); 147 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "day")); 148 CHK((tk = strtok(NULL, " ")) == NULL); 149 150 res = build_time(0, 0, 1, 0, 235, 10, 0); 151 time_dump(&res, TIME_ALL, NULL, dump, sizeof(dump)); 152 CHK((tk = strtok(dump, " ")) && !strcmp(tk, "1")); 153 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "min")); 154 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "235")); 155 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "msecs")); 156 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "10")); 157 CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "usecs")); 158 CHK((tk = strtok(NULL, " ")) == NULL); 159 return 0; 160 }