From 6423a44bb398495dc0c72cf18d16ae9db8a4ab7f Mon Sep 17 00:00:00 2001 From: Roman Mokych Date: Fri, 26 Oct 2018 23:46:05 +0300 Subject: [PATCH 01/21] Fixed build issues. Fixed linking with gtest. Added dependecy on gtest for some homeworks. Fixed build of gmock. Fixed std::abs conflict. --- .../gmock/include/gmock/internal/gmock-internal-utils.h | 7 +++++++ tdd_intro/cleanroom/cleanroom.pro | 4 ++-- tdd_intro/gtest.pri | 4 ++-- tdd_intro/homework/04_weather_client/04_weather_client.pro | 1 + tdd_intro/homework/04_weather_client/test.cpp | 2 ++ tdd_intro/homework/06_coffee/06_coffee.pro | 1 + 6 files changed, 15 insertions(+), 4 deletions(-) diff --git a/tdd_intro/3rd_party/gmock/include/gmock/internal/gmock-internal-utils.h b/tdd_intro/3rd_party/gmock/include/gmock/internal/gmock-internal-utils.h index e12b7d7d..5507611a 100644 --- a/tdd_intro/3rd_party/gmock/include/gmock/internal/gmock-internal-utils.h +++ b/tdd_intro/3rd_party/gmock/include/gmock/internal/gmock-internal-utils.h @@ -46,6 +46,13 @@ #include "gmock/internal/gmock-port.h" #include "gtest/gtest.h" +namespace { + +const testing::internal::RelationToSourceCopy kCopy; +const testing::internal::RelationToSourceReference kReference; + +} + namespace testing { namespace internal { diff --git a/tdd_intro/cleanroom/cleanroom.pro b/tdd_intro/cleanroom/cleanroom.pro index b92a4278..31ff6a0b 100644 --- a/tdd_intro/cleanroom/cleanroom.pro +++ b/tdd_intro/cleanroom/cleanroom.pro @@ -1,4 +1,4 @@ TEMPLATE = subdirs -SUBDIRS += \ - chatclient +#SUBDIRS += \ +# chatclient diff --git a/tdd_intro/gtest.pri b/tdd_intro/gtest.pri index 586007bb..bc93ca62 100644 --- a/tdd_intro/gtest.pri +++ b/tdd_intro/gtest.pri @@ -1,6 +1,6 @@ win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../3rd_party/gtest/release/ -lgtest else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../../3rd_party/gtest/debug/ -lgtest -else:unix:!macx: LIBS += -L$$OUT_PWD/../../3rd_party/gtest/ -lgtest +else:unix: LIBS += -L$$OUT_PWD/../../3rd_party/gtest/ -lgtest INCLUDEPATH += $$PWD/3rd_party/gtest/googletest/include DEPENDPATH += $$PWD/3rd_party/gtest/googletest/include @@ -10,4 +10,4 @@ win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../3rd_ else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../3rd_party/gtest/debug/libgtest.a else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../3rd_party/gtest/release/gtest.lib else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../3rd_party/gtest/debug/gtest.lib -else:unix:!macx: PRE_TARGETDEPS += $$OUT_PWD/../../3rd_party/gtest/libgtest.a +else:unix: PRE_TARGETDEPS += $$OUT_PWD/../../3rd_party/gtest/libgtest.a diff --git a/tdd_intro/homework/04_weather_client/04_weather_client.pro b/tdd_intro/homework/04_weather_client/04_weather_client.pro index dec9b6a8..e8f86261 100644 --- a/tdd_intro/homework/04_weather_client/04_weather_client.pro +++ b/tdd_intro/homework/04_weather_client/04_weather_client.pro @@ -1,4 +1,5 @@ include(../../gmock.pri) +include(../../gtest.pri) TEMPLATE = app CONFIG += console c++11 diff --git a/tdd_intro/homework/04_weather_client/test.cpp b/tdd_intro/homework/04_weather_client/test.cpp index 346ea809..476405e2 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 +#include + struct Weather { short temperature = 0; diff --git a/tdd_intro/homework/06_coffee/06_coffee.pro b/tdd_intro/homework/06_coffee/06_coffee.pro index dec9b6a8..05f06c68 100644 --- a/tdd_intro/homework/06_coffee/06_coffee.pro +++ b/tdd_intro/homework/06_coffee/06_coffee.pro @@ -1,3 +1,4 @@ +include(../../gtest.pri) include(../../gmock.pri) TEMPLATE = app From 4919240d9081202294fc120e4345353a1e7bba81 Mon Sep 17 00:00:00 2001 From: Roman Mokych Date: Fri, 26 Oct 2018 23:48:10 +0300 Subject: [PATCH 02/21] Added build files to the git ignore. --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 259148fa..32673502 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,6 @@ *.exe *.out *.app +build +.DS_Store +*.pro.user From f8426489945827bb504c25827783edd4fc078b5b Mon Sep 17 00:00:00 2001 From: Roman Mokych Date: Wed, 7 Nov 2018 22:10:30 +0200 Subject: [PATCH 03/21] Disabled 6 homework because it is not buildable. --- tdd_intro/homework/homework.pro | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tdd_intro/homework/homework.pro b/tdd_intro/homework/homework.pro index cf6c01b9..13ac9d31 100644 --- a/tdd_intro/homework/homework.pro +++ b/tdd_intro/homework/homework.pro @@ -5,5 +5,5 @@ SUBDIRS += \ 02_ternary_numbers \ 03_bank_ocr \ 04_weather_client \ - 05_word_wrapp \ - 06_coffee + 05_word_wrapp +# 06_coffee From 68b34895eb4810b4cf6bf43744fd7a82cf437a1b Mon Sep 17 00:00:00 2001 From: Roman Mokych Date: Wed, 14 Nov 2018 22:24:43 +0200 Subject: [PATCH 04/21] Implemented lines splitting for string without new-lines --- tdd_intro/homework/03_bank_ocr/test.cpp | 112 ++---------------------- 1 file changed, 7 insertions(+), 105 deletions(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index a01540b9..4a7249b7 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -87,111 +87,13 @@ Example input and output #include #include -const unsigned short g_digitLen = 3; -const unsigned short g_linesInDigit = 3; -struct Digit +std::vector SplitStringForLines(const std::string& string) { - std::string lines[g_linesInDigit]; -}; + return {string}; +} -const unsigned short g_digitsOnDisplay = 9; -struct Display +TEST(SplitStringForLines, OneLineWithoutDelimiters) { - std::string lines[g_linesInDigit]; -}; - -const Digit s_digit0 = { " _ ", - "| |", - "|_|" - }; -const Digit s_digit1 = { " ", - " |", - " |" - }; -const Digit s_digit2 = { " _ ", - " _|", - "|_ " - }; -const Digit s_digit3 = { " _ ", - " _|", - " _|" - }; -const Digit s_digit4 = { " ", - "|_|", - " |" - }; -const Digit s_digit5 = { " _ ", - "|_ ", - " _|" - }; -const Digit s_digit6 = { " _ ", - "|_ ", - "|_|" - }; -const Digit s_digit7 = { " _ ", - " |", - " |" - }; -const Digit s_digit8 = { " _ ", - "|_|", - "|_|" - }; -const Digit s_digit9 = { " _ ", - "|_|", - " _|" - }; - -const Display s_displayAll0 = { " _ _ _ _ _ _ _ _ _ ", - "| || || || || || || || || |", - "|_||_||_||_||_||_||_||_||_|" -}; - -const Display s_displayAll1 = { " ", - " | | | | | | | | |", - " | | | | | | | | |" -}; - -const Display s_displayAll2 = { " _ _ _ _ _ _ _ _ _ ", - " _| _| _| _| _| _| _| _| _|", - "|_ |_ |_ |_ |_ |_ |_ |_ |_ " -}; - -const Display s_displayAll3 = { " _ _ _ _ _ _ _ _ _ ", - " _| _| _| _| _| _| _| _| _|", - " _| _| _| _| _| _| _| _| _|" -}; - -const Display s_displayAll4 = { " ", - "|_||_||_||_||_||_||_||_||_|", - " | | | | | | | | |" -}; - -const Display s_displayAll5 = { " _ _ _ _ _ _ _ _ _ ", - "|_ |_ |_ |_ |_ |_ |_ |_ |_ ", - " _| _| _| _| _| _| _| _| _|" -}; - -const Display s_displayAll6 = { " _ _ _ _ _ _ _ _ _ ", - "|_ |_ |_ |_ |_ |_ |_ |_ |_ ", - "|_||_||_||_||_||_||_||_||_|" -}; - -const Display s_displayAll7 = { " _ _ _ _ _ _ _ _ _ ", - " | | | | | | | | |", - " | | | | | | | | |" -}; - -const Display s_displayAll8 = { " _ _ _ _ _ _ _ _ _ ", - "|_||_||_||_||_||_||_||_||_|", - "|_||_||_||_||_||_||_||_||_|" -}; - -const Display s_displayAll9 = { " _ _ _ _ _ _ _ _ _ ", - "|_||_||_||_||_||_||_||_||_|", - " _| _| _| _| _| _| _| _| _|" -}; - -const Display s_display123456789 = { " _ _ _ _ _ _ _ ", - " | _| _||_||_ |_ ||_||_|", - " ||_ _| | _||_| ||_| _|" -}; + std::vector lines = {"a"}; + EXPECT_EQ(lines, SplitStringForLines("a")); +} From 82c13817173d8c43be4511fdc3a963ad3d389eca Mon Sep 17 00:00:00 2001 From: Roman Mokych Date: Wed, 14 Nov 2018 22:31:48 +0200 Subject: [PATCH 05/21] Implemented splitting for two lines. --- tdd_intro/homework/03_bank_ocr/test.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index 4a7249b7..78493a04 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -89,6 +89,12 @@ Example input and output std::vector SplitStringForLines(const std::string& string) { + size_t newLinePos = string.find("\n"); + if (newLinePos != std::string::npos) + { + return {string.substr(0, newLinePos), string.substr(newLinePos + 1)}; + } + return {string}; } @@ -97,3 +103,9 @@ TEST(SplitStringForLines, OneLineWithoutDelimiters) std::vector lines = {"a"}; EXPECT_EQ(lines, SplitStringForLines("a")); } + +TEST(SplitStringForLines, TwoLineWithOneDelimiter) +{ + std::vector lines = {"a", "b"}; + EXPECT_EQ(lines, SplitStringForLines("a\nb")); +} From a0f27728395b76498d3bc07ff10d0a1645dd4821 Mon Sep 17 00:00:00 2001 From: Roman Mokych Date: Wed, 14 Nov 2018 22:46:50 +0200 Subject: [PATCH 06/21] Implemented splitting for three parts for two delimiters. --- tdd_intro/homework/03_bank_ocr/test.cpp | 32 +++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index 78493a04..a8dba29d 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -89,13 +89,31 @@ Example input and output std::vector SplitStringForLines(const std::string& string) { - size_t newLinePos = string.find("\n"); + std::string mutableString = string; + + size_t newLinePos = mutableString.find("\n"); + if (newLinePos == std::string::npos) + { + return {mutableString}; + } + + std::vector lines; + lines.push_back(mutableString.substr(0, newLinePos)); + + mutableString = mutableString.substr(newLinePos + 1); + + newLinePos = mutableString.find("\n"); + lines.push_back(mutableString.substr(0, newLinePos)); + if (newLinePos != std::string::npos) { - return {string.substr(0, newLinePos), string.substr(newLinePos + 1)}; + mutableString = mutableString.substr(newLinePos + 1); + + newLinePos = mutableString.find("\n"); + lines.push_back(mutableString.substr(0, newLinePos)); } - return {string}; + return lines; } TEST(SplitStringForLines, OneLineWithoutDelimiters) @@ -104,8 +122,14 @@ TEST(SplitStringForLines, OneLineWithoutDelimiters) EXPECT_EQ(lines, SplitStringForLines("a")); } -TEST(SplitStringForLines, TwoLineWithOneDelimiter) +TEST(SplitStringForLines, TwoLinesWithOneDelimiters) { std::vector lines = {"a", "b"}; EXPECT_EQ(lines, SplitStringForLines("a\nb")); } + +TEST(SplitStringForLines, ThreeLinesWithTwoDelimiters) +{ + std::vector lines = {"a", "b", "c"}; + EXPECT_EQ(lines, SplitStringForLines("a\nb\nc")); +} From cc4813271688d4cad146d97dc3caf5d5720fc742 Mon Sep 17 00:00:00 2001 From: Roman Mokych Date: Wed, 14 Nov 2018 22:52:15 +0200 Subject: [PATCH 07/21] Refactoring. --- tdd_intro/homework/03_bank_ocr/test.cpp | 26 +++++++++---------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index a8dba29d..41712e1f 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -89,28 +89,20 @@ Example input and output std::vector SplitStringForLines(const std::string& string) { - std::string mutableString = string; - - size_t newLinePos = mutableString.find("\n"); - if (newLinePos == std::string::npos) - { - return {mutableString}; - } - std::vector lines; - lines.push_back(mutableString.substr(0, newLinePos)); - mutableString = mutableString.substr(newLinePos + 1); + std::string mutableString = string; + while (true) + { + size_t newLinePos = mutableString.find("\n"); + lines.push_back(mutableString.substr(0, newLinePos)); - newLinePos = mutableString.find("\n"); - lines.push_back(mutableString.substr(0, newLinePos)); + if (newLinePos == std::string::npos) + { + break; + } - if (newLinePos != std::string::npos) - { mutableString = mutableString.substr(newLinePos + 1); - - newLinePos = mutableString.find("\n"); - lines.push_back(mutableString.substr(0, newLinePos)); } return lines; From c77dad117d23e22ffcfd9a4f79bc7468869cefb9 Mon Sep 17 00:00:00 2001 From: Roman Mokych Date: Wed, 14 Nov 2018 22:53:34 +0200 Subject: [PATCH 08/21] Added already green test for empty line. --- tdd_intro/homework/03_bank_ocr/test.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index 41712e1f..3706524b 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -125,3 +125,9 @@ TEST(SplitStringForLines, ThreeLinesWithTwoDelimiters) std::vector lines = {"a", "b", "c"}; EXPECT_EQ(lines, SplitStringForLines("a\nb\nc")); } + +TEST(SplitStringForLines, OneLineForEmptyLine) +{ + std::vector lines = {""}; + EXPECT_EQ(lines, SplitStringForLines("")); +} From fc738e3a9d1db883ab3c0c4a8ef90214f4740cdb Mon Sep 17 00:00:00 2001 From: Roman Mokych Date: Wed, 14 Nov 2018 23:02:00 +0200 Subject: [PATCH 09/21] Implemented one digit extraction --- tdd_intro/homework/03_bank_ocr/test.cpp | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index 3706524b..5a6c992b 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -108,6 +108,19 @@ std::vector SplitStringForLines(const std::string& string) return lines; } +std::vector ExtractDigitsFromEntity(const std::string& entity) +{ + std::vector lines = SplitStringForLines(entity); + + std::string digit; + + digit += lines.at(0); + digit += lines.at(1); + digit += lines.at(2); + + return {digit}; +} + TEST(SplitStringForLines, OneLineWithoutDelimiters) { std::vector lines = {"a"}; @@ -131,3 +144,20 @@ TEST(SplitStringForLines, OneLineForEmptyLine) std::vector lines = {""}; EXPECT_EQ(lines, SplitStringForLines("")); } + +TEST(SplitStringForLines, TwoEmptyLinesForLineOnlyWithDelimiters) +{ + std::vector lines = {"", ""}; + EXPECT_EQ(lines, SplitStringForLines("\n")); +} + +TEST(ExtractDigitsFromEntity, OneDigitExtraction) +{ + std::vector digits = {" _ " + "| |" + "|_|"}; + + EXPECT_EQ(digits, ExtractDigitsFromEntity(" _ \n" + "| |\n" + "|_|\n")); +} From 1e849e10fd64b825bd72f3eef2c760a50fa16134 Mon Sep 17 00:00:00 2001 From: Roman Mokych Date: Wed, 14 Nov 2018 23:03:06 +0200 Subject: [PATCH 10/21] 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 5a6c992b..15492dcf 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -113,10 +113,10 @@ std::vector ExtractDigitsFromEntity(const std::string& entity) std::vector lines = SplitStringForLines(entity); std::string digit; - - digit += lines.at(0); - digit += lines.at(1); - digit += lines.at(2); + for (auto& line: lines) + { + digit += line; + } return {digit}; } From 0c378907726a09e896ac6943d462f171054b9bc4 Mon Sep 17 00:00:00 2001 From: Roman Mokych Date: Wed, 14 Nov 2018 23:11:51 +0200 Subject: [PATCH 11/21] Added two digits extraction. --- tdd_intro/homework/03_bank_ocr/test.cpp | 33 +++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index 15492dcf..f13ef47b 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -111,14 +111,29 @@ std::vector SplitStringForLines(const std::string& string) std::vector ExtractDigitsFromEntity(const std::string& entity) { std::vector lines = SplitStringForLines(entity); + lines.resize(3); + std::vector digits; std::string digit; for (auto& line: lines) { - digit += line; + digit += line.substr(0, 3); } - return {digit}; + digits.push_back(digit); + + if (lines[0].size() > 3) + { + std::string digit; + for (auto& line: lines) + { + digit += line.substr(3, 3); + } + + digits.push_back(digit); + } + + return digits; } TEST(SplitStringForLines, OneLineWithoutDelimiters) @@ -161,3 +176,17 @@ TEST(ExtractDigitsFromEntity, OneDigitExtraction) "| |\n" "|_|\n")); } + +TEST(ExtractDigitsFromEntity, TwoDigitsExtraction) +{ + std::vector digits = {" _ " + "| |" + "|_|", + " " + " |" + " |"}; + + EXPECT_EQ(digits, ExtractDigitsFromEntity(" _ \n" + "| | |\n" + "|_| |\n")); +} From bf100637a1760591037715bde28674950f0b85c7 Mon Sep 17 00:00:00 2001 From: Roman Mokych Date: Wed, 14 Nov 2018 23:13:31 +0200 Subject: [PATCH 12/21] Refactoring. --- tdd_intro/homework/03_bank_ocr/test.cpp | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index f13ef47b..6789044b 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -114,20 +114,13 @@ std::vector ExtractDigitsFromEntity(const std::string& entity) lines.resize(3); std::vector digits; - std::string digit; - for (auto& line: lines) - { - digit += line.substr(0, 3); - } - - digits.push_back(digit); - if (lines[0].size() > 3) + for (size_t i = 0; i < lines[0].size(); i += 3) { std::string digit; for (auto& line: lines) { - digit += line.substr(3, 3); + digit += line.substr(i, 3); } digits.push_back(digit); From 8df9886ea25296ec34021a8f03e230448dce5a83 Mon Sep 17 00:00:00 2001 From: Roman Mokych Date: Wed, 14 Nov 2018 23:15:08 +0200 Subject: [PATCH 13/21] Added already green test for three digits extraction. --- tdd_intro/homework/03_bank_ocr/test.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index 6789044b..4e98606b 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -183,3 +183,20 @@ TEST(ExtractDigitsFromEntity, TwoDigitsExtraction) "| | |\n" "|_| |\n")); } + +TEST(ExtractDigitsFromEntity, ThreeDigitsExtraction) +{ + std::vector digits = {" _ " + "| |" + "|_|", + " " + " |" + " |", + " _ " + " _|" + "|_ ",}; + + EXPECT_EQ(digits, ExtractDigitsFromEntity(" _ _ \n" + "| | | _|\n" + "|_| ||_ \n")); +} From 8ac24a36d150f456345bad5bb221e4541ce455f5 Mon Sep 17 00:00:00 2001 From: Roman Mokych Date: Wed, 14 Nov 2018 23:17:02 +0200 Subject: [PATCH 14/21] Already green test for empty entity. --- tdd_intro/homework/03_bank_ocr/test.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index 4e98606b..b5145aba 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -200,3 +200,11 @@ TEST(ExtractDigitsFromEntity, ThreeDigitsExtraction) "| | | _|\n" "|_| ||_ \n")); } + +TEST(ExtractDigitsFromEntity, NoDigitsForEmptyEntity) +{ + std::vector digits = {}; + EXPECT_EQ(digits, ExtractDigitsFromEntity("\n" + "\n" + "\n")); +} From ef7bfe7bb79960f6a7585409670678e5a96698cc Mon Sep 17 00:00:00 2001 From: Roman Mokych Date: Wed, 14 Nov 2018 23:21:43 +0200 Subject: [PATCH 15/21] Added check fir lines number in entity. --- tdd_intro/homework/03_bank_ocr/test.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index b5145aba..ba6a7963 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -111,6 +111,11 @@ std::vector SplitStringForLines(const std::string& string) std::vector ExtractDigitsFromEntity(const std::string& entity) { std::vector lines = SplitStringForLines(entity); + if (lines.size() != 4) + { + throw std::runtime_error("Entity format is wrong. Not enough lines."); + } + lines.resize(3); std::vector digits; @@ -208,3 +213,10 @@ TEST(ExtractDigitsFromEntity, NoDigitsForEmptyEntity) "\n" "\n")); } + +TEST(ExtractDigitsFromEntity, ExceptionForNotEnoughLines) +{ + EXPECT_THROW(ExtractDigitsFromEntity(" \n" + " |\n"), + std::runtime_error); +} From a46ce6ee0350772d5d328f8caf00fb22240be99e Mon Sep 17 00:00:00 2001 From: Roman Mokych Date: Wed, 14 Nov 2018 23:25:48 +0200 Subject: [PATCH 16/21] Added test for invalid digit. --- tdd_intro/homework/03_bank_ocr/test.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index ba6a7963..8637b07e 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -118,8 +118,15 @@ std::vector ExtractDigitsFromEntity(const std::string& entity) lines.resize(3); - std::vector digits; + for (auto& line: lines) + { + if (line.size() % 3 != 0) + { + throw std::runtime_error("Entity format is wrong. Too small line."); + } + } + std::vector digits; for (size_t i = 0; i < lines[0].size(); i += 3) { std::string digit; @@ -220,3 +227,11 @@ TEST(ExtractDigitsFromEntity, ExceptionForNotEnoughLines) " |\n"), std::runtime_error); } + +TEST(ExtractDigitsFromEntity, ExceptionForTooSmallLines) +{ + EXPECT_THROW(ExtractDigitsFromEntity("||\n" + "|||\n" + "|||\n"), + std::runtime_error); +} From dff31dcd2f323e326107472b79b662f2f0686dee Mon Sep 17 00:00:00 2001 From: Roman Mokych Date: Wed, 14 Nov 2018 23:28:26 +0200 Subject: [PATCH 17/21] Refactoring. --- tdd_intro/homework/03_bank_ocr/test.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index 8637b07e..3a448319 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -87,6 +87,9 @@ Example input and output #include #include +const size_t kCharsPerDigit = 3; +const size_t kLinesPerEntity = 3; + std::vector SplitStringForLines(const std::string& string) { std::vector lines; @@ -111,28 +114,28 @@ std::vector SplitStringForLines(const std::string& string) std::vector ExtractDigitsFromEntity(const std::string& entity) { std::vector lines = SplitStringForLines(entity); - if (lines.size() != 4) + if (lines.size() != kLinesPerEntity + 1) //1 for empty line in the end { throw std::runtime_error("Entity format is wrong. Not enough lines."); } - lines.resize(3); + lines.resize(kLinesPerEntity); for (auto& line: lines) { - if (line.size() % 3 != 0) + if (line.size() % kCharsPerDigit != 0) { throw std::runtime_error("Entity format is wrong. Too small line."); } } std::vector digits; - for (size_t i = 0; i < lines[0].size(); i += 3) + for (size_t i = 0; i < lines[0].size(); i += kCharsPerDigit) { std::string digit; for (auto& line: lines) { - digit += line.substr(i, 3); + digit += line.substr(i, kCharsPerDigit); } digits.push_back(digit); From cd4227a6b91b5a0273fcb7967a008c6e4c16e4f5 Mon Sep 17 00:00:00 2001 From: Roman Mokych Date: Wed, 14 Nov 2018 23:34:01 +0200 Subject: [PATCH 18/21] Added parsing of zero. --- tdd_intro/homework/03_bank_ocr/test.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index 3a448319..dedfdb07 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -144,6 +144,16 @@ std::vector ExtractDigitsFromEntity(const std::string& entity) return digits; } +std::string ParseNumbersFromEntity(const std::string& entity) +{ + std::vector digits = ExtractDigitsFromEntity(entity); + std::map numberFromDigit = {{" _ " + "| |" + "|_|", "0"}}; + + return numberFromDigit[digits[0]]; +} + TEST(SplitStringForLines, OneLineWithoutDelimiters) { std::vector lines = {"a"}; @@ -238,3 +248,10 @@ TEST(ExtractDigitsFromEntity, ExceptionForTooSmallLines) "|||\n"), std::runtime_error); } + +TEST(ParseNumbersFromEntity, ZeroFromEntity) +{ + EXPECT_EQ("0", ParseNumbersFromEntity(" _ \n" + "| |\n" + "|_|\n")); +} From 6c3c6e4f71bbab3b97aef2b872579ed23dc623f7 Mon Sep 17 00:00:00 2001 From: Roman Mokych Date: Wed, 14 Nov 2018 23:36:20 +0200 Subject: [PATCH 19/21] Added one parsing. --- tdd_intro/homework/03_bank_ocr/test.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index dedfdb07..f47ea171 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -149,7 +149,10 @@ std::string ParseNumbersFromEntity(const std::string& entity) std::vector digits = ExtractDigitsFromEntity(entity); std::map numberFromDigit = {{" _ " "| |" - "|_|", "0"}}; + "|_|", "0"}, + {" " + " |" + " |", "1"}}; return numberFromDigit[digits[0]]; } @@ -255,3 +258,10 @@ TEST(ParseNumbersFromEntity, ZeroFromEntity) "| |\n" "|_|\n")); } + +TEST(ParseNumbersFromEntity, OneFromEntity) +{ + EXPECT_EQ("1", ParseNumbersFromEntity(" \n" + " |\n" + " |\n")); +} From 33727336cd077a8dc8d1a12246af3440a2afee66 Mon Sep 17 00:00:00 2001 From: Roman Mokych Date: Wed, 14 Nov 2018 23:39:32 +0200 Subject: [PATCH 20/21] Added two and three parsing. --- tdd_intro/homework/03_bank_ocr/test.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index f47ea171..394dffb5 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -152,7 +152,13 @@ std::string ParseNumbersFromEntity(const std::string& entity) "|_|", "0"}, {" " " |" - " |", "1"}}; + " |", "1"}, + {" _ " + " _|" + "|_ ", "2"}, + {" _ " + " _|" + " _|", "3"}}; return numberFromDigit[digits[0]]; } @@ -265,3 +271,17 @@ TEST(ParseNumbersFromEntity, OneFromEntity) " |\n" " |\n")); } + +TEST(ParseNumbersFromEntity, TwoFromEntity) +{ + EXPECT_EQ("2", ParseNumbersFromEntity(" _ \n" + " _|\n" + "|_ \n")); +} + +TEST(ParseNumbersFromEntity, ThreeFromEntity) +{ + EXPECT_EQ("3", ParseNumbersFromEntity(" _ \n" + " _|\n" + " _|\n")); +} From 0d8b8fffed5e5288c8383f42384bb63c3a088400 Mon Sep 17 00:00:00 2001 From: Roman Mokych Date: Wed, 14 Nov 2018 23:42:30 +0200 Subject: [PATCH 21/21] Implemented parsing of multiple numbers at ones --- tdd_intro/homework/03_bank_ocr/test.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index 394dffb5..5b090595 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -160,7 +160,13 @@ std::string ParseNumbersFromEntity(const std::string& entity) " _|" " _|", "3"}}; - return numberFromDigit[digits[0]]; + std::string numbers; + for (auto& digit: digits) + { + numbers += numberFromDigit[digit]; + } + + return numbers; } TEST(SplitStringForLines, OneLineWithoutDelimiters) @@ -285,3 +291,10 @@ TEST(ParseNumbersFromEntity, ThreeFromEntity) " _|\n" " _|\n")); } + +TEST(ParseNumbersFromEntity, ServeralNumbersFromEntity) +{ + EXPECT_EQ("0123", ParseNumbersFromEntity(" _ _ _ \n" + "| | | _| _|\n" + "|_| ||_ _|\n")); +}