diff --git a/CMakeLists.txt b/CMakeLists.txt index 4edde409e..5389dac89 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,8 @@ project(${PROJECT_NAME} CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED YES) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror") + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -Wconversion -Wformat -Werror") find_package(Boost REQUIRED) find_package(GTest REQUIRED) @@ -22,7 +23,6 @@ set(LIBRARY_NAME faker-cxx) set(SOURCES src/Person.cpp src/Internet.cpp - src/Number.cpp src/String.cpp src/Lorem.cpp src/Datatype.cpp diff --git a/include/faker-cxx/Helper.h b/include/faker-cxx/Helper.h index 45f40c8ca..a2fb05e00 100644 --- a/include/faker-cxx/Helper.h +++ b/include/faker-cxx/Helper.h @@ -12,14 +12,14 @@ class Helper template static T arrayElement(const std::vector& data) { - const auto index = Number::integer(data.size() - 1); + const auto index = Number::integer(data.size() - 1); return data.at(index); } static char arrayElement(const std::string& data) { - const auto index = Number::integer(static_cast(data.size()) - 1); + const auto index = Number::integer(static_cast(data.size()) - 1); return data.at(index); } diff --git a/include/faker-cxx/Number.h b/include/faker-cxx/Number.h index 59fa333f4..e18d711d7 100644 --- a/include/faker-cxx/Number.h +++ b/include/faker-cxx/Number.h @@ -1,12 +1,60 @@ #pragma once #include +#include +#include +#include namespace faker { +template class Number { + +private: + static std::random_device randomDevice; + static std::mt19937 pseudoRandomGenerator; + + public: - static int integer(int max = 10000, int min = 0); + + /** + * @brief Generates a random 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 T the type of the generated number, must be an integral type + */ + static T integer(T min, T max) + { + if (min > max) + { + throw std::invalid_argument("Minimum value must be smaller than maximum value."); + } + + std::uniform_int_distribution distribution(min, max); + + return distribution(pseudoRandomGenerator); + } + + /** + * @brief Generates a random integer between 0 and the given maximum value, bounds included. + * + * @param max the maximum value of the range + * @return T the type of the generated number, must be an integral type + */ + static T integer(T max) + { + return integer(0, max); + } + + }; -} + +template +std::random_device Number::randomDevice; + +template +std::mt19937 Number::pseudoRandomGenerator(Number::randomDevice()); + +} \ No newline at end of file diff --git a/src/Internet.cpp b/src/Internet.cpp index 372644639..c440548db 100644 --- a/src/Internet.cpp +++ b/src/Internet.cpp @@ -11,15 +11,14 @@ namespace faker std::string Internet::username(std::optional firstNameInit, std::optional lastNameInit) { const auto firstName = firstNameInit ? *firstNameInit : Person::firstName(); - const auto lastName = lastNameInit ? *lastNameInit : Person::lastName(); std::string username; - switch (Number::integer(4)) + switch (Number::integer(4)) { case 0: - username = std::format("{}{}{}", firstName, lastName, Number::integer(99999)); + username = std::format("{}{}{}", firstName, lastName, Number::integer(99999)); break; case 1: username = @@ -27,15 +26,15 @@ std::string Internet::username(std::optional firstNameInit, std::op break; case 2: username = std::format("{}{}{}{}", firstName, Helper::arrayElement(std::vector{".", "_", ""}), - lastName, Number::integer(99)); + lastName, Number::integer(99)); break; case 3: username = std::format("{}{}{}", lastName, Helper::arrayElement(std::vector{".", "_", ""}), firstName); break; case 4: - username = std::format("{}{}{}{}", lastName, Helper::arrayElement(std::vector{".", "_", ""}), - firstName, Number::integer(2023, 1960)); + username = std::format("{}{}{}{}", lastName, Helper::arrayElement(std::vector { ".", "_", "" }), + firstName, Number::integer(1960, 2023)); } return username; diff --git a/src/Number.cpp b/src/Number.cpp deleted file mode 100644 index 65d58bd72..000000000 --- a/src/Number.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "Number.h" - -#include -#include - -namespace faker -{ -int Number::integer(int max, int min) -{ - if (min > max) - { - throw std::invalid_argument("Minimum value must be smaller than maximum value."); - } - - std::random_device randomDevice; - - std::mt19937 pseudoRandomGenerator{randomDevice()}; - - std::uniform_int_distribution distribution(min, max); - - return distribution(pseudoRandomGenerator); -} -} diff --git a/src/NumberTest.cpp b/src/NumberTest.cpp index 9d9bdb21a..5ebeef061 100644 --- a/src/NumberTest.cpp +++ b/src/NumberTest.cpp @@ -12,20 +12,29 @@ class NumberTest : public Test TEST_F(NumberTest, givenInvalidRangeArguments_shouldThrowInvalidArgument) { - ASSERT_THROW(Number::integer(2, 10), std::invalid_argument); + ASSERT_THROW(Number::integer(10, 2), std::invalid_argument); } TEST_F(NumberTest, givenRangeWithSameNumberSection_shouldGenerateThisNumber) { - const auto actualRandomNumber = Number::integer(2, 2); + const auto actualRandomNumber = Number::integer(2, 2); ASSERT_EQ(actualRandomNumber, 2); } TEST_F(NumberTest, givenValidRangeArguments_shouldGenerateNumberThatIsInGivenRange) { - const auto actualRandomNumber = Number::integer(10, 2); + const auto actualRandomNumber = Number::integer(2, 10); ASSERT_TRUE(actualRandomNumber >= 2); ASSERT_TRUE(actualRandomNumber <= 10); } + +TEST_F(NumberTest, givenSingleArgument_shouldCorrectlyResolveToTwoArgsOverload) +{ + const auto randomNumber = Number::integer(10); + + + ASSERT_TRUE(randomNumber >= 0); + ASSERT_TRUE(randomNumber <= 10); +} \ No newline at end of file