diff --git a/core/src/main/java/com/tngtech/valueprovider/AbstractValueProvider.java b/core/src/main/java/com/tngtech/valueprovider/AbstractValueProvider.java index c87bf94..0c2308f 100644 --- a/core/src/main/java/com/tngtech/valueprovider/AbstractValueProvider.java +++ b/core/src/main/java/com/tngtech/valueprovider/AbstractValueProvider.java @@ -560,6 +560,22 @@ public BigDecimal bigDecimalNumberWithScale(BigDecimal min, BigDecimal max, int return result.setScale(scale, RoundingMode.HALF_EVEN); } + /** + * Generates a {@link BigDecimal} in [{@code 0} ; {@code 1}], + *

+ * Example: + *

+     * ValueProvider vp = ValueProviderFactory.createRandomValueProvider();
+     * vp.bigDecimalPercentage();
+     * 
+ *

+ * + * @return the generated percentage. + */ + public BigDecimal bigDecimalPercentage() { + return bigDecimalNumber(BigDecimal.ZERO, BigDecimal.ONE); + } + /** * Returns the reference {@link LocalDateTime} as passed in the {@link ValueProviderInitialization}. *

Note: diff --git a/core/src/test/java/com/tngtech/valueprovider/ValueProviderTest.java b/core/src/test/java/com/tngtech/valueprovider/ValueProviderTest.java index 831c7d7..8af20e7 100644 --- a/core/src/test/java/com/tngtech/valueprovider/ValueProviderTest.java +++ b/core/src/test/java/com/tngtech/valueprovider/ValueProviderTest.java @@ -118,6 +118,7 @@ private static Collection allMethodInvocations() { invoke("url", address), invoke("bigDecimalNumber", min, max), invoke("bigIntegerNumber", BigInteger.valueOf(min), BigInteger.valueOf(max)), + invoke("bigDecimalPercentage"), invoke("intNumber", min, max), invoke("longNumber", min, max), invoke("fixedLocalDate"), @@ -502,6 +503,16 @@ void bigDecimalNumberWithScale_should_return_numbers_within_specified_range_as_l .isBetween(BigDecimal.valueOf(min - offsetForScale), BigDecimal.valueOf(max + offsetForScale)); } + @Test + void bigDecimalPercentage_should_return_numbers_within_0_and_1() { + ValueProvider random = withRandomValues(); + for (int i = 0; i < 1000; i++) { + BigDecimal percentage = random.bigDecimalPercentage(); + assertThat(percentage).isGreaterThanOrEqualTo(BigDecimal.ZERO); + assertThat(percentage).isLessThanOrEqualTo(BigDecimal.ONE); + } + } + @TestFactory List numericString_should_allow_restricting_min_and_max_in_addition_to_length() { return newArrayList(