Skip to content
pi-c Artifact [ccb65eb532]
Login

Artifact [ccb65eb532]

Artifact ccb65eb532c6795ed172387862c1e165a090bc97218d97d91ad0ec1f8d124db4:


#include <assert.h>
#include <stdio.h>
#include <string.h>

#include "qual.h"

static q_ctx *g;

static size_t run(q_op *ops, size_t n, q_trace *t) {
    q_reset(g, 1);
    q_run_ops(g, ops, n, t);
    return t->count;
}

static void test_dedup_same_key(void) {
    q_op ops[Q_MAX_OPS];
    q_trace t;
    size_t nrec, neff, n;
    const q_record *recs;
    const q_effect *effs;

    n = 0;
    ops[n++] = (q_op){Q_OP_SUBMIT, 7, 1, 0};
    ops[n++] = (q_op){Q_OP_COMMIT, 7, 1, 0};
    ops[n++] = (q_op){Q_OP_SUBMIT, 7, 1, 0};
    ops[n++] = (q_op){Q_OP_COMMIT, 7, 1, 0};
    (void)run(ops, n, &t);

    recs = q_records(g, &nrec);
    effs = q_effects(g, &neff);
    assert(nrec == 1);
    assert(recs[0].request_id == 7);
    assert(recs[0].outcome == Q_STORED);
    assert(neff == 0);
    /* exactly one admission, one dedup, never a second admit */
    {
        size_t sub = 0, ded = 0;
        for (size_t i = 0; i < t.count; i++) {
            if (t.ev[i].kind == Q_EV_SUBMIT) sub++;
            if (t.ev[i].kind == Q_EV_DEDUP) ded++;
        }
        assert(sub == 1);
        assert(ded == 1);
    }
    assert(q_oracle_check(ops, n, &t, recs, nrec, effs, neff, NULL, 0) == Q_OK);
}

/* An admission without a storage receipt may disappear across either boundary. */
static void test_uncommitted_admission_can_be_retried(void) {
    const uint8_t boundaries[] = {Q_OP_CRASH_BEFORE_COMMIT, Q_OP_REOPEN};
    for (size_t i = 0; i < sizeof boundaries / sizeof boundaries[0]; i++) {
        q_op ops[] = {
            {Q_OP_SUBMIT, 1, 1, 0},
            {boundaries[i], 1, 1, 0},
            {Q_OP_SUBMIT, 1, 1, 0},
            {Q_OP_COMMIT, 1, 1, 0}
        };
        q_trace t;
        size_t nr, ne;
        const q_record *r;
        const q_effect *e;
        (void)run(ops, sizeof ops / sizeof ops[0], &t);
        r = q_records(g, &nr);
        e = q_effects(g, &ne);
        assert(nr == 1);
        assert(r[0].outcome == Q_STORED);
        assert(ne == 0);
        assert(q_oracle_check(ops, sizeof ops / sizeof ops[0], &t,
                              r, nr, e, ne, NULL, 0) == Q_OK);
    }
}

static void test_dedup_type_conflict_refused(void) {
    q_op ops[Q_MAX_OPS];
    q_trace t;
    size_t n = 0;
    ops[n++] = (q_op){Q_OP_SUBMIT, 3, 1, 0};
    ops[n++] = (q_op){Q_OP_SUBMIT, 3, 2, 0}; /* different type, same key */
    (void)run(ops, n, &t);
    {
        size_t nrec, neff, refused = 0;
        const q_record *r = q_records(g, &nrec);
        const q_effect *e = q_effects(g, &neff);
        for (size_t i = 0; i < t.count; i++)
            if (t.ev[i].kind == Q_EV_REFUSED) refused++;
        assert(refused == 1);
        assert(nrec == 1 && r[0].kind == 1);
        assert(neff == 0);
        assert(q_oracle_check(ops, n, &t, r, nrec, e, neff, NULL, 0) == Q_OK);
    }
}

static void test_effect_before_receipt_refused(void) {
    q_op ops[Q_MAX_OPS];
    q_trace t;
    size_t n = 0, neff, nrec;
    const q_effect *effs;
    const q_record *recs;
    size_t starts = 0;
    ops[n++] = (q_op){Q_OP_SUBMIT, 9, 1, 0};
    ops[n++] = (q_op){Q_OP_EXEC_EFFECT, 9, 1, 0}; /* no COMMIT yet */
    (void)run(ops, n, &t);
    effs = q_effects(g, &neff);
    recs = q_records(g, &nrec);
    for (size_t i = 0; i < t.count; i++)
        if (t.ev[i].kind == Q_EV_EFFECT_START) starts++;
    /* Correct fixture never starts the effect; the bad transition that would
     * start it is rejected by the oracle via the mutant matrix. */
    assert(neff == 0);
    assert(starts == 0);
    assert(nrec == 1 && recs[0].outcome == Q_UNKNOWN);
    assert(q_oracle_check(ops, n, &t, recs, nrec, effs, neff, NULL, 0) == Q_OK);
}

