aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-09-13 20:03:39 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-09-13 20:13:28 +0200
commit64c3a1b528a88b4ae21bf3cbcff2537200477806 (patch)
tree85da00748d7bbb5036aeb5a7a4b60b5abf75aeea /tests
downloadpyseg-64c3a1b528a88b4ae21bf3cbcff2537200477806.tar.gz
pyseg-64c3a1b528a88b4ae21bf3cbcff2537200477806.zip
Init
Diffstat (limited to 'tests')
-rw-r--r--tests/test_kernels.c151
-rw-r--r--tests/test_parity.c214
-rw-r--r--tests/test_robust.c133
-rw-r--r--tests/test_slide.c212
-rw-r--r--tests/test_util.h29
5 files changed, 739 insertions, 0 deletions
diff --git a/tests/test_kernels.c b/tests/test_kernels.c
new file mode 100644
index 0000000..d4f25d6
--- /dev/null
+++ b/tests/test_kernels.c
@@ -0,0 +1,151 @@
+/* The dense primitive against a naive triple loop, over the shapes the model
+ * actually uses plus enough odd ones to exercise every edge path.
+ *
+ * The reference here is deliberately the most obvious implementation possible:
+ * the point is to catch a blocking or tail-handling mistake, and a second
+ * clever implementation would be liable to the same mistakes. */
+
+#include "kernels.h"
+#include "test_util.h"
+
+#include <math.h>
+#include <stdlib.h>
+
+static unsigned rng_state = 12345u;
+
+static float next_float(void) {
+ rng_state = rng_state * 1664525u + 1013904223u;
+ return (float)((rng_state >> 8) & 0xFFFF) / 32768.0f - 1.0f;
+}
+
+static void gemm_ref(float *y,
+ int ldy,
+ const float *a,
+ int lda,
+ const float *w,
+ const float *bias,
+ int m,
+ int n,
+ int k) {
+ for (int s = 0; s < m; ++s)
+ for (int i = 0; i < n; ++i) {
+ double acc = bias ? bias[i] : 0.0;
+ for (int j = 0; j < k; ++j)
+ acc += (double)a[(size_t)s * lda + j] * w[(size_t)i * k + j];
+ y[(size_t)s * ldy + i] = (float)acc;
+ }
+}
+
+/* Tolerance scales with the window length because that is how many terms the
+ * two orders can disagree over. */
+static int
+check(const char *label, const pyseg_kernels *kern, int m, int n, int k, int lda, int with_bias) {
+ const size_t a_len = (size_t)(m - 1) * lda + k + 8;
+ float *a = malloc(a_len * sizeof(float));
+ float *w = malloc((size_t)n * k * sizeof(float));
+ float *bias = malloc((size_t)n * sizeof(float));
+ float *got = malloc((size_t)m * n * sizeof(float));
+ float *want = malloc((size_t)m * n * sizeof(float));
+
+ for (size_t i = 0; i < a_len; ++i)
+ a[i] = next_float();
+ for (size_t i = 0; i < (size_t)n * k; ++i)
+ w[i] = next_float();
+ for (int i = 0; i < n; ++i)
+ bias[i] = next_float();
+
+ kern->gemm_nt(got, n, a, lda, w, with_bias ? bias : NULL, m, n, k);
+ gemm_ref(want, n, a, lda, w, with_bias ? bias : NULL, m, n, k);
+
+ double worst = 0.0;
+ for (int i = 0; i < m * n; ++i) {
+ const double d = fabs((double)got[i] - want[i]);
+ if (d > worst)
+ worst = d;
+ }
+ const double limit = 2e-6 * k;
+ TEST_ASSERT(worst <= limit, "%s %s m=%d n=%d k=%d lda=%d bias=%d: worst %.3e > %.3e", label,
+ kern->name, m, n, k, lda, with_bias, worst, limit);
+
+ free(a);
+ free(w);
+ free(bias);
+ free(got);
+ free(want);
+ return 0;
+}
+
+/* The SIMD cell update replaces libm's exponential and hyperbolic tangent with
+ * polynomial approximations, so it is checked against libm directly rather
+ * than only through the model. The gate arguments are swept well past the
+ * range where both saturate, because that range is where a range reduction
+ * built on assembling an exponent by hand goes wrong. */
+static void check_lstm_cell(void) {
+ enum { HID = 128 };
+ float rec[4 * HID], in[4 * HID];
+ float h_scalar[HID], c_scalar[HID], h_simd[HID], c_simd[HID];
+
+ for (int trial = 0; trial < 64; ++trial) {
+ const float span = 1.0f + (float)trial * 3.0f; /* up to about 190 */
+ for (int i = 0; i < 4 * HID; ++i) {
+ rec[i] = next_float() * span;
+ in[i] = next_float() * span;
+ }
+ for (int i = 0; i < HID; ++i)
+ c_scalar[i] = c_simd[i] = next_float() * 8.0f;
+
+ pyseg_kernels_scalar()->lstm_cell(h_scalar, c_scalar, rec, in, HID);
+ pyseg_kernels_simd()->lstm_cell(h_simd, c_simd, rec, in, HID);
+
+ double worst = 0.0;
+ for (int i = 0; i < HID; ++i) {
+ const double dh = fabs((double)h_scalar[i] - h_simd[i]);
+ const double dc = fabs((double)c_scalar[i] - c_simd[i]);
+ if (dh > worst)
+ worst = dh;
+ if (dc > worst)
+ worst = dc;
+ if (!isfinite(h_simd[i]) || !isfinite(c_simd[i]))
+ worst = 1.0;
+ }
+ TEST_ASSERT(worst <= 1e-6, "lstm_cell over +/-%.0f: worst %.3e", span, worst);
+ }
+}
+
+int main(void) {
+ const pyseg_kernels *sets[] = {pyseg_kernels_scalar(), pyseg_kernels_simd()};
+
+ /* The shapes the forward pass issues, in order. */
+ static const struct {
+ const char *label;
+ int m, n, k, lda, bias;
+ } model_shapes[] = {
+ {"sinc", 384, 80, 256, 10, 0}, {"sinc-tail", 231, 80, 256, 10, 0},
+ {"conv2", 384, 60, 400, 80, 1}, {"conv3", 231, 60, 300, 60, 1},
+ {"lstm-in-l0", 589, 512, 60, 60, 1}, {"lstm-in-l1", 589, 512, 256, 256, 1},
+ {"lstm-hh", 1, 512, 128, 0, 1}, {"linear0", 589, 128, 256, 256, 1},
+ {"linear1", 589, 128, 128, 128, 1}, {"classifier", 589, 7, 128, 128, 1},
+ };
+
+ for (size_t s = 0; s < sizeof sets / sizeof *sets; ++s)
+ for (size_t i = 0; i < sizeof model_shapes / sizeof *model_shapes; ++i)
+ check(model_shapes[i].label, sets[s], model_shapes[i].m, model_shapes[i].n,
+ model_shapes[i].k, model_shapes[i].lda ? model_shapes[i].lda : model_shapes[i].k,
+ model_shapes[i].bias);
+
+ /* Every combination of leftover rows and leftover weight rows. */
+ for (size_t s = 0; s < sizeof sets / sizeof *sets; ++s)
+ for (int m = 1; m <= 9; ++m)
+ for (int n = 1; n <= 13; ++n)
+ for (int k = 4; k <= 20; k += 4)
+ check("edge", sets[s], m, n, k, k + 3, m & 1);
+
+ /* Panels: a shape larger than one panel and one that straddles a boundary. */
+ for (size_t s = 0; s < sizeof sets / sizeof *sets; ++s) {
+ check("panel-exact", sets[s], 2 * PYSEG_MPANEL, 8, 16, 16, 1);
+ check("panel-split", sets[s], 2 * PYSEG_MPANEL + 3, 9, 12, 17, 1);
+ }
+
+ check_lstm_cell();
+ return TEST_REPORT();
+}
diff --git a/tests/test_parity.c b/tests/test_parity.c
new file mode 100644
index 0000000..5002185
--- /dev/null
+++ b/tests/test_parity.c
@@ -0,0 +1,214 @@
+/* The engine against the PyTorch model on real speech.
+ *
+ * Two thresholds, because they answer different questions. The probability
+ * difference says the arithmetic agrees; the decision agreement says nothing
+ * downstream changes, which is what actually matters, since every consumer of
+ * this model takes the most likely class per frame and throws the rest away.
+ *
+ * Bit-exactness is not a goal and is not achievable: the reference sums in a
+ * different order, and float32 addition is not associative. */
+
+#include "pyseg.h"
+#include "test_util.h"
+
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define PROB_TOLERANCE 2e-3
+
+typedef struct {
+ unsigned windows;
+ float *inputs; /* [windows][PYSEG_WINDOW_SAMPLES] */
+ float *probs; /* [windows][PYSEG_FRAMES][PYSEG_CLASSES] */
+} fixture;
+
+static int load(const char *path, fixture *f) {
+ FILE *fp = fopen(path, "rb");
+ if (!fp) {
+ fprintf(stderr, "cannot open %s\n", path);
+ return -1;
+ }
+ char magic[4];
+ unsigned n, samples, frames;
+ if (fread(magic, 1, 4, fp) != 4 || memcmp(magic, "PSGG", 4) != 0 || fread(&n, 4, 1, fp) != 1 ||
+ fread(&samples, 4, 1, fp) != 1 || fread(&frames, 4, 1, fp) != 1 ||
+ samples != PYSEG_WINDOW_SAMPLES || frames != PYSEG_FRAMES) {
+ fprintf(stderr, "%s: not a matching fixture\n", path);
+ fclose(fp);
+ return -1;
+ }
+ const size_t in_len = (size_t)n * PYSEG_WINDOW_SAMPLES;
+ const size_t out_len = (size_t)n * PYSEG_FRAMES * PYSEG_CLASSES;
+ f->windows = n;
+ f->inputs = malloc(in_len * sizeof(float));
+ f->probs = malloc(out_len * sizeof(float));
+ const int ok = f->inputs && f->probs && fread(f->inputs, sizeof(float), in_len, fp) == in_len &&
+ fread(f->probs, sizeof(float), out_len, fp) == out_len;
+ fclose(fp);
+ if (!ok) {
+ fprintf(stderr, "%s: truncated\n", path);
+ return -1;
+ }
+ return 0;
+}
+
+static int argmax(const float *row) {
+ int best = 0;
+ for (int i = 1; i < PYSEG_CLASSES; ++i)
+ if (row[i] > row[best])
+ best = i;
+ return best;
+}
+
+static int run(const fixture *f, unsigned flags, const char *label, float *first_window_out) {
+ pyseg_t *v = pyseg_create_ex(NULL, flags);
+ TEST_ASSERT(v != NULL, "%s: create failed", label);
+ if (!v)
+ return -1;
+
+ float *probs = malloc((size_t)PYSEG_FRAMES * PYSEG_CLASSES * sizeof(float));
+ double worst_prob = 0.0, worst_sum = 0.0;
+ size_t disagreements = 0, total = 0;
+
+ for (unsigned c = 0; c < f->windows; ++c) {
+ pyseg_process_f32(v, f->inputs + (size_t)c * PYSEG_WINDOW_SAMPLES, probs);
+ if (c == 0 && first_window_out)
+ memcpy(first_window_out, probs, (size_t)PYSEG_FRAMES * PYSEG_CLASSES * sizeof(float));
+
+ const float *want = f->probs + (size_t)c * PYSEG_FRAMES * PYSEG_CLASSES;
+ for (int t = 0; t < PYSEG_FRAMES; ++t) {
+ const float *g = probs + (size_t)t * PYSEG_CLASSES;
+ const float *e = want + (size_t)t * PYSEG_CLASSES;
+ double sum = 0.0;
+ for (int i = 0; i < PYSEG_CLASSES; ++i) {
+ const double d = fabs((double)g[i] - e[i]);
+ if (d > worst_prob)
+ worst_prob = d;
+ sum += g[i];
+ }
+ if (fabs(sum - 1.0) > worst_sum)
+ worst_sum = fabs(sum - 1.0);
+ if (argmax(g) != argmax(e))
+ ++disagreements;
+ ++total;
+ }
+ }
+
+ fprintf(stderr, "%-8s %-6s %u windows: max |prob diff| %.3e, %zu/%zu frames disagree\n", label,
+ pyseg_backend_name(v), f->windows, worst_prob, disagreements, total);
+
+ TEST_ASSERT(worst_prob <= PROB_TOLERANCE, "%s: max |prob diff| %.3e > %.3e", label, worst_prob,
+ PROB_TOLERANCE);
+ TEST_ASSERT(worst_sum <= 1e-5, "%s: probabilities sum to 1 +/- %.3e", label, worst_sum);
+ TEST_ASSERT(disagreements == 0, "%s: %zu of %zu frames pick a different class", label,
+ disagreements, total);
+
+ free(probs);
+ pyseg_destroy(v);
+ return 0;
+}
+
+int main(int argc, char **argv) {
+ if (argc < 2) {
+ fprintf(stderr, "usage: test_parity <golden.bin>\n");
+ return 2;
+ }
+ fixture f;
+ if (load(argv[1], &f) != 0)
+ return 2;
+
+ const size_t out_len = (size_t)PYSEG_FRAMES * PYSEG_CLASSES;
+ float *simd_first = malloc(out_len * sizeof(float));
+ float *scalar_first = malloc(out_len * sizeof(float));
+
+ run(&f, 0, "simd", simd_first);
+ run(&f, PYSEG_SCALAR_KERNELS, "scalar", scalar_first);
+
+ /* The two kernel sets accumulate in different orders, so they are held to
+ * each other more tightly than either is held to the reference. */
+ double worst = 0.0;
+ for (size_t i = 0; i < out_len; ++i) {
+ const double d = fabs((double)simd_first[i] - scalar_first[i]);
+ if (d > worst)
+ worst = d;
+ }
+ fprintf(stderr, "simd vs scalar: max |prob diff| %.3e\n", worst);
+ TEST_ASSERT(worst <= 1e-4, "simd vs scalar diverge by %.3e", worst);
+
+ /* Batching is a scheduling decision, not a numerical one: the windows of a
+ * batch share weight loads but never touch each other's activations, so
+ * grouping must not move a single bit. */
+ if (f.windows >= 4) {
+ pyseg_t *batched = pyseg_create_batch(NULL, 0, 4);
+ pyseg_t *single = pyseg_create();
+ TEST_ASSERT(batched && pyseg_batch(batched) == 4, "batched create failed");
+ if (batched && single) {
+ float *grouped = malloc(4 * out_len * sizeof(float));
+ float *apart = malloc(4 * out_len * sizeof(float));
+ pyseg_process_batch_f32(batched, f.inputs, 4, grouped);
+ for (int i = 0; i < 4; ++i)
+ pyseg_process_f32(single, f.inputs + (size_t)i * PYSEG_WINDOW_SAMPLES,
+ apart + (size_t)i * out_len);
+ TEST_ASSERT(memcmp(grouped, apart, 4 * out_len * sizeof(float)) == 0,
+ "a batch of four disagrees with four separate windows");
+
+ /* A partial batch is the tail of every recording. */
+ pyseg_process_batch_f32(batched, f.inputs, 3, grouped);
+ TEST_ASSERT(memcmp(grouped, apart, 3 * out_len * sizeof(float)) == 0,
+ "a partial batch disagrees with separate windows");
+ free(grouped);
+ free(apart);
+ }
+ pyseg_destroy(batched);
+ pyseg_destroy(single);
+ }
+
+ TEST_ASSERT(pyseg_create_batch(NULL, 0, 0) == NULL, "a batch of zero was accepted");
+ TEST_ASSERT(pyseg_create_batch(NULL, 0, PYSEG_MAX_BATCH + 1) == NULL,
+ "an oversized batch was accepted");
+
+ /* Determinism: the engine carries no state between windows, so re-running
+ * a window must reproduce it bit for bit. */
+ pyseg_t *v = pyseg_create();
+ float *again = malloc(out_len * sizeof(float));
+ if (v) {
+ pyseg_process_f32(v, f.inputs + (size_t)(f.windows - 1) * PYSEG_WINDOW_SAMPLES, again);
+ float *repeat = malloc(out_len * sizeof(float));
+ pyseg_process_f32(v, f.inputs, repeat);
+ pyseg_process_f32(v, f.inputs + (size_t)(f.windows - 1) * PYSEG_WINDOW_SAMPLES, repeat);
+ TEST_ASSERT(memcmp(again, repeat, out_len * sizeof(float)) == 0,
+ "a repeated window produced different bits");
+ free(repeat);
+ pyseg_destroy(v);
+ }
+
+ /* A weight file must parse to the same numbers as the embedded copy. */
+ pyseg_t *fromfile = pyseg_create_from_file("pyseg_weights.bin");
+ if (fromfile) {
+ float *filed = malloc(out_len * sizeof(float));
+ pyseg_process_f32(fromfile, f.inputs, filed);
+ pyseg_t *embedded = pyseg_create();
+ float *builtin = malloc(out_len * sizeof(float));
+ pyseg_process_f32(embedded, f.inputs, builtin);
+ TEST_ASSERT(memcmp(filed, builtin, out_len * sizeof(float)) == 0,
+ "mapped weights disagree with the embedded copy");
+ free(filed);
+ free(builtin);
+ pyseg_destroy(embedded);
+ pyseg_destroy(fromfile);
+ } else {
+ fprintf(stderr, "note: pyseg_weights.bin not readable, file path untested\n");
+ }
+
+ TEST_ASSERT(pyseg_create_from_file("/nonexistent/pyseg.bin") == NULL,
+ "a missing weight file was accepted");
+
+ free(again);
+ free(simd_first);
+ free(scalar_first);
+ free(f.inputs);
+ free(f.probs);
+ return TEST_REPORT();
+}
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();
+}
diff --git a/tests/test_slide.c b/tests/test_slide.c
new file mode 100644
index 0000000..a0fc8ca
--- /dev/null
+++ b/tests/test_slide.c
@@ -0,0 +1,212 @@
+/* The sliding-window driver against the aggregation pyannote performs, and the
+ * binarizer against its own definition.
+ *
+ * The aggregate is not just the model run repeatedly: window placement,
+ * rounding onto the frame grid, the Hamming weights and the dropped tail all
+ * have to match, and each is a place where an off-by-one produces output that
+ * looks entirely reasonable and is shifted by a frame. */
+
+#include "pyseg.h"
+#include "test_util.h"
+
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef struct {
+ unsigned num_samples;
+ unsigned frames;
+ double step;
+ float *samples;
+ float *tracks; /* [frames][PYSEG_TRACKS] */
+} fixture;
+
+static int load(const char *path, fixture *f) {
+ FILE *fp = fopen(path, "rb");
+ if (!fp) {
+ fprintf(stderr, "cannot open %s\n", path);
+ return -1;
+ }
+ char magic[4];
+ if (fread(magic, 1, 4, fp) != 4 || memcmp(magic, "PSGS", 4) != 0 ||
+ fread(&f->num_samples, 4, 1, fp) != 1 || fread(&f->frames, 4, 1, fp) != 1 ||
+ fread(&f->step, 8, 1, fp) != 1) {
+ fprintf(stderr, "%s: not a matching fixture\n", path);
+ fclose(fp);
+ return -1;
+ }
+ f->samples = malloc((size_t)f->num_samples * sizeof(float));
+ f->tracks = malloc((size_t)f->frames * PYSEG_TRACKS * sizeof(float));
+ const int ok = f->samples && f->tracks &&
+ fread(f->samples, sizeof(float), f->num_samples, fp) == f->num_samples &&
+ fread(f->tracks, sizeof(float), (size_t)f->frames * PYSEG_TRACKS, fp) ==
+ (size_t)f->frames * PYSEG_TRACKS;
+ fclose(fp);
+ if (!ok) {
+ fprintf(stderr, "%s: truncated\n", path);
+ return -1;
+ }
+ return 0;
+}
+
+static double compare(const float *got, const float *want, size_t n) {
+ double worst = 0.0;
+ for (size_t i = 0; i < n; ++i) {
+ const double d = fabs((double)got[i] - want[i]);
+ if (d > worst)
+ worst = d;
+ }
+ return worst;
+}
+
+static void check_slide(const fixture *f, int threads) {
+ pyseg_slide_params p;
+ pyseg_slide_params_default(&p);
+ p.step = f->step;
+ p.threads = threads;
+
+ const size_t frames = pyseg_slide_frames(f->num_samples, p.step);
+ TEST_ASSERT(frames == f->frames, "threads=%d: %zu frames, reference has %u", threads, frames,
+ f->frames);
+ if (frames != f->frames)
+ return;
+
+ float *tracks = malloc(frames * PYSEG_TRACKS * sizeof(float));
+ const size_t got = pyseg_slide(f->samples, f->num_samples, &p, tracks);
+ TEST_ASSERT(got == frames, "threads=%d: slide returned %zu", threads, got);
+
+ if (got == frames) {
+ const double worst = compare(tracks, f->tracks, frames * PYSEG_TRACKS);
+ fprintf(stderr, "slide threads=%d: %zu frames, max |track diff| %.3e\n", threads, frames,
+ worst);
+ TEST_ASSERT(worst <= 1e-5, "threads=%d: max |track diff| %.3e", threads, worst);
+ }
+ free(tracks);
+}
+
+/* A hand-built score sequence, so the expected segments can be reasoned about
+ * rather than captured. */
+static void check_binarize(void) {
+ enum { N = 40 };
+ float scores[N * PYSEG_TRACKS];
+ memset(scores, 0, sizeof scores);
+ for (int f = 10; f < 20; ++f)
+ scores[f * PYSEG_TRACKS] = 1.0f;
+ for (int f = 22; f < 25; ++f)
+ scores[f * PYSEG_TRACKS] = 1.0f;
+
+ pyseg_binarize_params p;
+ pyseg_binarize_params_default(&p);
+
+ pyseg_segment out[8];
+ size_t n = pyseg_binarize(scores, N, PYSEG_TRACK_SPEECH, &p, out, 8);
+ TEST_ASSERT(n == 2, "two runs of ones gave %zu segments", n);
+ if (n == 2) {
+ const double half = 0.5 * PYSEG_FRAME_SIZE / PYSEG_SAMPLE_RATE;
+ TEST_ASSERT(fabs(out[0].start - (pyseg_frame_time(10) + half)) < 1e-9,
+ "first segment starts at %.6f", out[0].start);
+ TEST_ASSERT(fabs(out[0].end - (pyseg_frame_time(20) + half)) < 1e-9,
+ "first segment ends at %.6f", out[0].end);
+ TEST_ASSERT(fabs(out[1].start - (pyseg_frame_time(22) + half)) < 1e-9,
+ "second segment starts at %.6f", out[1].start);
+ }
+
+ /* The gap between the two runs is two frames, about 34 ms. */
+ p.min_duration_off = 0.1;
+ n = pyseg_binarize(scores, N, PYSEG_TRACK_SPEECH, &p, out, 8);
+ TEST_ASSERT(n == 1, "a 34 ms gap survived a 100 ms fill: %zu segments", n);
+
+ p.min_duration_off = 0.0;
+ p.min_duration_on = 0.1;
+ n = pyseg_binarize(scores, N, PYSEG_TRACK_SPEECH, &p, out, 8);
+ TEST_ASSERT(n == 1, "a 51 ms segment survived a 100 ms minimum: %zu segments", n);
+
+ /* Counting without writing must agree with writing. */
+ pyseg_binarize_params_default(&p);
+ TEST_ASSERT(pyseg_binarize(scores, N, PYSEG_TRACK_SPEECH, &p, NULL, 0) == 2,
+ "counting pass disagrees with the writing pass");
+ TEST_ASSERT(pyseg_binarize(scores, N, PYSEG_TRACK_SPEECH, &p, out, 1) == 2,
+ "an undersized buffer did not report the full count");
+
+ /* A track that is high from the first frame to the last is one segment. */
+ for (int f = 0; f < N; ++f)
+ scores[f * PYSEG_TRACKS + PYSEG_TRACK_OVERLAP] = 1.0f;
+ n = pyseg_binarize(scores, N, PYSEG_TRACK_OVERLAP, &p, out, 8);
+ TEST_ASSERT(n == 1, "an always-active track gave %zu segments", n);
+
+ TEST_ASSERT(pyseg_binarize(scores, N, PYSEG_TRACKS, &p, out, 8) == 0,
+ "an out-of-range track was accepted");
+}
+
+/* Frame counts read off pyannote's Inference for the same inputs. The formula
+ * involves two roundings and a tail-window rule, so a table of answers taken
+ * from the reference is worth more than a second derivation of the formula. */
+static void check_frame_counts(void) {
+ static const struct {
+ size_t samples;
+ double step;
+ size_t frames;
+ } cases[] = {
+ {720000, 1.0, 2668}, {640000, 2.0, 2371}, {592000, 0.5, 2194},
+ {400000, 3.0, 1482}, {144000, 1.0, 534}, {160000, 1.0, 594},
+ };
+ for (size_t i = 0; i < sizeof cases / sizeof *cases; ++i) {
+ const size_t got = pyseg_slide_frames(cases[i].samples, cases[i].step);
+ TEST_ASSERT(got == cases[i].frames, "%zu samples at step %g: %zu frames, expected %zu",
+ cases[i].samples, cases[i].step, got, cases[i].frames);
+ }
+ TEST_ASSERT(pyseg_slide_frames(0, 1.0) == 0, "an empty recording gave frames");
+ TEST_ASSERT(pyseg_slide_frames(160000, 0.0) == 0, "a zero step gave frames");
+}
+
+int main(int argc, char **argv) {
+ if (argc < 2) {
+ fprintf(stderr, "usage: test_slide <slide_golden.bin>\n");
+ return 2;
+ }
+ fixture f;
+ if (load(argv[1], &f) != 0)
+ return 2;
+
+ check_frame_counts();
+
+ check_slide(&f, 1);
+ check_slide(&f, 4);
+ check_binarize();
+
+ /* The 16-bit entry point must agree with the float one on audio that came
+ * from 16-bit samples in the first place, which the fixture did. */
+ {
+ int16_t *pcm = malloc(f.num_samples * sizeof(int16_t));
+ float *from_s16 = malloc((size_t)f.frames * PYSEG_TRACKS * sizeof(float));
+ for (unsigned i = 0; i < f.num_samples; ++i)
+ pcm[i] = (int16_t)lrintf(f.samples[i] * 32768.0f);
+ pyseg_slide_params p;
+ pyseg_slide_params_default(&p);
+ p.step = f.step;
+ TEST_ASSERT(pyseg_slide_s16(pcm, f.num_samples, &p, from_s16) == f.frames,
+ "the 16-bit slide failed");
+ const double worst = compare(from_s16, f.tracks, (size_t)f.frames * PYSEG_TRACKS);
+ TEST_ASSERT(worst <= 1e-5, "16-bit slide differs by %.3e", worst);
+ free(from_s16);
+ free(pcm);
+ }
+
+ /* Recordings shorter than one window are padded, not rejected. */
+ float *tiny = calloc(PYSEG_SAMPLE_RATE, sizeof(float));
+ for (int i = 0; i < PYSEG_SAMPLE_RATE; ++i)
+ tiny[i] = 0.1f * sinf((float)i * 0.01f);
+ const size_t tiny_frames = pyseg_slide_frames(PYSEG_SAMPLE_RATE, 1.0);
+ TEST_ASSERT(tiny_frames == PYSEG_SAMPLE_RATE / PYSEG_FRAME_STEP + 1,
+ "a one-second recording gave %zu frames", tiny_frames);
+ float *tiny_tracks = malloc(tiny_frames * PYSEG_TRACKS * sizeof(float));
+ TEST_ASSERT(pyseg_slide(tiny, PYSEG_SAMPLE_RATE, NULL, tiny_tracks) == tiny_frames,
+ "a one-second recording failed");
+ free(tiny_tracks);
+ free(tiny);
+
+ free(f.samples);
+ free(f.tracks);
+ return TEST_REPORT();
+}
diff --git a/tests/test_util.h b/tests/test_util.h
new file mode 100644
index 0000000..1d8c80e
--- /dev/null
+++ b/tests/test_util.h
@@ -0,0 +1,29 @@
+/* Assertion harness for the C tests: every check reports, nothing aborts, and
+ * the process exit status is the verdict. Keeping going after a failure is
+ * deliberate; when a numeric change breaks several gates at once, the full
+ * list localises it faster than the first failure alone. */
+
+#ifndef PYSEG_TEST_UTIL_H
+#define PYSEG_TEST_UTIL_H
+
+#include <stdio.h>
+
+static int test_failures;
+static int test_checks;
+
+#define TEST_ASSERT(cond, ...) \
+ do { \
+ ++test_checks; \
+ if (!(cond)) { \
+ ++test_failures; \
+ fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); \
+ fprintf(stderr, __VA_ARGS__); \
+ fputc('\n', stderr); \
+ } \
+ } while (0)
+
+#define TEST_REPORT() \
+ (fprintf(stderr, "%s: %d checks, %d failed\n", __FILE__, test_checks, test_failures), \
+ test_failures ? 1 : 0)
+
+#endif /* PYSEG_TEST_UTIL_H */