From 6cf0cfeba93819b3692f5ccead1601ba6c83c590 Mon Sep 17 00:00:00 2001 From: Daniel Grounin Date: Sat, 5 Oct 2024 10:28:21 +0300 Subject: [PATCH] fix: add updated bigInt function with tests --- include/faker-cxx/number.h | 15 +++++++++++++++ src/modules/number.cpp | 19 +++++++++++++++++++ tests/modules/number_test.cpp | 31 +++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/include/faker-cxx/number.h b/include/faker-cxx/number.h index a989b8527..4891233a7 100644 --- a/include/faker-cxx/number.h +++ b/include/faker-cxx/number.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "faker-cxx/export.h" #include "faker-cxx/types/hex.h" @@ -272,4 +273,18 @@ FAKER_CXX_EXPORT std::string binary(int length = 1); * @endcode */ FAKER_CXX_EXPORT std::string binary(int min, int max); + +/** + * @brief Generates a random big integer number in the given range, bounds included. + * + * @param min The minimum value of the range. + * @param max The maximum value of the range. + * + * @return A random big integer number. + * + * @code + * faker::number::bigInt(1000000000, 9999999999) // Example output: 4593830193 + * @endcode + */ +FAKER_CXX_EXPORT std::optional bigInt(std::optional min = std::nullopt, std::optional max = std::nullopt); } diff --git a/src/modules/number.cpp b/src/modules/number.cpp index 76a3c0ba3..99f722b34 100644 --- a/src/modules/number.cpp +++ b/src/modules/number.cpp @@ -115,4 +115,23 @@ std::string binary(int min, int max) return "0b" + output; } + +std::optional bigInt(std::optional min, std::optional max) +{ + const long long DEFAULT_MAX = 9999999999999999LL; + const long long DEFAULT_MIN = 1LL; + + long long actualMin = min.value_or(DEFAULT_MIN); + long long actualMax = max.value_or(DEFAULT_MAX); + + if (actualMin > actualMax) + { + throw std::invalid_argument("min cannot be greater than max"); + } + + static std::mt19937_64 gen(std::random_device{}()); + std::uniform_int_distribution dist(actualMin, actualMax); + + return dist(gen); +} } diff --git a/tests/modules/number_test.cpp b/tests/modules/number_test.cpp index c097b9bfa..01c527d39 100644 --- a/tests/modules/number_test.cpp +++ b/tests/modules/number_test.cpp @@ -254,3 +254,34 @@ TEST_F(NumberTest, givenMinBiggerThanMax_shouldThrowInvalidArgument) { ASSERT_THROW(binary(10, 1), std::invalid_argument); } + +TEST_F(NumberTest, givenValidRange_shouldGenerateBigIntWithinRange) +{ + const long min = 1000000000000000L; + const long max = 9999999999999999L; + + const long actualRandomBigInt = bigInt(min, max).value(); + + ASSERT_GE(actualRandomBigInt, min); + ASSERT_LE(actualRandomBigInt, max); +} + +TEST_F(NumberTest, givenNegativeRange_shouldGenerateBigIntWithinRange) +{ + const long min = -1000000000000000L; + const long max = -1L; + + const long actualRandomBigInt = bigInt(min, max).value(); + + ASSERT_GE(actualRandomBigInt, min); + ASSERT_LE(actualRandomBigInt, max); +} + +TEST_F(NumberTest, givenSameRange_shouldGenerateExactBigInt) +{ + const long minMax = 5000000000000000L; + + const long actualRandomBigInt = bigInt(minMax, minMax).value(); + + ASSERT_EQ(actualRandomBigInt, minMax); +} \ No newline at end of file