1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
/* test *time_t code, for libreswan
*
* Copyright (C) 2019 Andrew Cagney
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <https://www.gnu.org/licenses/lgpl-2.1.txt>.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
* License for more details.
*
*/
#include <stdio.h>
#include <string.h>
#include "deltatime.h"
#include "timecheck.h"
#include "lswcdefs.h" /* for elemsof() */
#include "constants.h" /* for bool_str() */
#include "timescale.h"
struct test_op {
intmax_t lms, rms;
const char *str;
};
#define CHECK_DELTATIME_OP(OP) \
check_deltatime_op(test_deltatime_##OP, \
elemsof(test_deltatime_##OP), \
deltatime_##OP, #OP)
static void check_deltatime_op(const struct test_op *tests, size_t nr_tests,
deltatime_t (*op)(deltatime_t, deltatime_t),
const char *op_name)
{
for (unsigned i = 0; i < nr_tests; i++) {
const struct test_op *t = &tests[i];
deltatime_t l = deltatime_from_milliseconds(t->lms);
deltatime_t r = deltatime_from_milliseconds(t->rms);
deltatime_buf buf;
const char *str = str_deltatime(op(l, r), &buf);
FILE *out = (strcmp(str, t->str) == 0) ? stdout : stderr;
fprintf(out, "str_deltatime(deltatime_%s(%jdms, %jdms)) == %s",
op_name, t->lms, t->rms, t->str);
if (out == stderr) {
fprintf(out, "; FAIL: returned %s", str);
fails++;
}
fprintf(out, "\n");
}
}
static void check_ttodeltatime(void)
{
static const struct test_ttodeltatime {
const char *str;
intmax_t us;
enum timescale timescale;
bool ok;
} test_ttodeltatime[] = {
/* scale */
{ "1", (uintmax_t)1, TIMESCALE_MICROSECONDS, true, },
{ "1", (uintmax_t)1*1000, TIMESCALE_MILLISECONDS, true, },
{ "1", (uintmax_t)1*1000*1000, TIMESCALE_SECONDS, true, },
{ "1", (uintmax_t)1*1000*1000*60, TIMESCALE_MINUTES, true, },
{ "1", (uintmax_t)1*1000*1000*60*60, TIMESCALE_HOURS, true, },
{ "1", (uintmax_t)1*1000*1000*60*60*24, TIMESCALE_DAYS, true, },
{ "1", (uintmax_t)1*1000*1000*60*60*24*7, TIMESCALE_WEEKS, true, },
/* suffix */
{ "1us", (uintmax_t)1, TIMESCALE_SECONDS, true, },
{ "1ms", (uintmax_t)1*1000, TIMESCALE_SECONDS, true, },
{ "1s", (uintmax_t)1*1000*1000, TIMESCALE_SECONDS, true, },
{ "1m", (uintmax_t)1*1000*1000*60, TIMESCALE_SECONDS, true, },
{ "1h", (uintmax_t)1*1000*1000*60*60, TIMESCALE_SECONDS, true, },
{ "1d", (uintmax_t)1*1000*1000*60*60*24, TIMESCALE_SECONDS, true, },
{ "1w", (uintmax_t)1*1000*1000*60*60*24*7, TIMESCALE_SECONDS, true, },
/* fractions */
{ "1.234", (uintmax_t)1234*1000, TIMESCALE_SECONDS, true, },
{ ".034", (uintmax_t) 34*1000, TIMESCALE_SECONDS, true, },
{ "2.", (uintmax_t)2000*1000, TIMESCALE_SECONDS, true, },
{ "0.1ms", (uintmax_t) 100, TIMESCALE_SECONDS, true, },
{ "0.5m", (uintmax_t) 30*1000*1000, TIMESCALE_SECONDS, true, },
/* error */
{ "", (uintmax_t)0, TIMESCALE_SECONDS, false, },
{ "1x", (uintmax_t)0, TIMESCALE_MILLISECONDS, false, },
{ "x1", (uintmax_t)0, TIMESCALE_MILLISECONDS, false, },
{ "1mm", (uintmax_t)0, TIMESCALE_MILLISECONDS, false, },
{ "1seconds", (uintmax_t)0, TIMESCALE_MILLISECONDS, false, },
{ "1 s", (uintmax_t)0, TIMESCALE_MILLISECONDS, false, },
{ "0x10", (uintmax_t)0, TIMESCALE_SECONDS, false, },
{ "0.1", (uintmax_t)0, TIMESCALE_MICROSECONDS, false, },
{ "0.1x", (uintmax_t)0, TIMESCALE_SECONDS, false, },
{ ".ms", (uintmax_t)0, TIMESCALE_SECONDS, false, },
{ ".", (uintmax_t)0, TIMESCALE_SECONDS, false, },
};
for (unsigned i = 0; i < elemsof(test_ttodeltatime); i++) {
const struct test_ttodeltatime *t = &test_ttodeltatime[i];
fprintf(stdout, "ttodeltatime(%s, "PRI_SCALE") ok=%s\n",
t->str, pri_timescale(t->timescale), bool_str(t->ok));
deltatime_t d;
diag_t diag = ttodeltatime(t->str, &d, t->timescale);
if (t->ok) {
if (diag != NULL) {
fprintf(stderr, "FAIL: ttodeltatime(%s, "PRI_TIMESCALE") unexpectedly returned: %s\n",
t->str, pri_timescale(t->timescale), str_diag(diag));
fails++;
return;
}
} else if (diag == NULL) {
fprintf(stderr, "FAIL: ttodeltatime(%s, "PRI_TIMESCALE") unexpectedly succeeded\n",
t->str, pri_timescale(t->timescale));
fails++;
return;
} else {
pfree_diag(&diag);
}
intmax_t microseconds = microseconds_from_deltatime(d);
if (microseconds != t->us) {
fprintf(stderr, "FAIL: ttodeltatime(%s, "PRI_TIMESCALE") returned %jd, expecting %jd\n",
t->str, pri_timescale(t->timescale), microseconds, t->us);
fails++;
return;
}
}
}
void check_deltatime(void)
{
char what[1000];
static const struct test_str_deltatime {
intmax_t ms;
const char *str;
} test_str_deltatime[] = {
{ 1000, "1" },
{ -1000, "-1" },
{ - 100, "-0.1" },
};
for (unsigned i = 0; i < elemsof(test_str_deltatime); i++) {
const struct test_str_deltatime *t = &test_str_deltatime[i];
deltatime_t d = deltatime_from_milliseconds(t->ms);
deltatime_buf buf;
const char *str = str_deltatime(d, &buf);
snprintf(what, sizeof(what), "str_deltatime(%jdms) == %s", t->ms, t->str);
if (strcmp(str, t->str) != 0) {
fprintf(stderr, "FAIL: %s vs %s\n", what, str);
fails++;
} else {
printf("%s\n", what);
}
}
static const struct test_op test_deltatime_max[] = {
{ 1000, 100, "1" },
{ -1000, 0, "0" },
{ - 100, -200, "-0.1" },
};
CHECK_DELTATIME_OP(max);
static const struct test_op test_deltatime_min[] = {
{ 1000, 100, "0.1" },
{ -1000, 0, "-1" },
{ - 100, -200, "-0.2" },
};
CHECK_DELTATIME_OP(min);
static const struct test_op test_deltatime_add[] = {
{ 1000, 100, "1.1" },
{ -1000, 0, "-1" },
{ - 100, -200, "-0.3" },
};
CHECK_DELTATIME_OP(add);
static const struct test_op test_deltatime_sub[] = {
{ 1000, 100, "0.9" },
{ -1000, 0, "-1" },
{ - 100, -200, "0.1" },
};
CHECK_DELTATIME_OP(sub);
CHECK_TIME_CMP_SECONDS(deltatime);
CHECK_TIME_CMP_MILLISECONDS(deltatime);
check_ttodeltatime();
}
|