Skip to content

Commit

Permalink
Merge pull request #63 from ocadotechnology/test-arranger-53
Browse files Browse the repository at this point in the history
test-arranger-53 Add someFloat and someDouble
  • Loading branch information
mjureczko authored Jan 19, 2024
2 parents 8bfec03 + 8516234 commit 16eef3d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
25 changes: 25 additions & 0 deletions src/main/java/com/ocadotechnology/gembus/test/Arranger.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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> T someFrom(Collection<T> source) {
if (source.isEmpty()) {
throw new IllegalArgumentException("Cannot return element from empty collection.");
Expand Down
57 changes: 51 additions & 6 deletions src/test/java/com/ocadotechnology/gembus/test/ArrangerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());

}

Expand Down Expand Up @@ -165,7 +166,7 @@ void someFromList() {
}

@Test
void someFromSetOfSizeOne(){
void someFromSetOfSizeOne() {
//given
final Set<Integer> listOfSizeOne = Collections.singleton(Arranger.someInteger());

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 16eef3d

Please sign in to comment.