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

Number improvements #1

Merged
merged 2 commits into from
Jul 5, 2023
Merged
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
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions include/faker-cxx/Helper.h
Original file line number Diff line number Diff line change
@@ -12,14 +12,14 @@ class Helper
template <class T>
static T arrayElement(const std::vector<T>& data)
{
const auto index = Number::integer(data.size() - 1);
const auto index = Number<size_t>::integer(data.size() - 1);

return data.at(index);
}

static char arrayElement(const std::string& data)
{
const auto index = Number::integer(static_cast<int>(data.size()) - 1);
const auto index = Number<size_t>::integer(static_cast<int>(data.size()) - 1);

return data.at(index);
}
52 changes: 50 additions & 2 deletions include/faker-cxx/Number.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,60 @@
#pragma once

#include <string>
#include <stdexcept>
#include <random>
#include <concepts>

namespace faker
{
template <std::integral T>
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<T> 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::integral T>
std::random_device Number<T>::randomDevice;

template <std::integral T>
std::mt19937 Number<T>::pseudoRandomGenerator(Number<T>::randomDevice());

}
11 changes: 5 additions & 6 deletions src/Internet.cpp
Original file line number Diff line number Diff line change
@@ -11,31 +11,30 @@ namespace faker
std::string Internet::username(std::optional<std::string> firstNameInit, std::optional<std::string> lastNameInit)
{
const auto firstName = firstNameInit ? *firstNameInit : Person::firstName();

const auto lastName = lastNameInit ? *lastNameInit : Person::lastName();

std::string username;

switch (Number::integer(4))
switch (Number<int>::integer(4))
{
case 0:
username = std::format("{}{}{}", firstName, lastName, Number::integer(99999));
username = std::format("{}{}{}", firstName, lastName, Number<int>::integer(99999));
break;
case 1:
username =
std::format("{}{}{}", firstName, Helper::arrayElement(std::vector<std::string>{".", "_", ""}), lastName);
break;
case 2:
username = std::format("{}{}{}{}", firstName, Helper::arrayElement(std::vector<std::string>{".", "_", ""}),
lastName, Number::integer(99));
lastName, Number<int>::integer(99));
break;
case 3:
username =
std::format("{}{}{}", lastName, Helper::arrayElement(std::vector<std::string>{".", "_", ""}), firstName);
break;
case 4:
username = std::format("{}{}{}{}", lastName, Helper::arrayElement(std::vector<std::string>{".", "_", ""}),
firstName, Number::integer(2023, 1960));
username = std::format("{}{}{}{}", lastName, Helper::arrayElement(std::vector<std::string> { ".", "_", "" }),
firstName, Number<int>::integer(1960, 2023));
}

return username;
23 changes: 0 additions & 23 deletions src/Number.cpp

This file was deleted.

15 changes: 12 additions & 3 deletions src/NumberTest.cpp
Original file line number Diff line number Diff line change
@@ -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<int>::integer(10, 2), std::invalid_argument);
}

TEST_F(NumberTest, givenRangeWithSameNumberSection_shouldGenerateThisNumber)
{
const auto actualRandomNumber = Number::integer(2, 2);
const auto actualRandomNumber = Number<int>::integer(2, 2);

ASSERT_EQ(actualRandomNumber, 2);
}

TEST_F(NumberTest, givenValidRangeArguments_shouldGenerateNumberThatIsInGivenRange)
{
const auto actualRandomNumber = Number::integer(10, 2);
const auto actualRandomNumber = Number<int>::integer(2, 10);

ASSERT_TRUE(actualRandomNumber >= 2);
ASSERT_TRUE(actualRandomNumber <= 10);
}

TEST_F(NumberTest, givenSingleArgument_shouldCorrectlyResolveToTwoArgsOverload)
{
const auto randomNumber = Number<int>::integer(10);


ASSERT_TRUE(randomNumber >= 0);
ASSERT_TRUE(randomNumber <= 10);
}