Differential tests

Every optimisation in this book is only allowed to be faster — never to change the answer. Each suite below runs a deliberately slow, obviously-correct implementation alongside the optimised one and compares them. This is chapter 42, executing in your browser rather than being described.

A golden corpus and a slow oracle

These are not recorded results. Press run and both implementations execute, right here, and get compared.

Golden corpus
D1 = "kubernetes deployment"
D2 = "docker deployment"
D3 = "kubernetes service"

kubernetes             → D1, D3
deployment             → D1, D2
kubernetes AND service → D3
"kubernetes deployment"→ D1

Small enough to verify by eye. That is the requirement — a golden corpus you cannot check by hand is just another implementation.

Differential testing
same query
    ├─→ reference engine  → expected
    └─→ optimised engine  → actual

           equal?
        yes → correct
        no  → investigate

The reference is allowed to be slow. It is allowed to be embarrassing. It is not allowed to be deleted, because it is the only thing that can tell you the fast path is still right.

Nothing has run yet. Press run the suite.

What differential testing is especially good at

areareferenceoptimised
fuzzy searchbrute-force edit distance over every termLevenshtein automaton + trie/FST
postings compressionthe in-memory objectsgap + vint + bit-packed bytes
term dictionarya sorted arraytrie → minimized automaton → FST
range queriescheck every valuebinary search / BKD
top-Kscore every candidateWAND with upper-bound pruning
distributionone indexshards, replicas and a global merge