What this is about
How do you know that a new program is right? One honest way is to find an old program that is already trusted, give both the same inputs, and check that the answers match. The trusted program is called the oracle. The test is called an oracle test.
You do not need a special tool to start. You need two programs, a list of inputs and a comparison. This article shows a small example and then explains how we use the idea in all our Mojo libraries.
Why it matters
When you copy something that already exists, there is no new answer to look up. The answer already lives in the old tool. If your copy gives a different answer on any input, it is wrong, and you find that out the same hour.
This is a cheap way to catch mistakes in code that deals with numbers, text formats and rounding. People make small errors there, and the errors are hard to see by eye.
How to do it, step by step
Our example hashes the text hello world with sha256, once in Python and once in Mojo with our mojo-hashlib library.
1. Pick the oracle. Python's hashlib is trusted by millions of programs.
import hashlib
print(hashlib.sha256(b"hello world").hexdigest())
2. Write your version.
from hashlib import Sha256
def main() raises:
var h = Sha256()
h.update("hello world")
print(h.hexdigest())
3. Run both on the same input and compare. On our machine both printed:
b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
4. Make it a script. Run both programs, save each output to a file, and compare the files with diff. If diff prints nothing, the outputs match. Put the script in your repository, so it runs every time you change the code.
5. Use many inputs. One input proves little. Our hashlib check uses 15 inputs from 0 to 100,000 bytes, including sizes 55, 56, 63, 64 and 65 bytes. Those sit at the edges of the hash's block size, where bugs like to hide. It also runs 17 quoted words, 5 joins and 9 splits for the shell-quoting half of the library.
How big libraries do it
Every library in mojo-libs has a check script like this. Here are three real results from their READMEs.
mojo-semverwas compared with Python semver 3.0.4 on 474 parse results, a 60 by 60 table of version comparisons and 880 more, plus every bump. All came out byte for byte the same.mojo-xxhashwas compared with the reference xxHash for every length from 0 to 300, with four seeds, and for streaming in nine chunk sizes.mojo-jsonaccepted 95 of 95 "must accept" files from the JSONTestSuite and rejected 188 of 188 "must reject" files.
Plant a bug, and watch the test fail
A test that cannot fail is not a test. We learned this the hard way. In one library, the Python oracle failed to start on the nightly toolchain. The check counted zero test cases and still printed PASS, and every planted bug survived. In another case, a before-and-after check of a fix ran the same old program twice, because the build step skipped a file that already existed. The checks stayed green for a reason that had nothing to do with quality. Now a check that counts zero cases fails.
So we add a second step. After the oracle test passes, we plant a deliberate mistake in the code, such as flipping one operator, and run the test again. It must fail. We call each planted mistake a mutant. If the test still passes, it does not look at that part of the code, and we need more inputs.
Our mojo-port tool automates both halves. mojo-port verify runs every case through the Python original and the Mojo port and compares output and exit status. mojo-port mutants flips one operator per line of your port and fails if no case notices. On the demo word counter, it reported 8 of 8 mutants killed.
What an oracle test cannot prove
- It covers only your inputs. The mojo-port README says parity covers the paths your cases take. Inputs you did not try can still disagree.
- The oracle can be wrong. The oracle is a program too. We pin its version. Our argparse copy is checked against Python 3.14's argparse, and the README warns that another Python version may word a message differently.
- Some things are out of scope. mojo-port compares output and exit status, not error text or timing. Each library lists its limits in its README.
A ten-line script you can use today
You can do the whole thing in a shell. Say oracle.py is the trusted Python program and mine is your Mojo program, and inputs.txt holds one input per line.
while read -r line; do
expected=$(python3 oracle.py "$line")
actual=$(./mine "$line")
[ "$expected" = "$actual" ] || { echo "FAIL: $line"; exit 1; }
done < inputs.txt
echo PASS
This runs both programs on every line and stops at the first mismatch, naming the input. It is small, and it already catches most slips. Add the edge cases first: an empty line, a very long line, a line with strange characters.
Words you may not know
- Oracle: the trusted program whose answers you copy.
- Mutant: a copy of your code with one deliberate mistake planted in it.
- Parity: giving the same output as the original on the same input.
- Hash: a short fingerprint made from data. We cover it in a later article.
- Edge case: an input at the border of what the code expects, such as empty or very large.
Try it
- Run the Python and Mojo sha256 example above and compare with
diff. - Read how mojo-libs describes its checks for the library you care about.
- Install mojo-port and run its demo,
MOJO_PORT_ROOT=examples .bin/mojo-port verify wcl. The next article does this step by step.
Read next
- A gate you have never seen fail is not a gate yet tells the story of a check that had never failed, and what a planted bug showed.

Commenti
Ancora nessun commento.
Accedi per commentare.