master
c 425 lines 20 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "libnetdata/libnetdata.h"
4 #include "duration.h"
5
6 typedef struct {
7 const char *input;
8 const char *default_unit; // Default unit for parsing (NULL means "s")
9 const char *output_unit;
10 int64_t expected_value;
11 bool should_succeed;
12 const char *expected_reformat; // Expected reformatted string (NULL to skip check)
13 const char *description;
14 } duration_test_case_t;
15
16 static const duration_test_case_t test_cases[] = {
17 // Basic abbreviated forms
18 { "5m", NULL, "s", 300, true, "5m", "5 minutes to seconds" },
19 { "2h", NULL, "s", 7200, true, "2h", "2 hours to seconds" },
20 { "7d", NULL, "s", 604800, true, "7d", "7 days to seconds" },
21 { "1w", NULL, "d", 7, true, "7d", "1 week to days" },
22 { "30s", NULL, "s", 30, true, "30s", "30 seconds to seconds" },
23
24 // Full unit names (lowercase)
25 { "7 days", NULL, "s", 604800, true, "7d", "7 days (full) to seconds" },
26 { "2 hours", NULL, "s", 7200, true, "2h", "2 hours (full) to seconds" },
27 { "30 seconds", NULL, "s", 30, true, "30s", "30 seconds (full) to seconds" },
28 { "5 minutes", NULL, "s", 300, true, "5m", "5 minutes (full) to seconds" },
29 { "1 week", NULL, "d", 7, true, "7d", "1 week (full) to days" },
30 { "2 months", NULL, "d", 60, true, "2mo", "2 months (full) to days" },
31 { "1 year", NULL, "d", 365, true, "1y", "1 year (full) to days" },
32
33 // Case variations
34 { "7 DAYS", NULL, "s", 604800, true, "7d", "7 DAYS (uppercase) to seconds" },
35 { "2 Hours", NULL, "s", 7200, true, "2h", "2 Hours (mixed case) to seconds" },
36 { "30 SECONDS", NULL, "s", 30, true, "30s", "30 SECONDS (uppercase) to seconds" },
37 { "5 Minutes", NULL, "s", 300, true, "5m", "5 Minutes (mixed case) to seconds" },
38
39 // Without spaces
40 { "7days", NULL, "s", 604800, true, "7d", "7days (no space) to seconds" },
41 { "2hours", NULL, "s", 7200, true, "2h", "2hours (no space) to seconds" },
42 { "30seconds", NULL, "s", 30, true, "30s", "30seconds (no space) to seconds" },
43 { "5minutes", NULL, "s", 300, true, "5m", "5minutes (no space) to seconds" },
44
45 // Singular forms
46 { "1 day", NULL, "s", 86400, true, "1d", "1 day (singular) to seconds" },
47 { "1 hour", NULL, "s", 3600, true, "1h", "1 hour (singular) to seconds" },
48 { "1 second", NULL, "s", 1, true, "1s", "1 second (singular) to seconds" },
49 { "1 minute", NULL, "s", 60, true, "1m", "1 minute (singular) to seconds" },
50 { "1 week", NULL, "d", 7, true, "7d", "1 week (singular) to days" },
51 { "1 month", NULL, "d", 30, true, "1mo", "1 month (singular) to days" },
52 { "1 year", NULL, "d", 365, true, "1y", "1 year (singular) to days" },
53
54 // Complex expressions with full names
55 { "2 hours 30 minutes", NULL, "s", 9000, true, "2h30m", "2 hours 30 minutes to seconds" },
56 { "1 day 12 hours", NULL, "s", 129600, true, "1d12h", "1 day 12 hours to seconds" },
57 { "1 week 2 days", NULL, "d", 9, true, "9d", "1 week 2 days to days" },
58 { "1 year 2 months 3 days", NULL, "d", 428, true, "1y2mo3d", "1 year 2 months 3 days to days" },
59
60 // Mixed abbreviated and full names
61 { "2h 30 minutes", NULL, "s", 9000, true, "2h30m", "2h 30 minutes (mixed) to seconds" },
62 { "1d 12 hours", NULL, "s", 129600, true, "1d12h", "1d 12 hours (mixed) to seconds" },
63 { "1 week 2d", NULL, "d", 9, true, "9d", "1 week 2d (mixed) to days" },
64
65 // Other time units
66 { "100 milliseconds", NULL, "ms", 100, true, "100ms", "100 milliseconds to ms" },
67 { "50 microseconds", NULL, "us", 50, true, "50us", "50 microseconds to us" },
68 { "25 nanoseconds", NULL, "ns", 25, true, "25ns", "25 nanoseconds to ns" },
69 { "100 MILLISECONDS", NULL, "ms", 100, true, "100ms", "100 MILLISECONDS (uppercase) to ms" },
70 { "50 Microseconds", NULL, "us", 50, true, "50us", "50 Microseconds (mixed case) to us" },
71
72 // Fractional values with full names
73 { "1.5 days", NULL, "h", 36, true, "1d12h", "1.5 days to hours" },
74 { "2.5 hours", NULL, "m", 150, true, "2h30m", "2.5 hours to minutes" },
75 { "0.5 minutes", NULL, "s", 30, true, "30s", "0.5 minutes to seconds" },
76
77 // Alternative abbreviations
78 { "30 sec", NULL, "s", 30, true, "30s", "30 sec to seconds" },
79 { "30 secs", NULL, "s", 30, true, "30s", "30 secs to seconds" },
80 { "2 hr", NULL, "m", 120, true, "2h", "2 hr to minutes" },
81 { "2 hrs", NULL, "m", 120, true, "2h", "2 hrs to minutes" },
82
83 // Special keywords (case-insensitive)
84 { "never", NULL, "s", 0, true, "off", "never keyword" },
85 { "NEVER", NULL, "s", 0, true, "off", "NEVER keyword (uppercase)" },
86 { "Never", NULL, "s", 0, true, "off", "Never keyword (mixed case)" },
87 { "off", NULL, "s", 0, true, "off", "off keyword" },
88 { "OFF", NULL, "s", 0, true, "off", "OFF keyword (uppercase)" },
89 { "Off", NULL, "s", 0, true, "off", "Off keyword (mixed case)" },
90
91 // Negative durations
92 { "-5 minutes", NULL, "s", -300, true, "-5m", "negative 5 minutes to seconds" },
93 { "-2 hours", NULL, "s", -7200, true, "-2h", "negative 2 hours to seconds" },
94 { "-1 day", NULL, "s", -86400, true, "-1d", "negative 1 day to seconds" },
95
96 // "ago" suffix support (should negate the value)
97 { "7 days ago", NULL, "s", -604800, true, "-7d", "7 days ago to negative seconds" },
98 { "7d ago", NULL, "s", -604800, true, "-7d", "7d ago to negative seconds" },
99 { "2 hours ago", NULL, "s", -7200, true, "-2h", "2 hours ago to negative seconds" },
100 { "2h ago", NULL, "s", -7200, true, "-2h", "2h ago to negative seconds" },
101 { "30 minutes ago", NULL, "s", -1800, true, "-30m", "30 minutes ago to negative seconds" },
102 { "30m ago", NULL, "s", -1800, true, "-30m", "30m ago to negative seconds" },
103 { "1 year ago", NULL, "d", -365, true, "-1y", "1 year ago to negative days" },
104 { "1y ago", NULL, "d", -365, true, "-1y", "1y ago to negative days" },
105
106 // Complex expressions with "ago"
107 { "2 hours 30 minutes ago", NULL, "s", -9000, true, "-2h30m", "2 hours 30 minutes ago to negative seconds" },
108 { "2h30m ago", NULL, "s", -9000, true, "-2h30m", "2h30m ago to negative seconds" },
109 { "1 day 12 hours ago", NULL, "s", -129600, true, "-1d12h", "1 day 12 hours ago to negative seconds" },
110 { "1d12h ago", NULL, "s", -129600, true, "-1d12h", "1d12h ago to negative seconds" },
111
112 // Case variations with "ago"
113 { "7 days AGO", NULL, "s", -604800, true, "-7d", "7 days AGO (uppercase) to negative seconds" },
114 { "7 days Ago", NULL, "s", -604800, true, "-7d", "7 days Ago (mixed case) to negative seconds" },
115 { "7daysago", NULL, "s", -604800, true, "-7d", "7daysago (no spaces) to negative seconds" },
116
117 // Edge case: negative duration with "ago" - redundant but intent is clear, treat as negative
118 { "-7 days ago", NULL, "s", -604800, true, "-7d", "negative duration with 'ago' stays negative" },
119 { "-2h ago", NULL, "s", -7200, true, "-2h", "negative duration with 'ago' stays negative" },
120
121 // Invalid cases that should fail
122 { "invalid", NULL, "s", 0, false, NULL, "invalid unit should fail" },
123 { "5 invalidunit", NULL, "s", 0, false, NULL, "invalid full unit name should fail" },
124 { "abc days", NULL, "s", 0, false, NULL, "non-numeric value should fail" },
125 { "", NULL, "s", 0, false, NULL, "empty string should fail" },
126 { "7 days ago extra", NULL, "s", 0, false, NULL, "trailing text after 'ago' should fail" },
127 { "7 days agooo", NULL, "s", 0, false, NULL, "misspelled 'ago' should fail" },
128 { "ago", NULL, "s", 0, false, NULL, "'ago' without duration should fail" },
129 { "7 ago days", NULL, "s", 0, false, NULL, "'ago' in wrong position should fail" },
130 { "7 days ago 1 hour", NULL, "s", 0, false, NULL, "text after 'ago' should fail" },
131 { "7d ago 1h", NULL, "s", 0, false, NULL, "duration after 'ago' should fail" },
132
133 // Complex expressions with arithmetic
134 { "-7d+1h", NULL, "s", -608400, true, "-7d1h", "negative days plus positive hours" },
135 { "1d-12h", NULL, "s", 43200, true, "12h", "positive days minus hours" },
136 { "2h-3h", NULL, "s", -3600, true, "-1h", "results in negative duration" },
137
138 // Test various formatting edge cases
139 { "3661s", NULL, "s", 3661, true, "1h1m1s", "many seconds to h/m/s" },
140 { "90000s", NULL, "s", 90000, true, "1d1h", "many seconds to d/h" },
141 { "31536000s", NULL, "s", 31536000, true, "1y", "seconds in a year" },
142 { "366d", NULL, "d", 366, true, "1y1d", "more than a year in days" },
143 { "100000000ns", NULL, "ns", 100000000, true, "100ms", "nanoseconds to milliseconds" },
144 { "3600000ms", NULL, "ms", 3600000, true, "1h", "milliseconds to hours" },
145 { "0.001s", NULL, "ms", 1, true, "1ms", "fractional seconds to ms" },
146 { "1440m", NULL, "h", 24, true, "1d", "minutes to hours shows days" },
147 { "10080m", NULL, "d", 7, true, "7d", "minutes to days" },
148
149 // Zero value
150 { "0s", NULL, "s", 0, true, "off", "zero seconds" },
151 { "0d", NULL, "d", 0, true, "off", "zero days" },
152
153 // Plain numbers (no unit) - should use default unit
154 { "60", "s", "s", 60, true, "1m", "plain 60 with default seconds" },
155 { "3600", "s", "s", 3600, true, "1h", "plain 3600 with default seconds" },
156 { "86400", "s", "s", 86400, true, "1d", "plain 86400 with default seconds" },
157 { "7", "d", "d", 7, true, "7d", "plain 7 with default days" },
158 { "24", "h", "h", 24, true, "1d", "plain 24 with default hours" },
159 { "60", "m", "m", 60, true, "1h", "plain 60 with default minutes" },
160 { "1000", "ms", "ms", 1000, true, "1s", "plain 1000 with default milliseconds" },
161 { "1000000", "us", "us", 1000000, true, "1s", "plain 1000000 with default microseconds" },
162 { "1000000000", "ns", "ns", 1000000000, true, "1s", "plain 1000000000 with default nanoseconds" },
163
164 // Negative plain numbers
165 { "-60", "s", "s", -60, true, "-1m", "negative 60 with default seconds" },
166 { "-3600", "s", "s", -3600, true, "-1h", "negative 3600 with default seconds" },
167 { "-86400", "s", "s", -86400, true, "-1d", "negative 86400 with default seconds" },
168 { "-7", "d", "d", -7, true, "-7d", "negative 7 with default days" },
169 { "-24", "h", "h", -24, true, "-1d", "negative 24 with default hours" },
170 { "-60", "m", "m", -60, true, "-1h", "negative 60 with default minutes" },
171
172 // Fractional plain numbers
173 { "1.5", "d", "d", 2, true, "2d", "fractional 1.5 with default days rounds to 2" },
174 { "2.5", "h", "h", 3, true, "3h", "fractional 2.5 with default hours rounds to 3" },
175 { "0.5", "m", "m", 1, true, "1m", "fractional 0.5 with default minutes rounds to 1" },
176 { "1.5", "s", "s", 2, true, "2s", "fractional 1.5 with default seconds rounds to 2" },
177 { "-1.5", "h", "h", -2, true, "-2h", "negative fractional with default hours rounds to -2" },
178
179 // Edge cases with plain numbers
180 { "0", "s", "s", 0, true, "off", "plain zero" },
181 { "-0", "s", "s", 0, true, "off", "negative zero" },
182 { "+60", "s", "s", 60, true, "1m", "explicit positive sign" },
183 { " 60 ", "s", "s", 60, true, "1m", "spaces around number" },
184
185 // Unix epoch timestamps (large numbers)
186 { "1705318200", NULL, "s", 1705318200, true, NULL, "Unix timestamp: Mon Jan 15 2024 10:30:00 UTC" },
187 { "1609459200", NULL, "s", 1609459200, true, NULL, "Unix timestamp: Fri Jan 01 2021 00:00:00 UTC" },
188 { "946684800", NULL, "s", 946684800, true, NULL, "Unix timestamp: Sat Jan 01 2000 00:00:00 UTC" },
189 { "0", NULL, "s", 0, true, "off", "Unix timestamp: epoch (Jan 01 1970)" },
190 { "-86400", NULL, "s", -86400, true, "-1d", "Unix timestamp: negative (before epoch)" },
191
192 // Very large numbers (future timestamps)
193 { "2147483647", NULL, "s", 2147483647, true, NULL, "Unix timestamp: max 32-bit (Jan 19 2038)" },
194 { "4102444800", NULL, "s", 4102444800, true, NULL, "Unix timestamp: Jan 01 2100" },
195
196 // Timestamp-like numbers with units (should parse as duration)
197 { "1705318200s", NULL, "s", 1705318200, true, NULL, "timestamp with 's' unit" },
198 { "1705318200 seconds", NULL, "s", 1705318200, true, NULL, "timestamp with 'seconds' unit" },
199
200 // End marker
201 { NULL, NULL, NULL, 0, false, NULL, NULL }
202 };
203
204 static int run_duration_test(const duration_test_case_t *test) {
205 int64_t result = 0;
206 const char *default_unit = test->default_unit ? test->default_unit : "s";
207 bool success = duration_parse(test->input, &result, default_unit, test->output_unit);
208
209 if (success != test->should_succeed) {
210 fprintf(stderr, "FAILED: %s\n", test->description);
211 fprintf(stderr, " Input: '%s'\n", test->input);
212 fprintf(stderr, " Expected to %s but %s\n",
213 test->should_succeed ? "succeed" : "fail",
214 success ? "succeeded" : "failed");
215 return 1;
216 }
217
218 if (success) {
219 // Check the parsed value
220 if (result != test->expected_value) {
221 fprintf(stderr, "FAILED: %s\n", test->description);
222 fprintf(stderr, " Input: '%s'\n", test->input);
223 fprintf(stderr, " Expected: %" PRId64 " %s\n", test->expected_value, test->output_unit);
224 fprintf(stderr, " Got: %" PRId64 " %s\n", result, test->output_unit);
225
226 // Also show the reformatted version
227 char buffer[256];
228 ssize_t len = duration_snprintf(buffer, sizeof(buffer), result, test->output_unit, false);
229 if (len > 0) {
230 fprintf(stderr, " Reformatted: '%s'\n", buffer);
231 }
232 return 1;
233 }
234
235 // Check the reformatted output if expected
236 if (test->expected_reformat != NULL) {
237 char buffer[256];
238 ssize_t len = duration_snprintf(buffer, sizeof(buffer), result, test->output_unit, false);
239
240 if (len < 0) {
241 fprintf(stderr, "FAILED: %s (reformat failed)\n", test->description);
242 fprintf(stderr, " Input: '%s'\n", test->input);
243 fprintf(stderr, " Value: %" PRId64 " %s\n", result, test->output_unit);
244 return 1;
245 }
246
247 if (strcmp(buffer, test->expected_reformat) != 0) {
248 fprintf(stderr, "FAILED: %s (reformat mismatch)\n", test->description);
249 fprintf(stderr, " Input: '%s'\n", test->input);
250 fprintf(stderr, " Value: %" PRId64 " %s\n", result, test->output_unit);
251 fprintf(stderr, " Expected reformat: '%s'\n", test->expected_reformat);
252 fprintf(stderr, " Got reformat: '%s'\n", buffer);
253 return 1;
254 }
255 }
256 }
257
258 return 0;
259 }
260
261 static int test_duration_generation(void) {
262 int failed = 0;
263 char buffer[256];
264
265 // Test that generation still uses abbreviated forms
266 struct {
267 int64_t value;
268 const char *unit;
269 const char *expected;
270 } gen_tests[] = {
271 { 300, "s", "5m" },
272 { 7200, "s", "2h" },
273 { 86400, "s", "1d" },
274 { 604800, "s", "7d" },
275 { 2592000, "s", "1mo" },
276 { 31536000, "s", "1y" },
277 { 9000, "s", "2h30m" },
278 { 129600, "s", "1d12h" },
279 { 0, "s", "off" },
280 { -300, "s", "-5m" },
281 { 0, NULL, NULL }
282 };
283
284 for (int i = 0; gen_tests[i].unit != NULL; i++) {
285 ssize_t len = duration_snprintf(buffer, sizeof(buffer), gen_tests[i].value, gen_tests[i].unit, false);
286
287 if (len < 0) {
288 fprintf(stderr, "FAILED: Generation test %d - snprintf failed\n", i);
289 failed++;
290 continue;
291 }
292
293 if (strcmp(buffer, gen_tests[i].expected) != 0) {
294 fprintf(stderr, "FAILED: Generation test %d\n", i);
295 fprintf(stderr, " Value: %" PRId64 " %s\n", gen_tests[i].value, gen_tests[i].unit);
296 fprintf(stderr, " Expected: '%s'\n", gen_tests[i].expected);
297 fprintf(stderr, " Got: '%s'\n", buffer);
298 failed++;
299 }
300 }
301
302 return failed;
303 }
304
305 static int test_parse_and_format_roundtrip(void) {
306 int failed = 0;
307 char buffer[256];
308
309 // Test cases for parse -> format -> parse roundtrip
310 struct {
311 const char *input;
312 const char *expected_format; // What we expect after parsing and reformatting
313 const char *description;
314 } roundtrip_tests[] = {
315 // Simple cases
316 { "7d", "7d", "simple days" },
317 { "7 days", "7d", "full days name" },
318 { "2h30m", "2h30m", "hours and minutes" },
319 { "2 hours 30 minutes", "2h30m", "full names" },
320
321 // Arithmetic expressions
322 { "1d-12h", "12h", "day minus hours" },
323 { "2h-3h", "-1h", "negative result" },
324 { "1d12h", "1d12h", "day plus hours" },
325
326 // Negative durations
327 { "-7d", "-7d", "negative days" },
328 { "-2h30m", "-2h30m", "negative complex" },
329
330 // With "ago" (should be formatted without "ago")
331 { "7 days ago", "-7d", "days ago" },
332 { "2h30m ago", "-2h30m", "complex ago" },
333 { "-7 days ago", "-7d", "redundant negative ago" },
334
335 // Edge cases
336 { "0s", "off", "zero seconds" },
337 { "never", "off", "never keyword" },
338 { "off", "off", "off keyword" },
339
340 { NULL, NULL, NULL }
341 };
342
343 for (int i = 0; roundtrip_tests[i].input != NULL; i++) {
344 // Parse the input
345 int64_t value;
346 bool success = duration_parse(roundtrip_tests[i].input, &value, "s", "s");
347
348 if (!success) {
349 fprintf(stderr, "FAILED: Roundtrip parse failed for '%s' (%s)\n",
350 roundtrip_tests[i].input, roundtrip_tests[i].description);
351 failed++;
352 continue;
353 }
354
355 // Format it back
356 ssize_t len = duration_snprintf(buffer, sizeof(buffer), value, "s", false);
357 if (len < 0) {
358 fprintf(stderr, "FAILED: Roundtrip format failed for '%s' (%s)\n",
359 roundtrip_tests[i].input, roundtrip_tests[i].description);
360 failed++;
361 continue;
362 }
363
364 // Check if it matches expected format
365 if (strcmp(buffer, roundtrip_tests[i].expected_format) != 0) {
366 fprintf(stderr, "FAILED: Roundtrip test '%s'\n", roundtrip_tests[i].description);
367 fprintf(stderr, " Input: '%s'\n", roundtrip_tests[i].input);
368 fprintf(stderr, " Expected format: '%s'\n", roundtrip_tests[i].expected_format);
369 fprintf(stderr, " Got format: '%s'\n", buffer);
370 fprintf(stderr, " Parsed value: %" PRId64 " seconds\n", value);
371 failed++;
372 }
373
374 // Parse the formatted string again to verify it gives the same value
375 int64_t value2;
376 success = duration_parse(buffer, &value2, "s", "s");
377 if (!success || value != value2) {
378 fprintf(stderr, "FAILED: Roundtrip re-parse failed for '%s'\n", roundtrip_tests[i].description);
379 fprintf(stderr, " Original: '%s' -> %" PRId64 "\n", roundtrip_tests[i].input, value);
380 fprintf(stderr, " Formatted: '%s' -> %" PRId64 "\n", buffer, value2);
381 failed++;
382 }
383 }
384
385 return failed;
386 }
387
388 int duration_unittest(void) {
389 int passed = 0;
390 int failed = 0;
391
392 printf("Starting duration parser unit tests with full unit name support\n");
393 printf("===============================================================\n\n");
394
395 // Run parsing tests
396 printf("Running parsing tests...\n");
397 for (const duration_test_case_t *test = test_cases; test->input != NULL; test++) {
398 if (run_duration_test(test) == 0) {
399 passed++;
400 } else {
401 failed++;
402 }
403 }
404
405 printf("\nRunning generation tests...\n");
406 int gen_failed = test_duration_generation();
407 if (gen_failed == 0) {
408 printf("All generation tests passed\n");
409 } else {
410 failed += gen_failed;
411 }
412
413 printf("\nRunning parse/format roundtrip tests...\n");
414 int roundtrip_failed = test_parse_and_format_roundtrip();
415 if (roundtrip_failed == 0) {
416 printf("All roundtrip tests passed\n");
417 } else {
418 failed += roundtrip_failed;
419 }
420
421 printf("\n===============================================================\n");
422 printf("Duration parser tests completed: %d passed, %d failed\n", passed, failed);
423
424 return failed;
425 }