|
| 1 | +#include "common/parsing.h" |
| 2 | +#include "util/textModifier.h" |
| 3 | +#include "fastfetch.h" |
| 4 | + |
| 5 | +#include <stdlib.h> |
| 6 | + |
| 7 | +static void verify(uint64_t totalSeconds, const char* expected, int lineNo) |
| 8 | +{ |
| 9 | + FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate(); |
| 10 | + ffParseDuration(totalSeconds, &result); |
| 11 | + if (!ffStrbufEqualS(&result, expected)) |
| 12 | + { |
| 13 | + fprintf(stderr, FASTFETCH_TEXT_MODIFIER_ERROR "[%d] %llu: expected \"%s\", got \"%s\"\n" FASTFETCH_TEXT_MODIFIER_RESET, lineNo, (unsigned long long) totalSeconds, expected, result.chars); |
| 14 | + exit(1); |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +#define VERIFY(color, expected) verify((color), (expected), __LINE__) |
| 19 | + |
| 20 | +int main(void) |
| 21 | +{ |
| 22 | + // Test seconds less than 60 |
| 23 | + VERIFY(0, "0 seconds"); |
| 24 | + VERIFY(1, "1 second"); |
| 25 | + VERIFY(2, "2 seconds"); |
| 26 | + VERIFY(59, "59 seconds"); |
| 27 | + |
| 28 | + // Test minute rounding (when seconds >= 30) |
| 29 | + VERIFY(60, "1 min"); |
| 30 | + VERIFY(60 + 29, "1 min"); |
| 31 | + VERIFY(60 + 30, "2 mins"); |
| 32 | + |
| 33 | + // Test only minutes |
| 34 | + VERIFY(60 * 2 - 1, "2 mins"); |
| 35 | + VERIFY(60 * 2, "2 mins"); |
| 36 | + VERIFY(60 * 59 + 29, "59 mins"); |
| 37 | + |
| 38 | + // Test only hours (no minutes) |
| 39 | + VERIFY(60 * 59 + 30, "1 hour"); |
| 40 | + VERIFY(60 * 60, "1 hour"); |
| 41 | + VERIFY(2 * 60 * 60, "2 hours"); |
| 42 | + VERIFY(23 * 60 * 60, "23 hours"); |
| 43 | + |
| 44 | + // Test combination of hours and minutes |
| 45 | + VERIFY(60 * 60 + 60, "1 hour, 1 min"); |
| 46 | + VERIFY(60 * 60 + 60 * 2, "1 hour, 2 mins"); |
| 47 | + VERIFY(60 * 60 * 2 + 60 + 29, "2 hours, 1 min"); |
| 48 | + VERIFY(60 * 60 * 2 + 60 + 30, "2 hours, 2 mins"); |
| 49 | + |
| 50 | + // Test days |
| 51 | + VERIFY(60 * 60 * 24, "1 day"); |
| 52 | + VERIFY(60 * 60 * 24 - 1, "1 day"); |
| 53 | + VERIFY(60 * 60 * 24 * 2, "2 days"); |
| 54 | + |
| 55 | + // Test combination of days and hours |
| 56 | + VERIFY(60 * 60 * 24 + 60 * 60, "1 day, 1 hour"); |
| 57 | + VERIFY(60 * 60 * 24 * 2 + 60 * 60, "2 days, 1 hour"); |
| 58 | + VERIFY(60 * 60 * 24 * 2 + 60 * 60 * 2, "2 days, 2 hours"); |
| 59 | + |
| 60 | + // Test combination of days, hours, and minutes |
| 61 | + VERIFY(60 * 60 * 24 + 60 * 60 + 60, "1 day, 1 hour, 1 min"); |
| 62 | + VERIFY(60 * 60 * 24 * 2 + 60 * 60 + 60 * 2, "2 days, 1 hour, 2 mins"); |
| 63 | + VERIFY(60 * 60 * 24 * 2 + 60 * 2, "2 days, 2 mins"); |
| 64 | + |
| 65 | + // Test very large number of days |
| 66 | + VERIFY(60 * 60 * 24 * 100, "100 days(!)"); |
| 67 | + VERIFY(60 * 60 * 24 * 200, "200 days(!)"); |
| 68 | + |
| 69 | + //Success |
| 70 | + puts("\033[32mAll tests passed!" FASTFETCH_TEXT_MODIFIER_RESET); |
| 71 | +} |
0 commit comments