Skip to content

Commit

Permalink
refactor project structure into modules (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal authored Jul 19, 2023
1 parent 2829fc2 commit 7eaa93a
Show file tree
Hide file tree
Showing 77 changed files with 248 additions and 216 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ IncludeCategories:
- Regex: '^"(gtest)/'
Priority: 2
- Regex: '^"Aa'
Priority: 3
Priority: 5
- Regex: '".+"'
Priority: 10
62 changes: 31 additions & 31 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,40 @@ endif ()
set(LIBRARY_NAME faker-cxx)

set(FAKER_SOURCES
src/Book.cpp
src/Color.cpp
src/Commerce.cpp
src/Company.cpp
src/Datatype.cpp
src/Date.cpp
src/Finance.cpp
src/Helper.cpp
src/Internet.cpp
src/Location.cpp
src/Lorem.cpp
src/Number.cpp
src/Person.cpp
src/String.cpp
src/StringHelper.cpp
src/Word.cpp
src/modules/book/Book.cpp
src/modules/color/Color.cpp
src/modules/commerce/Commerce.cpp
src/modules/company/Company.cpp
src/modules/datatype/Datatype.cpp
src/modules/date/Date.cpp
src/modules/finance/Finance.cpp
src/modules/helper/Helper.cpp
src/modules/internet/Internet.cpp
src/modules/location/Location.cpp
src/modules/lorem/Lorem.cpp
src/modules/number/Number.cpp
src/modules/person/Person.cpp
src/modules/string/String.cpp
src/modules/word/Word.cpp
src/common/StringHelper.cpp
)

set(FAKER_UT_SOURCES
src/BookTest.cpp
src/ColorTest.cpp
src/CommerceTest.cpp
src/CompanyTest.cpp
src/DatatypeTest.cpp
src/DateTest.cpp
src/FinanceTest.cpp
src/InternetTest.cpp
src/LocationTest.cpp
src/LoremTest.cpp
src/NumberTest.cpp
src/PersonTest.cpp
src/StringTest.cpp
src/StringHelperTest.cpp
src/WordTest.cpp
src/modules/book/BookTest.cpp
src/modules/color/ColorTest.cpp
src/modules/commerce/CommerceTest.cpp
src/modules/company/CompanyTest.cpp
src/modules/datatype/DatatypeTest.cpp
src/modules/date/DateTest.cpp
src/modules/finance/FinanceTest.cpp
src/modules/internet/InternetTest.cpp
src/modules/location/LocationTest.cpp
src/modules/lorem/LoremTest.cpp
src/modules/number/NumberTest.cpp
src/modules/person/PersonTest.cpp
src/modules/string/StringTest.cpp
src/modules/word/WordTest.cpp
src/common/StringHelperTest.cpp
)

add_library(${LIBRARY_NAME} ${FAKER_SOURCES})
Expand Down
168 changes: 93 additions & 75 deletions include/faker-cxx/Number.h
Original file line number Diff line number Diff line change
@@ -1,85 +1,86 @@
#pragma once

#include <algorithm>
#include <concepts>
#include <random>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
#include <algorithm>

namespace faker {
namespace faker
{
/**
* @brief A concept that checks if a type is a valid distribution.
*
* The concept enforces a good number of the requirements specified for a RandomNumberDistribution in the C++ standard library.
* We check that the provided distribution has all the necessary member functions and nested types,
* and that it is equality comparable, copy constructible and copy assignable.
* The concept enforces a good number of the requirements specified for a RandomNumberDistribution in the C++ standard
* library. We check that the provided distribution has all the necessary member functions and nested types, and that it
* is equality comparable, copy constructible and copy assignable.
*
* This provides compile-time guarantees that the distribution is valid and can be used with the Number module.
*
* @see https://en.cppreference.com/w/cpp/named_req/RandomNumberDistribution
* @tparam DISTRIBUTION a distribution type, such as std::uniform_int_distribution.
*/
template <class D>
concept Distribution = requires {
// check that the result of the distribution is the same as the result of the operator()
// also checks for existence of the result_type and param_type nested types, as well as the operator()
requires std::is_same_v<typename D::result_type,
decltype(std::declval<D>()(
std::declval<typename D::param_type&>()))>;
concept Distribution =
requires {
// check that the result of the distribution is the same as the result of the operator()
// also checks for existence of the result_type and param_type nested types, as well as the operator()
requires std::is_same_v<typename D::result_type,
decltype(std::declval<D>()(std::declval<typename D::param_type&>()))>;

// check that the distribution has a min(), max() and reset() member functions
{
std::declval<D>().min()
} -> std::same_as<typename D::result_type>;
{
std::declval<D>().max()
} -> std::same_as<typename D::result_type>;
{
std::declval<D>().reset()
} -> std::same_as<void>;
// check that the distribution has a min(), max() and reset() member functions
{
std::declval<D>().min()
} -> std::same_as<typename D::result_type>;
{
std::declval<D>().max()
} -> std::same_as<typename D::result_type>;
{
std::declval<D>().reset()
} -> std::same_as<void>;

// check that the distribution is equality comparable, copy constructible and copy assignable
{
std::equality_comparable<D>
};
{
std::is_copy_constructible_v<D>
};
{
std::is_copy_assignable_v<D>
};
// check that the parameter type is equality comparable, copy constructible and copy assignable
{
std::equality_comparable<typename D::param_type>
};
{
std::is_copy_constructible_v<typename D::param_type>
};
{
std::is_copy_assignable_v<typename D::param_type>
};
// check that the distribution is equality comparable, copy constructible and copy assignable
{
std::equality_comparable<D>
};
{
std::is_copy_constructible_v<D>
};
{
std::is_copy_assignable_v<D>
};
// check that the parameter type is equality comparable, copy constructible and copy assignable
{
std::equality_comparable<typename D::param_type>
};
{
std::is_copy_constructible_v<typename D::param_type>
};
{
std::is_copy_assignable_v<typename D::param_type>
};

// Check that the distribution has a param() -> param_type
// and param(const param_type&) member functions
{
std::declval<D>().param()
} -> std::same_as<typename D::param_type>;
{
std::declval<D>().param(std::declval<typename D::param_type&>())
} -> std::same_as<void>;
// Check that the distribution has a param() -> param_type
// and param(const param_type&) member functions
{
std::declval<D>().param()
} -> std::same_as<typename D::param_type>;
{
std::declval<D>().param(std::declval<typename D::param_type&>())
} -> std::same_as<void>;

// check that D is printable to std::ostream
{
std::declval<std::ostream&>() << std::declval<D>()
} -> std::same_as<std::ostream&>;
// check that D is printable to std::ostream
{
std::declval<std::ostream&>() << std::declval<D>()
} -> std::same_as<std::ostream&>;

// check that D can receive a value from std::istream
{
std::declval<std::istream&>() >> std::declval<D&>()
} -> std::same_as<std::istream&>;
};
// check that D can receive a value from std::istream
{
std::declval<std::istream&>() >> std::declval<D&>()
} -> std::same_as<std::istream&>;
};

/**
* @brief A concept that checks if a type is a valid integer distribution.
Expand Down Expand Up @@ -115,7 +116,8 @@ class Number
template <std::integral I>
static I integer(I min, I max)
{
if (min > max) {
if (min > max)
{
throw std::invalid_argument("Minimum value must be smaller than maximum value.");
}

Expand All @@ -127,7 +129,8 @@ class Number
/**
* @brief Generates a random integer between 0 and the given maximum value, bounds included.
*
* The function invokes the integer<I>(I, I) function with min = 0, hence the distribution used is std::uniform_int_distribution.
* The function invokes the integer<I>(I, I) function with min = 0, hence the distribution used is
* std::uniform_int_distribution.
*
* @tparam I the type of the generated number, must be an integral type (int, long, long long, etc.).
* @param max the maximum value of the range.
Expand All @@ -152,7 +155,8 @@ class Number
* especially for long tailed distributions.
*
* @tparam I the type of the generated number, must be an integral type (int, long, long long, etc.).
* @tparam D the type of the distribution, must be a valid integer distribution (std::uniform_int_distribution, std::binomial_distribution, etc.).
* @tparam D the type of the distribution, must be a valid integer distribution (std::uniform_int_distribution,
* std::binomial_distribution, etc.).
*
* @throws std::invalid_argument if min is greater than max.
*
Expand All @@ -165,11 +169,13 @@ class Number
template <std::integral I, IntegerDistribution D>
static I integer(D distribution, I min, I max)
{
if constexpr(std::is_same_v<D, std::uniform_int_distribution<I>>) {
if constexpr (std::is_same_v<D, std::uniform_int_distribution<I>>)
{
return Number::integer<I>(min, max);
}
{
if (min > max) {
if (min > max)
{
throw std::invalid_argument("Minimum value must be smaller than maximum value.");
}

Expand All @@ -178,12 +184,14 @@ class Number
}

/**
* @brief Generates a random integer between 0 and the given maximum value with a given distribution, bounds included.
* @brief Generates a random integer between 0 and the given maximum value with a given distribution, bounds
* included.
*
* Defaults to a min value of 0.
*
* @tparam I The type of the generated number, must be an integral type (int, long, long long, etc.).
* @tparam D The type of the distribution, must be a valid integer distribution (std::uniform_int_distribution, std::binomial_distribution, etc.).
* @tparam D The type of the distribution, must be a valid integer distribution (std::uniform_int_distribution,
* std::binomial_distribution, etc.).
* @param distribution The distribution to use.
* @param max The maximum value of the range.
*
Expand All @@ -204,7 +212,8 @@ class Number
* @brief Generates a random integer, unbounded, with a given distribution.
*
* @tparam I The type of the generated number, must be an integral type (int, long, long long, etc.).
* @tparam D The type of the distribution, must be a valid integer distribution (std::uniform_int_distribution, std::binomial_distribution, etc.).
* @tparam D The type of the distribution, must be a valid integer distribution (std::uniform_int_distribution,
* std::binomial_distribution, etc.).
*
* @param distribution The distribution to use.
*
Expand All @@ -221,7 +230,8 @@ class Number
* @brief Generates a random decimal number in the given range, bounds included.
*
* @tparam F the type of the generated number, must be a floating point type (float, double, long double).
* @tparam D the type of the distribution, must be a valid float distribution (std::uniform_real_distribution, std::normal_distribution, etc.).
* @tparam D the type of the distribution, must be a valid float distribution (std::uniform_real_distribution,
* std::normal_distribution, etc.).
*
* @param min The minimum value of the range.
* @param max The maximum value of the range.
Expand All @@ -234,7 +244,8 @@ class Number
template <std::floating_point F>
static F decimal(F min, F max)
{
if (min > max) {
if (min > max)
{
throw std::invalid_argument("Minimum value must be smaller than maximum value.");
}

Expand All @@ -250,7 +261,8 @@ class Number
* through a std::clamp call, hence the statistical properties of the distribution may be altered,
* especially for long tailed distributions.
*
* @tparam D the type of the distribution, must be a valid decimal distribution (std::uniform_real_distribution, std::normal_distribution, etc.).
* @tparam D the type of the distribution, must be a valid decimal distribution (std::uniform_real_distribution,
* std::normal_distribution, etc.).
* @tparam F the type of the generated number, must be a floating point type (float, double, long double).
*
* @throws std::invalid_argument if min is greater than max.
Expand All @@ -266,11 +278,13 @@ class Number
template <std::floating_point F, DecimalDistribution D>
static F decimal(D distribution, F min, F max)
{
if constexpr(std::is_same_v<D, std::uniform_real_distribution<F>>) {
if constexpr (std::is_same_v<D, std::uniform_real_distribution<F>>)
{
return Number::decimal<F>(min, max);
}
{
if (min > max) {
if (min > max)
{
throw std::invalid_argument("Minimum value must be smaller than maximum value.");
}

Expand All @@ -281,7 +295,8 @@ class Number
/**
* @brief Generates a random decimal number between 0 and the given maximum value, bounds included.
*
* The function invokes the decimal<F, D>(F, F) function with min = 0, hence the distribution used is std::uniform_real_distribution.
* The function invokes the decimal<F, D>(F, F) function with min = 0, hence the distribution used is
* std::uniform_real_distribution.
*
* @tparam F The type of the generated number, must be a floating point type (float, double, long double).
* @param max The maximum value of the range.
Expand All @@ -299,10 +314,12 @@ class Number
}

/**
* @brief Generates a random decimal number between 0 and the given maximum value with a given distribution, bounds included.
* @brief Generates a random decimal number between 0 and the given maximum value with a given distribution, bounds
* included.
*
* @tparam F The type of the generated number, must be a floating point type (float, double, long double).
* @tparam D The type of the distribution, must be a valid float distribution (std::uniform_real_distribution, std::normal_distribution, etc.).
* @tparam D The type of the distribution, must be a valid float distribution (std::uniform_real_distribution,
* std::normal_distribution, etc.).
*
* @param distribution The distribution to use.
* @param max The maximum value of the range.
Expand All @@ -325,7 +342,8 @@ class Number
* @brief Generates a random decimal number in the given range, bounds included.
*
* @tparam F the type of the generated number, must be a floating point type (float, double, long double).
* @tparam D the type of the distribution, must be a valid float distribution (std::uniform_real_distribution, std::normal_distribution, etc.).
* @tparam D the type of the distribution, must be a valid float distribution (std::uniform_real_distribution,
* std::normal_distribution, etc.).
*
* @see DecimalDistribution
*
Expand Down
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion src/StringHelperTest.cpp → src/common/StringHelperTest.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "StringHelper.h"

#include "gtest/gtest.h"
#include <algorithm>

#include "gtest/gtest.h"

using namespace ::testing;
using namespace faker;

Expand Down
Loading

0 comments on commit 7eaa93a

Please sign in to comment.