Skip to content

Commit

Permalink
Merge pull request #5 from gustavobastian/feature/word-locale
Browse files Browse the repository at this point in the history
removing option in words with (length<=256), and s capture the datase…
  • Loading branch information
gustavobastian authored Oct 3, 2024
2 parents bb7cf04 + 7be658f commit c9c8a76
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 41 deletions.
44 changes: 8 additions & 36 deletions src/modules/word.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
namespace faker::word
{



std::string_view sample( std::optional<unsigned int> length,const Locale locale)
{
unsigned int aux_length{0};
Expand All @@ -34,7 +32,6 @@ std::string_view sample( std::optional<unsigned int> length,const Locale locale)

auto sorted= _allWords_map.at(localeLocal);
return sortedSizeRandomElement(aux_length, sorted);

}

std::string words(unsigned numberOfWords,const Locale locale)
Expand All @@ -50,44 +47,19 @@ std::string words(unsigned numberOfWords,const Locale locale)
localeExt = Locale::en_US;
}
std::string combined_words;
if (numberOfWords <= 256)
{
std::array<unsigned int, 256> tmp{}; // fitting 1024 bytes worth of integers*
const size_t last_index = ((_allWords_map.at(localeExt)).size()) - 1;
size_t reserve_size = 0;

for (unsigned i = 0; i < numberOfWords; i++)
{
tmp[i] = number::integer<unsigned int>(last_index);
auto vw = (_allWords_map.at(localeExt))[tmp[i]];
reserve_size += vw.size();
}

unsigned space_words = (numberOfWords - 1);
combined_words.reserve(reserve_size + (numberOfWords - 1));
for (unsigned i = 0; i < space_words; i++)
{
auto vw = (_allWords_map.at(localeExt))[tmp[i]];
combined_words.append(vw.begin(), vw.end());
combined_words.push_back(' ');
}
auto vw = (_allWords_map.at(localeExt))[tmp[numberOfWords - 1]];
combined_words.append(vw.begin(), vw.end());
}
else

unsigned space_words = (numberOfWords - 1);
for (unsigned i = 0; i < space_words; i++)
{
unsigned space_words = (numberOfWords - 1);
for (unsigned i = 0; i < space_words; i++)
{
auto s = sample(1, localeExt);
combined_words.append(s.begin(), s.end());
combined_words.push_back(' ');
}

auto s = sample(1, localeExt);
combined_words.append(s.begin(), s.end());
combined_words.push_back(' ');
}

auto s = sample(1, localeExt);
combined_words.append(s.begin(), s.end());


return combined_words;
}

Expand Down
12 changes: 7 additions & 5 deletions tests/modules/word_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,20 +606,22 @@ TEST_P(WordTestLocale, shouldGenerateSampleWithNonExistingLength)

TEST_P(WordTestLocale, shouldGenerateWords)
{
const auto locale = GetParam();
const auto locale = GetParam();
Locale extra=locale;

if(!checkLocale(locale))
{
extra=Locale::en_US;
}

const auto generatedWords = words(5,locale);

const auto generatedWords = words(5,locale);
const auto separatedWords = common::split(generatedWords, " ");

ASSERT_TRUE(std::ranges::all_of(separatedWords, [extra](const std::string& separatedWord)
{ return std::ranges::find(_allWords_map.at(extra), separatedWord) !=_allWords_map.at(extra).end(); }));
const auto &datamap=_allWords_map.at(extra);

ASSERT_TRUE(std::ranges::all_of(separatedWords, [&datamap](const std::string& separatedWord)
{ return std::ranges::find(datamap, separatedWord) !=datamap.end(); }));
}

TEST_F(WordTestLocale, shouldReturnRandomElementWhenExactLengthNotFound)
Expand Down

0 comments on commit c9c8a76

Please sign in to comment.