From f0dc6d86aba7a541b126708bd69648cc9c884fb8 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 31 Oct 2018 16:53:29 +0200 Subject: [PATCH 01/72] First test --- tdd_intro/homework/01_leap_year/test.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tdd_intro/homework/01_leap_year/test.cpp b/tdd_intro/homework/01_leap_year/test.cpp index 4f186c8b..25d13325 100644 --- a/tdd_intro/homework/01_leap_year/test.cpp +++ b/tdd_intro/homework/01_leap_year/test.cpp @@ -13,3 +13,9 @@ If your language provides a method in the standard library that does this look-u */ #include + + +TEST(LeapYear, Test400) +{ + EXPECT_TRUE(IsLeapYear(400)); +} From ef5f02c4b288d78b031d0595871b35d5d0024c97 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 31 Oct 2018 17:21:32 +0200 Subject: [PATCH 02/72] Green --- tdd_intro/homework/01_leap_year/test.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tdd_intro/homework/01_leap_year/test.cpp b/tdd_intro/homework/01_leap_year/test.cpp index 25d13325..c176aeda 100644 --- a/tdd_intro/homework/01_leap_year/test.cpp +++ b/tdd_intro/homework/01_leap_year/test.cpp @@ -14,6 +14,15 @@ If your language provides a method in the standard library that does this look-u #include +bool IsLeapYear(size_t year) +{ + if (year == 400) + { + return true; + } + + return false; +} TEST(LeapYear, Test400) { From 7808524a5442c5c2e566f0b1aa2a4b37bb25336e Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 31 Oct 2018 17:23:05 +0200 Subject: [PATCH 03/72] Refactoring --- tdd_intro/homework/01_leap_year/test.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tdd_intro/homework/01_leap_year/test.cpp b/tdd_intro/homework/01_leap_year/test.cpp index c176aeda..e07241eb 100644 --- a/tdd_intro/homework/01_leap_year/test.cpp +++ b/tdd_intro/homework/01_leap_year/test.cpp @@ -14,9 +14,11 @@ If your language provides a method in the standard library that does this look-u #include +const size_t g_fourHundred = 400; + bool IsLeapYear(size_t year) { - if (year == 400) + if (year == g_fourHundred) { return true; } @@ -26,5 +28,5 @@ bool IsLeapYear(size_t year) TEST(LeapYear, Test400) { - EXPECT_TRUE(IsLeapYear(400)); + EXPECT_TRUE(IsLeapYear(g_fourHundred)); } From 62d0f6d1998231f89df3b30b138cf6c29d5e0849 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 31 Oct 2018 17:25:04 +0200 Subject: [PATCH 04/72] Red --- tdd_intro/homework/01_leap_year/test.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tdd_intro/homework/01_leap_year/test.cpp b/tdd_intro/homework/01_leap_year/test.cpp index e07241eb..5e0c4935 100644 --- a/tdd_intro/homework/01_leap_year/test.cpp +++ b/tdd_intro/homework/01_leap_year/test.cpp @@ -29,4 +29,6 @@ bool IsLeapYear(size_t year) TEST(LeapYear, Test400) { EXPECT_TRUE(IsLeapYear(g_fourHundred)); + EXPECT_TRUE(IsLeapYear(g_fourHundred * 2)); + EXPECT_TRUE(IsLeapYear(g_fourHundred * 3)); } From 44b969665282d76cc43589f13a3931310686a824 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 31 Oct 2018 17:25:57 +0200 Subject: [PATCH 05/72] Green --- tdd_intro/homework/01_leap_year/test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tdd_intro/homework/01_leap_year/test.cpp b/tdd_intro/homework/01_leap_year/test.cpp index 5e0c4935..c11ba757 100644 --- a/tdd_intro/homework/01_leap_year/test.cpp +++ b/tdd_intro/homework/01_leap_year/test.cpp @@ -18,7 +18,7 @@ const size_t g_fourHundred = 400; bool IsLeapYear(size_t year) { - if (year == g_fourHundred) + if (year % g_fourHundred == 0) { return true; } From de711704a392bb92bcd42b105ec88a8c2cb52f3d Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 31 Oct 2018 17:28:31 +0200 Subject: [PATCH 06/72] refactoring --- tdd_intro/homework/01_leap_year/test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tdd_intro/homework/01_leap_year/test.cpp b/tdd_intro/homework/01_leap_year/test.cpp index c11ba757..82d59f56 100644 --- a/tdd_intro/homework/01_leap_year/test.cpp +++ b/tdd_intro/homework/01_leap_year/test.cpp @@ -26,7 +26,7 @@ bool IsLeapYear(size_t year) return false; } -TEST(LeapYear, Test400) +TEST(LeapYear, TestDivisible400) { EXPECT_TRUE(IsLeapYear(g_fourHundred)); EXPECT_TRUE(IsLeapYear(g_fourHundred * 2)); From 0cfe9024ff38c4c4ad95aff55dba9b6ddde60dc1 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 31 Oct 2018 17:31:10 +0200 Subject: [PATCH 07/72] Read --- tdd_intro/homework/01_leap_year/test.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tdd_intro/homework/01_leap_year/test.cpp b/tdd_intro/homework/01_leap_year/test.cpp index 82d59f56..d32be8ce 100644 --- a/tdd_intro/homework/01_leap_year/test.cpp +++ b/tdd_intro/homework/01_leap_year/test.cpp @@ -32,3 +32,9 @@ TEST(LeapYear, TestDivisible400) EXPECT_TRUE(IsLeapYear(g_fourHundred * 2)); EXPECT_TRUE(IsLeapYear(g_fourHundred * 3)); } + +TEST(LeapYear, TestDivisible4AndNot100) +{ + EXPECT_TRUE(IsLeapYear(104)); + EXPECT_TRUE(IsLeapYear(404)); +} From 93dd7e4d92e78e6fc7aedd0017348c04794edf3a Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 31 Oct 2018 17:33:00 +0200 Subject: [PATCH 08/72] Green --- tdd_intro/homework/01_leap_year/test.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tdd_intro/homework/01_leap_year/test.cpp b/tdd_intro/homework/01_leap_year/test.cpp index d32be8ce..fe666f95 100644 --- a/tdd_intro/homework/01_leap_year/test.cpp +++ b/tdd_intro/homework/01_leap_year/test.cpp @@ -22,6 +22,10 @@ bool IsLeapYear(size_t year) { return true; } + else if (year % 100 != 0 && year % 4 == 0) + { + return true; + } return false; } From 56f373b3b06d2697752688c6900994284f697812 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 31 Oct 2018 17:38:12 +0200 Subject: [PATCH 09/72] Refactoring --- tdd_intro/homework/01_leap_year/test.cpp | 26 +++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/tdd_intro/homework/01_leap_year/test.cpp b/tdd_intro/homework/01_leap_year/test.cpp index fe666f95..9301ac88 100644 --- a/tdd_intro/homework/01_leap_year/test.cpp +++ b/tdd_intro/homework/01_leap_year/test.cpp @@ -14,15 +14,27 @@ If your language provides a method in the standard library that does this look-u #include -const size_t g_fourHundred = 400; + +const size_t g_four = 4; +const size_t g_oneHundred = 100; +const size_t g_fourHundred = g_four * g_oneHundred; + +bool isDivisible(size_t number, size_t divider) +{ + return number % divider == 0; +} bool IsLeapYear(size_t year) { - if (year % g_fourHundred == 0) + if (isDivisible(year, g_fourHundred)) { return true; } - else if (year % 100 != 0 && year % 4 == 0) + if (isDivisible(year, g_oneHundred)) + { + return false; + } + if (isDivisible(year, g_four)) { return true; } @@ -41,4 +53,12 @@ TEST(LeapYear, TestDivisible4AndNot100) { EXPECT_TRUE(IsLeapYear(104)); EXPECT_TRUE(IsLeapYear(404)); + EXPECT_TRUE(IsLeapYear(2008)); + EXPECT_TRUE(IsLeapYear(2012)); +} + +TEST(LeapYear, TestDivisible100) +{ + EXPECT_FALSE(IsLeapYear(g_oneHundred)); + EXPECT_FALSE(IsLeapYear(g_oneHundred * 5)); } From de6260990a2042dd1bc1d8fd85242a557b628e19 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 31 Oct 2018 17:52:43 +0200 Subject: [PATCH 10/72] Green --- tdd_intro/homework/01_leap_year/test.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tdd_intro/homework/01_leap_year/test.cpp b/tdd_intro/homework/01_leap_year/test.cpp index 9301ac88..19bae075 100644 --- a/tdd_intro/homework/01_leap_year/test.cpp +++ b/tdd_intro/homework/01_leap_year/test.cpp @@ -62,3 +62,9 @@ TEST(LeapYear, TestDivisible100) EXPECT_FALSE(IsLeapYear(g_oneHundred)); EXPECT_FALSE(IsLeapYear(g_oneHundred * 5)); } + +TEST(LeapYear, TestNotDivisible4_400_100) +{ + EXPECT_FALSE(IsLeapYear(333)); + EXPECT_FALSE(IsLeapYear(555)); +} From c54701bdc596800c01cdc06d58979c509ebc4950 Mon Sep 17 00:00:00 2001 From: Mokych Andrey Date: Fri, 2 Nov 2018 13:43:03 +0200 Subject: [PATCH 11/72] WS#2 added word count demo project --- .../demo/02_word_count/02_word_count.pro | 10 ++++++++++ tdd_intro/demo/02_word_count/test.cpp | 19 +++++++++++++++++++ tdd_intro/demo/demo.pro | 1 + 3 files changed, 30 insertions(+) create mode 100644 tdd_intro/demo/02_word_count/02_word_count.pro create mode 100644 tdd_intro/demo/02_word_count/test.cpp diff --git a/tdd_intro/demo/02_word_count/02_word_count.pro b/tdd_intro/demo/02_word_count/02_word_count.pro new file mode 100644 index 00000000..e1123a37 --- /dev/null +++ b/tdd_intro/demo/02_word_count/02_word_count.pro @@ -0,0 +1,10 @@ +include(../../gtest.pri) + +TEMPLATE = app +CONFIG += console c++11 +CONFIG -= app_bundle +CONFIG -= qt + +SOURCES += \ + test.cpp + diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp new file mode 100644 index 00000000..6bdc8367 --- /dev/null +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -0,0 +1,19 @@ +/* +Given a phrase, count the occurrences of each word in that phrase. Ignore whitespaces and punctual symbols +For example for the input "olly olly in come free please please let it be in such manner olly" +olly: 3 +in: 2 +come: 1 +free: 1 +please: 2 +let: 1 +it: 1 +be: 1 +manner: 1 +such: 1 +*/ + +#include +#include +#include + diff --git a/tdd_intro/demo/demo.pro b/tdd_intro/demo/demo.pro index c2905cb9..9e9ba630 100644 --- a/tdd_intro/demo/demo.pro +++ b/tdd_intro/demo/demo.pro @@ -4,6 +4,7 @@ SUBDIRS += \ 01_bob \ 01_fizz_buzz \ 02_anagram \ + 02_word_count \ #03_allergies \ 03_roman_numerals \ 04_timer From b020cff2f743b91dfda7b6c520163f24c12bef63 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 09:37:57 +0200 Subject: [PATCH 12/72] First red test --- tdd_intro/demo/02_word_count/test.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 6bdc8367..9a19ea34 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -17,3 +17,9 @@ such: 1 #include #include + +TEST(WordsCount, TestSeparateFirstWord) +{ + std::map words = SeparateWords("hello bro"); + EXPECT_EQ(1, words.size()); +} From 2e932c29feadfbba594011fa35897f2dc77f1761 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 10:19:06 +0200 Subject: [PATCH 13/72] Green --- tdd_intro/demo/02_word_count/test.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 9a19ea34..29300489 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -17,9 +17,19 @@ such: 1 #include #include +using words_mt = std::map; +const static char wordSeparator = ' '; + +words_mt SeparateWords(const std::string& phrase) +{ + const size_t index = phrase.find_first_of(wordSeparator); + return {{phrase.substr(0, index), 1}}; +} TEST(WordsCount, TestSeparateFirstWord) { - std::map words = SeparateWords("hello bro"); - EXPECT_EQ(1, words.size()); + words_mt words = SeparateWords("hello"); + ASSERT_EQ(1, words.size()); + EXPECT_TRUE(words.find("hello") != words.end()); + EXPECT_EQ(1, words["hello"]); } From a22a075302f8a51348a3c3114248bc4c613d1807 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 10:24:37 +0200 Subject: [PATCH 14/72] Refactoring --- tdd_intro/demo/02_word_count/test.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 29300489..72675392 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -23,7 +23,12 @@ const static char wordSeparator = ' '; words_mt SeparateWords(const std::string& phrase) { const size_t index = phrase.find_first_of(wordSeparator); - return {{phrase.substr(0, index), 1}}; + + if (index != std::string::npos) + { + return {{phrase.substr(0, index), 1}}; + } + return {{phrase, 1}}; } TEST(WordsCount, TestSeparateFirstWord) From aa48d05aea905270a48b7c3186010ad576fb5c8d Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 10:26:08 +0200 Subject: [PATCH 15/72] Red --- tdd_intro/demo/02_word_count/test.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 72675392..c0925593 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -34,7 +34,20 @@ words_mt SeparateWords(const std::string& phrase) TEST(WordsCount, TestSeparateFirstWord) { words_mt words = SeparateWords("hello"); - ASSERT_EQ(1, words.size()); + EXPECT_EQ(1, words.size()); EXPECT_TRUE(words.find("hello") != words.end()); EXPECT_EQ(1, words["hello"]); } + +TEST(WordsCount, TestSeparateSeveralWords) +{ + words_mt words = SeparateWords("hello bro"); + EXPECT_EQ(2, words.size()); + + EXPECT_TRUE(words.find("hello") != words.end()); + EXPECT_EQ(1, words["hello"]); + + EXPECT_TRUE(words.find("bro") != words.end()); + EXPECT_EQ(1, words["bro"]); + +} From 47c09e6fe27c9edd8492441dfff2d238130f2931 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 10:48:47 +0200 Subject: [PATCH 16/72] Green --- tdd_intro/demo/02_word_count/test.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index c0925593..24681f25 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -22,13 +22,26 @@ const static char wordSeparator = ' '; words_mt SeparateWords(const std::string& phrase) { - const size_t index = phrase.find_first_of(wordSeparator); + std::string separatedPhrase = phrase; + words_mt words; + size_t wordEndIndex = 0; - if (index != std::string::npos) + do { - return {{phrase.substr(0, index), 1}}; + separatedPhrase = phrase.substr(wordEndIndex, phrase.size() - wordEndIndex); + wordEndIndex = separatedPhrase.find_first_of(wordSeparator); + if (wordEndIndex == std::string::npos) + { + wordEndIndex = phrase.size(); + } + + std::string word = separatedPhrase.substr(0, wordEndIndex); + ++words[word]; + ++wordEndIndex; } - return {{phrase, 1}}; + while(wordEndIndex < phrase.size()); + + return words; } TEST(WordsCount, TestSeparateFirstWord) @@ -49,5 +62,4 @@ TEST(WordsCount, TestSeparateSeveralWords) EXPECT_TRUE(words.find("bro") != words.end()); EXPECT_EQ(1, words["bro"]); - } From cfcbb1d9e4ba8193ce997bfb601e1ed4956a8e04 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 10:53:05 +0200 Subject: [PATCH 17/72] Red --- tdd_intro/demo/02_word_count/test.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 24681f25..c8ea4ed6 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -52,6 +52,17 @@ TEST(WordsCount, TestSeparateFirstWord) EXPECT_EQ(1, words["hello"]); } +TEST(WordsCount, TestTrimWord) +{ + std::string phrase = "tdd course"; + + EXPECT_EQ("tdd", TrimWord(phrase, ' ')); + EXPECT_EQ("course", phrase); + + EXPECT_EQ("course", TrimWord(phrase, ' ')); + EXPECT_EQ("course", phrase); +} + TEST(WordsCount, TestSeparateSeveralWords) { words_mt words = SeparateWords("hello bro"); From c6d369a9add8ec7db6a46fabc9dc8735531eb365 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 11:00:34 +0200 Subject: [PATCH 18/72] Green --- tdd_intro/demo/02_word_count/test.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index c8ea4ed6..9a034053 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -20,6 +20,25 @@ such: 1 using words_mt = std::map; const static char wordSeparator = ' '; +std::string TrimWord(std::string& phrase, const char seperator) +{ + size_t index = phrase.find_first_of(seperator); + std::string word; + + if (index == std::string::npos || index == phrase.size()) + { + word = phrase; + phrase.clear(); + } + else + { + word = phrase.substr(0, index); + phrase = phrase.substr(index + 1, phrase.size() - index); + } + + return word; +} + words_mt SeparateWords(const std::string& phrase) { std::string separatedPhrase = phrase; @@ -60,7 +79,7 @@ TEST(WordsCount, TestTrimWord) EXPECT_EQ("course", phrase); EXPECT_EQ("course", TrimWord(phrase, ' ')); - EXPECT_EQ("course", phrase); + EXPECT_EQ("", phrase); } TEST(WordsCount, TestSeparateSeveralWords) From c4d0e8da32c7436f168ce9361b28251a732daec7 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 11:09:01 +0200 Subject: [PATCH 19/72] Refactoring --- tdd_intro/demo/02_word_count/test.cpp | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 9a034053..e525adcc 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -41,24 +41,14 @@ std::string TrimWord(std::string& phrase, const char seperator) words_mt SeparateWords(const std::string& phrase) { - std::string separatedPhrase = phrase; words_mt words; - size_t wordEndIndex = 0; + std::string currentPhrase = phrase; + std::string curentWord; - do + while (!(curentWord = TrimWord(currentPhrase, wordSeparator)).empty()) { - separatedPhrase = phrase.substr(wordEndIndex, phrase.size() - wordEndIndex); - wordEndIndex = separatedPhrase.find_first_of(wordSeparator); - if (wordEndIndex == std::string::npos) - { - wordEndIndex = phrase.size(); - } - - std::string word = separatedPhrase.substr(0, wordEndIndex); - ++words[word]; - ++wordEndIndex; + ++words[curentWord]; } - while(wordEndIndex < phrase.size()); return words; } @@ -71,7 +61,7 @@ TEST(WordsCount, TestSeparateFirstWord) EXPECT_EQ(1, words["hello"]); } -TEST(WordsCount, TestTrimWord) +TEST(WordsCount, TestTrimOneWord) { std::string phrase = "tdd course"; From 1b77c23578f948c3a1f974579e42b8e569d699d0 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 11:10:32 +0200 Subject: [PATCH 20/72] Red --- tdd_intro/demo/02_word_count/test.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index e525adcc..760f7e80 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -64,12 +64,14 @@ TEST(WordsCount, TestSeparateFirstWord) TEST(WordsCount, TestTrimOneWord) { std::string phrase = "tdd course"; - - EXPECT_EQ("tdd", TrimWord(phrase, ' ')); + std::string word; + EXPECT_TRUE(TrimWord(phrase, word, ' ')); EXPECT_EQ("course", phrase); - EXPECT_EQ("course", TrimWord(phrase, ' ')); + EXPECT_TRUE(TrimWord(phrase, word, ' ')); EXPECT_EQ("", phrase); + + EXPECT_FALSE(TrimWord(phrase, word, ' ')); } TEST(WordsCount, TestSeparateSeveralWords) From 66d9de7e6c5d543f12afd53c417399da749d578c Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 11:14:08 +0200 Subject: [PATCH 21/72] Green --- tdd_intro/demo/02_word_count/test.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 760f7e80..21f635be 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -20,11 +20,14 @@ such: 1 using words_mt = std::map; const static char wordSeparator = ' '; -std::string TrimWord(std::string& phrase, const char seperator) +bool TrimWord(std::string& phrase, std::string& word, const char seperator) { - size_t index = phrase.find_first_of(seperator); - std::string word; + if (phrase.empty()) + { + return false; + } + size_t index = phrase.find_first_of(seperator); if (index == std::string::npos || index == phrase.size()) { word = phrase; @@ -36,7 +39,7 @@ std::string TrimWord(std::string& phrase, const char seperator) phrase = phrase.substr(index + 1, phrase.size() - index); } - return word; + return true; } words_mt SeparateWords(const std::string& phrase) @@ -45,7 +48,7 @@ words_mt SeparateWords(const std::string& phrase) std::string currentPhrase = phrase; std::string curentWord; - while (!(curentWord = TrimWord(currentPhrase, wordSeparator)).empty()) + while (TrimWord(currentPhrase, curentWord, wordSeparator)) { ++words[curentWord]; } From e90471676fb4d97d8506856ebeeaa50b5e0587f5 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 11:18:12 +0200 Subject: [PATCH 22/72] Added several green tests --- tdd_intro/demo/02_word_count/test.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 21f635be..59f6bf08 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -88,3 +88,27 @@ TEST(WordsCount, TestSeparateSeveralWords) EXPECT_TRUE(words.find("bro") != words.end()); EXPECT_EQ(1, words["bro"]); } + +TEST(WordsCount, TestSeparateSeveralSameWords) +{ + words_mt words = SeparateWords("hello bro hello"); + + EXPECT_EQ(2, words.size()); + EXPECT_TRUE(words.find("hello") != words.end()); + EXPECT_EQ(2, words["hello"]); +} + +TEST(WordsCount, TestEmptyString) +{ + words_mt words = SeparateWords(""); + EXPECT_TRUE(words.empty()); +} + +TEST(WordsCount, TestSeparatorInEnd) +{ + words_mt words = SeparateWords("hello "); + + EXPECT_EQ(1, words.size()); + EXPECT_TRUE(words.find("hello") != words.end()); + EXPECT_EQ(1, words["hello"]); +} From f1b20f51d04f1958bb83afd982b4e99606a33ba5 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 11:22:22 +0200 Subject: [PATCH 23/72] AcceptanceTest --- tdd_intro/demo/02_word_count/test.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 59f6bf08..311def6f 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -112,3 +112,21 @@ TEST(WordsCount, TestSeparatorInEnd) EXPECT_TRUE(words.find("hello") != words.end()); EXPECT_EQ(1, words["hello"]); } + +TEST(WordsCount, AcceptanceTest) +{ + words_mt words = SeparateWords("olly olly in come free please please let it be in such manner olly"); + + ASSERT_EQ(10, words.size()); + + ASSERT_EQ(3, words["olly"]); + ASSERT_EQ(2, words["in"]); + ASSERT_EQ(1, words["come"]); + ASSERT_EQ(1, words["free"]); + ASSERT_EQ(2, words["please"]); + ASSERT_EQ(1, words["let"]); + ASSERT_EQ(1, words["it"]); + ASSERT_EQ(1, words["be"]); + ASSERT_EQ(1, words["manner"]); + ASSERT_EQ(1, words["such"]); +} From 4080ca57e3ace9fa3e5b522631e75c97342c0dc0 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 11:32:23 +0200 Subject: [PATCH 24/72] Refactoring tests --- tdd_intro/demo/02_word_count/test.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 311def6f..e8ee188b 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -77,7 +77,7 @@ TEST(WordsCount, TestTrimOneWord) EXPECT_FALSE(TrimWord(phrase, word, ' ')); } -TEST(WordsCount, TestSeparateSeveralWords) +TEST(WordsCount, TestCountSeveralDifferentWords) { words_mt words = SeparateWords("hello bro"); EXPECT_EQ(2, words.size()); @@ -89,7 +89,7 @@ TEST(WordsCount, TestSeparateSeveralWords) EXPECT_EQ(1, words["bro"]); } -TEST(WordsCount, TestSeparateSeveralSameWords) +TEST(WordsCount, TestCountSeveralSameWords) { words_mt words = SeparateWords("hello bro hello"); @@ -98,13 +98,13 @@ TEST(WordsCount, TestSeparateSeveralSameWords) EXPECT_EQ(2, words["hello"]); } -TEST(WordsCount, TestEmptyString) +TEST(WordsCount, TestNoWordsWhenEmptyInput) { words_mt words = SeparateWords(""); EXPECT_TRUE(words.empty()); } -TEST(WordsCount, TestSeparatorInEnd) +TEST(WordsCount, TestOneWordWhenSeparatorInEnd) { words_mt words = SeparateWords("hello "); From 0e34d7e1667b90aced725a6aff6904f83298a906 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Tue, 13 Nov 2018 12:47:18 +0200 Subject: [PATCH 25/72] Red --- tdd_intro/homework/02_ternary_numbers/test.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index 17503028..dae5d43e 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -16,3 +16,13 @@ The last place in a ternary number is the 1's place. The second to last is the 3 If your language provides a method in the standard library to perform the conversion, pretend it doesn't exist and implement it yourself. */ + +size_t ConvertTernaryToDecimal(size_t numerical, size_t position) +{ + +} + +TEST(TernaryNumber, ConvertTernaryNum0_Pos0_ToDecimal) +{ + EXPECT_EQ(0, ConvertTernaryToDecimal(0, 0)); +} From 98552d9baa6cc7cc34bc1b9584fe4d4d7e251ded Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Tue, 13 Nov 2018 13:00:02 +0200 Subject: [PATCH 26/72] Green + refactoring --- tdd_intro/homework/02_ternary_numbers/test.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index dae5d43e..5aade477 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -1,4 +1,5 @@ #include +#include /* Convert a ternary number, represented as a string (e.g. '102012'), to its decimal equivalent using first principles. @@ -19,10 +20,13 @@ If your language provides a method in the standard library to perform the conver size_t ConvertTernaryToDecimal(size_t numerical, size_t position) { - + size_t pow = std::pow(3, position); + return pow * numerical; } -TEST(TernaryNumber, ConvertTernaryNum0_Pos0_ToDecimal) +TEST(TernaryNumber, ConvertCorrectTernaryNumerical_Pos0_ToDecimal) { EXPECT_EQ(0, ConvertTernaryToDecimal(0, 0)); + EXPECT_EQ(1, ConvertTernaryToDecimal(1, 0)); + EXPECT_EQ(2, ConvertTernaryToDecimal(2, 0)); } From fb099752f2c35dbf741fb28a7a6bcf4ab80c5557 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Tue, 13 Nov 2018 13:04:04 +0200 Subject: [PATCH 27/72] Red --- tdd_intro/homework/02_ternary_numbers/test.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index 5aade477..9d0429f3 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -24,9 +24,16 @@ size_t ConvertTernaryToDecimal(size_t numerical, size_t position) return pow * numerical; } -TEST(TernaryNumber, ConvertCorrectTernaryNumerical_Pos0_ToDecimal) +TEST(TernaryNumber, TestConvertCorrectTernaryNumerical_Pos0_ToDecimal) { EXPECT_EQ(0, ConvertTernaryToDecimal(0, 0)); EXPECT_EQ(1, ConvertTernaryToDecimal(1, 0)); EXPECT_EQ(2, ConvertTernaryToDecimal(2, 0)); } + +TEST(TernaryNumber, TestConvertInvalidTernaryNumerical) +{ + EXPECT_EQ(0, ConvertTernaryToDecimal(4, 0)); + EXPECT_EQ(0, ConvertTernaryToDecimal(5, 0)); + EXPECT_EQ(0, ConvertTernaryToDecimal(20, 0)); +} From 99cdcddbf3bd29d3ba689c113a099deb6f3f3636 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Tue, 13 Nov 2018 13:11:14 +0200 Subject: [PATCH 28/72] Green --- tdd_intro/homework/02_ternary_numbers/test.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index 9d0429f3..8de2ae73 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -20,6 +20,11 @@ If your language provides a method in the standard library to perform the conver size_t ConvertTernaryToDecimal(size_t numerical, size_t position) { + if(numerical > 2) + { + return 0; + } + size_t pow = std::pow(3, position); return pow * numerical; } From a90cf74e04b90fba929635977dd9744453511ff6 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Tue, 13 Nov 2018 13:19:26 +0200 Subject: [PATCH 29/72] Refactoring --- tdd_intro/homework/02_ternary_numbers/test.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index 8de2ae73..1fb434b0 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -29,13 +29,20 @@ size_t ConvertTernaryToDecimal(size_t numerical, size_t position) return pow * numerical; } -TEST(TernaryNumber, TestConvertCorrectTernaryNumerical_Pos0_ToDecimal) +TEST(TernaryNumber, TestConvertTernaryNum_Pos0_ToDecimal) { EXPECT_EQ(0, ConvertTernaryToDecimal(0, 0)); EXPECT_EQ(1, ConvertTernaryToDecimal(1, 0)); EXPECT_EQ(2, ConvertTernaryToDecimal(2, 0)); } +TEST(TernaryNumber, TestConvertTernaryNum_Larger0_ToDecimal) +{ + EXPECT_EQ(3, ConvertTernaryToDecimal(1, 1)); + EXPECT_EQ(0, ConvertTernaryToDecimal(0, 2)); + EXPECT_EQ(54, ConvertTernaryToDecimal(2, 3)); +} + TEST(TernaryNumber, TestConvertInvalidTernaryNumerical) { EXPECT_EQ(0, ConvertTernaryToDecimal(4, 0)); From 9ff8ab3383d10c0fb54c211c6c7c597c50d1e01e Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Tue, 13 Nov 2018 13:28:48 +0200 Subject: [PATCH 30/72] Red --- tdd_intro/homework/02_ternary_numbers/test.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index 1fb434b0..f046430c 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -1,5 +1,6 @@ #include #include +#include /* Convert a ternary number, represented as a string (e.g. '102012'), to its decimal equivalent using first principles. @@ -29,6 +30,11 @@ size_t ConvertTernaryToDecimal(size_t numerical, size_t position) return pow * numerical; } +size_t ConvertTernaryToDecimal(const std::string& ternary) +{ + return 0; +} + TEST(TernaryNumber, TestConvertTernaryNum_Pos0_ToDecimal) { EXPECT_EQ(0, ConvertTernaryToDecimal(0, 0)); @@ -49,3 +55,8 @@ TEST(TernaryNumber, TestConvertInvalidTernaryNumerical) EXPECT_EQ(0, ConvertTernaryToDecimal(5, 0)); EXPECT_EQ(0, ConvertTernaryToDecimal(20, 0)); } + +TEST(TernaryNumber, ConvertFirstTernaryNumFromString) +{ + EXPECT_EQ(1, ConvertTernaryToDecimal("1")); +} From 3cd4607e29f407c3bdf08ef731cb67673432b377 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Tue, 13 Nov 2018 18:50:51 +0200 Subject: [PATCH 31/72] Green --- tdd_intro/homework/02_ternary_numbers/test.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index f046430c..5043f076 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -32,7 +32,8 @@ size_t ConvertTernaryToDecimal(size_t numerical, size_t position) size_t ConvertTernaryToDecimal(const std::string& ternary) { - return 0; + char sync = ternary[0]; + return std::atoi(&sync); } TEST(TernaryNumber, TestConvertTernaryNum_Pos0_ToDecimal) From 9b15365dac62519c7024adb9b15a4fbc7444c81a Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 17:39:18 +0200 Subject: [PATCH 32/72] Refactoring --- tdd_intro/homework/02_ternary_numbers/test.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index 5043f076..c0e2e21f 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -32,8 +32,9 @@ size_t ConvertTernaryToDecimal(size_t numerical, size_t position) size_t ConvertTernaryToDecimal(const std::string& ternary) { - char sync = ternary[0]; - return std::atoi(&sync); + char symbChar = ternary[0]; + int numb = std::atoi(&symbChar); + return ConvertTernaryToDecimal(static_cast(numb), 0); } TEST(TernaryNumber, TestConvertTernaryNum_Pos0_ToDecimal) @@ -50,14 +51,17 @@ TEST(TernaryNumber, TestConvertTernaryNum_Larger0_ToDecimal) EXPECT_EQ(54, ConvertTernaryToDecimal(2, 3)); } -TEST(TernaryNumber, TestConvertInvalidTernaryNumerical) +TEST(TernaryNumber, TestConvertInvalidTernary) { EXPECT_EQ(0, ConvertTernaryToDecimal(4, 0)); EXPECT_EQ(0, ConvertTernaryToDecimal(5, 0)); EXPECT_EQ(0, ConvertTernaryToDecimal(20, 0)); } -TEST(TernaryNumber, ConvertFirstTernaryNumFromString) +TEST(TernaryNumber, TestConvertOneTernaryNumericalStrToDecimal) { + EXPECT_EQ(0, ConvertTernaryToDecimal("0")); EXPECT_EQ(1, ConvertTernaryToDecimal("1")); + EXPECT_EQ(2, ConvertTernaryToDecimal("2")); + EXPECT_EQ(0, ConvertTernaryToDecimal("3")); } From c8073c78b7e699b418976a57a42cff09a307ac46 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 17:40:34 +0200 Subject: [PATCH 33/72] Red --- tdd_intro/homework/02_ternary_numbers/test.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index c0e2e21f..c702c284 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -65,3 +65,8 @@ TEST(TernaryNumber, TestConvertOneTernaryNumericalStrToDecimal) EXPECT_EQ(2, ConvertTernaryToDecimal("2")); EXPECT_EQ(0, ConvertTernaryToDecimal("3")); } + +TEST(TernaryNumber, TestConvertTernaryStrToDecimal) +{ + EXPECT_EQ(302, ConvertTernaryToDecimal("102012")); +} From 944f4c703062d98e9f4a2df547d7875dfb824c06 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 17:47:25 +0200 Subject: [PATCH 34/72] Green --- tdd_intro/homework/02_ternary_numbers/test.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index c702c284..acaa8360 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -32,9 +32,15 @@ size_t ConvertTernaryToDecimal(size_t numerical, size_t position) size_t ConvertTernaryToDecimal(const std::string& ternary) { - char symbChar = ternary[0]; - int numb = std::atoi(&symbChar); - return ConvertTernaryToDecimal(static_cast(numb), 0); + size_t result = 0; + size_t position = ternary.size(); + for (const char ternarySymbChar: ternary) + { + int ternaryNumb = std::atoi(&ternarySymbChar); + result += ConvertTernaryToDecimal(static_cast(ternaryNumb), --position); + } + + return result; } TEST(TernaryNumber, TestConvertTernaryNum_Pos0_ToDecimal) From a004cfcd1666655a4c14e98592b0b2070879192f Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 17:49:38 +0200 Subject: [PATCH 35/72] Added some test --- tdd_intro/homework/02_ternary_numbers/test.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index acaa8360..2ce4093b 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -26,8 +26,8 @@ size_t ConvertTernaryToDecimal(size_t numerical, size_t position) return 0; } - size_t pow = std::pow(3, position); - return pow * numerical; + double ternaryPow = std::pow(3, position); + return static_cast(ternaryPow) * numerical; } size_t ConvertTernaryToDecimal(const std::string& ternary) @@ -76,3 +76,8 @@ TEST(TernaryNumber, TestConvertTernaryStrToDecimal) { EXPECT_EQ(302, ConvertTernaryToDecimal("102012")); } + +TEST(TernaryNumber, TestEmptyTernaryString) +{ + EXPECT_EQ(0, ConvertTernaryToDecimal("")); +} From afdf9fb77366ae00ebe64069d41ec1eefbce61fb Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 17:51:18 +0200 Subject: [PATCH 36/72] Added some test --- tdd_intro/homework/02_ternary_numbers/test.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index 2ce4093b..0c4f30c9 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -81,3 +81,8 @@ TEST(TernaryNumber, TestEmptyTernaryString) { EXPECT_EQ(0, ConvertTernaryToDecimal("")); } + +TEST(TernaryNumber, TestIncorrectTernaryNumberInString) +{ + EXPECT_EQ(302, ConvertTernaryToDecimal("192612")); +} From 456014fe42a2209c07b193174016e5cdb697f469 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 18:09:39 +0200 Subject: [PATCH 37/72] Refactoring --- tdd_intro/homework/02_ternary_numbers/test.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index 0c4f30c9..2245f55e 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -85,4 +85,5 @@ TEST(TernaryNumber, TestEmptyTernaryString) TEST(TernaryNumber, TestIncorrectTernaryNumberInString) { EXPECT_EQ(302, ConvertTernaryToDecimal("192612")); + EXPECT_EQ(302, ConvertTernaryToDecimal("1*2%12")); } From f417ef67c42c9c9cae17cf92b61acee31457818a Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 19:11:49 +0200 Subject: [PATCH 38/72] Red --- tdd_intro/homework/03_bank_ocr/test.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index a01540b9..a2b6e5dc 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -195,3 +195,13 @@ const Display s_display123456789 = { " _ _ _ _ _ _ _ ", " | _| _||_||_ |_ ||_||_|", " ||_ _| | _||_| ||_| _|" }; + +size_t DetectDigit(const Digit& digit) +{ + return 0; +} + +TEST(BankOcr, TestDetectDigit9) +{ + EXPECT_EQ(9, DetectDigit(s_digit9)); +} From 6f1e44a063734945de97db4395e88ce6ccd140fb Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 19:26:54 +0200 Subject: [PATCH 39/72] Green --- tdd_intro/homework/03_bank_ocr/test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index a2b6e5dc..ac43e28e 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -198,7 +198,7 @@ const Display s_display123456789 = { " _ _ _ _ _ _ _ ", size_t DetectDigit(const Digit& digit) { - return 0; + return 9; } TEST(BankOcr, TestDetectDigit9) From d4daf34edbe1750b80e523ceaa733db83d30c250 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 19:29:46 +0200 Subject: [PATCH 40/72] Red --- tdd_intro/homework/03_bank_ocr/test.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index ac43e28e..0720bcd9 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -205,3 +205,8 @@ TEST(BankOcr, TestDetectDigit9) { EXPECT_EQ(9, DetectDigit(s_digit9)); } + +TEST(BankOcr, TestDetectDigit8) +{ + EXPECT_EQ(8, DetectDigit(s_digit8)); +} From c8c79c79ed218a977f973df84ab4658b6d3d0bd7 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 19:35:21 +0200 Subject: [PATCH 41/72] Green --- tdd_intro/homework/03_bank_ocr/test.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index 0720bcd9..9da158c4 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -100,6 +100,19 @@ struct Display std::string lines[g_linesInDigit]; }; +std::map g_digitsMap{ + { " _ | ||_|", 0}, + { " | |", 1}, + { " _ _||_ ", 2}, + { " _ _| _|", 3}, + { " |_| |", 4}, + { " _ |_ _|", 5}, + { " _ |_ |_|", 6}, + { " _ | |", 7}, + { " _ |_||_|", 8}, + { " _ |_| _|", 9} +}; + const Digit s_digit0 = { " _ ", "| |", "|_|" @@ -198,7 +211,8 @@ const Display s_display123456789 = { " _ _ _ _ _ _ _ ", size_t DetectDigit(const Digit& digit) { - return 9; + std::string digitHash = digit.lines[0] + digit.lines[1] + digit.lines[2]; + return g_digitsMap[digitHash]; } TEST(BankOcr, TestDetectDigit9) From 23dea11b5453516a61f17750cbba7d55fdf8b403 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 19:36:46 +0200 Subject: [PATCH 42/72] Refactoring --- tdd_intro/homework/03_bank_ocr/test.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index 9da158c4..f8068e8c 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -211,16 +211,20 @@ const Display s_display123456789 = { " _ _ _ _ _ _ _ ", size_t DetectDigit(const Digit& digit) { - std::string digitHash = digit.lines[0] + digit.lines[1] + digit.lines[2]; + const std::string digitHash = digit.lines[0] + digit.lines[1] + digit.lines[2]; return g_digitsMap[digitHash]; } -TEST(BankOcr, TestDetectDigit9) -{ - EXPECT_EQ(9, DetectDigit(s_digit9)); -} - -TEST(BankOcr, TestDetectDigit8) +TEST(BankOcr, TestDetectValidDigits) { + EXPECT_EQ(0, DetectDigit(s_digit0)); + EXPECT_EQ(1, DetectDigit(s_digit1)); + EXPECT_EQ(2, DetectDigit(s_digit2)); + EXPECT_EQ(3, DetectDigit(s_digit3)); + EXPECT_EQ(4, DetectDigit(s_digit4)); + EXPECT_EQ(5, DetectDigit(s_digit5)); + EXPECT_EQ(6, DetectDigit(s_digit6)); + EXPECT_EQ(7, DetectDigit(s_digit7)); EXPECT_EQ(8, DetectDigit(s_digit8)); + EXPECT_EQ(9, DetectDigit(s_digit9)); } From 73f188edadb6a59e9bdd539b281a50f38ccdbb94 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 19:39:26 +0200 Subject: [PATCH 43/72] Red --- tdd_intro/homework/03_bank_ocr/test.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index f8068e8c..6f7e19e7 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -228,3 +228,12 @@ TEST(BankOcr, TestDetectValidDigits) EXPECT_EQ(8, DetectDigit(s_digit8)); EXPECT_EQ(9, DetectDigit(s_digit9)); } + +TEST(BankOcr, TestThrowExceptionWhenDetectInvalidDigits) +{ + const static Digit invalidDigit = { " _ ", + "| |", + "| |"}; + + EXPECT_THROW(DetectDigit(invalidDigit), std::runtime_error); +} From 858047c98345773ace0d6cd2b3b14cea0ed2bb10 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 19:41:48 +0200 Subject: [PATCH 44/72] Green --- tdd_intro/homework/03_bank_ocr/test.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index 6f7e19e7..82b409d5 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -212,7 +212,14 @@ const Display s_display123456789 = { " _ _ _ _ _ _ _ ", size_t DetectDigit(const Digit& digit) { const std::string digitHash = digit.lines[0] + digit.lines[1] + digit.lines[2]; - return g_digitsMap[digitHash]; + + const auto& value = g_digitsMap.find(digitHash); + if (value != g_digitsMap.end()) + { + return g_digitsMap[digitHash]; + } + + throw std::runtime_error("Invalid digit format"); } TEST(BankOcr, TestDetectValidDigits) @@ -232,8 +239,8 @@ TEST(BankOcr, TestDetectValidDigits) TEST(BankOcr, TestThrowExceptionWhenDetectInvalidDigits) { const static Digit invalidDigit = { " _ ", - "| |", - "| |"}; + "| |", + "| |"}; EXPECT_THROW(DetectDigit(invalidDigit), std::runtime_error); } From 967c443a1df943177f42274a2df26e437db87abb Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 19:50:24 +0200 Subject: [PATCH 45/72] Red --- tdd_intro/homework/03_bank_ocr/test.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index 82b409d5..d3e09926 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -222,6 +222,11 @@ size_t DetectDigit(const Digit& digit) throw std::runtime_error("Invalid digit format"); } +size_t ParseDigits(const Display& display) +{ + return 1; +} + TEST(BankOcr, TestDetectValidDigits) { EXPECT_EQ(0, DetectDigit(s_digit0)); @@ -244,3 +249,8 @@ TEST(BankOcr, TestThrowExceptionWhenDetectInvalidDigits) EXPECT_THROW(DetectDigit(invalidDigit), std::runtime_error); } + +TEST(BankOcr, TestParseFirstDigitFromDisplay0) +{ + EXPECT_EQ(0, ParseDigits(s_displayAll0)); +} From 550d7fd615fb25f964af7328b273d9145e8c7d28 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 19:53:38 +0200 Subject: [PATCH 46/72] Refactoring --- tdd_intro/homework/03_bank_ocr/test.cpp | 48 ++++++++++++------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index d3e09926..f4f6ad98 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -101,16 +101,16 @@ struct Display }; std::map g_digitsMap{ - { " _ | ||_|", 0}, - { " | |", 1}, - { " _ _||_ ", 2}, - { " _ _| _|", 3}, - { " |_| |", 4}, - { " _ |_ _|", 5}, - { " _ |_ |_|", 6}, - { " _ | |", 7}, - { " _ |_||_|", 8}, - { " _ |_| _|", 9} + { " _ | ||_|", '0'}, + { " | |", '1'}, + { " _ _||_ ", '2'}, + { " _ _| _|", '3'}, + { " |_| |", '4'}, + { " _ |_ _|", '5'}, + { " _ |_ |_|", '6'}, + { " _ | |", '7'}, + { " _ |_||_|", '8'}, + { " _ |_| _|", '9'} }; const Digit s_digit0 = { " _ ", @@ -209,7 +209,7 @@ const Display s_display123456789 = { " _ _ _ _ _ _ _ ", " ||_ _| | _||_| ||_| _|" }; -size_t DetectDigit(const Digit& digit) +char DetectDigit(const Digit& digit) { const std::string digitHash = digit.lines[0] + digit.lines[1] + digit.lines[2]; @@ -222,23 +222,23 @@ size_t DetectDigit(const Digit& digit) throw std::runtime_error("Invalid digit format"); } -size_t ParseDigits(const Display& display) +std::string ParseDigits(const Display& display) { - return 1; + return "0"; } TEST(BankOcr, TestDetectValidDigits) { - EXPECT_EQ(0, DetectDigit(s_digit0)); - EXPECT_EQ(1, DetectDigit(s_digit1)); - EXPECT_EQ(2, DetectDigit(s_digit2)); - EXPECT_EQ(3, DetectDigit(s_digit3)); - EXPECT_EQ(4, DetectDigit(s_digit4)); - EXPECT_EQ(5, DetectDigit(s_digit5)); - EXPECT_EQ(6, DetectDigit(s_digit6)); - EXPECT_EQ(7, DetectDigit(s_digit7)); - EXPECT_EQ(8, DetectDigit(s_digit8)); - EXPECT_EQ(9, DetectDigit(s_digit9)); + EXPECT_EQ('0', DetectDigit(s_digit0)); + EXPECT_EQ('1', DetectDigit(s_digit1)); + EXPECT_EQ('2', DetectDigit(s_digit2)); + EXPECT_EQ('3', DetectDigit(s_digit3)); + EXPECT_EQ('4', DetectDigit(s_digit4)); + EXPECT_EQ('5', DetectDigit(s_digit5)); + EXPECT_EQ('6', DetectDigit(s_digit6)); + EXPECT_EQ('7', DetectDigit(s_digit7)); + EXPECT_EQ('8', DetectDigit(s_digit8)); + EXPECT_EQ('9', DetectDigit(s_digit9)); } TEST(BankOcr, TestThrowExceptionWhenDetectInvalidDigits) @@ -252,5 +252,5 @@ TEST(BankOcr, TestThrowExceptionWhenDetectInvalidDigits) TEST(BankOcr, TestParseFirstDigitFromDisplay0) { - EXPECT_EQ(0, ParseDigits(s_displayAll0)); + EXPECT_EQ('0', ParseDigits(s_displayAll0)[0]); } From 1a5875328ce0cf68c9bf64e8dd017fc9291860e2 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 20:14:06 +0200 Subject: [PATCH 47/72] Red + Green --- tdd_intro/homework/03_bank_ocr/test.cpp | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index f4f6ad98..956b382e 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -100,7 +100,7 @@ struct Display std::string lines[g_linesInDigit]; }; -std::map g_digitsMap{ +std::map g_digitsMap{ { " _ | ||_|", '0'}, { " | |", '1'}, { " _ _||_ ", '2'}, @@ -227,6 +227,18 @@ std::string ParseDigits(const Display& display) return "0"; } +Digit ParseDigitFromDisplay(const Display& display, size_t pos) +{ + Digit digit; + const size_t offset = pos * g_digitLen; + for(size_t i = 0; i < g_linesInDigit; ++i) + { + digit.lines[i] = display.lines[i].substr(offset, g_digitLen); + } + + return digit; +} + TEST(BankOcr, TestDetectValidDigits) { EXPECT_EQ('0', DetectDigit(s_digit0)); @@ -250,7 +262,16 @@ TEST(BankOcr, TestThrowExceptionWhenDetectInvalidDigits) EXPECT_THROW(DetectDigit(invalidDigit), std::runtime_error); } -TEST(BankOcr, TestParseFirstDigitFromDisplay0) +TEST(BankOcr, DISABLED_TestParseFirstDigitFromDisplay0) { EXPECT_EQ('0', ParseDigits(s_displayAll0)[0]); } + +TEST(BankOcr, TestParseDigit1FromDisplayNumber0) +{ + const Digit digit = ParseDigitFromDisplay(s_display123456789, 0); + + EXPECT_EQ(s_digit1.lines[0], digit.lines[0]); + EXPECT_EQ(s_digit1.lines[1], digit.lines[1]); + EXPECT_EQ(s_digit1.lines[2], digit.lines[2]); +} From ef3ee1801739b749e943421af3199a54267e1a5f Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 20:14:46 +0200 Subject: [PATCH 48/72] Red --- tdd_intro/homework/03_bank_ocr/test.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index 956b382e..2ce483b1 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -275,3 +275,8 @@ TEST(BankOcr, TestParseDigit1FromDisplayNumber0) EXPECT_EQ(s_digit1.lines[1], digit.lines[1]); EXPECT_EQ(s_digit1.lines[2], digit.lines[2]); } + +TEST(BankOcr, TestExceptionWhenParseUnknownDisplayOffset) +{ + EXPECT_THROW(ParseDigitFromDisplay(s_display123456789, 10), std::runtime_error); +} From a220bac50b07b7365f489c2f7ab7b8236c39700e Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 20:16:08 +0200 Subject: [PATCH 49/72] Green --- tdd_intro/homework/03_bank_ocr/test.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index 2ce483b1..c6a7db42 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -227,10 +227,15 @@ std::string ParseDigits(const Display& display) return "0"; } -Digit ParseDigitFromDisplay(const Display& display, size_t pos) +Digit ParseDigitFromDisplay(const Display& display, size_t digitPos) { + if (digitPos > 9) + { + throw std::runtime_error("Invalid display offset"); + } + Digit digit; - const size_t offset = pos * g_digitLen; + const size_t offset = digitPos * g_digitLen; for(size_t i = 0; i < g_linesInDigit; ++i) { digit.lines[i] = display.lines[i].substr(offset, g_digitLen); From 3068f7dac270f3eb2516353dd95dcd3ce1d15a85 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 20:17:11 +0200 Subject: [PATCH 50/72] Red --- tdd_intro/homework/03_bank_ocr/test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index c6a7db42..ba8994be 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -224,7 +224,7 @@ char DetectDigit(const Digit& digit) std::string ParseDigits(const Display& display) { - return "0"; + return "1"; } Digit ParseDigitFromDisplay(const Display& display, size_t digitPos) @@ -267,7 +267,7 @@ TEST(BankOcr, TestThrowExceptionWhenDetectInvalidDigits) EXPECT_THROW(DetectDigit(invalidDigit), std::runtime_error); } -TEST(BankOcr, DISABLED_TestParseFirstDigitFromDisplay0) +TEST(BankOcr, TestParseFirstDigitFromDisplay0) { EXPECT_EQ('0', ParseDigits(s_displayAll0)[0]); } From f0722932f8f27d324a12cb7cbc9d53595b8491c0 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 20:20:40 +0200 Subject: [PATCH 51/72] Green --- tdd_intro/homework/03_bank_ocr/test.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index ba8994be..348e3963 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -222,11 +222,6 @@ char DetectDigit(const Digit& digit) throw std::runtime_error("Invalid digit format"); } -std::string ParseDigits(const Display& display) -{ - return "1"; -} - Digit ParseDigitFromDisplay(const Display& display, size_t digitPos) { if (digitPos > 9) @@ -244,6 +239,16 @@ Digit ParseDigitFromDisplay(const Display& display, size_t digitPos) return digit; } + +std::string ParseDigits(const Display& display) +{ + std::string finalDigits; + Digit digit = ParseDigitFromDisplay(display, 0); + finalDigits += DetectDigit(digit); + + return finalDigits; +} + TEST(BankOcr, TestDetectValidDigits) { EXPECT_EQ('0', DetectDigit(s_digit0)); From 272870e819b4a7cf384f796d1378d6a5e69fdd07 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 20:23:34 +0200 Subject: [PATCH 52/72] Added test --- tdd_intro/homework/03_bank_ocr/test.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index 348e3963..4a8ea91f 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -286,6 +286,15 @@ TEST(BankOcr, TestParseDigit1FromDisplayNumber0) EXPECT_EQ(s_digit1.lines[2], digit.lines[2]); } +TEST(BankOcr, TestParseDigit2FromDisplayNumber2) +{ + const Digit digit = ParseDigitFromDisplay(s_display123456789, 1); + + EXPECT_EQ(s_digit2.lines[0], digit.lines[0]); + EXPECT_EQ(s_digit2.lines[1], digit.lines[1]); + EXPECT_EQ(s_digit2.lines[2], digit.lines[2]); +} + TEST(BankOcr, TestExceptionWhenParseUnknownDisplayOffset) { EXPECT_THROW(ParseDigitFromDisplay(s_display123456789, 10), std::runtime_error); From a97542adb26988dd2c7d83d2f5c42d85f1dd1b65 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 20:24:47 +0200 Subject: [PATCH 53/72] Red --- tdd_intro/homework/03_bank_ocr/test.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index 4a8ea91f..77bc8e7c 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -277,6 +277,11 @@ TEST(BankOcr, TestParseFirstDigitFromDisplay0) EXPECT_EQ('0', ParseDigits(s_displayAll0)[0]); } +TEST(BankOcr, TestParseFirstDigitFromDisplay1) +{ + EXPECT_EQ('1', ParseDigits(s_display123456789)[1]); +} + TEST(BankOcr, TestParseDigit1FromDisplayNumber0) { const Digit digit = ParseDigitFromDisplay(s_display123456789, 0); From 14f8f04340920c8f2615cc472f762b654e5211f8 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 20:30:08 +0200 Subject: [PATCH 54/72] Green --- tdd_intro/homework/03_bank_ocr/test.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index 77bc8e7c..82291e52 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -243,8 +243,11 @@ Digit ParseDigitFromDisplay(const Display& display, size_t digitPos) std::string ParseDigits(const Display& display) { std::string finalDigits; - Digit digit = ParseDigitFromDisplay(display, 0); - finalDigits += DetectDigit(digit); + for (size_t i = 0; i < g_digitsOnDisplay; ++i) + { + Digit digit = ParseDigitFromDisplay(display, i); + finalDigits += DetectDigit(digit); + } return finalDigits; } @@ -279,7 +282,7 @@ TEST(BankOcr, TestParseFirstDigitFromDisplay0) TEST(BankOcr, TestParseFirstDigitFromDisplay1) { - EXPECT_EQ('1', ParseDigits(s_display123456789)[1]); + EXPECT_EQ('2', ParseDigits(s_display123456789)[1]); } TEST(BankOcr, TestParseDigit1FromDisplayNumber0) From 7fa9305901fc109c7581384768203fa0f5b07b83 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 20:35:46 +0200 Subject: [PATCH 55/72] AcceptanceTest --- tdd_intro/homework/03_bank_ocr/test.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index 82291e52..b5fc26d4 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -307,3 +307,19 @@ TEST(BankOcr, TestExceptionWhenParseUnknownDisplayOffset) { EXPECT_THROW(ParseDigitFromDisplay(s_display123456789, 10), std::runtime_error); } + +TEST(BankOcr, AcceptanceTest) +{ + EXPECT_EQ("000000000", ParseDigits(s_displayAll0)); + EXPECT_EQ("111111111", ParseDigits(s_displayAll1)); + EXPECT_EQ("222222222", ParseDigits(s_displayAll2)); + EXPECT_EQ("333333333", ParseDigits(s_displayAll3)); + EXPECT_EQ("444444444", ParseDigits(s_displayAll4)); + EXPECT_EQ("555555555", ParseDigits(s_displayAll5)); + EXPECT_EQ("666666666", ParseDigits(s_displayAll6)); + EXPECT_EQ("777777777", ParseDigits(s_displayAll7)); + EXPECT_EQ("888888888", ParseDigits(s_displayAll8)); + EXPECT_EQ("999999999", ParseDigits(s_displayAll9)); + EXPECT_EQ("123456789", ParseDigits(s_display123456789)); + +} From a71969690a5f16c219bdb7a4b4c23c56570ec605 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Wed, 14 Nov 2018 20:40:16 +0200 Subject: [PATCH 56/72] Refactoring --- tdd_intro/homework/03_bank_ocr/test.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index b5fc26d4..248f7b7c 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -100,7 +100,7 @@ struct Display std::string lines[g_linesInDigit]; }; -std::map g_digitsMap{ +const std::map s_digitsMap{ { " _ | ||_|", '0'}, { " | |", '1'}, { " _ _||_ ", '2'}, @@ -213,10 +213,10 @@ char DetectDigit(const Digit& digit) { const std::string digitHash = digit.lines[0] + digit.lines[1] + digit.lines[2]; - const auto& value = g_digitsMap.find(digitHash); - if (value != g_digitsMap.end()) + const auto& value = s_digitsMap.find(digitHash); + if (value != s_digitsMap.end()) { - return g_digitsMap[digitHash]; + return value->second; } throw std::runtime_error("Invalid digit format"); From 532681f416474d0cebeb35b2ad0d5830e296ce08 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Thu, 22 Nov 2018 10:50:35 +0200 Subject: [PATCH 57/72] First red test --- tdd_intro/homework/04_weather_client/test.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tdd_intro/homework/04_weather_client/test.cpp b/tdd_intro/homework/04_weather_client/test.cpp index 346ea809..d9fee788 100644 --- a/tdd_intro/homework/04_weather_client/test.cpp +++ b/tdd_intro/homework/04_weather_client/test.cpp @@ -79,3 +79,17 @@ class IWeatherClient virtual double GetAverageWindDirection(IWeatherServer& server, const std::string& date) = 0; virtual double GetMaximumWindSpeed(IWeatherServer& server, const std::string& date) = 0; }; + +Weather ParseWeather(const std::string& response) +{ + Weather weather; + + return weather; +} + + +TEST(WeatherClient, TestParseTemperatureFromResponse) +{ + Weather weather = ParseWeather("1;1;1"); + EXPECT_EQ(1, weather.temperature); +} From 70c89b52ba6b3796f3d17a6d9f058fe0c6a74d55 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Thu, 22 Nov 2018 10:54:42 +0200 Subject: [PATCH 58/72] green --- tdd_intro/homework/04_weather_client/test.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tdd_intro/homework/04_weather_client/test.cpp b/tdd_intro/homework/04_weather_client/test.cpp index d9fee788..6d29619f 100644 --- a/tdd_intro/homework/04_weather_client/test.cpp +++ b/tdd_intro/homework/04_weather_client/test.cpp @@ -84,10 +84,13 @@ Weather ParseWeather(const std::string& response) { Weather weather; + int temperatureEndOffset = response.find(';'); + std::string temperature = response.substr(0, temperatureEndOffset); + weather.temperature = std::atoi(temperature.c_str()); + return weather; } - TEST(WeatherClient, TestParseTemperatureFromResponse) { Weather weather = ParseWeather("1;1;1"); From e0859a2b8185c53e550eedc512387b0aaf949572 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Thu, 22 Nov 2018 10:58:36 +0200 Subject: [PATCH 59/72] Refactoring + red test --- tdd_intro/homework/04_weather_client/test.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tdd_intro/homework/04_weather_client/test.cpp b/tdd_intro/homework/04_weather_client/test.cpp index 6d29619f..1783f753 100644 --- a/tdd_intro/homework/04_weather_client/test.cpp +++ b/tdd_intro/homework/04_weather_client/test.cpp @@ -47,6 +47,8 @@ Each line means "" : "": #include #include +static const char s_responseDataSeprator = ';'; + struct Weather { short temperature = 0; @@ -84,7 +86,7 @@ Weather ParseWeather(const std::string& response) { Weather weather; - int temperatureEndOffset = response.find(';'); + int temperatureEndOffset = response.find(s_responseDataSeprator); std::string temperature = response.substr(0, temperatureEndOffset); weather.temperature = std::atoi(temperature.c_str()); @@ -96,3 +98,8 @@ TEST(WeatherClient, TestParseTemperatureFromResponse) Weather weather = ParseWeather("1;1;1"); EXPECT_EQ(1, weather.temperature); } + +TEST(WeatherClient, TestThrowWhenCannotParseTemperature) +{ + EXPECT_THROW(ParseWeather("1"), std::runtime_error); +} From 73da9dcee17091594511741162e98308eaf18530 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Thu, 22 Nov 2018 11:01:14 +0200 Subject: [PATCH 60/72] Green --- tdd_intro/homework/04_weather_client/test.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tdd_intro/homework/04_weather_client/test.cpp b/tdd_intro/homework/04_weather_client/test.cpp index 1783f753..8d02d029 100644 --- a/tdd_intro/homework/04_weather_client/test.cpp +++ b/tdd_intro/homework/04_weather_client/test.cpp @@ -87,6 +87,11 @@ Weather ParseWeather(const std::string& response) Weather weather; int temperatureEndOffset = response.find(s_responseDataSeprator); + if (temperatureEndOffset == std::string::npos) + { + throw std::runtime_error("Cannot parse temperature"); + } + std::string temperature = response.substr(0, temperatureEndOffset); weather.temperature = std::atoi(temperature.c_str()); From d834675be5c4895d4958b4b18b43c7690d28a485 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Thu, 22 Nov 2018 11:03:09 +0200 Subject: [PATCH 61/72] Red --- tdd_intro/homework/04_weather_client/test.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tdd_intro/homework/04_weather_client/test.cpp b/tdd_intro/homework/04_weather_client/test.cpp index 8d02d029..75c28482 100644 --- a/tdd_intro/homework/04_weather_client/test.cpp +++ b/tdd_intro/homework/04_weather_client/test.cpp @@ -108,3 +108,9 @@ TEST(WeatherClient, TestThrowWhenCannotParseTemperature) { EXPECT_THROW(ParseWeather("1"), std::runtime_error); } + +TEST(WeatherClient, TestParseWindDirectionFromResponse) +{ + Weather weather = ParseWeather("1;1;1"); + EXPECT_EQ(1, weather.windDirection); +} From 82e6d5028fc27265381678db916c50580760ce37 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Thu, 22 Nov 2018 11:07:59 +0200 Subject: [PATCH 62/72] Green --- tdd_intro/homework/04_weather_client/test.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tdd_intro/homework/04_weather_client/test.cpp b/tdd_intro/homework/04_weather_client/test.cpp index 75c28482..b706fdd2 100644 --- a/tdd_intro/homework/04_weather_client/test.cpp +++ b/tdd_intro/homework/04_weather_client/test.cpp @@ -95,6 +95,10 @@ Weather ParseWeather(const std::string& response) std::string temperature = response.substr(0, temperatureEndOffset); weather.temperature = std::atoi(temperature.c_str()); + int directionEndOffset = response.find(s_responseDataSeprator, temperatureEndOffset); + std::string windDirection = response.substr(temperatureEndOffset + 1, directionEndOffset); + weather.windDirection = std::atoi(windDirection.c_str()); + return weather; } From 34862d70fa7ec98e62cf21ebec6801ae4590b661 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Thu, 22 Nov 2018 11:17:40 +0200 Subject: [PATCH 63/72] Refactoring --- tdd_intro/homework/04_weather_client/test.cpp | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/tdd_intro/homework/04_weather_client/test.cpp b/tdd_intro/homework/04_weather_client/test.cpp index b706fdd2..26c2481b 100644 --- a/tdd_intro/homework/04_weather_client/test.cpp +++ b/tdd_intro/homework/04_weather_client/test.cpp @@ -82,22 +82,25 @@ class IWeatherClient virtual double GetMaximumWindSpeed(IWeatherServer& server, const std::string& date) = 0; }; -Weather ParseWeather(const std::string& response) -{ - Weather weather; - int temperatureEndOffset = response.find(s_responseDataSeprator); - if (temperatureEndOffset == std::string::npos) +double GetLineDoble(std::istringstream& stream) +{ + std::string value; + if (!std::getline(stream, value, s_responseDataSeprator)) { - throw std::runtime_error("Cannot parse temperature"); + throw std::runtime_error("Error parsing"); } - std::string temperature = response.substr(0, temperatureEndOffset); - weather.temperature = std::atoi(temperature.c_str()); + return std::stod(value); +} + +Weather ParseWeather(const std::string& response) +{ + Weather weather; + std::istringstream responseStream(response); - int directionEndOffset = response.find(s_responseDataSeprator, temperatureEndOffset); - std::string windDirection = response.substr(temperatureEndOffset + 1, directionEndOffset); - weather.windDirection = std::atoi(windDirection.c_str()); + weather.temperature = GetLineDoble(responseStream); + weather.windDirection = GetLineDoble(responseStream); return weather; } From 6ec87e27b1e84cdf76403cd23dcfc5dcf7751b29 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Thu, 22 Nov 2018 11:18:58 +0200 Subject: [PATCH 64/72] Red --- tdd_intro/homework/04_weather_client/test.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tdd_intro/homework/04_weather_client/test.cpp b/tdd_intro/homework/04_weather_client/test.cpp index 26c2481b..ce64b6fd 100644 --- a/tdd_intro/homework/04_weather_client/test.cpp +++ b/tdd_intro/homework/04_weather_client/test.cpp @@ -121,3 +121,9 @@ TEST(WeatherClient, TestParseWindDirectionFromResponse) Weather weather = ParseWeather("1;1;1"); EXPECT_EQ(1, weather.windDirection); } + +TEST(WeatherClient, TestParseWindSpeedFromResponse) +{ + Weather weather = ParseWeather("1;1;1"); + EXPECT_EQ(1, weather.windSpeed); +} From ca60ec4c37645c873cee695e0a069681d082ac60 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Thu, 22 Nov 2018 11:19:39 +0200 Subject: [PATCH 65/72] Green --- tdd_intro/homework/04_weather_client/test.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/tdd_intro/homework/04_weather_client/test.cpp b/tdd_intro/homework/04_weather_client/test.cpp index ce64b6fd..656537fc 100644 --- a/tdd_intro/homework/04_weather_client/test.cpp +++ b/tdd_intro/homework/04_weather_client/test.cpp @@ -101,6 +101,7 @@ Weather ParseWeather(const std::string& response) weather.temperature = GetLineDoble(responseStream); weather.windDirection = GetLineDoble(responseStream); + weather.windSpeed = GetLineDoble(responseStream); return weather; } From 8a3099d547cea509d1e5b4c2582af1e64602c3c7 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Thu, 22 Nov 2018 11:23:55 +0200 Subject: [PATCH 66/72] Refactoring + next Redtest --- tdd_intro/homework/04_weather_client/test.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tdd_intro/homework/04_weather_client/test.cpp b/tdd_intro/homework/04_weather_client/test.cpp index 656537fc..2ed744c9 100644 --- a/tdd_intro/homework/04_weather_client/test.cpp +++ b/tdd_intro/homework/04_weather_client/test.cpp @@ -99,8 +99,8 @@ Weather ParseWeather(const std::string& response) Weather weather; std::istringstream responseStream(response); - weather.temperature = GetLineDoble(responseStream); - weather.windDirection = GetLineDoble(responseStream); + weather.temperature = static_cast(GetLineDoble(responseStream)); + weather.windDirection = static_cast(GetLineDoble(responseStream)); weather.windSpeed = GetLineDoble(responseStream); return weather; @@ -123,8 +123,18 @@ TEST(WeatherClient, TestParseWindDirectionFromResponse) EXPECT_EQ(1, weather.windDirection); } +TEST(WeatherClient, TestParseInvalidWindDirectionFromResponse) +{ + EXPECT_THROW(ParseWeather("1;401;1"), std::runtime_error); +} TEST(WeatherClient, TestParseWindSpeedFromResponse) { Weather weather = ParseWeather("1;1;1"); EXPECT_EQ(1, weather.windSpeed); } + +TEST(WeatherClient, TestParseDoubleWindSpeedFromResponse) +{ + Weather weather = ParseWeather("1;1;4.6"); + EXPECT_EQ(4.6, weather.windSpeed); +} From fb6d2f21363038fc6065e369cca431a314a5f4a7 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Thu, 22 Nov 2018 11:25:25 +0200 Subject: [PATCH 67/72] Green --- tdd_intro/homework/04_weather_client/test.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tdd_intro/homework/04_weather_client/test.cpp b/tdd_intro/homework/04_weather_client/test.cpp index 2ed744c9..c80c83e6 100644 --- a/tdd_intro/homework/04_weather_client/test.cpp +++ b/tdd_intro/homework/04_weather_client/test.cpp @@ -101,6 +101,11 @@ Weather ParseWeather(const std::string& response) weather.temperature = static_cast(GetLineDoble(responseStream)); weather.windDirection = static_cast(GetLineDoble(responseStream)); + if(weather.windDirection > 359) + { + throw std::runtime_error("Invalid paserd value for wind direction"); + } + weather.windSpeed = GetLineDoble(responseStream); return weather; @@ -125,7 +130,7 @@ TEST(WeatherClient, TestParseWindDirectionFromResponse) TEST(WeatherClient, TestParseInvalidWindDirectionFromResponse) { - EXPECT_THROW(ParseWeather("1;401;1"), std::runtime_error); + EXPECT_THROW(ParseWeather("1;360;1"), std::runtime_error); } TEST(WeatherClient, TestParseWindSpeedFromResponse) { From 6c1fbe733b95579f54de13b20bbd95eb885c53cf Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Thu, 22 Nov 2018 11:38:00 +0200 Subject: [PATCH 68/72] Red --- tdd_intro/homework/04_weather_client/test.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tdd_intro/homework/04_weather_client/test.cpp b/tdd_intro/homework/04_weather_client/test.cpp index c80c83e6..bd58795e 100644 --- a/tdd_intro/homework/04_weather_client/test.cpp +++ b/tdd_intro/homework/04_weather_client/test.cpp @@ -70,6 +70,12 @@ class IWeatherServer virtual std::string GetWeather(const std::string& request) = 0; }; +class MockWeatherServer : public IWeatherServer +{ +public: + MOCK_METHOD1(GetWeather, std::string(const std::string& request)); +}; + // Implement this interface class IWeatherClient { @@ -83,6 +89,36 @@ class IWeatherClient }; +class WeatherClient : public IWeatherClient +{ +public: + virtual double GetAverageTemperature(IWeatherServer& server, const std::string& date) override + { + throw std::runtime_error("not implemented"); + } + virtual double GetMinimumTemperature(IWeatherServer& server, const std::string& date) + { + throw std::runtime_error("not implemented"); + } + + virtual double GetMaximumTemperature(IWeatherServer& server, const std::string& date) override + { + throw std::runtime_error("not implemented"); + } + + virtual double GetAverageWindDirection(IWeatherServer& server, const std::string& date) override + { + throw std::runtime_error("not implemented"); + } + + virtual double GetMaximumWindSpeed(IWeatherServer& server, const std::string& date) override + { + throw std::runtime_error("not implemented"); + } +}; + + + double GetLineDoble(std::istringstream& stream) { std::string value; @@ -143,3 +179,14 @@ TEST(WeatherClient, TestParseDoubleWindSpeedFromResponse) Weather weather = ParseWeather("1;1;4.6"); EXPECT_EQ(4.6, weather.windSpeed); } + +//Test client + +TEST(WeatherClient, TestGetMinimumTemperature) +{ + MockWeatherServer server; + WeatherClient client; + + EXPECT_EQ(5, client.GetMinimumTemperature(server, "31.08.2018")); +} + From 1be25c4505415af752c505882d57c68b8feb526c Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Thu, 22 Nov 2018 11:54:09 +0200 Subject: [PATCH 69/72] Green --- tdd_intro/homework/04_weather_client/test.cpp | 85 ++++++++++++------- 1 file changed, 56 insertions(+), 29 deletions(-) diff --git a/tdd_intro/homework/04_weather_client/test.cpp b/tdd_intro/homework/04_weather_client/test.cpp index bd58795e..9d05d6ac 100644 --- a/tdd_intro/homework/04_weather_client/test.cpp +++ b/tdd_intro/homework/04_weather_client/test.cpp @@ -62,6 +62,34 @@ struct Weather } }; +double GetLineDoble(std::istringstream& stream) +{ + std::string value; + if (!std::getline(stream, value, s_responseDataSeprator)) + { + throw std::runtime_error("Error parsing"); + } + + return std::stod(value); +} + +Weather ParseWeather(const std::string& response) +{ + Weather weather; + std::istringstream responseStream(response); + + weather.temperature = static_cast(GetLineDoble(responseStream)); + weather.windDirection = static_cast(GetLineDoble(responseStream)); + if(weather.windDirection > 359) + { + throw std::runtime_error("Invalid paserd value for wind direction"); + } + + weather.windSpeed = GetLineDoble(responseStream); + + return weather; +} + class IWeatherServer { public: @@ -98,7 +126,24 @@ class WeatherClient : public IWeatherClient } virtual double GetMinimumTemperature(IWeatherServer& server, const std::string& date) { - throw std::runtime_error("not implemented"); + auto t1 = GetWeatherByDate(server, date + ";03:00").temperature; + auto t2 = GetWeatherByDate(server, date + ";09:00").temperature; + if (t2 < t1) + { + t1 = t2; + } + auto t3 = GetWeatherByDate(server, date + ";15:00").temperature; + if (t3 < t1) + { + t1 = t3; + } + auto t4 = GetWeatherByDate(server, date + ";21:00").temperature; + if (t4 < t1) + { + t1 = t4; + } + + return t1; } virtual double GetMaximumTemperature(IWeatherServer& server, const std::string& date) override @@ -115,37 +160,14 @@ class WeatherClient : public IWeatherClient { throw std::runtime_error("not implemented"); } -}; - - -double GetLineDoble(std::istringstream& stream) -{ - std::string value; - if (!std::getline(stream, value, s_responseDataSeprator)) +private: + Weather GetWeatherByDate(IWeatherServer& server, const std::string& date) { - throw std::runtime_error("Error parsing"); + std::string response = server.GetWeather(date); + return ParseWeather(response); } - - return std::stod(value); -} - -Weather ParseWeather(const std::string& response) -{ - Weather weather; - std::istringstream responseStream(response); - - weather.temperature = static_cast(GetLineDoble(responseStream)); - weather.windDirection = static_cast(GetLineDoble(responseStream)); - if(weather.windDirection > 359) - { - throw std::runtime_error("Invalid paserd value for wind direction"); - } - - weather.windSpeed = GetLineDoble(responseStream); - - return weather; -} +}; TEST(WeatherClient, TestParseTemperatureFromResponse) { @@ -187,6 +209,11 @@ TEST(WeatherClient, TestGetMinimumTemperature) MockWeatherServer server; WeatherClient client; + EXPECT_CALL(server, GetWeather("31.08.2018;03:00")).WillOnce(testing::Return("20;0;0")); + EXPECT_CALL(server, GetWeather("31.08.2018;09:00")).WillOnce(testing::Return("6;0;0")); + EXPECT_CALL(server, GetWeather("31.08.2018;15:00")).WillOnce(testing::Return("7;0;0")); + EXPECT_CALL(server, GetWeather("31.08.2018;21:00")).WillOnce(testing::Return("5;0;0")); + EXPECT_EQ(5, client.GetMinimumTemperature(server, "31.08.2018")); } From 3e857a5f36a7c37193d3ea6146e638f21e071df3 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Thu, 22 Nov 2018 12:02:22 +0200 Subject: [PATCH 70/72] Refactoring --- tdd_intro/homework/04_weather_client/test.cpp | 32 ++++++++----------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/tdd_intro/homework/04_weather_client/test.cpp b/tdd_intro/homework/04_weather_client/test.cpp index 9d05d6ac..20c498c1 100644 --- a/tdd_intro/homework/04_weather_client/test.cpp +++ b/tdd_intro/homework/04_weather_client/test.cpp @@ -49,6 +49,11 @@ Each line means "" : "": static const char s_responseDataSeprator = ';'; +static const std::string s_nineAM = "09:00"; +static const std::string s_ninePM = "21:00"; +static const std::string s_threeAM = "03:00"; +static const std::string s_threePM = "15:00"; + struct Weather { short temperature = 0; @@ -126,24 +131,13 @@ class WeatherClient : public IWeatherClient } virtual double GetMinimumTemperature(IWeatherServer& server, const std::string& date) { - auto t1 = GetWeatherByDate(server, date + ";03:00").temperature; - auto t2 = GetWeatherByDate(server, date + ";09:00").temperature; - if (t2 < t1) - { - t1 = t2; - } - auto t3 = GetWeatherByDate(server, date + ";15:00").temperature; - if (t3 < t1) - { - t1 = t3; - } - auto t4 = GetWeatherByDate(server, date + ";21:00").temperature; - if (t4 < t1) - { - t1 = t4; - } - - return t1; + std::set temperatures; + temperatures.emplace(GetWeatherByDate(server, date + s_responseDataSeprator + s_threeAM).temperature); + temperatures.emplace(GetWeatherByDate(server, date + s_responseDataSeprator + s_nineAM).temperature); + temperatures.emplace(GetWeatherByDate(server, date + s_responseDataSeprator + s_threePM).temperature); + temperatures.emplace(GetWeatherByDate(server, date + s_responseDataSeprator + s_ninePM).temperature); + + return *temperatures.begin(); } virtual double GetMaximumTemperature(IWeatherServer& server, const std::string& date) override @@ -204,7 +198,7 @@ TEST(WeatherClient, TestParseDoubleWindSpeedFromResponse) //Test client -TEST(WeatherClient, TestGetMinimumTemperature) +TEST(WeatherClient, TestGetMinimumTemperature_21pm) { MockWeatherServer server; WeatherClient client; From f121f21f270a643b5a0b8410c920fd760d5a8d17 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Thu, 22 Nov 2018 12:03:20 +0200 Subject: [PATCH 71/72] Red --- tdd_intro/homework/04_weather_client/test.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tdd_intro/homework/04_weather_client/test.cpp b/tdd_intro/homework/04_weather_client/test.cpp index 20c498c1..5267660b 100644 --- a/tdd_intro/homework/04_weather_client/test.cpp +++ b/tdd_intro/homework/04_weather_client/test.cpp @@ -211,3 +211,16 @@ TEST(WeatherClient, TestGetMinimumTemperature_21pm) EXPECT_EQ(5, client.GetMinimumTemperature(server, "31.08.2018")); } +TEST(WeatherClient, TestGetMaximumTemperature_3pm) +{ + MockWeatherServer server; + WeatherClient client; + + EXPECT_CALL(server, GetWeather("31.08.2018;03:00")).WillOnce(testing::Return("20;0;0")); + EXPECT_CALL(server, GetWeather("31.08.2018;09:00")).WillOnce(testing::Return("6;0;0")); + EXPECT_CALL(server, GetWeather("31.08.2018;15:00")).WillOnce(testing::Return("30;0;0")); + EXPECT_CALL(server, GetWeather("31.08.2018;21:00")).WillOnce(testing::Return("5;0;0")); + + EXPECT_EQ(5, client.GetMaximumTemperature(server, "31.08.2018")); +} + From c5129572abc0e23faced5f51bac3df85ff0e1696 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Thu, 22 Nov 2018 12:05:58 +0200 Subject: [PATCH 72/72] Green --- tdd_intro/homework/04_weather_client/test.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tdd_intro/homework/04_weather_client/test.cpp b/tdd_intro/homework/04_weather_client/test.cpp index 5267660b..8d91384e 100644 --- a/tdd_intro/homework/04_weather_client/test.cpp +++ b/tdd_intro/homework/04_weather_client/test.cpp @@ -142,7 +142,13 @@ class WeatherClient : public IWeatherClient virtual double GetMaximumTemperature(IWeatherServer& server, const std::string& date) override { - throw std::runtime_error("not implemented"); + std::set temperatures; + temperatures.emplace(GetWeatherByDate(server, date + s_responseDataSeprator + s_threeAM).temperature); + temperatures.emplace(GetWeatherByDate(server, date + s_responseDataSeprator + s_nineAM).temperature); + temperatures.emplace(GetWeatherByDate(server, date + s_responseDataSeprator + s_threePM).temperature); + temperatures.emplace(GetWeatherByDate(server, date + s_responseDataSeprator + s_ninePM).temperature); + + return *temperatures.rbegin(); } virtual double GetAverageWindDirection(IWeatherServer& server, const std::string& date) override @@ -221,6 +227,6 @@ TEST(WeatherClient, TestGetMaximumTemperature_3pm) EXPECT_CALL(server, GetWeather("31.08.2018;15:00")).WillOnce(testing::Return("30;0;0")); EXPECT_CALL(server, GetWeather("31.08.2018;21:00")).WillOnce(testing::Return("5;0;0")); - EXPECT_EQ(5, client.GetMaximumTemperature(server, "31.08.2018")); + EXPECT_EQ(30, client.GetMaximumTemperature(server, "31.08.2018")); }