Test an optimized or concise implementation against a simple reference. The reference is an oracle: an independent way to determine the expected result. Keep it structurally different enough that the two versions are unlikely to share the same accidental omission.
Understand the contract
The task is a small report: select nonnegative integers at or above a threshold and sum them. The pipeline version uses filter and mapToInt; the reference uses an explicit loop and if. Generated values are small so arithmetic overflow is outside the test domain. Do not reuse the same helper predicate in both versions if the predicate itself is what you are trying to check.
The example shows both versions on one known input. The second demonstrates a named boundary that distinguishes >= from >. The independent exercise combines explicit expectations with reproducible generated cases. A failing case should include its input so you can turn it into a small permanent regression test.
This approach is especially useful after refactoring a report. You can change implementation shape while keeping the observable contract. Once confidence is established, avoid leaving unnecessary production duplicates; the simple reference belongs in the tests.
Before running, write the output you expect and explain which rule produces each line. Each editor is an independent program.
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> values = List.of(10, 25, 40);
System.out.println(pipeline(values, 25));
System.out.println(reference(values, 25));
}
static int pipeline(List<Integer> values, int minimum) {
return values.stream()
.filter(value -> value >= minimum)
.mapToInt(value -> value)
.sum();
}
static int reference(List<Integer> values, int minimum) {
int total = 0;
for (int value : values) {
if (value >= minimum) {
total += value;
}
}
return total;
}
}Check your prediction
65
65Trace a different case
The input [25] with threshold 25 is more useful for the inclusive-boundary rule than a large list with no exact match. An independent expected number still matters even if both implementations agree. Two versions can agree on the same wrong requirement.
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> values = List.of(25);
int correct = values.stream().filter(value -> value >= 25).mapToInt(value -> value).sum();
int wrong = values.stream().filter(value -> value > 25).mapToInt(value -> value).sum();
System.out.println(correct);
System.out.println(wrong);
}
}Check your prediction
25
0Plan your acceptance cases
Before writing the complete implementation, predict these cases without running the solution. Keep the requirements above as the authority; this table highlights distinct promises rather than prescribing a particular class layout.
| Case | Expected behavior | Skill it checks |
|---|---|---|
| 25 at minimum 25 | 25 | Inclusive boundary |
| Empty source | Zero | Identity |
| Seeded generated lists | Pipeline equals independent loop; source preserved | Oracle and ownership |
After your first version works, choose one row and introduce a plausible defect that it should expose. Restore the correct version, then recreate the central operation without the worked example. If one skill is missing, revisit that lesson before combining more features.
Build it yourself
Checkpoint: Report Oracle: practice
Implement both report methods and compare them on 60 seeded cases. Generate lists of 0–5 integers in 0–49 and thresholds in 0–49 with seed 23. Also check [25] at threshold 25 gives 25 and empty input gives zero. Preserve each source list. Print Oracle checks passed. Include the list and threshold in a mismatch message.
public class Main {
public static void main(String[] args) {
// Add your implementation and checks below.
System.out.println("Your workspace");
}
}Hint 1: the idea
Generate inputs once per case and feed the same values to both independent implementations.
Hint 2: a first step
Copy the source before both calls so a separate assertion can check that neither implementation mutates it.
Show one solution
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random random = new Random(23);
for (int index = 0; index < 60; index++) {
List<Integer> values = new ArrayList<>();
int size = random.nextInt(6);
for (int item = 0; item < size; item++) {
values.add(random.nextInt(50));
}
int minimum = random.nextInt(50);
List<Integer> before = List.copyOf(values);
if (pipeline(values, minimum) != reference(values, minimum)) {
throw new AssertionError(values + " at " + minimum);
}
if (!values.equals(before)) {
throw new AssertionError("input changed");
}
}
if (pipeline(List.of(25), 25) != 25 || pipeline(List.of(), 25) != 0) {
throw new AssertionError("explicit boundaries");
}
System.out.println("Oracle checks passed");
}
static int pipeline(List<Integer> values, int minimum) {
return values.stream()
.filter(value -> value >= minimum)
.mapToInt(value -> value)
.sum();
}
static int reference(List<Integer> values, int minimum) {
int total = 0;
for (int value : values) {
if (value >= minimum) {
total += value;
}
}
return total;
}
}Expected output:
Oracle checks passedThe reference version is a plain loop you can check by eye; the pipeline is trusted only because it agrees with that loop on 60 seeded cases. The fixed seed means a failure can be reproduced, and the message includes the list and threshold so you can turn it into an explicit case. The two explicit checks cover what random data may miss: a value equal to the threshold, and empty input.
Change it and test again
Change only the pipeline boundary to strict > and run again. Record the first failure, then restore it. Next imagine adding huge integers: the current property needs a new arithmetic policy and new tests. Do not extend an input domain silently because the generator happens to accept a larger bound.
Explain before moving on
Which evidence comes from the fixed expected examples, the independent reference, and source-preservation checks? Prepare the same style of evidence for the ledger: rollback, replay, exact totals, canonical export and isolation all need distinguishable failure cases.
Completion is saved in this browser only. It does not measure mastery.