brewery
notashelf /
4d100741815950891775083d36a2f08d8da3b500

chroma

public

Lightweight wallpaper daemon for Wayland

Star0tarball
clone
ssh://git@git.notashelf.dev:33/notashelf/chroma.git
tests/util/test_common.hC88 lines2.5 KB
1
#ifndef TEST_COMMON_H
2
#define TEST_COMMON_H
3
4
#include <stdio.h>
5
#include <stdlib.h>
6
#include <stdint.h>
7
#include <string.h>
8
#include <math.h>
9
#include <time.h>
10
11
#define TEST_PASSED 0
12
#define TEST_FAILED 1
13
14
typedef struct {
15
    const char *name;
16
    int (*fn)(void);
17
} TestCase;
18
19
typedef struct {
20
    const char *name;
21
    double (*fn)(void);
22
} BenchCase;
23
24
extern int test_failures;
25
extern int test_total;
26
27
#define TEST_ASSERT(cond, msg) do { \
28
    if (!(cond)) { \
29
        fprintf(stderr, "  [FAIL] %s\n", msg); \
30
        test_failures++; \
31
        return TEST_FAILED; \
32
    } \
33
} while (0)
34
35
#define TEST_ASSERT_EQ(actual, expected, msg) do { \
36
    if ((actual) != (expected)) { \
37
        fprintf(stderr, "  [FAIL] %s: expected %ld, got %ld\n", msg, (long)(expected), (long)(actual)); \
38
        test_failures++; \
39
        return TEST_FAILED; \
40
    } \
41
} while (0)
42
43
#define TEST_ASSERT_FTZ(actual, expected, tol, msg) do { \
44
    double _diff = fabs((actual) - (expected)); \
45
    if (_diff > (tol)) { \
46
        fprintf(stderr, "  [FAIL] %s: expected %.6f, got %.6f (diff %.6f)\n", msg, (expected), (actual), _diff); \
47
        test_failures++; \
48
        return TEST_FAILED; \
49
    } \
50
} while (0)
51
52
#define TEST_ASSERT_PTR_NOT_NULL(ptr, msg) do { \
53
    if ((ptr) == NULL) { \
54
        fprintf(stderr, "  [FAIL] %s: pointer is NULL\n", msg); \
55
        test_failures++; \
56
        return TEST_FAILED; \
57
    } \
58
} while (0)
59
60
#define RUN_TEST(tests, name) do { \
61
    int _result = (name)(); \
62
    test_total++; \
63
    if (_result == TEST_PASSED) { \
64
        printf("  [PASS] %s\n", #name); \
65
    } \
66
} while (0)
67
68
#define RUN_BENCH(benchmarks, name, iter) do { \
69
    double _time = run_benchmark((name), (iter)); \
70
    printf("  [BENCH] %-40s %.3f ms\n", #name, _time); \
71
} while (0)
72
73
extern int test_failures;
74
extern int test_total;
75
76
uint8_t *load_image(const char *path, int *width, int *height, int *channels);
77
int save_image(const char *path, uint8_t *data, int width, int height, int channels);
78
uint8_t *downsample_image(uint8_t *src, int sw, int sh, int sc, int *dw, int *dh, float scale);
79
double get_time_ms(void);
80
double run_benchmark(double (*fn)(void), int iterations);
81
int compare_images(uint8_t *a, uint8_t *b, int w, int h, int ch, float threshold);
82
float compute_psnr(uint8_t *a, uint8_t *b, int w, int h, int ch);
83
uint8_t *create_gradient_image(int w, int h);
84
uint8_t *create_noise_image(int w, int h, unsigned int seed);
85
uint8_t *create_uniform_image(int w, int h, uint8_t r, uint8_t g, uint8_t b);
86
uint8_t *create_checkerboard(int w, int h, int check_size);
87
88
#endif