Skip to content

Commit

Permalink
Added numberBetween for doubles. (#1392)
Browse files Browse the repository at this point in the history
* Added numberBetween for doubles.
  • Loading branch information
bodiam authored Nov 2, 2024
1 parent 87a3803 commit f073184
Show file tree
Hide file tree
Showing 20 changed files with 67 additions and 40 deletions.
12 changes: 11 additions & 1 deletion src/main/java/net/datafaker/providers/base/Number.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public int negative() {
* @return a random number on faker.number() between min and max
* if min = max, return min
*/
public int numberBetween(final int min, final int max) {
public int numberBetween(int min, int max) {
if (min == max) return min;
final int realMin = Math.min(min, max);
final int realMax = Math.max(min, max);
Expand All @@ -67,6 +67,16 @@ public int numberBetween(final int min, final int max) {
* @return a random number on faker.number() between min and max
* if min = max, return min
*/
public double numberBetween(double min, double max) {
return faker.random().nextDouble(min, max);
}

/**
* @param min the lower bound (include min)
* @param max the upper bound (not include max)
* @return a random number on faker.number() between min and max
* if min = max, return min
*/
public long numberBetween(long min, long max) {
if (min == max) return min;
final long realMin = Math.min(min, max);
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/net/datafaker/FakerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ void badExpressionCouldntCoerce() {
void expression() {
assertThat(faker.expression("#{options.option 'a','b','c','d'}")).matches("([abcd])");
assertThat(faker.expression("#{options.option 'single'}")).isEqualTo("single");
assertThat(faker.expression("#{options.option ''''}")).isEqualTo("'");
assertThat(faker.expression("#{options.option '12','345','89','54321'}")).matches("(12|345|89|54321)");
assertThat(faker.expression("#{regexify '(a|b){2,3}'}")).matches("([ab]){2,3}");
assertThat(faker.expression("#{regexify '\\.\\*\\?\\+'}")).matches("\\.\\*\\?\\+");
Expand All @@ -163,7 +162,7 @@ void expression() {
assertThat(faker.expression("#{letterify '????','true'}")).matches("[A-Z]{4}");
assertThat(faker.expression("#{templatify '????','?','1','2','q','r'}")).matches("([12qr]){4}");
assertThat(faker.expression("#{Name.first_name} #{Name.first_name} #{Name.last_name}")).matches("[a-zA-Z']+ [a-zA-Z']+ [a-zA-Z']+");
assertThat(faker.expression("#{number.number_between '1','10'}")).matches("[1-9]");
assertThat(faker.expression("#{number.number_between '1','10'}")).matches("[1-9](\\.[0-9]+)?");
assertThat(faker.expression("#{color.name}")).matches("[a-z\\s]+");
assertThat(faker.expression("#{date.past '15','SECONDS','dd/MM/yyyy hh:mm:ss'}"))
.matches("[0-9]{2}/[0-9]{2}/[0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2}");
Expand Down Expand Up @@ -211,7 +210,7 @@ void testLimitForCsvExpression(int limit) {

@RepeatedTest(100)
void numberBetweenRepeated() {
assertThat(faker.expression("#{number.number_between '1','10'}")).matches("[1-9]");
assertThat(faker.expression("#{number.number_between '1','10'}")).matches("[1-9](\\.[0-9]+)?");
}

@Test
Expand Down
22 changes: 11 additions & 11 deletions src/test/java/net/datafaker/providers/base/AddressTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ void testStreetAddressIsANumber() {
assertThat(streetAddressNumber).matches("[0-9]+");
}

@RepeatedTest(100)
@RepeatedTest(10)
void testLatitude() {
String latStr = faker.address().latitude().replace(decimalSeparator, '.');
assertThat(latStr).is(IS_A_NUMBER);
Double lat = Double.valueOf(latStr);
assertThat(lat).isBetween(-90.0, 90.0);
}

@RepeatedTest(100)
@RepeatedTest(10)
void testLongitude() {
String longStr = faker.address().longitude().replace(decimalSeparator, '.');
assertThat(longStr).is(IS_A_NUMBER);
Expand Down Expand Up @@ -232,25 +232,25 @@ void testZipPlus4IsNineDigits() {
assertThat(zipCodeParts[1]).matches("[0-9]{4}");
}

@RepeatedTest(100)
@RepeatedTest(10)
void testLatLonEnUs() {
assertThat(US_FAKER.address().latLon())
.matches(BI_LAT_LON_REGEX.apply(ESCAPED_DECIMAL_SEPARATOR.apply(US_FAKER.getContext().getLocale()), ","));
}

@RepeatedTest(100)
@RepeatedTest(10)
void testLatLonNl() {
assertThat(NL_FAKER.address().latLon())
.matches(BI_LAT_LON_REGEX.apply(ESCAPED_DECIMAL_SEPARATOR.apply(NL_FAKER.getContext().getLocale()), ","));
}

@RepeatedTest(100)
@RepeatedTest(10)
void testLonLatEnUs() {
assertThat(US_FAKER.address().lonLat())
.matches(BI_LON_LAT_REGEX.apply(ESCAPED_DECIMAL_SEPARATOR.apply(US_FAKER.getContext().getLocale()), ","));
}

@RepeatedTest(100)
@RepeatedTest(10)
void testLonLatNl() {
assertThat(NL_FAKER.address().lonLat())
.matches(BI_LON_LAT_REGEX.apply(ESCAPED_DECIMAL_SEPARATOR.apply(NL_FAKER.getContext().getLocale()), ","));
Expand All @@ -275,28 +275,28 @@ void cyrillicStreetName(String locale) {
assertThat(localFaker.address().streetName()).isNotEmpty().matches(CYRILLIC_LETTERS);
}

@RepeatedTest(100)
@RepeatedTest(10)
void dutchAddress() {
assertThat(NL_FAKER.address().stateAbbr()).matches("[A-Z]{2}");
assertThat(NL_FAKER.address().fullAddress()).matches("[A-Z].+, [0-9]{4} [A-Z]{2}, [A-Z].+");
}

@RepeatedTest(100)
@RepeatedTest(10)
void belgianSAddress() {
assertThat(BELGIAN_FAKER.address().stateAbbr()).matches("[A-Z]{3}");
assertThat(BELGIAN_FAKER.address().fullAddress()).matches("[A-Z].+, [0-9]{4}, [A-Z].+");
}

@RepeatedTest(100)
@RepeatedTest(10)
void belgianZipcode() {
assertThat(Integer.valueOf(BELGIAN_FAKER.address().zipCode())).isBetween(1000, 9992);
}

@RepeatedTest(100)
@RepeatedTest(10)
void australiaAddress() {
assertThat(AU_FAKER.address().fullAddress()).matches("(Unit|[0-9]).+, [A-Z].+, [A-Z]{2,3} [0-9]{4}");
}
@RepeatedTest(100)
@RepeatedTest(10)
void testCityCnSuffix() {
assertThat(new Faker(Locale.CHINA).address().citySuffix()).matches("[\\u4e00-\\u9fa5]{1,7}(?:省|自治区)");
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/net/datafaker/providers/base/AnimalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ protected Collection<TestSpec> providerListTest() {
return List.of(TestSpec.of(animal::name, "creature.animal.name"));
}

@RepeatedTest(100)
@RepeatedTest(10)
void scientificName() {
assertThat(animal.scientificName()).matches("[A-Z][a-z]+ [a-z]+");
}

@RepeatedTest(100)
@RepeatedTest(10)
void genus() {
assertThat(animal.genus()).matches("[A-Z][a-z]+");
}

@RepeatedTest(100)
@RepeatedTest(10)
void species() {
assertThat(animal.species()).matches("[a-z]+");
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/net/datafaker/providers/base/BoolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class BoolTest extends BaseFakerTest<BaseFaker> {

@RepeatedTest(100)
@RepeatedTest(10)
void testBool() {
assertThat(faker.bool().bool()).isIn(true, false);
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/net/datafaker/providers/base/CNPJTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CNPJTest extends BaseFakerTest<BaseFaker> {
/**
* A valid CNPJ is either a real number or a generated valid number.
*/
@RepeatedTest(1000)
@RepeatedTest(10)
void isValidCNPJ() {
CNPJ cnpj = faker.cnpj();
assertThat(isCNPJValid(cnpj.valid())).describedAs("Current value " + cnpj).isTrue();
Expand All @@ -23,7 +23,7 @@ void isValidCNPJ() {
/**
* A invalid CNPJ is that does not meet the requirements of the algorithm
*/
@RepeatedTest(1000)
@RepeatedTest(10)
void isInvalidCNPJ() {
CNPJ cnpj = faker.cnpj();
assertThat(isCNPJValid(cnpj.invalid())).describedAs("Current value " + cnpj).isFalse();
Expand All @@ -46,7 +46,7 @@ void valid_multiBranchIsTrue_shouldGenerateCNPJWithBranchNumberGreaterThan0001()
assertThat(isCNPJValid(cnpj)).describedAs("Current value " + cnpj).isTrue();
}

@RepeatedTest(1000)
@RepeatedTest(10)
void invalid_multiBranchIsTrue_shouldGenerateCNPJWithBranchNumberGreaterThan0001() {
final CNPJ cnpj1 = faker.cnpj();
String cnpj = cnpj1.invalid(true, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void testLogo() {
assertThat(company.logo()).matches("https://pigment.github.io/fake-logos/logos/medium/color/\\d+\\.png");
}

@RepeatedTest(100)
@RepeatedTest(10)
void testUrl() {
assertThat(company.url()).matches(URL_PATTERN);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class FamousLastWordsTest extends BaseFakerTest<BaseFaker> {

@RepeatedTest(1000)
@RepeatedTest(10)
void testLastWords() {
assertThat(faker.famousLastWords().lastWords()).matches("^[A-Za-z- .,'!?-…]+$");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void localeStringRandom() {
* Test to check Locality's localeStringWithRandom method. It verifies that the randomly selected
* locale is within the set of all supported locales
*/
@RepeatedTest(100)
@RepeatedTest(10)
void localeStringWithRandom() {
Random random = new Random();
String randomLocale = locality.localeStringWithRandom(random);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/net/datafaker/providers/base/MusicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void chord() {
assertThat(faker.music().chord()).matches("([A-Z])+([b#])?+(-?[a-zA-Z0-9]{0,4})");
}

@RepeatedTest(100)
@RepeatedTest(10)
void genre() {
assertThat(faker.music().genre()).matches("[ -?\\w+]+");
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/net/datafaker/providers/base/NumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,24 @@ void testIntNumberBetweenRepeated() {
.isGreaterThanOrEqualTo(low);
}

@RepeatedTest(100)
void testDoubleNumberBetweenRepeatedLowHigh() {
double low = 1.0;
double high = 10.0;
double v = faker.number().numberBetween(low, high);
assertThat(v).isLessThan(high)
.isGreaterThanOrEqualTo(low);
}

@RepeatedTest(100)
void testDoubleNumberBetweenRepeatedHighLow() {
double low = 1.0;
double high = 10.0;
double v = faker.number().numberBetween(high, low);
assertThat(v).isLessThan(high)
.isGreaterThanOrEqualTo(low);
}

@Test
void testNumberBetweenOneAndThree() {
Set<Integer> nums = new HashSet<>(3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final void beforeEach() {
reset(mockFaker, fakeValuesService);
}

@RepeatedTest(100)
@RepeatedTest(10)
void anyTest() {
assertThat(relationship.any()).isNotEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ protected Collection<TestSpec> providerListTest() {
TestSpec.of(restaurant::review, "restaurant.review"));
}

@RepeatedTest(100)
@RepeatedTest(10)
void namePrefix() {
assertThat(restaurant.namePrefix())
.isNotEmpty()
.doesNotContain("#", "?") // make sure bothify is applied
.matches("[A-Z0-9].*"); // and that bothify only uses uppercase characters
}

@RepeatedTest(100)
@RepeatedTest(10)
void name() {
assertThat(restaurant.name())
.isNotEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void testShortTwitterIdLength() {
assertThat(generatedID).hasSize(expectedLength);
}

@RepeatedTest(100)
@RepeatedTest(10)
void testLongTwitterIdLength() {
int expectedLength = 25;
String generatedID = twitter.twitterId(expectedLength);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ void testLicensePlateWithParam() {
assertThat(faker.vehicle().licensePlate("AL")).matches(WORDS_MATCH);
}

@RepeatedTest(100)
@RepeatedTest(10)
void testLicensePlateWithParam_Canada() {
BaseFaker test = new BaseFaker(Locale.CANADA);
assertThat(test.vehicle().licensePlate("MB")).matches(WORDS_MATCH);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class OscarMovieTest extends EntertainmentFakerTest {

private final OscarMovie oscarMovie = getFaker().oscarMovie();

@RepeatedTest(100)
@RepeatedTest(10)
void actor() {
assertThat(oscarMovie.actor()).matches("\\P{Cc}+");
}
Expand All @@ -25,12 +25,12 @@ void quote() {
assertThat(isNullOrEmpty(oscarMovie.quote())).isFalse();
}

@RepeatedTest(100)
@RepeatedTest(10)
void character() {
assertThat(oscarMovie.character()).matches("\\P{Cc}+");
}

@RepeatedTest(100)
@RepeatedTest(10)
void releaseDate() {
assertThat(oscarMovie.releaseDate()).matches("[A-Za-z,0-9 ]+");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected Collection<TestSpec> providerListTest() {
TestSpec.of(disease::dermatology, DERMATOLOGY_DISEASE_KEY.yamlKey));
}

@RepeatedTest(100)
@RepeatedTest(10)
void testAnyDisease() {
// when
String anyDisease = disease.anyDisease();
Expand All @@ -48,7 +48,7 @@ void testAnyDisease() {
.isIn(allDiseases);
}

@RepeatedTest(100)
@RepeatedTest(10)
void testDiseaseCodes() {
String diseaseCode = disease.icd10();
assertThat(diseaseCode).matches("^[A-Z][0-9]{1,2}(\\.[0-9])?$");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ void expressionWithValidFakerObjectValidMethodInvalidArgs() {
"Unable to resolve #{Number.number_between 'x','y'} directive");
}

@RepeatedTest(100)
@RepeatedTest(10)
void futureDateExpression() {
LocalDateTime now = LocalDateTime.now(OffsetDateTime.now(ZoneId.systemDefault()).getOffset());
LocalDateTime nowPlus10Days = now.plusDays(10);
Expand All @@ -292,7 +292,7 @@ void futureDateExpression() {
assertThat(date).isStrictlyBetween(now, nowPlus10Days);
}

@RepeatedTest(100)
@RepeatedTest(10)
void pastDateExpression() {
LocalDateTime now = LocalDateTime.now(OffsetDateTime.now(ZoneId.systemDefault()).getOffset());
LocalDateTime nowMinus5Hours = now.minusHours(5);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/net/datafaker/service/RandomNumbersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void range_exclusive() {
assertThat(all(() -> randomService.nextInt(Range.exclusive(2, 5)))).containsExactly(3, 4);
}

@RepeatedTest(100)
@RepeatedTest(10)
void nextDouble() {
assertThat(randomService.nextDouble(2, 3)).isGreaterThanOrEqualTo(2);
assertThat(randomService.nextDouble(2, 3)).isStrictlyBetween(1.9999, 3.0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
net.datafaker.TestSetupExtension
#net.datafaker.TestSetupExtension

0 comments on commit f073184

Please sign in to comment.