diff options
Diffstat (limited to 'test/gen_test_data.c')
| -rw-r--r-- | test/gen_test_data.c | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/test/gen_test_data.c b/test/gen_test_data.c new file mode 100644 index 0000000..6ef54af --- /dev/null +++ b/test/gen_test_data.c @@ -0,0 +1,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; +} |