Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add bigInt function with comprehensive tests #949

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions include/faker-cxx/number.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <optional>
#include <random>
#include <stdexcept>
#include <optional>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional is already included above


#include "faker-cxx/export.h"
#include "faker-cxx/types/hex.h"
Expand Down Expand Up @@ -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<long long> bigInt(std::optional<long long> min = std::nullopt, std::optional<long long> max = std::nullopt);
}
19 changes: 19 additions & 0 deletions src/modules/number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,23 @@ std::string binary(int min, int max)

return "0b" + output;
}

std::optional<long long> bigInt(std::optional<long long> min, std::optional<long long> 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<long long> dist(actualMin, actualMax);

return dist(gen);
}
}
31 changes: 31 additions & 0 deletions tests/modules/number_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Loading