Skip to content

Commit

Permalink
feat: add symbol function #941 (#946)
Browse files Browse the repository at this point in the history
Co-authored-by: Tony Cao <[email protected]>
  • Loading branch information
tonykcao and tonykcao authored Oct 4, 2024
1 parent 3aef81d commit c33908d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
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

0 comments on commit c33908d

Please sign in to comment.