master ohmu/fastfetch-openbsd / tests / duration.c
ohmu  ·  2026-10-15
  1#include "common/duration.h"
  2#include "common/textModifier.h"
  3#include "fastfetch.h"
  4
  5#include <stdlib.h>
  6
  7static void verify(uint64_t totalSeconds, const char* expected, int lineNo) {
  8    FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
  9    ffDurationAppendNum(totalSeconds, &result);
 10    if (!ffStrbufEqualS(&result, expected)) {
 11        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);
 12        exit(1);
 13    }
 14}
 15
 16#define VERIFY(color, expected) verify((color), (expected), __LINE__)
 17
 18int main(void) {
 19    // Test seconds less than 60
 20    VERIFY(0, "0 seconds");
 21    VERIFY(1, "1 second");
 22    VERIFY(2, "2 seconds");
 23    VERIFY(59, "59 seconds");
 24
 25    // Test minute rounding (when seconds >= 30)
 26    VERIFY(60, "1 min");
 27    VERIFY(60 + 29, "1 min");
 28    VERIFY(60 + 30, "2 mins");
 29
 30    // Test only minutes
 31    VERIFY(60 * 2 - 1, "2 mins");
 32    VERIFY(60 * 2, "2 mins");
 33    VERIFY(60 * 59 + 29, "59 mins");
 34
 35    // Test only hours (no minutes)
 36    VERIFY(60 * 59 + 30, "1 hour");
 37    VERIFY(60 * 60, "1 hour");
 38    VERIFY(2 * 60 * 60, "2 hours");
 39    VERIFY(23 * 60 * 60, "23 hours");
 40
 41    // Test combination of hours and minutes
 42    VERIFY(60 * 60 + 60, "1 hour, 1 min");
 43    VERIFY(60 * 60 + 60 * 2, "1 hour, 2 mins");
 44    VERIFY(60 * 60 * 2 + 60 + 29, "2 hours, 1 min");
 45    VERIFY(60 * 60 * 2 + 60 + 30, "2 hours, 2 mins");
 46
 47    // Test days
 48    VERIFY(60 * 60 * 24, "1 day");
 49    VERIFY(60 * 60 * 24 - 1, "1 day");
 50    VERIFY(60 * 60 * 24 * 2, "2 days");
 51
 52    // Test combination of days and hours
 53    VERIFY(60 * 60 * 24 + 60 * 60, "1 day, 1 hour");
 54    VERIFY(60 * 60 * 24 * 2 + 60 * 60, "2 days, 1 hour");
 55    VERIFY(60 * 60 * 24 * 2 + 60 * 60 * 2, "2 days, 2 hours");
 56
 57    // Test combination of days, hours, and minutes
 58    VERIFY(60 * 60 * 24 + 60 * 60 + 60, "1 day, 1 hour, 1 min");
 59    VERIFY(60 * 60 * 24 * 2 + 60 * 60 + 60 * 2, "2 days, 1 hour, 2 mins");
 60    VERIFY(60 * 60 * 24 * 2 + 60 * 2, "2 days, 2 mins");
 61
 62    // Test very large number of days
 63    VERIFY(60 * 60 * 24 * 100, "100 days(!)");
 64    VERIFY(60 * 60 * 24 * 200, "200 days(!)");
 65
 66    instance.config.display.durationAbbreviation = true;
 67    instance.config.display.durationSpaceBeforeUnit = FF_SPACE_BEFORE_UNIT_NEVER;
 68    // Test seconds less than 60
 69    VERIFY(0, "0secs");
 70    VERIFY(1, "1sec");
 71    VERIFY(2, "2secs");
 72    VERIFY(59, "59secs");
 73
 74    // Test minute rounding (when seconds >= 30)
 75    VERIFY(60, "1m");
 76    VERIFY(60 + 29, "1m");
 77    VERIFY(60 + 30, "2m");
 78
 79    // Test only minutes
 80    VERIFY(60 * 2 - 1, "2m");
 81    VERIFY(60 * 2, "2m");
 82    VERIFY(60 * 59 + 29, "59m");
 83
 84    // Test only hours (no minutes)
 85    VERIFY(60 * 59 + 30, "1h");
 86    VERIFY(60 * 60, "1h");
 87    VERIFY(2 * 60 * 60, "2h");
 88    VERIFY(23 * 60 * 60, "23h");
 89
 90    // Test combination of hours and minutes
 91    VERIFY(60 * 60 + 60, "1h 1m");
 92    VERIFY(60 * 60 + 60 * 2, "1h 2m");
 93    VERIFY(60 * 60 * 2 + 60 + 29, "2h 1m");
 94    VERIFY(60 * 60 * 2 + 60 + 30, "2h 2m");
 95
 96    // Test days
 97    VERIFY(60 * 60 * 24, "1d");
 98    VERIFY(60 * 60 * 24 - 1, "1d");
 99    VERIFY(60 * 60 * 24 * 2, "2d");
100
101    // Test combination of days and hours
102    VERIFY(60 * 60 * 24 + 60 * 60, "1d 1h");
103    VERIFY(60 * 60 * 24 * 2 + 60 * 60, "2d 1h");
104    VERIFY(60 * 60 * 24 * 2 + 60 * 60 * 2, "2d 2h");
105
106    // Test combination of days, hours, and minutes
107    VERIFY(60 * 60 * 24 + 60 * 60 + 60, "1d 1h 1m");
108    VERIFY(60 * 60 * 24 * 2 + 60 * 60 + 60 * 2, "2d 1h 2m");
109    VERIFY(60 * 60 * 24 * 2 + 60 * 2, "2d 2m");
110
111    // Test very large number of days
112    VERIFY(60 * 60 * 24 * 100, "100d");
113    VERIFY(60 * 60 * 24 * 200, "200d");
114
115    // Success
116    puts("\033[32mAll tests passed!" FASTFETCH_TEXT_MODIFIER_RESET);
117}