Skip to content
44 changes: 36 additions & 8 deletions tdd_intro/homework/05_word_wrapp/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,52 @@ ignoring any possible match beginning after pos
#include <gtest/gtest.h>
#include <cctype>


// empty string
// string shorter than wrap number
// word longer than wrap number
// word much longer than wrap number (more than 2 strings)
// string longer than wrap number
// string ends with one whitespace longer than limit


// string wrapped by several whitespaces (less than wrapLength)
// string wrapped by several whitespaces (more than wrapLength)
// string should be wrapped by space if it is present under limit (two words in string, space before limit)
// string should be wrapped by space if it is present under limit (three words in string, two spaces before limit)
// string should be wrapped by space if it is present under limit (three words in string, no space before limit)
// only whitespaces in string

using WrappedStrings = std::vector<std::string>;

WrappedStrings WrapString(const std::string& str, size_t wrapLength)
{
WrappedStrings result;
for(size_t i = 0; i < str.length(); i += wrapLength)
size_t curLimit = wrapLength;
for(size_t i = 0; i < str.length(); i += curLimit)
{
std::string cur = str.substr(i, wrapLength);
if (cur.back() == ' ')
auto pos = str.find_first_of(' ');
std::cout << "POs: " << pos << std::endl;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

если потребовалась такое, значит шаг был великоват )

if(pos != std::string::npos && (pos > i && pos < wrapLength))
{
cur.pop_back();
curLimit = pos + 1;
}
else
{
curLimit = wrapLength;
}

if(!cur.empty() && cur.front() == ' ')
std::string cur = str.substr(i, curLimit);
if (cur.back() == ' ')
{
cur = cur.substr(1);
cur.pop_back();
}

if(!cur.empty())
{
if(cur.front() == ' ')
{
cur = cur.substr(1);
}

result.push_back(cur);
}
}
Expand Down Expand Up @@ -94,3 +110,15 @@ TEST(WrapString, StringWrappedBySeveralWhitespace)
WrappedStrings expected = {"12", "34"};
ASSERT_EQ(expected, WrapString("12 34", 3));
}

TEST(WrapString, StringEndsWithOneWhitespace)
{
WrappedStrings expected = {"123"};
ASSERT_EQ(expected, WrapString("123 ", 3));
}

TEST(WrapString, TwoWordsSpaceBeforeLimit)
{
WrappedStrings expected = {"1", "234"};
ASSERT_EQ(expected, WrapString("1 234", 3));
}
2 changes: 1 addition & 1 deletion tdd_intro/homework/homework.pro
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ SUBDIRS += \
03_bank_ocr \
04_weather_client \
05_word_wrapp \
06_coffee