PeaBox Lab › PeaLang › Learn › Chapter 10 › Lesson 4

Part II — Intermediate Development · Chapter 10 · Lesson 4 of 6 · 9 min read

Lesson 4: Interpreter/VM Differential Testing, Fuzzing, and Conformance

Lesson level: Advanced intermediate

Purpose: Verify that PeaLang programs behave identically on both execution engines, probe code with generated and hostile inputs, and check behavior against the written language specification.

Learning objectives

By the end of this lesson, you should be able to:

Release note: command names, options, and output formats in this lesson are illustrative. Verify them against the installed v0.63 release and its built-in help before relying on them.

4.1 Two engines, one language

PeaLang programs can run on a tree-walking interpreter or on a portable stack-based virtual machine that executes PEAC bytecode. The language promises the same observable behavior from both. A program that prints one result on one engine and a different result on the other has found a defect in an engine, in the compiler, or in the test.

Because the host chooses an engine, disagreement is a real production risk: a rule validated on one engine could behave differently when deployed on the other.

4.2 What “same behavior” means

Define the comparison explicitly before running it. For deterministic programs the observable behavior includes:

Some details are not required to match, such as internal timing, memory use, or the exact wording of an implementation-specific diagnostic. List these exclusions in the test configuration rather than discovering them by accident.

4.3 A differential run

A differential test runs the same source on both engines with the same inputs and limits, then compares the observable results.

pea run rules.pea
pea compile rules.pea -o rules.peac
pea vm-run rules.peac

Capture output and exit status from each run into separate files and compare them. Use fixed inputs and a fixed seed so the only difference between runs is the engine.

4.4 Classifying a difference

When outputs differ, do not assume the VM is wrong. Classify the difference first.

Only the first two are defects to fix. The last two are test-design work: tighten the comparison or remove the variation.

4.5 What to compare in the order-discount project

Run every case from the Chapter 9 suite, A through K, on both engines. Compare the returned map, the reason code, and the error category for the invalid cases. Include precedence cases such as a gold customer at the bulk threshold, since evaluation order is where engines most often diverge.

case A  standard qty 9    interpreter: NONE        vm: NONE
case C  gold     qty 10   interpreter: GOLD_BULK   vm: GOLD_BULK
case J  gold     qty 0    interpreter: VALIDATION_ERROR   vm: VALIDATION_ERROR

The data above is synthetic. A real run records every case and flags any mismatch.

4.6 Areas where engines tend to diverge

Use this list to choose focused programs. A small program that exercises one of these areas is more useful than a large program that exercises all of them.

4.7 What fuzzing is

Fuzzing feeds a program large numbers of generated inputs to find failures that hand-written cases miss. For a rules module, the goal is to confirm that no input causes a crash, a hang, an inconsistent result, or a violated invariant. It complements boundary testing: boundaries test the cases you thought of, and fuzzing tests the ones you did not.

4.8 Generating inputs safely

pea fuzz rules.pea --entry decide --seed 7 --cases 500 --max-time 60

4.9 Properties to check

A fuzzing campaign needs a pass/fail rule for every generated input. Express it as a property that must always hold.

PropertyExample for the discount rules
Never crashesAny input returns a decision or a documented VALIDATION_ERROR, never an unhandled error
Result is completeEvery result contains a reason code and a discount amount
Invariant holdsThe discount is never negative and never exceeds the order subtotal
DeterministicThe same input returns the same result on repeated calls
Engines agreeInterpreter and VM return the same result for the same input

The last property combines fuzzing with differential testing: generate inputs, run both engines, and report the first disagreement.

4.10 From failure to regression test

test "missing tier field returns VALIDATION_ERROR (fuzz seed 7, case 212)" {
    let result = rules.decide({"quantity": 10})
    assert result["reason"] == "VALIDATION_ERROR"
}

4.11 Limits of fuzzing

4.12 Conformance testing

A conformance suite checks the implementation against the written specification, independent of any one program. Each test states the specified behavior, the input, and the expected result, and runs on every engine and host.

The supplied v0.63 materials describe a specification-verification command and conformance gates. Use them as the starting point, and extend with project-specific cases rather than replacing them.

4.13 Conformance versus project tests

AspectProject testConformance test
Question answeredDoes my rule do what the business wants?Does the engine do what the language specifies?
OwnerApplication teamLanguage and tooling maintainers
InputDomain dataMinimal language constructs
Failure meansPolicy or rule defectEngine, compiler, or specification defect
Runs onThe deployed engineEvery engine and host

4.14 Common mistakes

MistakeConsequenceCorrection
Comparing printed text that includes timing or addressesFalse differencesDefine and normalize the compared fields
Running the two engines with different limitsSpurious divergenceUse identical limits and capabilities
Unseeded fuzzingFailures cannot be reproducedRecord the seed with every failure
A fuzz property that only checks for crashesWrong results passAdd result-shape and invariant properties
Assuming the VM is the referenceReal interpreter defects ignoredCompare both against the specification
Fixing a bug without a regression testThe bug returnsAdd the minimal input as a named test

4.15 Diagnostic workflow

4.16 Guided practice

4.17 Hands-on lab: Differential and fuzz testing of the order-discount rules

Goal: Show that the Chapter 9 rules behave identically on both engines and survive a bounded fuzzing campaign.

Deliverable: The comparison table, the property definitions, the seed and campaign settings, the shrunk failing input and its regression test, and a short note on what the campaign cannot prove.

4.18 Review questions

4.19 Applied challenge

Design a verification plan for a new rules module that will be deployed on both engines. Specify the differential comparison and its exclusions, three fuzz properties, the campaign bounds and seed policy, the conformance cases you would add, and the rule for deciding which difference blocks a release.

Lesson summary

PeaLang promises the same behavior on its interpreter and VM, so differential testing compares them on identical inputs and limits and classifies every difference. Seeded, bounded, structure-aware fuzzing finds failures that hand-written cases miss, and every failure becomes a minimal regression test. Conformance tests tie behavior to the specification and run on every engine and host. None of these proves correctness, so they supplement, rather than replace, project tests.

Transition to Lesson 5

The next lesson covers performance gates and regression prevention: measuring speed and resource use repeatably, setting thresholds, and stopping slowdowns from reaching a release.

Lesson status: Ready for review.

← Previous Next →