summaryrefslogtreecommitdiffstats
path: root/test/gen_test_data.c
blob: 6ef54afde0cb71bce652d0afce066793317b53e4 (plain)
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
/* gen_test_data.c - Validate all ts functions run without error.
 * Note: excluded from default build. Fix and add target to use.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "ts.h"

static fp_t rand_fp(void) {
    return (double)rand() / (double)RAND_MAX * 100.0;
}

/*
 * Smoke-test a single function by name. Returns 0 on success.
 * This is a placeholder — a full auto-discovery registry would require
 * adding ts_functions[] / ts_functions_n to ts.h or generating them.
 */
int main(void) {
    unsigned int seed = 42;
    srand(seed);
    uint_t n = 100;

    /* Test a representative sample of functions */
    struct {
        const char *name;
        int (*func)(fp_t * const y[], fp_t const * const x[], uint_t, ts_constant_t const *);
        uint_t x_n, y_n;
    } samples[] = {
        { "average", ts_average, 1, 1 },
        { "rsi", ts_rsi, 1, 1 },
        { "deviation_standard", ts_deviation_standard, 1, 1 },
        { "variance", ts_variance, 1, 1 },
        { "addition", ts_addition, 2, 1 },
        { "subtraction", ts_subtraction, 2, 1 },
        { "multiplication", ts_multiplication, 2, 1 },
        { "division", ts_division, 2, 1 },
        { "absolute_value", ts_absolute_value, 1, 1 },
        { "nan_fn", ts_nan_fn, 1, 1 },
    };
    size_t nsamples = sizeof(samples) / sizeof(samples[0]);

    for (size_t fi = 0; fi < nsamples; fi++) {
        typeof(samples[fi]) f = samples[fi];

        /* Allocate input arrays */
        fp_t **x = calloc(f.x_n, sizeof(fp_t *));
        fp_t **y = calloc(f.y_n, sizeof(fp_t *));
        if (!x || !y) { free(x); free(y); continue; }

        for (uint_t i = 0; i < f.x_n; i++) {
            x[i] = malloc(n * sizeof(fp_t));
            if (!x[i]) goto next;
            for (uint_t j = 0; j < n; j++) x[i][j] = rand_fp();
        }
        for (uint_t i = 0; i < f.y_n; i++) {
            y[i] = calloc(n, sizeof(fp_t));
            if (!y[i]) goto next;
        }

        /* Try with default params (all zeros) */
        uint8_t params[TS_CONSTANT_SIZE] = { 0 };
        int ret = f.func(y, x, n, (ts_constant_t const *)params);
        const char *pass_label = "PASS";
        if (ret != 0) {
            /* Try with k=10 */
            {
                uint8_t c[TS_CONSTANT_SIZE] = { 0 };
                *(uint_t *)c = 10;
                ret = f.func(y, x, n, (ts_constant_t const *)c);
                if (ret == 0) pass_label = "PASS (k=10)";
            }
        }
        if (ret != 0) {
            /* Try with k=14 */
            {
                uint8_t c[TS_CONSTANT_SIZE] = { 0 };
                *(uint_t *)c = 14;
                ret = f.func(y, x, n, (ts_constant_t const *)c);
                if (ret == 0) pass_label = "PASS (k=14)";
            }
        }

        if (ret != 0) {
            printf("SKIP %s (ret=%d after all retries)\n", f.name, ret);
            (void)fflush(stdout);
            goto next;
        }

        printf("%s %s\n", pass_label, f.name);
        (void)fflush(stdout);

    next:
        for (uint_t i = 0; i < f.x_n; i++) free(x[i]);
        for (uint_t i = 0; i < f.y_n; i++) free(y[i]);
        free(x);
        free(y);
    }

    return 0;
}