diff --git a/include/faker-cxx/Datatype.h b/include/faker-cxx/Datatype.h index b9613f043..383e8b08f 100644 --- a/include/faker-cxx/Datatype.h +++ b/include/faker-cxx/Datatype.h @@ -1,39 +1,35 @@ #pragma once -namespace faker +namespace faker::datatype { -class Datatype -{ -public: - /** - * @brief Returns a random boolean. - * - * @returns Boolean. - * - * @code - * Datatype::boolean() // "false" - * @endcode - */ - static bool boolean(); +/** + * @brief Returns a random boolean. + * + * @returns Boolean. + * + * @code + * datatype::boolean() // "false" + * @endcode + */ +bool boolean(); - /** - * @brief Returns a random boolean. - * **Note:** - * A probability of `0.75` results in `true` being returned `75%` of the calls; likewise `0.3` => `30%`. - * If the probability is `<= 0.0`, it will always return `false`. - * If the probability is `>= 1.0`, it will always return `true`. - * The probability is limited to two decimal places. - * - * @param probability The probability (`[0.00, 1.00]`) of returning `true`. - * - * @returns Boolean. - * - * @code - * Datatype::boolean() // "false" - * Datatype::boolean(0.9) // "true" - * Datatype::boolean(0.1) // "false" - * @endcode - */ - static bool boolean(double probability); -}; +/** + * @brief Returns a random boolean. + * **Note:** + * A probability of `0.75` results in `true` being returned `75%` of the calls; likewise `0.3` => `30%`. + * If the probability is `<= 0.0`, it will always return `false`. + * If the probability is `>= 1.0`, it will always return `true`. + * The probability is limited to two decimal places. + * + * @param probability The probability (`[0.00, 1.00]`) of returning `true`. + * + * @returns Boolean. + * + * @code + * datatype::boolean() // "false" + * datatype::boolean(0.9) // "true" + * datatype::boolean(0.1) // "false" + * @endcode + */ +bool boolean(double probability); } diff --git a/include/faker-cxx/Helper.h b/include/faker-cxx/Helper.h index 093910151..9bf118d29 100644 --- a/include/faker-cxx/Helper.h +++ b/include/faker-cxx/Helper.h @@ -60,7 +60,7 @@ class Helper template static auto arrayElement(It start, It end) -> decltype(*::std::declval()) { - size_t size = static_cast(end - start); + auto size = static_cast(end - start); if (size == 0) { @@ -282,7 +282,7 @@ class Helper template static TResult maybe(std::function callback, double probability = 0.5) { - if (Datatype::boolean(probability)) + if (datatype::boolean(probability)) { return callback(); } diff --git a/src/modules/datatype/Datatype.cpp b/src/modules/datatype/Datatype.cpp index f800392fa..260896d11 100644 --- a/src/modules/datatype/Datatype.cpp +++ b/src/modules/datatype/Datatype.cpp @@ -4,14 +4,14 @@ #include "faker-cxx/Number.h" -namespace faker +namespace faker::datatype { -bool Datatype::boolean() +bool boolean() { return Number::decimal(0.f, 1.f) > 0.5f; } -bool Datatype::boolean(double probability) +bool boolean(double probability) { if (probability != nan("")) { diff --git a/src/modules/system/System.cpp b/src/modules/system/System.cpp index 72b0c7a49..f2f6083c8 100644 --- a/src/modules/system/System.cpp +++ b/src/modules/system/System.cpp @@ -173,9 +173,9 @@ std::string filePath() std::string semver() { - int major = Number::integer(9); - int minor = Number::integer(9); - int patch = Number::integer(9); + const int major = Number::integer(9); + const int minor = Number::integer(9); + const int patch = Number::integer(9); return FormatHelper::format("{}.{}.{}", major, minor, patch); } @@ -253,14 +253,15 @@ std::string cron(const CronOptions& options) years = {std::to_string(Number::integer(1970, 2099)), "*"}; } - auto minute = Helper::arrayElement(minutes); - auto hour = Helper::arrayElement(hours); - auto day = Helper::arrayElement(days); - auto month = Helper::arrayElement(months); - auto dayOfWeek = Helper::arrayElement(daysOfWeek); - auto year = Helper::arrayElement(years); + const auto minute = Helper::arrayElement(minutes); + const auto hour = Helper::arrayElement(hours); + const auto day = Helper::arrayElement(days); + const auto month = Helper::arrayElement(months); + const auto dayOfWeek = Helper::arrayElement(daysOfWeek); + const auto year = Helper::arrayElement(years); std::string standardExpression = minute + " " + hour + " " + day + " " + month + " " + dayOfWeek; + if (includeYear) { standardExpression += " " + year; @@ -269,7 +270,7 @@ std::string cron(const CronOptions& options) std::vector nonStandardExpressions = {"@annually", "@daily", "@hourly", "@monthly", "@reboot", "@weekly", "@yearly"}; - return (!includeNonStandard || Datatype::boolean(0)) ? standardExpression : + return (!includeNonStandard || datatype::boolean(0)) ? standardExpression : Helper::arrayElement(nonStandardExpressions); } } diff --git a/tests/modules/datatype/DatatypeTest.cpp b/tests/modules/datatype/DatatypeTest.cpp index 8ab80c003..39bf18618 100644 --- a/tests/modules/datatype/DatatypeTest.cpp +++ b/tests/modules/datatype/DatatypeTest.cpp @@ -6,7 +6,7 @@ #include "gtest/gtest.h" using namespace ::testing; -using namespace faker; +using namespace faker::datatype; class DatatypeTest : public Test { @@ -17,22 +17,23 @@ TEST_F(DatatypeTest, shouldGenerateBoolean) { std::vector booleanValues{false, true}; - const auto boolean = Datatype::boolean(); + const auto generatedBoolean = boolean(); - ASSERT_TRUE(std::ranges::any_of(booleanValues, [boolean](bool booleanValue) { return boolean == booleanValue; })); + ASSERT_TRUE(std::ranges::any_of(booleanValues, [generatedBoolean](bool booleanValue) + { return generatedBoolean == booleanValue; })); } TEST_F(DatatypeTest, BooleanWithProbTest) { - const auto result2 = Datatype::boolean(0.3); + const auto result2 = boolean(0.3); EXPECT_TRUE(result2 || !result2); - const auto result3 = Datatype::boolean(0.8); + const auto result3 = boolean(0.8); EXPECT_TRUE(result3 || !result3); - const auto result4 = Datatype::boolean(0.0); + const auto result4 = boolean(0.0); EXPECT_FALSE(result4); - const auto result5 = Datatype::boolean(1.0); + const auto result5 = boolean(1.0); EXPECT_TRUE(result5); }