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 symbol function #941 #946

Merged
merged 1 commit into from
Oct 4, 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
29 changes: 29 additions & 0 deletions include/faker-cxx/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,35 @@ FAKER_CXX_EXPORT std::string sample(unsigned length = 10);
*/
FAKER_CXX_EXPORT std::string sample(GuaranteeMap&& guarantee, unsigned length = 10);

/**
* @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 maxLength The number of maximum characters to generate. Defaults to `10`.
*
* @returns Sample string.
*
* @code
* faker::string::sample() // "#$%^&#$%^&"
* faker::string::sample(1,5) // "#$%^&"
* @endcode
*/
FAKER_CXX_EXPORT std::string symbol(unsigned int minLength, unsigned int maxLength);

/**
* @brief Generates a string consisting of given characters.
*
Expand Down
19 changes: 19 additions & 0 deletions src/modules/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,25 @@ std::string sample(GuaranteeMap&& guarantee, unsigned int length)
return generateStringWithGuarantee(guarantee, targetCharacters, length);
}

std::string symbol(unsigned int minLength, unsigned int maxLength)
{
if (minLength > maxLength) {
std::swap(minLength, maxLength);
}
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);
}

std::string fromCharacters(const std::string& characters, unsigned int length)
{
std::string result;
Expand Down
45 changes: 45 additions & 0 deletions tests/modules/string_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,51 @@ TEST_F(StringTest, shouldGenerateUuid4)
ASSERT_EQ(generatedUuid[23], '-');
}

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

ASSERT_EQ(generatedSymbol.size(), 10);

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

}
}

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

ASSERT_EQ(generatedSymbol.size(), length);

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

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

ASSERT_GE(generatedSymbol.size(), 10);
ASSERT_LE(generatedSymbol.size(), 20);

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

TEST_F(StringTest, shouldGenerateDefaultSampleString)
{
const auto generatedSample = sample();
Expand Down
Loading