|
| 1 | +/* |
| 2 | + * Copyright 2025 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.mutation.combinator; |
| 18 | + |
| 19 | +import static org.junit.jupiter.params.provider.Arguments.arguments; |
| 20 | + |
| 21 | +import com.code_intelligence.jazzer.mutation.api.PseudoRandom; |
| 22 | +import com.code_intelligence.jazzer.mutation.engine.SeededPseudoRandom; |
| 23 | +import java.util.function.Function; |
| 24 | +import java.util.stream.IntStream; |
| 25 | +import java.util.stream.Stream; |
| 26 | +import org.junit.jupiter.params.ParameterizedTest; |
| 27 | +import org.junit.jupiter.params.provider.MethodSource; |
| 28 | + |
| 29 | +public class SamplingUtilsTest { |
| 30 | + static Stream<?> weightsProvider() { |
| 31 | + final int N = 1000000; |
| 32 | + final double T = 0.03; |
| 33 | + return Stream.of( |
| 34 | + arguments(N, T, new double[] {1.0, 1.0, 1.0}), |
| 35 | + arguments(N, T, new double[] {1.0, 2.0, 3.0, 4.0, 5.0}), |
| 36 | + arguments(N, T, new double[] {0.1, 0.2, 0.3, 0.4}), |
| 37 | + arguments(N, T, new double[] {10.0, 0.0, 0.1, 0.0, 90.0}), |
| 38 | + arguments(N, T, new double[] {5.0, 5.0, 0.0, 0.0, 0.01, 5.0, 5.0}), |
| 39 | + arguments(N, T, new double[] {0.0, 0.0, 0.0, 1.0}), |
| 40 | + arguments(N, T, new double[] {1.0}), |
| 41 | + arguments(N, T, new double[] {0.01, 0.01, 0.01, 0.97}), |
| 42 | + arguments(N, T, new double[] {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}), |
| 43 | + arguments(N, T, new double[] {0.001, 0.002, 0.003, 0.004, 0.005}), |
| 44 | + arguments(N, T, new double[] {0.001, 0.002, 0.003, 0.004, 0.000001, 10.0}), |
| 45 | + arguments(N, T, new double[] {0.001, 1000.0, 0.003, 10000.0, 0.005}), |
| 46 | + arguments(N, T, IntStream.range(1, 10).mapToDouble(i -> i).toArray()), |
| 47 | + arguments(N, 0.09, IntStream.range(1, 100).mapToDouble(i -> 1.0).toArray()), |
| 48 | + arguments(N, 0.15, IntStream.range(1, 1000).mapToDouble(i -> 1.0).toArray()), |
| 49 | + arguments(10000000, 0.15, IntStream.range(1, 10000).mapToDouble(i -> 1.0).toArray()), |
| 50 | + arguments(100000000, 0.16, IntStream.range(1, 100000).mapToDouble(i -> 1.0).toArray())); |
| 51 | + } |
| 52 | + |
| 53 | + @ParameterizedTest |
| 54 | + @MethodSource("weightsProvider") |
| 55 | + public void testWeightedSampler(int trials, double tolerance, double[] weights) { |
| 56 | + Integer[] indices = IntStream.range(0, weights.length).boxed().toArray(Integer[]::new); |
| 57 | + Function<PseudoRandom, Integer> sampler = SamplingUtils.weightedSampler(indices, weights); |
| 58 | + |
| 59 | + PseudoRandom random = new SeededPseudoRandom(12345); |
| 60 | + int[] counts = new int[indices.length]; |
| 61 | + for (int i = 0; i < trials; i++) { |
| 62 | + counts[sampler.apply(random)]++; |
| 63 | + } |
| 64 | + |
| 65 | + // Calculate expected probabilities that are proportional to the weights. |
| 66 | + double[] pExpected = new double[weights.length]; |
| 67 | + double sum = 0.0; |
| 68 | + for (double w : weights) { |
| 69 | + sum += w; |
| 70 | + } |
| 71 | + for (int i = 0; i < weights.length; i++) { |
| 72 | + pExpected[i] = weights[i] / sum; |
| 73 | + } |
| 74 | + |
| 75 | + double tol = (double) trials / weights.length * tolerance; // 5% of expected count |
| 76 | + // Ensure that the frequencies are within 5% of the expected frequencies. |
| 77 | + for (int i = 0; i < weights.length; i++) { |
| 78 | + double expectedCount = trials * pExpected[i]; |
| 79 | + assert Math.abs(counts[i] - expectedCount) < tol |
| 80 | + : String.format( |
| 81 | + "Count for index %d out of tolerance: got %d, expected ~%.2f", |
| 82 | + i, counts[i], expectedCount); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments