|
| 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