File: duration.c

package info (click to toggle)
fastfetch 2.55.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,692 kB
  • sloc: ansic: 65,950; cpp: 1,716; python: 274; sh: 123; makefile: 26; xml: 19
file content (120 lines) | stat: -rw-r--r-- 3,819 bytes parent folder | download
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
#include "common/duration.h"
#include "util/textModifier.h"
#include "fastfetch.h"

#include <stdlib.h>

static void verify(uint64_t totalSeconds, const char* expected, int lineNo)
{
    FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
    ffDurationAppendNum(totalSeconds, &result);
    if (!ffStrbufEqualS(&result, expected))
    {
        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);
        exit(1);
    }
}

#define VERIFY(color, expected) verify((color), (expected), __LINE__)

int main(void)
{
    // Test seconds less than 60
    VERIFY(0, "0 seconds");
    VERIFY(1, "1 second");
    VERIFY(2, "2 seconds");
    VERIFY(59, "59 seconds");

    // Test minute rounding (when seconds >= 30)
    VERIFY(60, "1 min");
    VERIFY(60 + 29, "1 min");
    VERIFY(60 + 30, "2 mins");

    // Test only minutes
    VERIFY(60 * 2 - 1, "2 mins");
    VERIFY(60 * 2, "2 mins");
    VERIFY(60 * 59 + 29, "59 mins");

    // Test only hours (no minutes)
    VERIFY(60 * 59 + 30, "1 hour");
    VERIFY(60 * 60, "1 hour");
    VERIFY(2 * 60 * 60, "2 hours");
    VERIFY(23 * 60 * 60, "23 hours");

    // Test combination of hours and minutes
    VERIFY(60 * 60 + 60, "1 hour, 1 min");
    VERIFY(60 * 60 + 60 * 2, "1 hour, 2 mins");
    VERIFY(60 * 60 * 2 + 60 + 29, "2 hours, 1 min");
    VERIFY(60 * 60 * 2 + 60 + 30, "2 hours, 2 mins");

    // Test days
    VERIFY(60 * 60 * 24, "1 day");
    VERIFY(60 * 60 * 24 - 1, "1 day");
    VERIFY(60 * 60 * 24 * 2, "2 days");

    // Test combination of days and hours
    VERIFY(60 * 60 * 24 + 60 * 60, "1 day, 1 hour");
    VERIFY(60 * 60 * 24 * 2 + 60 * 60, "2 days, 1 hour");
    VERIFY(60 * 60 * 24 * 2 + 60 * 60 * 2, "2 days, 2 hours");

    // Test combination of days, hours, and minutes
    VERIFY(60 * 60 * 24 + 60 * 60 + 60, "1 day, 1 hour, 1 min");
    VERIFY(60 * 60 * 24 * 2 + 60 * 60 + 60 * 2, "2 days, 1 hour, 2 mins");
    VERIFY(60 * 60 * 24 * 2 + 60 * 2, "2 days, 2 mins");

    // Test very large number of days
    VERIFY(60 * 60 * 24 * 100, "100 days(!)");
    VERIFY(60 * 60 * 24 * 200, "200 days(!)");

    instance.config.display.durationAbbreviation = true;
    instance.config.display.durationSpaceBeforeUnit = FF_SPACE_BEFORE_UNIT_NEVER;
    // Test seconds less than 60
    VERIFY(0, "0secs");
    VERIFY(1, "1sec");
    VERIFY(2, "2secs");
    VERIFY(59, "59secs");

    // Test minute rounding (when seconds >= 30)
    VERIFY(60, "1m");
    VERIFY(60 + 29, "1m");
    VERIFY(60 + 30, "2m");

    // Test only minutes
    VERIFY(60 * 2 - 1, "2m");
    VERIFY(60 * 2, "2m");
    VERIFY(60 * 59 + 29, "59m");

    // Test only hours (no minutes)
    VERIFY(60 * 59 + 30, "1h");
    VERIFY(60 * 60, "1h");
    VERIFY(2 * 60 * 60, "2h");
    VERIFY(23 * 60 * 60, "23h");

    // Test combination of hours and minutes
    VERIFY(60 * 60 + 60, "1h 1m");
    VERIFY(60 * 60 + 60 * 2, "1h 2m");
    VERIFY(60 * 60 * 2 + 60 + 29, "2h 1m");
    VERIFY(60 * 60 * 2 + 60 + 30, "2h 2m");

    // Test days
    VERIFY(60 * 60 * 24, "1d");
    VERIFY(60 * 60 * 24 - 1, "1d");
    VERIFY(60 * 60 * 24 * 2, "2d");

    // Test combination of days and hours
    VERIFY(60 * 60 * 24 + 60 * 60, "1d 1h");
    VERIFY(60 * 60 * 24 * 2 + 60 * 60, "2d 1h");
    VERIFY(60 * 60 * 24 * 2 + 60 * 60 * 2, "2d 2h");

    // Test combination of days, hours, and minutes
    VERIFY(60 * 60 * 24 + 60 * 60 + 60, "1d 1h 1m");
    VERIFY(60 * 60 * 24 * 2 + 60 * 60 + 60 * 2, "2d 1h 2m");
    VERIFY(60 * 60 * 24 * 2 + 60 * 2, "2d 2m");

    // Test very large number of days
    VERIFY(60 * 60 * 24 * 100, "100d");
    VERIFY(60 * 60 * 24 * 200, "200d");

    //Success
    puts("\033[32mAll tests passed!" FASTFETCH_TEXT_MODIFIER_RESET);
}