|
| 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.code_intelligence.jazzer.api; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertTrue; |
| 20 | +import static org.junit.Assert.fail; |
| 21 | + |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +public class MinimizeTest { |
| 25 | + |
| 26 | + @Test |
| 27 | + public void testBasicRangeMapping() { |
| 28 | + // value=50 in [0, 100] with 1024 counters |
| 29 | + // offset = (100 - 50) / 100 * 1023 = 511 |
| 30 | + Jazzer.minimize(50, 0, 100, 1024, 600000); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testValueAtMinimum() { |
| 35 | + // value == minValue → offset = effectiveCounters - 1 (maximum signal) |
| 36 | + Jazzer.minimize(0, 0, 100, 1024, 600001); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testValueAtMaximum() { |
| 41 | + // value == maxValue → offset = 0 (minimum signal) |
| 42 | + Jazzer.minimize(100, 0, 100, 1024, 600002); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testValueAboveMaximum() { |
| 47 | + // value > maxValue → no signal (should not throw) |
| 48 | + Jazzer.minimize(200, 0, 100, 1024, 600003); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testValueBelowMinimum() { |
| 53 | + // value < minValue → clamped to minValue (offset = effectiveCounters - 1) |
| 54 | + Jazzer.minimize(-10, 0, 100, 1024, 600004); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testNegativeRange() { |
| 59 | + // Range with negative values: [-100, -50] |
| 60 | + Jazzer.minimize(-75, -100, -50, 1024, 600005); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testSingleValueRange() { |
| 65 | + // minValue == maxValue → offset is always 0 |
| 66 | + Jazzer.minimize(42, 42, 42, 1024, 600006); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testLargeRange() { |
| 71 | + // Long.MIN_VALUE to Long.MAX_VALUE — should not overflow |
| 72 | + Jazzer.minimize(0, Long.MIN_VALUE, Long.MAX_VALUE, 1024, 600007); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testCustomNumCounters() { |
| 77 | + // Expert overload with small counter count |
| 78 | + Jazzer.minimize(50, 0, 100, 10, 600008); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testZeroNumCountersThrows() { |
| 83 | + try { |
| 84 | + Jazzer.minimize(50, 0, 100, 0, 600009); |
| 85 | + fail("Expected JazzerApiException for zero numCounters"); |
| 86 | + } catch (JazzerApiException e) { |
| 87 | + assertTrue(e.getMessage().contains("must be positive")); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testNegativeNumCountersThrows() { |
| 93 | + try { |
| 94 | + Jazzer.minimize(50, 0, 100, -5, 600010); |
| 95 | + fail("Expected JazzerApiException for negative numCounters"); |
| 96 | + } catch (JazzerApiException e) { |
| 97 | + assertTrue(e.getMessage().contains("must be positive")); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testMaxValueLessThanMinValueThrows() { |
| 103 | + try { |
| 104 | + Jazzer.minimize(50, 100, 0, 1024, 600011); |
| 105 | + fail("Expected JazzerApiException for maxValue < minValue"); |
| 106 | + } catch (JazzerApiException e) { |
| 107 | + assertTrue(e.getMessage().contains("must not be less than")); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testMultipleCallsSameId() { |
| 113 | + // Multiple calls with the same id should succeed (idempotent allocation) |
| 114 | + Jazzer.minimize(10, 0, 100, 1024, 600012); |
| 115 | + Jazzer.minimize(50, 0, 100, 1024, 600012); |
| 116 | + Jazzer.minimize(90, 0, 100, 1024, 600012); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testDifferentIdsWithDifferentRanges() { |
| 121 | + Jazzer.minimize(50, 0, 100, 1024, 600013); |
| 122 | + Jazzer.minimize(500, 0, 1000, 512, 600014); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testInconsistentNumCountersThrows() { |
| 127 | + // Same id and range but different numCounters (where range doesn't cap) |
| 128 | + Jazzer.minimize(50, 0, 10000, 1024, 600017); |
| 129 | + try { |
| 130 | + Jazzer.minimize(50, 0, 10000, 2048, 600017); |
| 131 | + fail("Expected JazzerApiException for inconsistent numCounters"); |
| 132 | + } catch (JazzerApiException e) { |
| 133 | + assertTrue( |
| 134 | + e.getMessage().contains("numCounters") |
| 135 | + && e.getMessage().contains("must remain constant")); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void testInconsistentMinValueThrows() { |
| 141 | + Jazzer.minimize(50, 0, 100, 1024, 600015); |
| 142 | + try { |
| 143 | + Jazzer.minimize(50, 10, 100, 1024, 600015); |
| 144 | + fail("Expected JazzerApiException for inconsistent minValue"); |
| 145 | + } catch (JazzerApiException e) { |
| 146 | + assertTrue(e.getMessage().contains("must remain constant")); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void testInconsistentMaxValueThrows() { |
| 152 | + Jazzer.minimize(50, 0, 100, 1024, 600016); |
| 153 | + try { |
| 154 | + Jazzer.minimize(50, 0, 200, 1024, 600016); |
| 155 | + fail("Expected JazzerApiException for inconsistent maxValue"); |
| 156 | + } catch (JazzerApiException e) { |
| 157 | + assertTrue(e.getMessage().contains("must remain constant")); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments