Skip to content

Commit 2421680

Browse files
kyakdanoetr
authored andcommitted
feat: add fuzz test example for minimize
1 parent c61a728 commit 2421680

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

examples/junit/src/test/java/com/example/BUILD.bazel

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,24 @@ java_fuzz_target_test(
393393
"@maven//:org_junit_jupiter_junit_jupiter_api",
394394
],
395395
)
396+
397+
# Test for the minimize() hill-climbing API.
398+
# This test uses Jazzer.minimize() to guide the fuzzer toward minimizing
399+
# a "temperature" value, demonstrating inverse hill-climbing behavior.
400+
java_fuzz_target_test(
401+
name = "CoolerFuzzTest",
402+
srcs = ["CoolerFuzzTest.java"],
403+
allowed_findings = ["java.lang.RuntimeException"],
404+
env = {"JAZZER_FUZZ": "1"},
405+
target_class = "com.example.CoolerFuzzTest",
406+
verify_crash_reproducer = False,
407+
runtime_deps = [
408+
":junit_runtime",
409+
],
410+
deps = [
411+
"//src/main/java/com/code_intelligence/jazzer/api:hooks",
412+
"//src/main/java/com/code_intelligence/jazzer/junit:fuzz_test",
413+
"//src/main/java/com/code_intelligence/jazzer/mutation/annotation",
414+
"@maven//:org_junit_jupiter_junit_jupiter_api",
415+
],
416+
)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2026 Code Intelligence GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example;
18+
19+
import com.code_intelligence.jazzer.api.Jazzer;
20+
import com.code_intelligence.jazzer.junit.FuzzTest;
21+
import com.code_intelligence.jazzer.mutation.annotation.NotNull;
22+
23+
/**
24+
* Example demonstrating the minimize() hill-climbing API.
25+
*
26+
* <p>Mirror of ReactorFuzzTest: instead of heating up a reactor, we're trying to cool down a system
27+
* to the lowest possible temperature.
28+
*/
29+
public class CoolerFuzzTest {
30+
31+
@FuzzTest
32+
public void fuzz(@NotNull String input) {
33+
for (char c : input.toCharArray()) {
34+
if (c < 32 || c > 126) return;
35+
}
36+
controlCooler(input);
37+
}
38+
39+
private void controlCooler(String commands) {
40+
long temperature = 4000; // Starts hot
41+
42+
for (char cmd : commands.toCharArray()) {
43+
// Complex, chaotic feedback loop.
44+
// Hard to predict which character decreases temperature.
45+
if ((temperature ^ cmd) % 3 == 0) {
46+
temperature -= (cmd % 10); // Cool down slightly
47+
} else if ((temperature ^ cmd) % 3 == 1) {
48+
temperature += (cmd % 8); // Heat up slightly
49+
} else {
50+
temperature -= 1; // Tiny decrease
51+
}
52+
53+
// Cap at reasonable bounds
54+
if (temperature < 0) temperature = 0;
55+
if (temperature > 5000) temperature = 5000;
56+
}
57+
58+
// THE GOAL: MINIMIZATION
59+
// Drive 'temperature' to the lowest possible value.
60+
Jazzer.minimize(temperature, 0, 4000);
61+
if (temperature <= 100) {
62+
throw new RuntimeException("Supercooled! Temperature minimized.");
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)