ohmu
·
2026-10-15
1#include "common/frequency.h"
2#include "common/textModifier.h"
3#include "fastfetch.h"
4
5#include <stdint.h>
6#include <stdio.h>
7#include <stdlib.h>
8
9static void verify(uint32_t mhz, bool expectedResult, const char* expected, int lineNo) {
10 FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
11 const bool returned = ffFreqAppendNum(mhz, &result);
12
13 if (returned != expectedResult || !ffStrbufEqualS(&result, expected)) {
14 fprintf(stderr, FASTFETCH_TEXT_MODIFIER_ERROR "[%d] ffFreqAppendNum(%u): expected %s \"%s\", got %s \"%s\"\n" FASTFETCH_TEXT_MODIFIER_RESET,
15 lineNo, mhz, expectedResult ? "true" : "false", expected, returned ? "true" : "false", result.chars);
16 exit(1);
17 }
18}
19
20#define VERIFY_FREQ(mhz, expectedResult, expected) verify((mhz), (expectedResult), (expected), __LINE__)
21
22int main(void) {
23 // Use the real defaults rather than a hand built config, so a changed default is caught here too
24 ffOptionsInitDisplay(&instance.config.display);
25 FFOptionsDisplay* options = &instance.config.display;
26
27 // An unknown frequency prints nothing at all
28 {
29 VERIFY_FREQ(0, false, "");
30 }
31
32 // With `freqNdigits >= 0` the value is printed in GHz, with that many digits after the point
33 {
34 VERIFY_FREQ(1000, true, "1.00 GHz");
35 VERIFY_FREQ(3600, true, "3.60 GHz");
36 VERIFY_FREQ(5000, true, "5.00 GHz");
37 VERIFY_FREQ(100, true, "0.10 GHz");
38 VERIFY_FREQ(1, true, "0.00 GHz"); // rounds down to zero, but is still reported
39 VERIFY_FREQ(4294000, true, "4294.00 GHz"); // the whole `uint32_t` range is usable
40 }
41
42 // `freqNdigits = 0` rounds to whole GHz
43 {
44 options->freqNdigits = 0;
45 VERIFY_FREQ(1000, true, "1 GHz");
46 VERIFY_FREQ(3600, true, "4 GHz"); // rounded, not truncated
47 VERIFY_FREQ(1400, true, "1 GHz");
48 VERIFY_FREQ(0, false, "");
49 options->freqNdigits = 2;
50 }
51
52 // A negative `freqNdigits` selects the integer MHz form instead
53 {
54 options->freqNdigits = -1;
55 VERIFY_FREQ(3600, true, "3600 MHz");
56 VERIFY_FREQ(1000, true, "1000 MHz");
57 VERIFY_FREQ(1, true, "1 MHz");
58 VERIFY_FREQ(0, false, "");
59 options->freqNdigits = 2;
60 }
61
62 // Only `never` removes the space between the number and the unit
63 {
64 options->freqSpaceBeforeUnit = FF_SPACE_BEFORE_UNIT_NEVER;
65 VERIFY_FREQ(3600, true, "3.60GHz");
66 options->freqNdigits = -1;
67 VERIFY_FREQ(3600, true, "3600MHz");
68 options->freqNdigits = 2;
69
70 options->freqSpaceBeforeUnit = FF_SPACE_BEFORE_UNIT_ALWAYS;
71 VERIFY_FREQ(3600, true, "3.60 GHz");
72
73 options->freqSpaceBeforeUnit = FF_SPACE_BEFORE_UNIT_DEFAULT;
74 VERIFY_FREQ(3600, true, "3.60 GHz");
75 }
76
77 // The result is appended to the buffer instead of replacing it
78 {
79 FF_STRBUF_AUTO_DESTROY result = ffStrbufCreateS("CPU: ");
80 if (!ffFreqAppendNum(3600, &result) || !ffStrbufEqualS(&result, "CPU: 3.60 GHz")) {
81 fprintf(stderr, FASTFETCH_TEXT_MODIFIER_ERROR "[%d] ffFreqAppendNum did not append: got \"%s\"\n" FASTFETCH_TEXT_MODIFIER_RESET, __LINE__, result.chars);
82 exit(1);
83 }
84 }
85
86 ffOptionsDestroyDisplay(options);
87
88 puts("\033[32mAll tests passed!" FASTFETCH_TEXT_MODIFIER_RESET);
89 return 0;
90}