From 6fe007e96115c32c92e62698936dd1873dbbb813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Cie=C5=9Blar?= Date: Sat, 12 Oct 2024 11:20:01 +0100 Subject: [PATCH] feat: add missing tests in string module (#958) --- include/faker-cxx/string.h | 24 +++++--------------- src/modules/string.cpp | 38 ++++++++++++------------------- tests/modules/string_test.cpp | 42 +++++++++++++---------------------- 3 files changed, 34 insertions(+), 70 deletions(-) diff --git a/include/faker-cxx/string.h b/include/faker-cxx/string.h index ed386bcef..e87555fd2 100644 --- a/include/faker-cxx/string.h +++ b/include/faker-cxx/string.h @@ -21,8 +21,8 @@ enum class StringCasing struct FAKER_CXX_EXPORT CharCount { - unsigned int atLeastCount{(std::numeric_limits::min)()}; - unsigned int atMostCount{(std::numeric_limits::max)()}; + unsigned int atLeastCount{(std::numeric_limits::min)()}; + unsigned int atMostCount{(std::numeric_limits::max)()}; }; /** @@ -46,7 +46,7 @@ using GuaranteeMap = std::map; * faker::string::isValidGuarantee(guarantee,targetCharacters,length) // false * @endcode */ -FAKER_CXX_EXPORT bool isValidGuarantee(GuaranteeMap& guarantee, std::set& targetCharacters, unsigned int length); +FAKER_CXX_EXPORT bool isValidGuarantee(GuaranteeMap& guarantee, std::set& targetCharacters, unsigned length); /** * @brief Generates the least required string for a given guarantee map. @@ -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. @@ -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. diff --git a/src/modules/string.cpp b/src/modules/string.cpp index 49356048a..c41bb7f9e 100644 --- a/src/modules/string.cpp +++ b/src/modules/string.cpp @@ -32,7 +32,7 @@ const std::map> stringCasingToAlphaCharSetMapping{ {StringCasing::Mixed, mixedAlphaCharSet}, }; -std::string generateStringWithGuarantee(GuaranteeMap& guarantee, std::set& targetCharacters, unsigned int length) +std::string generateStringWithGuarantee(GuaranteeMap& guarantee, std::set& targetCharacters, unsigned length) { std::string output{}; output += generateAtLeastString(guarantee); @@ -68,10 +68,10 @@ std::string generateStringWithGuarantee(GuaranteeMap& guarantee, std::set& } } -bool isValidGuarantee(GuaranteeMap& guarantee, std::set& targetCharacters, unsigned int length) +bool isValidGuarantee(GuaranteeMap& guarantee, std::set& targetCharacters, unsigned length) { - unsigned int atLeastCountSum{}; - unsigned int atMostCountSum{}; + unsigned atLeastCountSum{}; + unsigned atMostCountSum{}; for (auto& it : guarantee) { @@ -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; @@ -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; @@ -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 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; @@ -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); @@ -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); @@ -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); @@ -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) { diff --git a/tests/modules/string_test.cpp b/tests/modules/string_test.cpp index 1e0eda8ac..94f5c0f7c 100644 --- a/tests/modules/string_test.cpp +++ b/tests/modules/string_test.cpp @@ -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)