Skip to content

Commit

Permalink
Merge branch 'main' into zodiacSigns
Browse files Browse the repository at this point in the history
  • Loading branch information
ViviGayming authored Nov 12, 2023
2 parents 5e31c96 + d43d6ea commit d2e5e4e
Show file tree
Hide file tree
Showing 75 changed files with 2,434 additions and 1,775 deletions.
53 changes: 53 additions & 0 deletions include/faker-cxx/Helper.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <functional>
#include <numeric>
#include <span>
#include <string>

Expand All @@ -9,6 +10,7 @@

namespace faker
{
// TODO: Add error handling if data empty
class Helper
{
public:
Expand All @@ -29,6 +31,11 @@ class Helper
template <class T>
static T arrayElement(std::span<const T> data)
{
if (data.empty())
{
throw std::invalid_argument{"Data is empty."};
}

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

return data[index];
Expand All @@ -37,11 +44,57 @@ class Helper
template <class T>
static T arrayElement(const std::vector<T>& data)
{
if (data.empty())
{
throw std::invalid_argument{"Data is empty."};
}

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

return data[index];
}

template <class T>
struct WeightedElement
{
unsigned weight;
T value;
};

template <class T>
static T weightedArrayElement(const std::vector<WeightedElement<T>>& data)
{
if (data.empty())
{
throw std::invalid_argument{"Data is empty."};
}

const auto sumOfWeights =
std::accumulate(data.begin(), data.end(), 0u,
[](size_t sum, const WeightedElement<T>& element) { return sum + element.weight; });

if (sumOfWeights == 0)
{
throw std::invalid_argument{"Sum of weights is zero."};
}

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

unsigned currentSum = 0;

for (const auto& element : data)
{
currentSum += element.weight;

if (targetWeightValue <= currentSum)
{
return element.value;
}
}

return data.at(data.size() - 1).value;
}

/**
* @brief Returns shuffled STL container.
*
Expand Down
26 changes: 26 additions & 0 deletions include/faker-cxx/Person.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Person
* @brief Returns a random last name.
*
* @param language The local name language. Defaults to `Language::English`.
* @param sex The optional sex to use.
*
* @returns Last name starting with a capital letter.
*
Expand All @@ -40,10 +41,24 @@ class Person
*/
static std::string lastName(Language language = Language::English, std::optional<Sex> = std::nullopt);

/**
* @brief Returns a random middle name.
*
* @param sex The optional sex to use.
*
* @returns Middle name starting with a capital letter.
*
* @code
* Person::middleName() // "Васильевич"
* @endcode
*/
static std::string middleName(std::optional<Sex> = std::nullopt);

/**
* @brief Returns a random full name.
*
* @param language The local name language. Defaults to `Language::English`.
* @param sex The optional sex to use.
*
* @returns Full name starting with first name.
*
Expand All @@ -70,6 +85,17 @@ class Person
*/
static std::string prefix(std::optional<Sex> = std::nullopt);

/**
* @brief Returns a random name suffix.
*
* @returns Name suffix.
*
* @code
* Person::suffix() // "Jr."
* @endcode
*/
static std::string suffix();

/**
* @brief Returns a sex.
*
Expand Down
50 changes: 49 additions & 1 deletion include/faker-cxx/String.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#pragma once

#include <iostream>
#include <random>
#include <sstream>
#include <string>

#include "types/Hex.h"
#include "types/RandomGenerator.hpp"

namespace faker
{
Expand All @@ -21,11 +25,55 @@ class String
*
* @returns UUIDv4.
*
* @param gen A random number generator (type RandomGenerator)
* @code
* String::uuid() // "27666229-cedb-4a45-8018-98b1e1d921e2"
* @endcode
*/
static std::string uuid();
template <typename T = std::mt19937>
static std::string uuid(RandomGenerator<T> gen = RandomGenerator<std::mt19937>{})
{
static std::uniform_int_distribution<> dist(0, 15);
static std::uniform_int_distribution<> dist2(8, 11);

std::stringstream ss;
ss << std::hex;

for (int i = 0; i < 8; i++)
{
ss << gen(dist);
}

ss << "-";
for (int i = 0; i < 4; i++)
{
ss << gen(dist);
}

ss << "-4";
for (int i = 0; i < 3; i++)
{
ss << gen(dist);
}

ss << "-";

ss << gen(dist2);

for (int i = 0; i < 3; i++)
{
ss << gen(dist);
}

ss << "-";

for (int i = 0; i < 12; i++)
{
ss << gen(dist);
};

return ss.str();
}

/**
* @brief Returns a string containing UTF-16 chars between 33 and 125 (`!` to `}`).
Expand Down
24 changes: 24 additions & 0 deletions include/faker-cxx/types/RandomGenerator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once
#include <random>

template <typename T>
requires std::uniform_random_bit_generator<T>
class RandomGenerator
{
public:
RandomGenerator() : generator_{T(std::random_device{}())} {}
~RandomGenerator() = default;

RandomGenerator(const RandomGenerator&) = default;
RandomGenerator(RandomGenerator&&) = default;
RandomGenerator& operator=(const RandomGenerator&) = default;
RandomGenerator& operator=(RandomGenerator&&) = default;

int operator()(std::uniform_int_distribution<>& dist)
{
return dist(generator_);
}

private:
T generator_;
};
12 changes: 12 additions & 0 deletions include/faker-cxx/types/Sex.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,16 @@ enum class Sex
Male,
Female,
};

inline std::string toString(Sex sex)
{
std::map<Sex, std::string> sexToStringMapping{{Sex::Male, "Male"}, {Sex::Female, "Female"}};

return sexToStringMapping.at(sex);
}

inline std::ostream& operator<<(std::ostream& os, Sex sex)
{
return os << toString(sex);
}
}
2 changes: 2 additions & 0 deletions src/modules/company/Company.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

namespace faker
{
// TODO: add internalization

std::string Company::name()
{
std::string companyName;
Expand Down
9 changes: 4 additions & 5 deletions src/modules/company/CompanyTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
#include "gtest/gtest.h"

#include "../../common/StringHelper.h"
#include "../person/data/english/EnglishFirstNamesFemales.h"
#include "../person/data/english/EnglishFirstNamesMales.h"
#include "../person/data/english/EnglishFirstNames.h"
#include "../person/data/english/EnglishLastNames.h"
#include "../person/data/JobTitles.h"
#include "data/BuzzAdjectives.h"
Expand All @@ -33,10 +32,10 @@ TEST_F(CompanyTest, shouldGenerateCompanyName)

const auto companyNameElements = StringHelper::split(companyName, " ");

std::vector<std::string> expectedFirstNames{englishFirstNamesMales};
std::vector<std::string> expectedFirstNames{englishMalesFirstNames};

expectedFirstNames.insert(expectedFirstNames.end(), englishFirstNamesFemales.begin(),
englishFirstNamesFemales.end());
expectedFirstNames.insert(expectedFirstNames.end(), englishFemalesFirstNames.begin(),
englishFemalesFirstNames.end());

if (companyNameElements.size() == 2)
{
Expand Down
28 changes: 14 additions & 14 deletions src/modules/food/Food.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#include "faker-cxx/Food.h"

#include "data/alcoholicBeverages.h"
#include "data/dishNames.h"
#include "data/foodCategories.h"
#include "data/fruits.h"
#include "data/grains.h"
#include "data/meats.h"
#include "data/milkProducts.h"
#include "data/nonalcoholicBeverages.h"
#include "data/nuts.h"
#include "data/oils.h"
#include "data/seafoods.h"
#include "data/seeds.h"
#include "data/sugarProducts.h"
#include "data/vegetables.h"
#include "data/AlcoholicBeverages.h"
#include "data/DishNames.h"
#include "data/FoodCategories.h"
#include "data/Fruits.h"
#include "data/Grains.h"
#include "data/Meats.h"
#include "data/MilkProducts.h"
#include "data/NonalcoholicBeverages.h"
#include "data/Nuts.h"
#include "data/Oils.h"
#include "data/Seafoods.h"
#include "data/Seeds.h"
#include "data/SugarProducts.h"
#include "data/Vegetables.h"

#include "faker-cxx/Helper.h"

Expand Down
28 changes: 14 additions & 14 deletions src/modules/food/FoodTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

#include "gtest/gtest.h"

#include "data/alcoholicBeverages.h"
#include "data/dishNames.h"
#include "data/foodCategories.h"
#include "data/fruits.h"
#include "data/grains.h"
#include "data/meats.h"
#include "data/milkProducts.h"
#include "data/nonalcoholicBeverages.h"
#include "data/nuts.h"
#include "data/oils.h"
#include "data/seafoods.h"
#include "data/seeds.h"
#include "data/sugarProducts.h"
#include "data/vegetables.h"
#include "data/AlcoholicBeverages.h"
#include "data/DishNames.h"
#include "data/FoodCategories.h"
#include "data/Fruits.h"
#include "data/Grains.h"
#include "data/Meats.h"
#include "data/MilkProducts.h"
#include "data/NonalcoholicBeverages.h"
#include "data/Nuts.h"
#include "data/Oils.h"
#include "data/Seafoods.h"
#include "data/Seeds.h"
#include "data/SugarProducts.h"
#include "data/Vegetables.h"

using namespace ::testing;
using namespace faker;
Expand Down
1 change: 1 addition & 0 deletions src/modules/internet/Internet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const std::map<EmojiType, std::vector<std::string>> emojiTypeToEmojisMapping{
};
}

// TODO: add internalization
std::string Internet::username(std::optional<std::string> firstNameInit, std::optional<std::string> lastNameInit)
{
const auto firstName = firstNameInit ? *firstNameInit : Person::firstName();
Expand Down
Loading

0 comments on commit d2e5e4e

Please sign in to comment.