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

feat: add missing tests in string module #958

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
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
24 changes: 5 additions & 19 deletions include/faker-cxx/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ enum class StringCasing

struct FAKER_CXX_EXPORT CharCount
{
unsigned int atLeastCount{(std::numeric_limits<unsigned int>::min)()};
unsigned int atMostCount{(std::numeric_limits<unsigned int>::max)()};
unsigned int atLeastCount{(std::numeric_limits<unsigned>::min)()};
unsigned int atMostCount{(std::numeric_limits<unsigned>::max)()};
};

/**
Expand All @@ -46,7 +46,7 @@ using GuaranteeMap = std::map<char, CharCount>;
* faker::string::isValidGuarantee(guarantee,targetCharacters,length) // false
* @endcode
*/
FAKER_CXX_EXPORT bool isValidGuarantee(GuaranteeMap& guarantee, std::set<char>& targetCharacters, unsigned int length);
FAKER_CXX_EXPORT bool isValidGuarantee(GuaranteeMap& guarantee, std::set<char>& targetCharacters, unsigned length);

/**
* @brief Generates the least required string for a given guarantee map.
Expand Down Expand Up @@ -150,21 +150,7 @@ FAKER_CXX_EXPORT std::string sample(GuaranteeMap&& guarantee, unsigned length =
/**
* @brief Returns a string containing "~`!@#$%^&*()_-+={[}]|:;\"'<,>.?/".
*
* @param length The number of characters to generate. Defaults to `10`.
*
* @returns Sample string.
*
* @code
* faker::string::sample() // "#$%^&#$%^&"
* faker::string::sample(5) // "#$%^&"
* @endcode
*/
FAKER_CXX_EXPORT std::string symbol(unsigned length = 10);

/**
* @brief Returns a string containing "~`!@#$%^&*()_-+={[}]|:;\"'<,>.?/".
*
* @param minlength The number of minimum characters to generate. Defaults to `1`.
* @param minLength The number of minimum characters to generate. Defaults to `1`.
* @param maxLength The number of maximum characters to generate. Defaults to `10`.
*
* @returns Sample string.
Expand All @@ -174,7 +160,7 @@ FAKER_CXX_EXPORT std::string symbol(unsigned length = 10);
* faker::string::sample(1,5) // "#$%^&"
* @endcode
*/
FAKER_CXX_EXPORT std::string symbol(unsigned int minLength, unsigned int maxLength);
FAKER_CXX_EXPORT std::string symbol(unsigned minLength = 1, unsigned maxLength = 10);

