ifesdjeen commented on code in PR #119: URL: https://github.com/apache/cassandra-accord/pull/119#discussion_r1739326585
########## accord-core/src/test/java/accord/utils/GensTest.java: ########## @@ -0,0 +1,90 @@ +/* + * 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 accord.utils; + +import org.junit.jupiter.api.Test; + +import org.assertj.core.api.Assertions; +import org.mockito.Mockito; +import org.mockito.stubbing.Answer; + +class GensTest +{ + private static final Answer REJECT_ALL = ignore -> { + throw new UnsupportedOperationException(ignore.toString()); + }; + + @Test + void intMixedDistributionWithBucketsOutOfBounds() + { + Assertions.assertThatThrownBy(() -> Gens.mixedDistribution(0, 100, 101)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Num buckets must be between 1 and 100; given 101"); + } + + @Test + void intMixedDistributionWithBucketsUniform() + { + Gen<Gen.IntGen> distroGren = Gens.mixedDistribution(0, 100, 10); + RandomSource rs = Mockito.mock(RandomSource.class, REJECT_ALL); + Mockito.doReturn(0).when(rs).nextInt(0, 4); // uniform disto for bucket selection Review Comment: nit: disto ########## accord-core/src/test/java/accord/utils/GensTest.java: ########## @@ -0,0 +1,90 @@ +/* + * 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 accord.utils; + +import org.junit.jupiter.api.Test; + +import org.assertj.core.api.Assertions; +import org.mockito.Mockito; +import org.mockito.stubbing.Answer; + +class GensTest +{ + private static final Answer REJECT_ALL = ignore -> { + throw new UnsupportedOperationException(ignore.toString()); + }; + + @Test + void intMixedDistributionWithBucketsOutOfBounds() + { + Assertions.assertThatThrownBy(() -> Gens.mixedDistribution(0, 100, 101)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Num buckets must be between 1 and 100; given 101"); + } + + @Test + void intMixedDistributionWithBucketsUniform() + { + Gen<Gen.IntGen> distroGren = Gens.mixedDistribution(0, 100, 10); + RandomSource rs = Mockito.mock(RandomSource.class, REJECT_ALL); + Mockito.doReturn(0).when(rs).nextInt(0, 4); // uniform disto for bucket selection + Mockito.doReturn(0).when(rs).nextInt(0, 2); // uniform distro for within the bucket + + Gen.IntGen gen = distroGren.next(rs); + + // to avoid conflicts, need to have i=0 as its own thing + Mockito.reset(rs); + Mockito.doReturn(0, 42).when(rs).nextInt(0, 10); + Assertions.assertThat(gen.next(rs)).isEqualTo(42); + + for (int i = 1; i < 10; i++) + { + int start = i * 10; + int end = start + 10; + Mockito.reset(rs); + Mockito.doReturn(i).when(rs).nextInt(0, 10); + Mockito.doReturn(42).when(rs).nextInt(start, end); // this is the value returned + Assertions.assertThat(gen.next(rs)).isEqualTo(42); + } + } + + @Test + void intMixedDistributionWithBucketsMedian() + { + Gen<Gen.IntGen> distroGren = Gens.mixedDistribution(0, 100, 10); + RandomSource rs = Mockito.mock(RandomSource.class, REJECT_ALL); + Mockito.doReturn(0).when(rs).nextInt(0, 4); // uniform disto for bucket selection Review Comment: nit: disto -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

