| /**************************************************************** |
| * Licensed to the Apache Software Foundation (ASF) under one * |
| * or more contributor license agreements. See the NOTICE file * |
| * distributed with this work for additional information * |
| * regarding copyright ownership. The ASF licenses this file * |
| * to you under the Apache License, Version 2.0 (the * |
| * "License"); you may not use this file except in compliance * |
| * with the License. You may obtain a copy of the License at * |
| * * |
| * http://www.apache.org/licenses/LICENSE-2.0 * |
| * * |
| * Unless required by applicable law or agreed to in writing, * |
| * software distributed under the License is distributed on an * |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
| * KIND, either express or implied. See the License for the * |
| * specific language governing permissions and limitations * |
| * under the License. * |
| ****************************************************************/ |
| |
| package org.apache.james.utils; |
| |
| import static org.assertj.core.api.Assertions.assertThat; |
| import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| |
| import java.util.Map; |
| import java.util.function.Function; |
| import java.util.stream.Collectors; |
| |
| import org.apache.james.utils.DiscreteDistribution.DistributionEntry; |
| import org.assertj.core.data.Offset; |
| import org.junit.jupiter.api.Test; |
| |
| import com.google.common.collect.ImmutableList; |
| |
| class DiscreteDistributionTest { |
| public static final Offset<Long> OFFSET = Offset.offset(10_000L); |
| |
| @Test |
| void createShouldNotSupportNegativeDistribution() { |
| assertThatThrownBy(() -> new DistributionEntry<>("a", -1)) |
| .isInstanceOf(IllegalArgumentException.class); |
| } |
| |
| @Test |
| void createShouldSupportZeroDistribution() { |
| DiscreteDistribution<String> testee = DiscreteDistribution.create( |
| ImmutableList.of( |
| new DistributionEntry<>("a", 0), |
| new DistributionEntry<>("b", 1))); |
| |
| assertThat(testee.generateRandomStream().limit(10)).containsOnly("b"); |
| } |
| |
| @Test |
| void createShouldNotSupportEmptyDistribution() { |
| assertThatThrownBy(() -> DiscreteDistribution.create(ImmutableList.of())) |
| .isInstanceOf(IllegalArgumentException.class); |
| } |
| |
| @Test |
| void createShouldNotSupportEffectivelyEmptyDistribution() { |
| assertThatThrownBy(() -> DiscreteDistribution.create( |
| ImmutableList.of( |
| new DistributionEntry<>("a", 0), |
| new DistributionEntry<>("b", 0)))) |
| .isInstanceOf(IllegalArgumentException.class); |
| } |
| |
| |
| @Test |
| void streamOfSingleDistributionMapShouldAlwaysReturnSameElement() { |
| DiscreteDistribution<String> testee = DiscreteDistribution.create( |
| ImmutableList.of( |
| new DistributionEntry<>("a", 1))); |
| |
| assertThat(testee.generateRandomStream().limit(10)).containsOnly("a"); |
| } |
| |
| @Test |
| void streamOfEvenDistributionMapShouldReturnSameNumberOfEachElement() { |
| DiscreteDistribution<String> testee = DiscreteDistribution.create( |
| ImmutableList.of( |
| new DistributionEntry<>("a", 10), |
| new DistributionEntry<>("b", 10))); |
| |
| Map<String, Long> experimentOutcome = testee.generateRandomStream().limit(1_000_000) |
| .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); |
| assertThat(experimentOutcome.get("a")) |
| .isCloseTo(experimentOutcome.get("b"), OFFSET); |
| } |
| |
| @Test |
| void streamOfSpecificDistributionMapShouldReturnTwiceMoreA() { |
| DiscreteDistribution<String> testee = DiscreteDistribution.create( |
| ImmutableList.of( |
| new DistributionEntry<>("a", 20), |
| new DistributionEntry<>("b", 10))); |
| |
| Map<String, Long> experimentOutcome = testee.generateRandomStream().limit(1_000_000) |
| .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); |
| assertThat(experimentOutcome.get("a")) |
| .isCloseTo(experimentOutcome.get("b") * 2, OFFSET); |
| } |
| |
| @Test |
| void partitionShouldSupportDuplicatedDistributionEntry() { |
| DiscreteDistribution<String> testee = DiscreteDistribution.create( |
| ImmutableList.of( |
| new DistributionEntry<>("a", 10), |
| new DistributionEntry<>("b", 10), |
| new DistributionEntry<>("a", 10))); |
| |
| Map<String, Long> experimentOutcome = testee.generateRandomStream().limit(1_000_000) |
| .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); |
| assertThat(experimentOutcome.get("a")) |
| .isCloseTo(experimentOutcome.get("b") * 2, OFFSET); |
| } |
| |
| } |