/**
* @brief Generates a string consisting of given characters.
Expand Down
38 changes: 14 additions & 24 deletions src/modules/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const std::map<StringCasing, std::set<char>> stringCasingToAlphaCharSetMapping{
{StringCasing::Mixed, mixedAlphaCharSet},
};

std::string generateStringWithGuarantee(GuaranteeMap& guarantee, std::set<char>& targetCharacters, unsigned int length)
std::string generateStringWithGuarantee(GuaranteeMap& guarantee, std::set<char>& targetCharacters, unsigned length)
{
std::string output{};
output += generateAtLeastString(guarantee);
Expand Down Expand Up @@ -68,10 +68,10 @@ std::string generateStringWithGuarantee(GuaranteeMap& guarantee, std::set<char>&
}
}

bool isValidGuarantee(GuaranteeMap& guarantee, std::set<char>& targetCharacters, unsigned int length)
bool isValidGuarantee(GuaranteeMap& guarantee, std::set<char>& targetCharacters, unsigned length)
{
unsigned int atLeastCountSum{};
unsigned int atMostCountSum{};
unsigned atLeastCountSum{};
unsigned atMostCountSum{};

for (auto& it : guarantee)
{
Expand Down Expand Up @@ -105,7 +105,7 @@ std::string generateAtLeastString(const GuaranteeMap& guarantee)
return result;
}

std::string sample(unsigned int length)
std::string sample(unsigned length)
{
std::string sample;

Expand All @@ -117,7 +117,7 @@ std::string sample(unsigned int length)
return sample;
}

std::string sample(GuaranteeMap&& guarantee, unsigned int length)
std::string sample(GuaranteeMap&& guarantee, unsigned length)
{
auto targetCharacters = utf16CharSet;

Expand All @@ -129,27 +129,17 @@ std::string sample(GuaranteeMap&& guarantee, unsigned int length)
return generateStringWithGuarantee(guarantee, targetCharacters, length);
}

std::string symbol(unsigned int minLength, unsigned int maxLength)
std::string symbol(unsigned minLength, unsigned maxLength)
{
if (minLength > maxLength)
{
std::swap(minLength, maxLength);
throw std::invalid_argument("min length cannot be greater than max length");
}
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<unsigned int> dist(minLength, maxLength);

unsigned int length = dist(gen);

return fromCharacters(symbolCharacters, length);
}

std::string symbol(unsigned int length)
{
return fromCharacters(symbolCharacters, length);
return fromCharacters(symbolCharacters, number::integer(minLength, maxLength));
}

std::string fromCharacters(const std::string& characters, unsigned int length)
std::string fromCharacters(const std::string& characters, unsigned length)
{
std::string result;

Expand Down Expand Up @@ -202,7 +192,7 @@ std::string alpha(unsigned length, StringCasing casing, const std::string& exclu
return alpha;
}

std::string alpha(GuaranteeMap&& guarantee, unsigned int length, StringCasing casing)
std::string alpha(GuaranteeMap&& guarantee, unsigned length, StringCasing casing)
{
auto targetCharacters = stringCasingToAlphaCharSetMapping.at(casing);

Expand All @@ -214,7 +204,7 @@ std::string alpha(GuaranteeMap&& guarantee, unsigned int length, StringCasing ca
return generateStringWithGuarantee(guarantee, targetCharacters, length);
}

std::string alphanumeric(unsigned int length, StringCasing casing, const std::string& excludeCharacters)
std::string alphanumeric(unsigned length, StringCasing casing, const std::string& excludeCharacters)
{
const auto& alphanumericCharacters = stringCasingToAlphanumericCharactersMapping.at(casing);

Expand Down Expand Up @@ -254,7 +244,7 @@ std::string alphanumeric(GuaranteeMap&& guarantee, unsigned length, StringCasing
return generateStringWithGuarantee(guarantee, targetCharacters, length);
}

std::string numeric(unsigned int length, bool allowLeadingZeros)
std::string numeric(unsigned length, bool allowLeadingZeros)
{
std::string alphanumericStr;
alphanumericStr.reserve(length);
Expand All @@ -274,7 +264,7 @@ std::string numeric(unsigned int length, bool allowLeadingZeros)
return alphanumericStr;
}

std::string numeric(GuaranteeMap&& guarantee, const unsigned length, bool allowLeadingZeros)
std::string numeric(GuaranteeMap&& guarantee, unsigned length, bool allowLeadingZeros)
{
if (!allowLeadingZeros)
{
Expand Down
42 changes: 15 additions & 27 deletions tests/modules/string_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,43 +126,31 @@ TEST_F(StringTest, shouldGenerateUuid4)

TEST_F(StringTest, ShouldGenerateSymbolStringDefault)
{
for (int i = 0; i < runCount; ++i)
{
const auto generatedSymbol = symbol();
const auto generatedSymbol = symbol();

ASSERT_EQ(generatedSymbol.size(), 10);
ASSERT_TRUE(!generatedSymbol.empty() && generatedSymbol.size() <= 10);

ASSERT_TRUE(std::all_of(generatedSymbol.begin(), generatedSymbol.end(),
[](char c) { return symbolCharacters.find(c) != std::string::npos; }));
}
ASSERT_TRUE(std::all_of(generatedSymbol.begin(), generatedSymbol.end(),
[](char c) { return symbolCharacters.find(c) != std::string::npos; }));
}

TEST_F(StringTest, ShouldGenerateSymbolStringWithLen)
TEST_F(StringTest, ShouldGenerateSymbolStringWithRange)
{
for (int i = 0; i < runCount; ++i)
{
unsigned int length = 20;
const auto generatedSymbol = symbol(length);
const auto minLength = 10;

ASSERT_EQ(generatedSymbol.size(), length);
const auto maxLength = 20;

ASSERT_TRUE(std::all_of(generatedSymbol.begin(), generatedSymbol.end(),
[](char c) { return symbolCharacters.find(c) != std::string::npos; }));
}
}
const auto generatedSymbols = symbol(minLength, maxLength);

TEST_F(StringTest, ShouldGenerateSymbolStringWithRange)
{
for (int i = 0; i < runCount; ++i)
{
const auto generatedSymbol = symbol(10, 20);
ASSERT_TRUE(std::all_of(generatedSymbols.begin(), generatedSymbols.end(),
[](char c) { return symbolCharacters.find(c) != std::string::npos; }));

ASSERT_GE(generatedSymbol.size(), 10);
ASSERT_LE(generatedSymbol.size(), 20);
ASSERT_TRUE(generatedSymbols.size() >= minLength && generatedSymbols.size() <= maxLength);
}

ASSERT_TRUE(std::all_of(generatedSymbol.begin(), generatedSymbol.end(),
[](char c) { return symbolCharacters.find(c) != std::string::npos; }));
}
TEST_F(StringTest, ShouldThrowExceptionForInvalidRange)
{
ASSERT_THROW(symbol(20, 10), std::invalid_argument);
}

TEST_F(StringTest, shouldGenerateDefaultSampleString)
Expand Down
Loading