From 85162344a81406664fbb149b036889c53236ffeb Mon Sep 17 00:00:00 2001 From: "marian.jureczko" Date: Fri, 19 Jan 2024 14:53:44 +0100 Subject: [PATCH] test-arranger-53 Add someFloat and someDouble --- .../ocadotechnology/gembus/test/Arranger.java | 25 ++++++++ .../gembus/test/ArrangerTest.java | 57 +++++++++++++++++-- 2 files changed, 76 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/ocadotechnology/gembus/test/Arranger.java b/src/main/java/com/ocadotechnology/gembus/test/Arranger.java index 186dbde..6dbb269 100644 --- a/src/main/java/com/ocadotechnology/gembus/test/Arranger.java +++ b/src/main/java/com/ocadotechnology/gembus/test/Arranger.java @@ -25,6 +25,7 @@ import java.time.Instant; import java.time.LocalDate; import java.util.*; +import java.util.concurrent.ThreadLocalRandom; import java.util.function.Predicate; import java.util.function.Supplier; import java.util.stream.Stream; @@ -239,6 +240,30 @@ public static boolean someBoolean() { return random.easyRandom.nextBoolean(); } + public static float someFloat() { + return random.easyRandom.nextFloat(); + } + + /** + * @param min inclusive + * @param max exclusive + */ + public static float someFloat(float min, float max) { + return (float) ThreadLocalRandom.current().nextDouble(min, max); + } + + public static double someDouble() { + return random.easyRandom.nextDouble(); + } + + /** + * @param min inclusive + * @param max exclusive + */ + public static double someDouble(double min, double max) { + return ThreadLocalRandom.current().nextDouble(min, max); + } + public static T someFrom(Collection source) { if (source.isEmpty()) { throw new IllegalArgumentException("Cannot return element from empty collection."); diff --git a/src/test/java/com/ocadotechnology/gembus/test/ArrangerTest.java b/src/test/java/com/ocadotechnology/gembus/test/ArrangerTest.java index 219e8fd..322be4c 100644 --- a/src/test/java/com/ocadotechnology/gembus/test/ArrangerTest.java +++ b/src/test/java/com/ocadotechnology/gembus/test/ArrangerTest.java @@ -16,6 +16,7 @@ package com.ocadotechnology.gembus.test; import com.ocadotechnology.gembus.ToTestNonPublic; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import java.math.BigDecimal; @@ -56,12 +57,12 @@ void email() { void priceLike() { - //when - final BigDecimal price = Arranger.somePriceLikeBigDecimal(); + //when + final BigDecimal price = Arranger.somePriceLikeBigDecimal(); - //then - assertTrue(price.compareTo(BigDecimal.ZERO) >= 0); - assertEquals(2, price.scale()); + //then + assertTrue(price.compareTo(BigDecimal.ZERO) >= 0); + assertEquals(2, price.scale()); } @@ -165,7 +166,7 @@ void someFromList() { } @Test - void someFromSetOfSizeOne(){ + void someFromSetOfSizeOne() { //given final Set listOfSizeOne = Collections.singleton(Arranger.someInteger()); @@ -377,6 +378,50 @@ public void should_generateLastName() { assertThat(actual.length()).isGreaterThan(1); assertThat(actual.charAt(0)).isUpperCase(); } + + @Test + void should_generateFloatFromTheRangeDefinedByMinAndMax() { + //given + float min = -999.99f; + float max = 999.99f; + + //when + float actualMin = 0.0f; + float actualMax = 0.0f; + for (int i = 0; i < 999; i++) { + double actual = Arranger.someFloat(min, max); + actualMin = (float) Math.min(actualMin, actual); + actualMax = (float) Math.max(actualMax, actual); + } + + //then + assertThat(actualMin).isGreaterThanOrEqualTo(min); + assertThat(actualMin).isLessThan(-1.0f); + assertThat(actualMax).isLessThan(max); + assertThat(actualMax).isGreaterThan(1.0f); + } + + @Test + void should_generateDoubleFromTheRangeDefinedByMinAndMax() { + //given + double min = -999.99; + double max = 999.99; + + //when + double actualMin = 0.0; + double actualMax = 0.0; + for (int i = 0; i < 999; i++) { + double actual = Arranger.someDouble(min, max); + actualMin = Math.min(actualMin, actual); + actualMax = Math.max(actualMax, actual); + } + + //then + assertThat(actualMin).isGreaterThanOrEqualTo(min); + assertThat(actualMin).isLessThan(-1.0); + assertThat(actualMax).isLessThan(max); + assertThat(actualMax).isGreaterThan(1.0); + } } class SomeClass {