static void test_interrupted_unsafe_effect_not_repeated(void) {
    q_op ops[Q_MAX_OPS];
    q_trace t;
    size_t n = 0, neff, nrec;
    const q_effect *effs;
    const q_record *recs;
    ops[n++] = (q_op){Q_OP_SUBMIT, 4, 1, 0};
    ops[n++] = (q_op){Q_OP_COMMIT, 4, 1, 0};
    ops[n++] = (q_op){Q_OP_EXEC_EFFECT, 4, 1, 0}; /* safe = arg 0 */
    ops[n++] = (q_op){Q_OP_CRASH_BEFORE_COMMIT, 4, 1, 0};
    ops[n++] = (q_op){Q_OP_REOPEN, 0, 0, 0};
    ops[n++] = (q_op){Q_OP_RESOLVE, 4, 1, 0};
    (void)run(ops, n, &t);
    effs = q_effects(g, &neff);
    recs = q_records(g, &nrec);
    assert(neff == 1);
    assert(effs[0].attempts == 1);
    assert(effs[0].status == 2); /* unknown, never done */
    assert(q_oracle_check(ops, n, &t, recs, nrec, effs, neff, NULL, 0) == Q_OK);
}

static void test_replay_safe_effect_reexecutes_once(void) {
    q_op ops[Q_MAX_OPS];
    q_trace t;
    size_t n = 0, neff, nrec;
    const q_effect *effs;
    const q_record *recs;
    ops[n++] = (q_op){Q_OP_SUBMIT, 5, 1, 0};
    ops[n++] = (q_op){Q_OP_COMMIT, 5, 1, 0};
    ops[n++] = (q_op){Q_OP_EXEC_EFFECT, 5, 1, 1}; /* safe */
    ops[n++] = (q_op){Q_OP_CRASH_BEFORE_COMMIT, 5, 1, 0};
    ops[n++] = (q_op){Q_OP_REOPEN, 0, 0, 0};
    ops[n++] = (q_op){Q_OP_RESOLVE, 5, 1, 0};
    (void)run(ops, n, &t);
    effs = q_effects(g, &neff);
    recs = q_records(g, &nrec);
    assert(neff == 1 && effs[0].attempts == 2 && effs[0].status == 3);
    assert(q_oracle_check(ops, n, &t, recs, nrec, effs, neff, NULL, 0) == Q_OK);
}

static void test_deterministic_seeded_traces(void) {
    static q_op ops[Q_MAX_OPS];
    q_trace a, b;
    size_t n = 0, na, nb;

    q_seeded_ops(42, ops, Q_MAX_OPS, &n);
    assert(n > 0 && n <= Q_MAX_OPS);
    q_reset(g, 42);
    q_run_ops(g, ops, n, &a);
    na = a.count;
    q_reset(g, 42);
    q_run_ops(g, ops, n, &b);
    nb = b.count;
    assert(na == nb);
    assert(na > 0);
    assert(memcmp(a.ev, b.ev, na * sizeof a.ev[0]) == 0);
}

static void test_bounded_seeds_oracle_and_uniqueness(void) {
    static q_op ops[Q_MAX_OPS];
    int seeds_checked = 0;
    for (uint64_t seed = 1; seed <= 100; seed++) {
        q_trace t;
        size_t n = 0, nrec, neff;
        const q_record *r;
        const q_effect *e;
        q_seeded_ops(seed, ops, Q_MAX_OPS, &n);
        assert(n <= Q_MAX_OPS);
        q_reset(g, seed);
        q_run_ops(g, ops, n, &t);
        assert(t.count <= Q_MAX_TRACE);
        r = q_records(g, &nrec);
        e = q_effects(g, &neff);
        assert(nrec <= Q_MAX_RECORDS && neff <= Q_MAX_EFFECTS);
        for (size_t i = 0; i < nrec; i++)
            for (size_t j = i + 1; j < nrec; j++)
                assert(r[i].request_id != r[j].request_id);
        {
            char d[128];
            q_verdict v = q_oracle_check(ops, n, &t, r, nrec, e, neff, d, sizeof d);
            if (v != Q_OK) {
                fprintf(stderr, "seed %llu: %s (%s)\n",
                        (unsigned long long)seed, q_verdict_name(v), d);
                assert(v == Q_OK);
            }
        }
        seeds_checked++;
    }
    assert(seeds_checked == 100);
}

static void test_seeded_chunks_deterministic(void) {
    q_chunk a[Q_MAX_CHUNKS], b[Q_MAX_CHUNKS];
    size_t na = 0, nb = 0;
    q_seeded_chunks(9, a, Q_MAX_CHUNKS, &na);
    q_seeded_chunks(9, b, Q_MAX_CHUNKS, &nb);
    assert(na == nb && na > 0 && na <= Q_MAX_CHUNKS);
    assert(memcmp(a, b, na * sizeof a[0]) == 0);
}

static void test_clock_advances_only_on_ops(void) {
    q_op ops[Q_MAX_OPS];
    q_trace t;
    size_t n = 0;
    uint64_t before, after;
    ops[n++] = (q_op){Q_OP_SUBMIT, 1, 1, 0};
    ops[n++] = (q_op){Q_OP_COMMIT, 1, 1, 0};
    q_reset(g, 3);
    before = q_clock_ns(g);
    q_run_ops(g, ops, n, &t);
    after = q_clock_ns(g);
    assert(before == 0);
    assert(after > before);
}

int main(void) {
    g = q_new(1);
    assert(g != NULL);
    test_dedup_same_key();
    test_uncommitted_admission_can_be_retried();
    test_dedup_type_conflict_refused();
    test_effect_before_receipt_refused();
    test_interrupted_unsafe_effect_not_repeated();
    test_replay_safe_effect_reexecutes_once();
    test_deterministic_seeded_traces();
    test_bounded_seeds_oracle_and_uniqueness();
    test_seeded_chunks_deterministic();
    test_clock_advances_only_on_ops();
    q_free(g);
    printf("qualification: 10 tests passed\n");
    return 0;
}