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

refactor: number module migrate #730

Merged
merged 3 commits into from
Jun 25, 2024
Merged
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
12 changes: 6 additions & 6 deletions include/faker-cxx/Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Helper
throw std::invalid_argument{"Data is empty."};
}

const auto index = Number::integer<size_t>(data.size() - 1);
const auto index = number::integer<size_t>(data.size() - 1);

return data[index];
}
Expand All @@ -52,7 +52,7 @@ class Helper
throw std::invalid_argument{"Data is empty."};
}

const auto index = Number::integer<size_t>(data.size() - 1);
const auto index = number::integer<size_t>(data.size() - 1);

return data[index];
}
Expand All @@ -67,7 +67,7 @@ class Helper
throw std::invalid_argument{"Range [start,end) is empty."};
}

const std::integral auto index = Number::integer<size_t>(size - 1);
const std::integral auto index = number::integer<size_t>(size - 1);

return start[index];
}
Expand All @@ -93,7 +93,7 @@ class Helper
throw std::invalid_argument{"Data is empty."};
}

const auto index = Number::integer<size_t>(data.size() - 1);
const auto index = number::integer<size_t>(data.size() - 1);

return data[index];
}
Expand All @@ -119,7 +119,7 @@ class Helper
throw std::invalid_argument{"Data is empty."};
}

const auto index = Number::integer<size_t>(data.size() - 1);
const auto index = number::integer<size_t>(data.size() - 1);

return *(data.begin() + index);
}
Expand Down Expand Up @@ -191,7 +191,7 @@ class Helper
throw std::invalid_argument{"Sum of weights is zero."};
}

const std::integral auto targetWeightValue = Number::integer<unsigned>(1, sumOfWeights);
const std::integral auto targetWeightValue = number::integer<unsigned>(1, sumOfWeights);

unsigned currentSum = 0;

Expand Down
23 changes: 9 additions & 14 deletions include/faker-cxx/Number.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
#include <random>
#include <stdexcept>

