diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-09-13 20:03:39 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-09-13 20:13:28 +0200 |
| commit | 64c3a1b528a88b4ae21bf3cbcff2537200477806 (patch) | |
| tree | 85da00748d7bbb5036aeb5a7a4b60b5abf75aeea /tests/test_robust.c | |
| download | pyseg-64c3a1b528a88b4ae21bf3cbcff2537200477806.tar.gz pyseg-64c3a1b528a88b4ae21bf3cbcff2537200477806.zip | |
Init
Diffstat (limited to 'tests/test_robust.c')
| -rw-r--r-- | tests/test_robust.c | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/tests/test_robust.c b/tests/test_robust.c new file mode 100644 index 0000000..7dd43a7 --- /dev/null +++ b/tests/test_robust.c @@ -0,0 +1,133 @@ +/* Inputs the model will never see in training but will see in production. + * + * The model normalises its input by the window's own standard deviation, so + * digital silence divides by roughly the epsilon in the normalisation and a + * constant offset does the same. Those are the cases that produce infinities + * if the epsilon is dropped or applied in the wrong place, and a whole window + * of silence is entirely ordinary in a real recording. */ + +#include "pyseg.h" +#include "test_util.h" + +#include <math.h> +#include <stdlib.h> +#include <string.h> + +static unsigned rng_state = 987654321u; + +static float next_float(void) { + rng_state = rng_state * 1664525u + 1013904223u; + return (float)((rng_state >> 8) & 0xFFFF) / 32768.0f - 1.0f; +} + +static void check_output(const char *label, const float *probs) { + for (int t = 0; t < PYSEG_FRAMES; ++t) { + const float *row = probs + (size_t)t * PYSEG_CLASSES; + double sum = 0.0; + for (int i = 0; i < PYSEG_CLASSES; ++i) { + if (!isfinite(row[i]) || row[i] < 0.0f || row[i] > 1.0f) { + TEST_ASSERT(0, "%s: frame %d class %d is %g", label, t, i, row[i]); + return; + } + sum += row[i]; + } + if (fabs(sum - 1.0) > 1e-5) { + TEST_ASSERT(0, "%s: frame %d sums to %.9f", label, t, sum); + return; + } + } + TEST_ASSERT(1, "%s", label); +} + +int main(void) { + pyseg_t *v = pyseg_create(); + TEST_ASSERT(v != NULL, "create failed"); + if (!v) + return TEST_REPORT(); + + float *wav = malloc(PYSEG_WINDOW_SAMPLES * sizeof(float)); + float *probs = malloc((size_t)PYSEG_FRAMES * PYSEG_CLASSES * sizeof(float)); + float *active = malloc((size_t)PYSEG_FRAMES * PYSEG_SPEAKERS * sizeof(float)); + int16_t *pcm = malloc(PYSEG_WINDOW_SAMPLES * sizeof(int16_t)); + + memset(wav, 0, PYSEG_WINDOW_SAMPLES * sizeof(float)); + pyseg_process_f32(v, wav, probs); + check_output("digital silence", probs); + + for (int i = 0; i < PYSEG_WINDOW_SAMPLES; ++i) + wav[i] = 0.5f; + pyseg_process_f32(v, wav, probs); + check_output("constant offset", probs); + + for (int i = 0; i < PYSEG_WINDOW_SAMPLES; ++i) + wav[i] = (i & 1) ? 1.0f : -1.0f; + pyseg_process_f32(v, wav, probs); + check_output("full-scale square wave", probs); + + for (int i = 0; i < PYSEG_WINDOW_SAMPLES; ++i) + wav[i] = next_float() * 4.0f; + pyseg_process_f32(v, wav, probs); + check_output("out-of-range noise", probs); + + for (int i = 0; i < PYSEG_WINDOW_SAMPLES; ++i) + wav[i] = next_float() * 1e-8f; + pyseg_process_f32(v, wav, probs); + check_output("near-denormal noise", probs); + + /* A single impulse in an otherwise silent window: the normalisation + * amplifies the silence around it by the full dynamic range. */ + memset(wav, 0, PYSEG_WINDOW_SAMPLES * sizeof(float)); + wav[PYSEG_WINDOW_SAMPLES / 2] = 1.0f; + pyseg_process_f32(v, wav, probs); + check_output("lone impulse", probs); + + for (int i = 0; i < PYSEG_WINDOW_SAMPLES; ++i) + pcm[i] = (int16_t)(i & 1 ? 32767 : -32768); + pyseg_process_s16(v, pcm, probs); + check_output("s16 full scale", probs); + + /* The s16 entry point must be the f32 one with a fixed scaling. */ + for (int i = 0; i < PYSEG_WINDOW_SAMPLES; ++i) { + pcm[i] = (int16_t)((int)(next_float() * 16000.0f)); + wav[i] = (float)pcm[i] * (1.0f / 32768.0f); + } + float *from_s16 = malloc((size_t)PYSEG_FRAMES * PYSEG_CLASSES * sizeof(float)); + pyseg_process_s16(v, pcm, from_s16); + pyseg_process_f32(v, wav, probs); + TEST_ASSERT(memcmp(from_s16, probs, (size_t)PYSEG_FRAMES * PYSEG_CLASSES * sizeof(float)) == 0, + "s16 and f32 entry points disagree on identical audio"); + + /* Decoding must land inside the powerset and stay consistent with it. */ + pyseg_to_multilabel(probs, active); + for (int t = 0; t < PYSEG_FRAMES; ++t) { + const float *a = active + (size_t)t * PYSEG_SPEAKERS; + int count = 0; + for (int s = 0; s < PYSEG_SPEAKERS; ++s) { + if (a[s] != 0.0f && a[s] != 1.0f) { + TEST_ASSERT(0, "frame %d speaker %d is %g, not a hard decision", t, s, a[s]); + break; + } + count += a[s] != 0.0f; + } + if (count > 2) { + TEST_ASSERT(0, "frame %d has %d speakers, more than the powerset allows", t, count); + break; + } + } + TEST_ASSERT(1, "multilabel decoding stays inside the powerset"); + + TEST_ASSERT(pyseg_powerset_class(-1, -1) == 0, "empty set is not class 0"); + TEST_ASSERT(pyseg_powerset_class(0, -1) == 1, "speaker 0 alone is not class 1"); + TEST_ASSERT(pyseg_powerset_class(2, -1) == 3, "speaker 2 alone is not class 3"); + TEST_ASSERT(pyseg_powerset_class(0, 1) == 4, "speakers 0 and 1 are not class 4"); + TEST_ASSERT(pyseg_powerset_class(1, 2) == 6, "speakers 1 and 2 are not class 6"); + + free(from_s16); + free(pcm); + free(active); + free(probs); + free(wav); + pyseg_destroy(v); + pyseg_destroy(NULL); + return TEST_REPORT(); +} |
