From d0d8c7e11930dd7e04014c484990f27c0b6b3965 Mon Sep 17 00:00:00 2001 From: Podrez Date: Wed, 31 Oct 2018 22:20:22 +0200 Subject: [PATCH 01/14] fixed the solution and done Lesson 1 homework --- tdd_intro/homework/06_coffee/{05_coffee.pro => 06_coffee.pro} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tdd_intro/homework/06_coffee/{05_coffee.pro => 06_coffee.pro} (100%) diff --git a/tdd_intro/homework/06_coffee/05_coffee.pro b/tdd_intro/homework/06_coffee/06_coffee.pro similarity index 100% rename from tdd_intro/homework/06_coffee/05_coffee.pro rename to tdd_intro/homework/06_coffee/06_coffee.pro From 97cd6a3116186c57960b1cc6af1c054a3d33fe3b Mon Sep 17 00:00:00 2001 From: Podrez Date: Wed, 31 Oct 2018 22:23:31 +0200 Subject: [PATCH 02/14] Lesson 1 hometask --- tdd_intro/homework/01_leap_year/test.cpp | 31 ++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tdd_intro/homework/01_leap_year/test.cpp b/tdd_intro/homework/01_leap_year/test.cpp index 4f186c8b..f7d903df 100644 --- a/tdd_intro/homework/01_leap_year/test.cpp +++ b/tdd_intro/homework/01_leap_year/test.cpp @@ -13,3 +13,34 @@ If your language provides a method in the standard library that does this look-u */ #include + +bool IsYearLeap(unsigned int year) +{ + if(year % 4 == 0) + { + if(year % 100 == 0 && year % 400 != 0) + { + return false; + } + return true; + } + return false; +} + +TEST(LeapYear, true_for_multiple_4) +{ + ASSERT_EQ(true, IsYearLeap(1996)); + ASSERT_EQ(true, IsYearLeap(2004)); +} +TEST(LeapYear, false_for_1997) +{ + ASSERT_EQ(false, IsYearLeap(1997)); +} +TEST(LeapYear, false_for_2100) +{ + ASSERT_EQ(false, IsYearLeap(2100)); +} +TEST(LeapYear, true_for_2000) +{ + ASSERT_EQ(true, IsYearLeap(2000)); +} From f83313111e6f0b2400bc73703c37fb48d8bae858 Mon Sep 17 00:00:00 2001 From: Mokych Andrey Date: Fri, 2 Nov 2018 13:43:03 +0200 Subject: [PATCH 03/14] 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 | 2 ++ 3 files changed, 31 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 722f7939..9e9ba630 100644 --- a/tdd_intro/demo/demo.pro +++ b/tdd_intro/demo/demo.pro @@ -4,5 +4,7 @@ SUBDIRS += \ 01_bob \ 01_fizz_buzz \ 02_anagram \ + 02_word_count \ + #03_allergies \ 03_roman_numerals \ 04_timer From 0ae1f32f037079414536be1355de5436ca6d55fa Mon Sep 17 00:00:00 2001 From: Podrez Date: Thu, 8 Nov 2018 10:35:37 +0200 Subject: [PATCH 04/14] first test --- tdd_intro/homework/02_ternary_numbers/test.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index 17503028..052b7c52 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -16,3 +16,17 @@ 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. */ + +int TernaryNumberValue(std::string number) +{ + return 0; +} + +TEST(TernaryNumberValue, zero_for_word) +{ + ASSERT_EQ(0, TernaryNumberValue("word")); +} +TEST(TernaryNumberValue, zero_for_word) +{ + ASSERT_EQ(0, TernaryNumberValue("word")); +} From e604361c1d8f68b0d75f0563bf1adc960a0d7f6a Mon Sep 17 00:00:00 2001 From: Podrez Date: Thu, 8 Nov 2018 10:45:28 +0200 Subject: [PATCH 05/14] second test green --- tdd_intro/homework/02_ternary_numbers/test.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index 052b7c52..c594c5ec 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -19,6 +19,13 @@ If your language provides a method in the standard library to perform the conver int TernaryNumberValue(std::string number) { + int zero = static_cast('0'); + int nine = static_cast('9'); + if(number.at(0) <= zero && number.at(0) >= nine) + { + return static_cast(number.at(0)) - zero; + } + return 0; } @@ -26,7 +33,8 @@ TEST(TernaryNumberValue, zero_for_word) { ASSERT_EQ(0, TernaryNumberValue("word")); } -TEST(TernaryNumberValue, zero_for_word) + +TEST(TernaryNumberValue, 2_for_2) { - ASSERT_EQ(0, TernaryNumberValue("word")); + ASSERT_EQ(2, TernaryNumberValue("2")); } From 90675a059f20022ea3ad3c0639d25cfac6eb3d57 Mon Sep 17 00:00:00 2001 From: Podrez Date: Thu, 8 Nov 2018 10:48:25 +0200 Subject: [PATCH 06/14] second test true green --- tdd_intro/homework/02_ternary_numbers/test.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index c594c5ec..ddc0b74a 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -21,7 +21,7 @@ int TernaryNumberValue(std::string number) { int zero = static_cast('0'); int nine = static_cast('9'); - if(number.at(0) <= zero && number.at(0) >= nine) + if(number.at(0) >= zero && number.at(0) <= nine) { return static_cast(number.at(0)) - zero; } @@ -34,7 +34,8 @@ TEST(TernaryNumberValue, zero_for_word) ASSERT_EQ(0, TernaryNumberValue("word")); } -TEST(TernaryNumberValue, 2_for_2) +TEST(TernaryNumberValue,two_for_two) { ASSERT_EQ(2, TernaryNumberValue("2")); } + From bec4e2d99419d3ad95b74833ea76fafc4ba279b5 Mon Sep 17 00:00:00 2001 From: Podrez Date: Thu, 8 Nov 2018 11:17:07 +0200 Subject: [PATCH 07/14] big step, so I wrote sub function for get value of ternary digit in any place of number and tested it. Main function test still red --- .../homework/02_ternary_numbers/test.cpp | 41 +++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index ddc0b74a..8060455d 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -16,16 +16,43 @@ 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. */ +int TernaryDigitValue(int digit, int digitNumber) +{ + return digit * pow(3,digitNumber); +} + +TEST(TernaryDigitValue, two_for_2_and_0) +{ + ASSERT_EQ(2, TernaryDigitValue(2, 0)); +} + +TEST(TernaryDigitValue, eighteen_for_2_and_2) +{ + ASSERT_EQ(18, TernaryDigitValue(2, 2)); +} + +TEST(TernaryDigitValue, zero_for_zero_with_any_numbmer) +{ + ASSERT_EQ(0, TernaryDigitValue(0, 3)); + ASSERT_EQ(0, TernaryDigitValue(0, 1)); +} int TernaryNumberValue(std::string number) { + if(number.empty()) + return 0; + int zero = static_cast('0'); int nine = static_cast('9'); - if(number.at(0) >= zero && number.at(0) <= nine) + int sum = 0; + for(int i = static_cast(number.size()) -1; i >= 0; --i) { - return static_cast(number.at(0)) - zero; + int digit = static_cast(number.at(i)); + if(digit >= zero && digit <= nine) + { + sum += ( digit - zero) * 3^i; + } } - return 0; } @@ -39,3 +66,11 @@ TEST(TernaryNumberValue,two_for_two) ASSERT_EQ(2, TernaryNumberValue("2")); } +TEST(TernaryNumberValue, zero_for_empty_string) +{ + ASSERT_EQ(0, TernaryNumberValue("")); +} +TEST(TernaryNumberValue,five_for_12) +{ + ASSERT_EQ(5, TernaryNumberValue("12")); +} From ce4c093a7cd18644be2f449155e592720664b2f9 Mon Sep 17 00:00:00 2001 From: Podrez Date: Thu, 8 Nov 2018 11:29:29 +0200 Subject: [PATCH 08/14] added two more sub function with tests. Main test is still red --- .../homework/02_ternary_numbers/test.cpp | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index 8060455d..8bacb05e 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -37,6 +37,34 @@ TEST(TernaryDigitValue, zero_for_zero_with_any_numbmer) ASSERT_EQ(0, TernaryDigitValue(0, 1)); } +bool IsTernaryDigit(char symbol) +{ + return symbol >= '0' && symbol <= '2'; +} + +TEST(IsTernaryDigit, true_for_0) +{ + ASSERT_TRUE(IsTernaryDigit('0')); +} +TEST(IsTernaryDigit, false_for_a) +{ + ASSERT_FALSE(IsTernaryDigit('a')); +} + +TEST(IsTernaryDigit, false_for_3) +{ + ASSERT_FALSE(IsTernaryDigit('3')); +} + +int ConvertToInt(char symbol) +{ + return static_cast(symbol - '0'); +} +TEST(ConvertToInt, two_for_2) +{ + ASSERT_EQ(2, ConvertToInt('2')); +} + int TernaryNumberValue(std::string number) { if(number.empty()) @@ -50,10 +78,10 @@ int TernaryNumberValue(std::string number) int digit = static_cast(number.at(i)); if(digit >= zero && digit <= nine) { - sum += ( digit - zero) * 3^i; + sum += TernaryDigitValue(digit - zero, i); } } - return 0; + return sum; } TEST(TernaryNumberValue, zero_for_word) From e46c26705e99e2263eb3c860a00ab7d56e10534e Mon Sep 17 00:00:00 2001 From: Podrez Date: Thu, 8 Nov 2018 11:44:02 +0200 Subject: [PATCH 09/14] five_for_12 is green in the end, I refactored it --- tdd_intro/homework/02_ternary_numbers/test.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index 8bacb05e..c438a356 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -70,16 +70,20 @@ int TernaryNumberValue(std::string number) if(number.empty()) return 0; - int zero = static_cast('0'); - int nine = static_cast('9'); + + int sum = 0; - for(int i = static_cast(number.size()) -1; i >= 0; --i) + for(size_t i = 0, size = number.size(); i < size; ++i) { - int digit = static_cast(number.at(i)); - if(digit >= zero && digit <= nine) + size_t digitNumber = size - i -1; + + char symbol = number.at(i); + if(!IsTernaryDigit(symbol)) { - sum += TernaryDigitValue(digit - zero, i); + return 0; } + int value = TernaryDigitValue(ConvertToInt(symbol), static_cast(digitNumber)); + sum+=value; } return sum; } From 1faa0da7bb044fd70c7c8214c8d5343caf2c9caf Mon Sep 17 00:00:00 2001 From: Podrez Date: Thu, 8 Nov 2018 11:49:53 +0200 Subject: [PATCH 10/14] added few additional tests just to ensure everything works fine and added acceptance test. Task has been done --- tdd_intro/homework/02_ternary_numbers/test.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index c438a356..1fd69b9d 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -106,3 +106,19 @@ TEST(TernaryNumberValue,five_for_12) { ASSERT_EQ(5, TernaryNumberValue("12")); } +TEST(TernaryNumberValue,0_for_decimal_number) +{ + ASSERT_EQ(0, TernaryNumberValue("13")); + ASSERT_EQ(0, TernaryNumberValue("1321")); +} + +TEST(TernaryNumberValue, correct_convert_for_multidigit_numbers) +{ + ASSERT_EQ(13, TernaryNumberValue("111")); + ASSERT_EQ(40, TernaryNumberValue("1111")); +} + +TEST(TernaryNumberValue, acceptance_302_for_102012) +{ + ASSERT_EQ(302, TernaryNumberValue("102012")); +} From 8ae8ea9051406b5b5ba61402c872f9615e616fd8 Mon Sep 17 00:00:00 2001 From: Podrez Date: Fri, 16 Nov 2018 09:46:31 +0200 Subject: [PATCH 11/14] First test passsed --- tdd_intro/homework/03_bank_ocr/test.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index a01540b9..cce0a35b 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -195,3 +195,23 @@ const Display s_display123456789 = { " _ _ _ _ _ _ _ ", " | _| _||_||_ |_ ||_||_|", " ||_ _| | _||_| ||_| _|" }; + + +std::string BankOsr(const Display& inputFax) +{ + if( inputFax.lines[0] == s_digit9.lines[0] && + inputFax.lines[1] == s_digit9.lines[1] && + inputFax.lines[2] == s_digit9.lines[2]) + { + return "9"; + } +} + +TEST(BankOcr, ParseDigitFromOneDigitNumber) +{ + Display oneDigitNumber = { " _ ", + "|_|", + " _|" + }; + ASSERT_EQ("9", BankOsr(oneDigitNumber)); +} From f3302fb4180e97fbf3075b2598b88b5a68f67756 Mon Sep 17 00:00:00 2001 From: Podrez Date: Fri, 16 Nov 2018 10:12:01 +0200 Subject: [PATCH 12/14] Added sum functions to dial with digits --- tdd_intro/homework/03_bank_ocr/test.cpp | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index cce0a35b..39773506 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -196,6 +196,40 @@ const Display s_display123456789 = { " _ _ _ _ _ _ _ ", " ||_ _| | _||_| ||_| _|" }; +bool IsDigitsEqual(const Digit& right, const Digit& left) +{ + for(size_t i = 0; i < g_digitLen;++i) + { + if(right.lines[i] != left.lines[i]) + return false; + } + return true; +} + +TEST(IsDigitsEqual, TrueFor9And9) +{ + ASSERT_TRUE(IsDigitsEqual(s_digit9, s_digit9)); +} +TEST(IsDigitsEqual, FALSEFor9And0) +{ + ASSERT_FALSE(IsDigitsEqual(s_digit9, s_digit0)); +} + + +Digit GetDigit(Display number, size_t digitOffset) +{ + Digit d = {number.lines[0].substr(0, g_digitLen), + number.lines[1].substr(0, g_digitLen), + number.lines[2].substr(0, g_digitLen) + + }; + return d; +} + +TEST(GetDigit, NineForAllNinesAnd0) +{ + ASSERT_TRUE(IsDigitsEqual(s_digit9, GetDigit(s_displayAll9, 0))); +} std::string BankOsr(const Display& inputFax) { From 26429f61bb740dfd63480fdd273df9c7a8cf8fdf Mon Sep 17 00:00:00 2001 From: Podrez Date: Fri, 16 Nov 2018 10:35:06 +0200 Subject: [PATCH 13/14] tested GetDigit with offset != 0 --- tdd_intro/homework/03_bank_ocr/test.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index 39773506..4da57b61 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -210,17 +210,18 @@ TEST(IsDigitsEqual, TrueFor9And9) { ASSERT_TRUE(IsDigitsEqual(s_digit9, s_digit9)); } + TEST(IsDigitsEqual, FALSEFor9And0) { ASSERT_FALSE(IsDigitsEqual(s_digit9, s_digit0)); } - Digit GetDigit(Display number, size_t digitOffset) { - Digit d = {number.lines[0].substr(0, g_digitLen), - number.lines[1].substr(0, g_digitLen), - number.lines[2].substr(0, g_digitLen) + size_t lineOffset = digitOffset * g_digitLen; + Digit d = {number.lines[0].substr(lineOffset, g_digitLen), + number.lines[1].substr(lineOffset, g_digitLen), + number.lines[2].substr(lineOffset, g_digitLen) }; return d; @@ -231,6 +232,11 @@ TEST(GetDigit, NineForAllNinesAnd0) ASSERT_TRUE(IsDigitsEqual(s_digit9, GetDigit(s_displayAll9, 0))); } +TEST(GetDigit, TwoFor123456789And1) +{ + ASSERT_TRUE(IsDigitsEqual(s_digit2, GetDigit(s_display123456789, 1))); +} + std::string BankOsr(const Display& inputFax) { if( inputFax.lines[0] == s_digit9.lines[0] && From 362ef6ef190796606df44810cc7ae7fc3b8fc67e Mon Sep 17 00:00:00 2001 From: Podrez Date: Fri, 16 Nov 2018 10:42:47 +0200 Subject: [PATCH 14/14] GetDigit is tested --- 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 4da57b61..c8aa2e7b 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -218,6 +218,10 @@ TEST(IsDigitsEqual, FALSEFor9And0) Digit GetDigit(Display number, size_t digitOffset) { + if(digitOffset >= g_digitsOnDisplay) + { + throw std::runtime_error("Offset is out range"); + } size_t lineOffset = digitOffset * g_digitLen; Digit d = {number.lines[0].substr(lineOffset, g_digitLen), number.lines[1].substr(lineOffset, g_digitLen), @@ -235,6 +239,12 @@ TEST(GetDigit, NineForAllNinesAnd0) TEST(GetDigit, TwoFor123456789And1) { ASSERT_TRUE(IsDigitsEqual(s_digit2, GetDigit(s_display123456789, 1))); + ASSERT_TRUE(IsDigitsEqual(s_digit9, GetDigit(s_display123456789, 8))); +} + +TEST(GetDigit, ExceptionIfOffsetMoreThan8) +{ + ASSERT_ANY_THROW(GetDigit(s_display123456789, 9)); } std::string BankOsr(const Display& inputFax)