Skip to content

Commit

Permalink
j-easy#507 Fixed upper limit range for HourRandomizer
Browse files Browse the repository at this point in the history
  • Loading branch information
increff-rounik-prashar committed Dec 8, 2024
1 parent 51150d4 commit d580164
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class HourRandomizer implements Randomizer<Integer> {
private final IntegerRangeRandomizer hourRandomizer;

public HourRandomizer() {
hourRandomizer = new IntegerRangeRandomizer(MIN_HOUR, MAX_HOUR);
hourRandomizer = new IntegerRangeRandomizer(MIN_HOUR, MAX_HOUR + 1); // Adding 1 since max value is exclusive
}

public HourRandomizer(final long seed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
*/
package org.jeasy.random.randomizers.time.internal;

import static org.jeasy.random.randomizers.time.HourRandomizer.MAX_HOUR;
import static org.jeasy.random.randomizers.time.HourRandomizer.MIN_HOUR;
import static org.assertj.core.api.Assertions.assertThat;

import org.jeasy.random.randomizers.AbstractRandomizerTest;
import org.jeasy.random.randomizers.time.HourRandomizer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.jeasy.random.randomizers.AbstractRandomizerTest;
import org.jeasy.random.randomizers.time.HourRandomizer;
import java.util.stream.IntStream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.jeasy.random.randomizers.time.HourRandomizer.MAX_HOUR;
import static org.jeasy.random.randomizers.time.HourRandomizer.MIN_HOUR;

class HourRandomizerTest extends AbstractRandomizerTest<Integer> {

Expand Down Expand Up @@ -62,4 +63,21 @@ void shouldGenerateTheSameValueForTheSameSeed() {
// Then
assertThat(actual).isEqualTo(expected);
}

@Test
void generatedValueMaximumShouldBeMaxHour() {
// Given
HourRandomizer hourRandomizer = new HourRandomizer();
Integer expected = MAX_HOUR;

// When
int maxHour = IntStream.range(0, 1_000_000)
.map(e -> hourRandomizer.getRandomValue())
.max()
.orElseThrow();

// Then
assertThat(maxHour).isEqualTo(expected);
}

}

0 comments on commit d580164

Please sign in to comment.