master ohmu/fastfetch-openbsd / tests / logo.c
ohmu  ·  2026-10-15
 1#include "logo/logo.h"
 2#include "common/textModifier.h"
 3
 4#include <stdio.h>
 5#include <stdlib.h>
 6#include <stdnoreturn.h>
 7#include <string.h>
 8
 9noreturn static void testFailed(const char* expression, int lineNo) {
10    fprintf(stderr, FASTFETCH_TEXT_MODIFIER_ERROR "[%d] %s\n" FASTFETCH_TEXT_MODIFIER_RESET, lineNo, expression);
11    exit(1);
12}
13
14noreturn static void testFailedEnum(FFLogoPosition expected, FFLogoPosition actual, const char* what, int lineNo) {
15    fprintf(stderr, FASTFETCH_TEXT_MODIFIER_ERROR "[%d] %s: expected %u, got %u\n" FASTFETCH_TEXT_MODIFIER_RESET, lineNo, what, (unsigned) expected, (unsigned) actual);
16    exit(1);
17}
18
19#define VERIFY(expression) \
20    if (!(expression)) testFailed(#expression, __LINE__)
21
22#define VERIFY_POSITION(expected, actual, what) \
23    if ((expected) != (actual)) testFailedEnum((expected), (actual), (what), __LINE__)
24
25int main(void) {
26    // Unknown terminal size keeps the historical left-side layout.
27    VERIFY_POSITION(FF_LOGO_POSITION_LEFT,
28        ffLogoSelectPosition(FF_LOGO_POSITION_AUTO, 0, 40),
29        "auto fallback");
30
31    // Auto keeps the logo beside the output while at least 32 columns remain for module text.
32    VERIFY_POSITION(FF_LOGO_POSITION_LEFT,
33        ffLogoSelectPosition(FF_LOGO_POSITION_AUTO, 72, 40),
34        "auto exact side-by-side fit");
35    VERIFY_POSITION(FF_LOGO_POSITION_TOP,
36        ffLogoSelectPosition(FF_LOGO_POSITION_AUTO, 71, 40),
37        "auto narrow terminal");
38    VERIFY_POSITION(FF_LOGO_POSITION_TOP,
39        ffLogoSelectPosition(FF_LOGO_POSITION_AUTO, 80, UINT32_MAX),
40        "auto width overflow");
41
42    // Explicit positions are never overridden.
43    VERIFY_POSITION(FF_LOGO_POSITION_LEFT,
44        ffLogoSelectPosition(FF_LOGO_POSITION_LEFT, 20, 40),
45        "explicit left");
46    VERIFY_POSITION(FF_LOGO_POSITION_TOP,
47        ffLogoSelectPosition(FF_LOGO_POSITION_TOP, 120, 40),
48        "explicit top");
49    VERIFY_POSITION(FF_LOGO_POSITION_RIGHT,
50        ffLogoSelectPosition(FF_LOGO_POSITION_RIGHT, 20, 40),
51        "explicit right");
52
53    VERIFY(strcmp(ffLogoPositionToString(FF_LOGO_POSITION_AUTO), "auto") == 0);
54    VERIFY(strcmp(ffLogoPositionToString(FF_LOGO_POSITION_LEFT), "left") == 0);
55    VERIFY(strcmp(ffLogoPositionToString(FF_LOGO_POSITION_TOP), "top") == 0);
56    VERIFY(strcmp(ffLogoPositionToString(FF_LOGO_POSITION_RIGHT), "right") == 0);
57
58    puts("\033[32mAll tests passed!" FASTFETCH_TEXT_MODIFIER_RESET);
59    return 0;
60}