diff --git a/include/faker-cxx/string.h b/include/faker-cxx/string.h index 27a57fc4..0a3d3c3b 100644 --- a/include/faker-cxx/string.h +++ b/include/faker-cxx/string.h @@ -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. * diff --git a/src/modules/string.cpp b/src/modules/string.cpp index 83fc0f2b..7e47f472 100644 --- a/src/modules/string.cpp +++ b/src/modules/string.cpp @@ -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 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; diff --git a/tests/modules/string_test.cpp b/tests/modules/string_test.cpp index 89d5dcfd..7e1fed65 100644 --- a/tests/modules/string_test.cpp +++ b/tests/modules/string_test.cpp @@ -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();