diff --git a/.gitignore b/.gitignore index 259148fa..3e94e61a 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,9 @@ *.exe *.out *.app + +# Build dirs +build-* + +# .user files +*.user \ No newline at end of file diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 6bdc8367..268c4d8f 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -15,5 +15,3 @@ such: 1 #include #include -#include - diff --git a/tdd_intro/homework/05_word_wrapp/05_word_wrapp.pro b/tdd_intro/demo/03_word_wrapp/03_word_wrapp.pro similarity index 100% rename from tdd_intro/homework/05_word_wrapp/05_word_wrapp.pro rename to tdd_intro/demo/03_word_wrapp/03_word_wrapp.pro diff --git a/tdd_intro/demo/03_word_wrapp/test.cpp b/tdd_intro/demo/03_word_wrapp/test.cpp new file mode 100644 index 00000000..83821e4e --- /dev/null +++ b/tdd_intro/demo/03_word_wrapp/test.cpp @@ -0,0 +1,77 @@ +/* +Write a function, that is given a string and a length limit, splits provided string into sequence of string, +where length of each string is not more, than provided limit. If there are spaces under provided limit - +last space should be used to wrapp this line. If there are no spaces - wrapp it on provided length limit. + +Example: +When pos is specified, the search only includes sequences of characters that begin at or before position pos, +ignoring any possible match beginning after pos + +split(hee, 1) -> h, e, e +split(hee, 2) -> he, e +split("ha ha", 2|3|4) -> "ha", "ha" +split("h a", 1) -> "h", "a" + +"When pos is specified, the", +"search only includes sequences", +"of characters that begin at or", +"before position pos, ignoring", +"any possible match beginning", +"after pos." +*/ + +#include +#include +#include + +using StringContainer = std::vector; +StringContainer SplitString(const std::string& string, size_t length) +{ + auto firstSpaceInSubstr = string.rfind(' ', length); + std::string::size_type offset = length; + if (firstSpaceInSubstr != std::string::npos) + { + offset = firstSpaceInSubstr + 1; + } + + StringContainer result {{ string.substr(0, offset) }}; + + if (length < string.size()) + { + result.push_back(string.substr(offset)); + } + return result; +} + +TEST(SplitString, OnePartForOneSymbol) +{ + StringContainer parts = {"a"}; + EXPECT_EQ(parts, SplitString("a", 1)); +} + +TEST(SplitString, AbFirstForAbbaAnd2) +{ + EXPECT_EQ("Ab", SplitString("Abba", 2).at(0)); +} + +TEST(SplitString, BaSecondForAbbba) +{ + EXPECT_EQ("ba", SplitString("Abba", 2).at(1)); +} + +TEST(SplitString, AbForAbAnd4) +{ + EXPECT_EQ("Ab", SplitString("Ab", 4).at(0)); +} + +TEST(SplitString, SplitAb_AbBeforeSpaceReturnsAbAndAb) +{ + StringContainer parts = {"Ab", "Ab"}; + EXPECT_EQ(parts, SplitString("Ab Ab", 2)); +} + +TEST(SplitString, SplitAb_AbAtSpaceReturnsAbAndAb) +{ + StringContainer parts = {"Ab", "Ab"}; + EXPECT_EQ(parts, SplitString("Ab Ab", 3)); +} diff --git a/tdd_intro/demo/demo.pro b/tdd_intro/demo/demo.pro index 9e9ba630..37ec0908 100644 --- a/tdd_intro/demo/demo.pro +++ b/tdd_intro/demo/demo.pro @@ -5,6 +5,7 @@ SUBDIRS += \ 01_fizz_buzz \ 02_anagram \ 02_word_count \ + 03_word_wrapp \ #03_allergies \ 03_roman_numerals \ 04_timer diff --git a/tdd_intro/homework/05_word_wrapp/test.cpp b/tdd_intro/homework/05_word_wrapp/test.cpp deleted file mode 100644 index 77e47210..00000000 --- a/tdd_intro/homework/05_word_wrapp/test.cpp +++ /dev/null @@ -1,96 +0,0 @@ -/* -Write a function, that is given a string and a length limit, splits provided string into sequence of string, -where length of each string is not more, than provided limit. If there are spaces under provided limit - -last space should be used to wrapp this line. If there are no spaces - wrapp it on provided length limit. - -Example: -When pos is specified, the search only includes sequences of characters that begin at or before position pos, -ignoring any possible match beginning after pos - -"When pos is specified, the", -"search only includes sequences", -"of characters that begin at or", -"before position pos, ignoring", -"any possible match beginning", -"after pos." -*/ - -#include -#include - -// 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 wrapped by several whitespaces (less than wrapLength) -// string wrapped by several whitespaces (more than wrapLength) -// only whitespaces in string - -using WrappedStrings = std::vector; - -WrappedStrings WrapString(const std::string& str, size_t wrapLength) -{ - WrappedStrings result; - for(size_t i = 0; i < str.length(); i += wrapLength) - { - std::string cur = str.substr(i, wrapLength); - if (cur.back() == ' ') - { - cur.pop_back(); - } - - if(!cur.empty() && cur.front() == ' ') - { - cur = cur.substr(1); - } - - if(!cur.empty()) - { - result.push_back(cur); - } - } - - return result; -} - -TEST(WrapString, EmptyString) -{ - ASSERT_EQ(WrappedStrings(), WrapString("", 25)); -} - -TEST(WrapString, StringShorterWrapNumber) -{ - ASSERT_EQ(WrappedStrings{"asdf"}, WrapString("asdf", 8)); -} - -TEST(WrapString, StringLongerThanWrapNumber) -{ - WrappedStrings expected = {"asd", "f"}; - ASSERT_EQ(expected, WrapString("asdf", 3)); -} - -TEST(WrapString, StringLongerThanWrapNumberSeveralParts) -{ - WrappedStrings expected = {"12", "34", "56"}; - ASSERT_EQ(expected, WrapString("123456", 2)); -} - -TEST(WrapString, MultipleWordsLonger) -{ - WrappedStrings expected = {"1", "2"}; - ASSERT_EQ(expected, WrapString("1 2", 1)); -} - -TEST(WrapString, SpaceStringEnd) -{ - WrappedStrings expected = {"1", "2"}; - ASSERT_EQ(expected, WrapString("1 2", 2)); -} - -TEST(WrapString, StringWrappedBySeveralWhitespace) -{ - WrappedStrings expected = {"12", "34"}; - ASSERT_EQ(expected, WrapString("12 34", 3)); -} diff --git a/tdd_intro/homework/06_coffee/test.cpp b/tdd_intro/homework/06_coffee/test.cpp index 33b9093e..ac403210 100644 --- a/tdd_intro/homework/06_coffee/test.cpp +++ b/tdd_intro/homework/06_coffee/test.cpp @@ -40,7 +40,10 @@ enum Cup enum Coffee { - Americano + Americano, + Cappucino, + Latte, + Marochino }; class MockSourceOfIngredients : public ISourceOfIngredients @@ -56,6 +59,11 @@ class MockSourceOfIngredients : public ISourceOfIngredients MOCK_METHOD1(AddCream, void(int)); }; +int GetCupSize(const Cup cup) +{ + return cup == Cup::Big ? 140 : 100; +} + class CoffeeMachine { public: @@ -65,9 +73,40 @@ class CoffeeMachine } void CreateCoffee(const Cup cup, const Coffee coffee) { - m_source.AddCoffee(0); - m_source.SetCupSize(0); - m_source.AddWater(0, 0); + int cupSize = GetCupSize(cup); + m_source.SetCupSize(cupSize); + switch (coffee) + { + case Coffee::Cappucino: + { + m_source.AddCoffee(cupSize / 6); + m_source.AddWater(cupSize / 2, 80); + m_source.AddMilk(cupSize / 6); + m_source.AddMilkFoam(cupSize / 6); + break; + } + case Coffee::Latte: + { + m_source.AddCoffee(cupSize / 4); + m_source.AddWater(cupSize / 2, 90); + m_source.AddMilk(cupSize / 8); + m_source.AddMilkFoam(cupSize / 8); + break; + } + case Coffee::Americano: + { + m_source.AddCoffee(cupSize / 4 * 3); + m_source.AddWater(cupSize / 4, 60); + break; + } + case Coffee::Marochino: + { + m_source.AddCoffee(cupSize / 4); + m_source.AddChocolate(cupSize / 4); + m_source.AddMilkFoam(cupSize / 4); + break; + } + } } private: ISourceOfIngredients& m_source; @@ -117,3 +156,131 @@ TEST(CoffeeMachine, Americano) cm.CreateCoffee(Cup::Normal, Coffee::Americano); } +TEST(CoffeeMachine, BigAmericano) +{ + MockSourceOfIngredients si; + CoffeeMachine cm(si); + + EXPECT_CALL(si, AddCoffee(105)).Times(1); + EXPECT_CALL(si, SetCupSize(140)).Times(1); + EXPECT_CALL(si, AddWater(35, 60)).Times(1); + + cm.CreateCoffee(Cup::Big, Coffee::Americano); +} + +TEST(CoffeeMachine, CappucinoCallsImportantThings) +{ + MockSourceOfIngredients si; + CoffeeMachine cm(si); + + EXPECT_CALL(si, AddCoffee(::testing::_)).Times(1); + EXPECT_CALL(si, SetCupSize(::testing::_)).Times(1); + EXPECT_CALL(si, AddMilk(::testing::_)).Times(1); + EXPECT_CALL(si, AddMilkFoam(::testing::_)).Times(1); + EXPECT_CALL(si, AddWater(::testing::_, ::testing::_)).Times(1); + + cm.CreateCoffee(Cup::Normal, Coffee::Cappucino); +} +TEST(CoffeeMachine, Cappucino) +{ + MockSourceOfIngredients si; + CoffeeMachine cm(si); + + EXPECT_CALL(si, AddCoffee(16)).Times(1); + EXPECT_CALL(si, SetCupSize(100)).Times(1); + EXPECT_CALL(si, AddMilk(16)).Times(1); + EXPECT_CALL(si, AddMilkFoam(16)).Times(1); + EXPECT_CALL(si, AddWater(50, 80)).Times(1); + + cm.CreateCoffee(Cup::Normal, Coffee::Cappucino); +} +TEST(CoffeeMachine, BigCappucino) +{ + MockSourceOfIngredients si; + CoffeeMachine cm(si); + + EXPECT_CALL(si, AddCoffee(23)).Times(1); + EXPECT_CALL(si, SetCupSize(140)).Times(1); + EXPECT_CALL(si, AddMilk(23)).Times(1); + EXPECT_CALL(si, AddMilkFoam(23)).Times(1); + EXPECT_CALL(si, AddWater(70, 80)).Times(1); + + cm.CreateCoffee(Cup::Big, Coffee::Cappucino); +} + +TEST(CoffeeMachine, LatteCallsImportantThings) +{ + MockSourceOfIngredients si; + CoffeeMachine cm(si); + + EXPECT_CALL(si, AddCoffee(::testing::_)).Times(1); + EXPECT_CALL(si, SetCupSize(::testing::_)).Times(1); + EXPECT_CALL(si, AddMilk(::testing::_)).Times(1); + EXPECT_CALL(si, AddMilkFoam(::testing::_)).Times(1); + EXPECT_CALL(si, AddWater(::testing::_, ::testing::_)).Times(1); + + cm.CreateCoffee(Cup::Normal, Coffee::Latte); +} +TEST(CoffeeMachine, Latte) +{ + MockSourceOfIngredients si; + CoffeeMachine cm(si); + + EXPECT_CALL(si, AddCoffee(25)).Times(1); + EXPECT_CALL(si, SetCupSize(100)).Times(1); + EXPECT_CALL(si, AddMilk(12)).Times(1); + EXPECT_CALL(si, AddMilkFoam(12)).Times(1); + EXPECT_CALL(si, AddWater(50, 90)).Times(1); + + cm.CreateCoffee(Cup::Normal, Coffee::Latte); +} +TEST(CoffeeMachine, BigLatte) +{ + MockSourceOfIngredients si; + CoffeeMachine cm(si); + + EXPECT_CALL(si, AddCoffee(35)).Times(1); + EXPECT_CALL(si, SetCupSize(140)).Times(1); + EXPECT_CALL(si, AddMilk(17)).Times(1); + EXPECT_CALL(si, AddMilkFoam(17)).Times(1); + EXPECT_CALL(si, AddWater(70, 90)).Times(1); + + cm.CreateCoffee(Cup::Big, Coffee::Latte); +} + +TEST(CoffeeMachine, MarochinoCallsImportantThings) +{ + MockSourceOfIngredients si; + CoffeeMachine cm(si); + + EXPECT_CALL(si, AddCoffee(::testing::_)).Times(1); + EXPECT_CALL(si, SetCupSize(::testing::_)).Times(1); + EXPECT_CALL(si, AddChocolate(::testing::_)).Times(1); + EXPECT_CALL(si, AddMilkFoam(::testing::_)).Times(1); + + cm.CreateCoffee(Cup::Normal, Coffee::Marochino); +} +TEST(CoffeeMachine, Marochino) +{ + MockSourceOfIngredients si; + CoffeeMachine cm(si); + + EXPECT_CALL(si, AddCoffee(25)).Times(1); + EXPECT_CALL(si, SetCupSize(100)).Times(1); + EXPECT_CALL(si, AddChocolate(25)).Times(1); + EXPECT_CALL(si, AddMilkFoam(25)).Times(1); + + cm.CreateCoffee(Cup::Normal, Coffee::Marochino); +} +TEST(CoffeeMachine, BigMarochino) +{ + MockSourceOfIngredients si; + CoffeeMachine cm(si); + + EXPECT_CALL(si, AddCoffee(35)).Times(1); + EXPECT_CALL(si, SetCupSize(140)).Times(1); + EXPECT_CALL(si, AddChocolate(35)).Times(1); + EXPECT_CALL(si, AddMilkFoam(35)).Times(1); + + cm.CreateCoffee(Cup::Big, Coffee::Marochino); +} diff --git a/tdd_intro/homework/homework.pro b/tdd_intro/homework/homework.pro index cf6c01b9..7e131270 100644 --- a/tdd_intro/homework/homework.pro +++ b/tdd_intro/homework/homework.pro @@ -5,5 +5,4 @@ SUBDIRS += \ 02_ternary_numbers \ 03_bank_ocr \ 04_weather_client \ - 05_word_wrapp \ 06_coffee