namespace faker
namespace faker::number
{
class Number
{
public:
namespace{
std::mt19937 pseudoRandomGenerator;
}
/**
* @brief Generates a random integer number in the given range, bounds included.
*
Expand All @@ -22,7 +22,7 @@ class Number
* @return T a random integer number
*/
template <std::integral I>
static I integer(I min, I max)
I integer(I min, I max)
{
if (min > max)
{
Expand All @@ -47,9 +47,9 @@ class Number
* @return T a random integer number
*/
template <std::integral I>
static I integer(I max)
I integer(I max)
{
return Number::integer<I>(static_cast<I>(0), max);
return integer<I>(static_cast<I>(0), max);
}

/**
Expand All @@ -65,7 +65,7 @@ class Number
* @return F a random decimal number.
*/
template <std::floating_point F>
static F decimal(F min, F max)
F decimal(F min, F max)
{
if (min > max)
{
Expand All @@ -90,13 +90,8 @@ class Number
* @return F, a random decimal number.
*/
template <std::floating_point F>
static F decimal(F max)
F decimal(F max)
{
return decimal<F>(static_cast<F>(0.), max);
}

private:
static std::random_device randomDevice;
static std::mt19937 pseudoRandomGenerator;
};
}
6 changes: 3 additions & 3 deletions src/modules/airline/Airline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Airport airport()

std::string seat(AircraftType aircraftType)
{
return std::to_string(Number::integer(1, aircraftTypeMaxRows.at(aircraftType))) +
return std::to_string(number::integer(1, aircraftTypeMaxRows.at(aircraftType))) +
Helper::arrayElement<char>(aircraftTypeSeatLetters.at(aircraftType));
}

Expand Down Expand Up @@ -60,9 +60,9 @@ std::string flightNumberByRange(bool addLeadingZeros, Range length)
{
if (addLeadingZeros)
{
return String::numeric(Number::integer(length.min, length.max), true);
return String::numeric(number::integer(length.min, length.max), true);
}

return String::numeric(Number::integer(length.min, length.max), false);
return String::numeric(number::integer(length.min, length.max), false);
}
}
56 changes: 28 additions & 28 deletions src/modules/color/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ std::string_view name()

std::string rgb(bool includeAlpha)
{
const std::integral auto red = Number::integer(255);
const std::integral auto green = Number::integer(255);
const std::integral auto blue = Number::integer(255);
const std::integral auto red = number::integer(255);
const std::integral auto green = number::integer(255);
const std::integral auto blue = number::integer(255);

if (!includeAlpha)
{
return FormatHelper::format("rgb({}, {}, {})", red, green, blue);
}

std::floating_point auto alpha = Number::decimal(1.0);
std::floating_point auto alpha = number::decimal(1.0);

return FormatHelper::format("rgba({}, {}, {}, {:.2f})", red, green, blue, alpha);
}
Expand All @@ -40,78 +40,78 @@ std::string hex(HexCasing casing, HexPrefix prefix, bool includeAlpha)

std::string hsl(bool include_alpha)
{
std::integral auto hue = Number::integer(360);
std::integral auto saturation = Number::integer(100);
std::integral auto lightness = Number::integer(100);
std::integral auto hue = number::integer(360);
std::integral auto saturation = number::integer(100);
std::integral auto lightness = number::integer(100);

if (!include_alpha)
{
return FormatHelper::format("hsl({}, {}, {})", hue, saturation, lightness);
}

std::floating_point auto alpha = Number::decimal(1.0);
std::floating_point auto alpha = number::decimal(1.0);

return FormatHelper::format("hsla({}, {}, {}, {:.2f})", hue, saturation, lightness, alpha);
}

std::string lch(bool include_alpha)
{
std::integral auto luminance = Number::integer(100);
std::integral auto chroma = Number::integer(100);
std::integral auto hue = Number::integer(360);
std::integral auto luminance = number::integer(100);
std::integral auto chroma = number::integer(100);
std::integral auto hue = number::integer(360);

if (!include_alpha)
{
return FormatHelper::format("lch({}, {}, {})", luminance, chroma, hue);
}

std::floating_point auto alpha = Number::decimal(1.0);
std::floating_point auto alpha = number::decimal(1.0);

return FormatHelper::format("lcha({}, {}, {}, {:.2f})", luminance, chroma, hue, alpha);
}

std::string cmyk()
{
std::floating_point auto cyan = Number::decimal(1.);
std::floating_point auto magenta = Number::decimal(1.);
std::floating_point auto yellow = Number::decimal(1.);
std::floating_point auto key = Number::decimal(1.);
std::floating_point auto cyan = number::decimal(1.);
std::floating_point auto magenta = number::decimal(1.);
std::floating_point auto yellow = number::decimal(1.);
std::floating_point auto key = number::decimal(1.);

return FormatHelper::format("cmyk({:.2f}, {:.2f}, {:.2f}, {:.2f})", cyan, magenta, yellow, key);
}

std::string lab()
{
std::floating_point auto lightness = Number::decimal(100.0);
std::floating_point auto red_green = Number::decimal(-128.0, 128.0);
std::floating_point auto blue_yellow = Number::decimal(-128.0, 128.0);
std::floating_point auto lightness = number::decimal(100.0);
std::floating_point auto red_green = number::decimal(-128.0, 128.0);
std::floating_point auto blue_yellow = number::decimal(-128.0, 128.0);

return FormatHelper::format("lab({:.2f}, {:.2f}, {:.2f})", lightness, red_green, blue_yellow);
}

std::string hsb()
{
std::integral auto hue = Number::integer(360);
std::integral auto saturation = Number::integer(100);
std::integral auto brightness = Number::integer(100);
std::integral auto hue = number::integer(360);
std::integral auto saturation = number::integer(100);
std::integral auto brightness = number::integer(100);

return FormatHelper::format("hsb({}, {}, {})", hue, saturation, brightness);
}

std::string hsv()
{
std::integral auto hue = Number::integer(360);
std::integral auto saturation = Number::integer(100);
std::integral auto value = Number::integer(100);
std::integral auto hue = number::integer(360);
std::integral auto saturation = number::integer(100);
std::integral auto value = number::integer(100);

return FormatHelper::format("hsv({}, {}, {})", hue, saturation, value);
}

std::string yuv()
{
std::integral auto luminance = Number::integer(255);
std::integral auto chrominance_blue = Number::integer(255);
std::integral auto chrominance_red = Number::integer(255);
std::integral auto luminance = number::integer(255);
std::integral auto chrominance_blue = number::integer(255);
std::integral auto chrominance_red = number::integer(255);

return FormatHelper::format("yuv({}, {}, {})", luminance, chrominance_blue, chrominance_red);
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/company/Company.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ std::string name()
{
std::string companyName;

switch (Number::integer<int>(3))
switch (number::integer<int>(3))
{
case 0:
companyName = FormatHelper::format("{} {}", Person::lastName(), Helper::arrayElement(companySuffixes));
Expand Down
6 changes: 3 additions & 3 deletions src/modules/datatype/Datatype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace faker::datatype
{
bool boolean()
{
return Number::decimal<float>(0.f, 1.f) > 0.5f;
return number::decimal<float>(0.f, 1.f) > 0.5f;
}

bool boolean(double probability)
Expand All @@ -27,9 +27,9 @@ bool boolean(double probability)
return true;
}

return Number::decimal(0., 1.) < prob;
return number::decimal(0., 1.) < prob;
}

return Number::decimal(0., 1.) < 0.5;
return number::decimal(0., 1.) < 0.5;
}
}
16 changes: 8 additions & 8 deletions src/modules/date/Date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ std::string betweenDate(const auto& from, const auto& to, DateFormat dateFormat)

const auto size = std::chrono::duration_cast<std::chrono::seconds>(to - from).count();

const auto randomDateWithinRange = from + std::chrono::seconds{Number::integer(size - 1)};
const auto randomDateWithinRange = from + std::chrono::seconds{number::integer(size - 1)};

return serializeTimePoint(randomDateWithinRange, dateFormat);
}
Expand Down Expand Up @@ -155,27 +155,27 @@ unsigned int year()
unsigned minYear = 1950;
unsigned maxYear = 2050;

return Number::integer<unsigned>(minYear, maxYear);
return number::integer<unsigned>(minYear, maxYear);
}

unsigned int month()
{
return Number::integer<unsigned>(1, 12);
return number::integer<unsigned>(1, 12);
}

unsigned int hour()
{
return Number::integer<unsigned>(0, 23);
return number::integer<unsigned>(0, 23);
}

unsigned int minute()
{
return Number::integer<unsigned>(0, 59);
return number::integer<unsigned>(0, 59);
}

unsigned int second()
{
return Number::integer<unsigned>(0, 59);
return number::integer<unsigned>(0, 59);
}

std::string time()
Expand All @@ -185,12 +185,12 @@ std::string time()

unsigned int dayOfMonth()
{
return Number::integer<unsigned>(1, 31);
return number::integer<unsigned>(1, 31);
}

unsigned dayOfWeek()
{
return Number::integer<unsigned>(1, 7);
return number::integer<unsigned>(1, 7);
}

std::string_view timezoneRandom()
Expand Down
6 changes: 3 additions & 3 deletions src/modules/finance/Finance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ std::string_view Finance::accountType()

std::string Finance::amount(double min, double max, Precision precision, const std::string& symbol)
{
const std::floating_point auto generatedNumber = Number::decimal<double>(min, max);
const std::floating_point auto generatedNumber = number::decimal<double>(min, max);

std::string result{symbol};

Expand Down Expand Up @@ -139,7 +139,7 @@ std::string Finance::creditCardCvv()

std::string Finance::bitcoinAddress()
{
const unsigned addressLength = Number::integer(26u, 33u);
const unsigned addressLength = number::integer(26u, 33u);

auto address = Helper::arrayElement(std::vector<std::string>{"1", "3"});

Expand All @@ -150,7 +150,7 @@ std::string Finance::bitcoinAddress()

std::string Finance::litecoinAddress()
{
const unsigned addressLength = Number::integer(26u, 33u);
const unsigned addressLength = number::integer(26u, 33u);

auto address = Helper::arrayElement(std::vector<std::string>{"L", "M", "3"});

Expand Down
Loading
Loading