diff --git a/tdd_intro/homework/01_leap_year/test.cpp b/tdd_intro/homework/01_leap_year/test.cpp index 4f186c8b..9f60fafd 100644 --- a/tdd_intro/homework/01_leap_year/test.cpp +++ b/tdd_intro/homework/01_leap_year/test.cpp @@ -13,3 +13,47 @@ If your language provides a method in the standard library that does this look-u */ #include + +bool LeapYear(int year) +{ + if (year%400 == 0) + { + return true; + } + if (year%100 == 0) + { + return false; + } + if (year%4 == 0) + { + return true; + } + return false; +} + +TEST(LeapYear, DevidedBy4) +{ + ASSERT_TRUE(LeapYear(4)); +} + +TEST(LeapYear, DevidedBy100) +{ + ASSERT_FALSE(LeapYear(100)); +} + +TEST(LeapYear, DevidedBy400) +{ + ASSERT_TRUE(LeapYear(400)); +} + +TEST(LeapYear, RandomYearsTest) +{ + ASSERT_TRUE(LeapYear(2000)); + ASSERT_FALSE(LeapYear(1000)); + ASSERT_TRUE(LeapYear(1996)); + ASSERT_FALSE(LeapYear(1997)); + ASSERT_TRUE(LeapYear(2020)); + ASSERT_FALSE(LeapYear(2018)); + ASSERT_TRUE(LeapYear(-2000)); + ASSERT_FALSE(LeapYear(-1000)); +} diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index 17503028..8ab93634 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -16,3 +16,61 @@ 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. */ + +#include + +int OneSignDemicalView(const std::string& oneSign) +{ + if (oneSign == "1" || oneSign == "2" || oneSign == "0") + { + + return std::stoi( oneSign ); + } + return -1; +} + +int DemicalView(const std::string& str) +{ + if (str.empty()) + { + return 0; + } + int answer = 0; + for(size_t i = 1; i <= str.size(); ++i) + { + answer += OneSignDemicalView(str.substr(str.size() - i , 1)) * pow(3, i - 1) ; + } + return (answer > 0) ? answer : 0; +} + +TEST(TernaryNumbers, EmptyString) +{ + ASSERT_EQ(0, DemicalView("")); +} + +TEST(TernaryNumbers, EqualOne) +{ + ASSERT_EQ(1, DemicalView("1")); +} + +TEST(TernaryNumbers, EqualTwo) +{ + ASSERT_EQ(2, DemicalView("2")); +} + +TEST(TernaryNumbers, TwoSignString01) +{ + ASSERT_EQ(1, DemicalView("01")); +} + + +TEST(TernaryNumbers, TwoSignString11) +{ + ASSERT_EQ(4, DemicalView("11")); +} + + +TEST(TernaryNumbers, SpecifcationTest) +{ + ASSERT_EQ(302, DemicalView("102012")); +} diff --git a/tdd_intro/homework/03_bank_ocr/test.cpp b/tdd_intro/homework/03_bank_ocr/test.cpp index cb4b28cb..06d3f3cc 100644 --- a/tdd_intro/homework/03_bank_ocr/test.cpp +++ b/tdd_intro/homework/03_bank_ocr/test.cpp @@ -86,6 +86,8 @@ Example input and output */ #include #include +#include +#include const unsigned short g_linesInDigit = 3; struct Digit @@ -139,6 +141,10 @@ const Digit s_digit9 = { " _ ", "|_|", " _|" }; +const Digit s_noDigit = { " _ ", + "|_|", + " _ " + }; const Display s_displayAll0 = { " _ _ _ _ _ _ _ _ _ ", "| || || || || || || || || |", @@ -194,3 +200,195 @@ const Display s_display123456789 = { " _ _ _ _ _ _ _ ", " | _| _||_||_ |_ ||_||_|", " ||_ _| | _||_| ||_| _|" }; +const Display s_wrongDisplay = { " _ _ _ _ _ _ _ _ _ ", + "|_||_||_||_||_||_||_||_||_|", + " _| _| _| _ _| _| _| _| _|" +}; + +// parse 1 number 0-9 +// right numbers +// wrong numbers +// empty digit + +// parse line +// more than 27 symbols in string +// less than 27 symbols in string +// 27 symbols in string + +// parse several structs + + + +// assumptions: +// one function, take vector of Display and return vector of string +// second function, take 1 Display and return one string with 9 digit. +// third function, take 1 Digit and return one int. +// No information about wrong entry* + +bool CompareDigits(const Digit& left, const Digit& right) +{ + return left.lines[0] == right.lines[0] && left.lines[1] == right.lines[1] && left.lines[2] == right.lines[2]; +} + +int ZipNumberParser(const Digit& digit) +{ + std::vector allNumbers; + allNumbers.reserve(10); + allNumbers.push_back(s_digit0); + allNumbers.push_back(s_digit1); + allNumbers.push_back(s_digit2); + allNumbers.push_back(s_digit3); + allNumbers.push_back(s_digit4); + allNumbers.push_back(s_digit5); + allNumbers.push_back(s_digit6); + allNumbers.push_back(s_digit7); + allNumbers.push_back(s_digit8); + allNumbers.push_back(s_digit9); + + for (int i = 0; i < allNumbers.size(); ++i) + { + if ( CompareDigits(digit, allNumbers[i]) ) + { + return i; + } + } + return -1; +} + +std::string ZipLineParser(const Display& display) +{ + if (display.lines[0].size() != 27 || display.lines[1].size() != 27 || display.lines[2].size() != 27) + { + return "-1"; + } + std::string answer=""; + for(int i = 0; i < 27; i+=3) + { + Digit tempDigit{ std::string(display.lines[0].begin() + i,display.lines[0].begin()+3 + i), + std::string(display.lines[1].begin() + i,display.lines[1].begin()+3 + i), + std::string(display.lines[2].begin() + i,display.lines[2].begin()+3 + i)}; + int temp = ZipNumberParser(tempDigit); + if(temp == -1) + { + return "-1"; + } + else + { + answer = answer + std::to_string(temp); + } + } + return answer; +} + +std::vector ZipVectorParser(std::vector displays) +{ + std::vector answer; + for(auto disp:displays) + { + answer.push_back(ZipLineParser(disp)); + } + return answer; +} + + +TEST(Bank, NumberZero) +{ + ASSERT_EQ(0, ZipNumberParser(s_digit0)); +} + +TEST(Bank, NumberOne) +{ + ASSERT_EQ(1, ZipNumberParser(s_digit1)); +} + +TEST(Bank, NumberThree) +{ + ASSERT_EQ(3, ZipNumberParser(s_digit3)); +} + +TEST(Bank, WrongNumber) +{ + ASSERT_EQ(-1, ZipNumberParser(s_noDigit)); +} + +TEST(Bank, EmptyDigit) +{ + const Digit emptyDigit{"", "", ""}; + ASSERT_EQ(-1, ZipNumberParser(emptyDigit)); +} + +TEST(Bank, LineOfZero) +{ + ASSERT_EQ("000000000", ZipLineParser(s_displayAll0)); +} + +TEST(Bank, emptyDisplay) +{ + Display emptyDisplay{"", "", ""}; + ASSERT_EQ("-1", ZipLineParser(emptyDisplay)); +} + +TEST(Bank, largeDisplay) +{ + Display largeDisplay{"______________________________", + "___________________________", + "___________________________"}; + ASSERT_EQ("-1", ZipLineParser(largeDisplay)); +} +TEST(Bank, LineOf1) +{ + ASSERT_EQ("111111111", ZipLineParser(s_displayAll1)); +} + +TEST(Bank, LineOf2) +{ + ASSERT_EQ("222222222", ZipLineParser(s_displayAll2)); +} + +TEST(Bank, Line123456789) +{ + ASSERT_EQ("123456789", ZipLineParser(s_display123456789)); +} + +TEST(Bank, WrongDispley) +{ + ASSERT_EQ("-1", ZipLineParser(s_wrongDisplay)); +} + +TEST(Bank, TwoLines) +{ + std::vector linesOfDisplay; + linesOfDisplay.push_back(s_displayAll0); + linesOfDisplay.push_back(s_displayAll1); + std::vector answer; + answer.push_back("000000000"); + answer.push_back("111111111"); + ASSERT_EQ(answer, ZipVectorParser(linesOfDisplay)); +} + +TEST(Bank, ThreeLines) +{ + std::vector linesOfDisplay; + linesOfDisplay.push_back(s_displayAll0); + linesOfDisplay.push_back(s_displayAll1); + linesOfDisplay.push_back(s_display123456789); + std::vector answer; + answer.push_back("000000000"); + answer.push_back("111111111"); + answer.push_back("123456789"); + ASSERT_EQ(answer, ZipVectorParser(linesOfDisplay)); +} + +TEST(Bank, ThreeLinesWithWrongDisplay) +{ + std::vector linesOfDisplay; + linesOfDisplay.push_back(s_displayAll0); + linesOfDisplay.push_back(s_wrongDisplay); + linesOfDisplay.push_back(s_display123456789); + std::vector answer; + answer.push_back("000000000"); + answer.push_back("-1"); + answer.push_back("123456789"); + ASSERT_EQ(answer, ZipVectorParser(linesOfDisplay)); +} + diff --git a/tdd_intro/homework/05_word_wrapp/05_word_wrapp.pro b/tdd_intro/homework/05_word_wrapp/05_word_wrapp.pro new file mode 100644 index 00000000..c7fec38c --- /dev/null +++ b/tdd_intro/homework/05_word_wrapp/05_word_wrapp.pro @@ -0,0 +1,9 @@ +include(../../gtest.pri) + +TEMPLATE = app +CONFIG += console c++11 +CONFIG -= app_bundle +CONFIG -= qt + +SOURCES += \ + test.cpp diff --git a/tdd_intro/homework/05_word_wrapp/test.cpp b/tdd_intro/homework/05_word_wrapp/test.cpp new file mode 100644 index 00000000..c2f69d79 --- /dev/null +++ b/tdd_intro/homework/05_word_wrapp/test.cpp @@ -0,0 +1,138 @@ +/* +Write a function, that is given a string and a length limit, splits provided string into sequence of string, +where length of each string is not more, than provided limit. If there are spaces under provided limit - +last space should be used to wrapp this line. If there are no spaces - wrapp it on provided length limit. + +Example: +When pos is specified, the search only includes sequences of characters that begin at or before position pos, +ignoring any possible match beginning after pos. + +"When pos is specified, the", +"search only includes sequences", +"of characters that begin at or", +"before position pos, ignoring", +"any possible match beginning", +"after pos." +*/ + +#include +#include + +// empty string +// string shorter than wrap number +// word longer than wrap number +// word much longer than wrap number (more than 2 strings) +// string longer than wrap number + +// string wrapped by several whitespaces (less than wrapLength) +// string wrapped by several whitespaces (more than wrapLength) +// only whitespaces in string + +using WrappedStrings = std::vector; + +WrappedStrings WrapString(const std::string& str, size_t wrapLength) +{ + WrappedStrings result; + + for(size_t i = 0; i < str.length(); ) + { + while(i < str.length() && str[i] == ' ') + { + ++i; + } + std::string cur = str.substr(i, wrapLength); + + if((i + wrapLength) < str.size() && str[i + wrapLength] != ' ' && str.find(' ') != std::string::npos) + { + size_t curLenth = wrapLength; + while(str[i+curLenth] != ' ') + { + --curLenth; + cur = str.substr(i, curLenth); + } + } + while (!cur.empty() && cur.back() == ' ') + { + cur.pop_back(); + } + + + + if(!cur.empty()) + { + result.push_back(cur); + } + i+=cur.size(); + } + + return result; +} + +TEST(WrapString, EmptyString) +{ + ASSERT_EQ(WrappedStrings(), WrapString("", 25)); +} + +TEST(WrapString, StringShorterWrapNumber) +{ + ASSERT_EQ(WrappedStrings{"asdf"}, WrapString("asdf", 8)); +} + +TEST(WrapString, StringLongerThanWrapNumber) +{ + WrappedStrings expected = {"asd", "f"}; + ASSERT_EQ(expected, WrapString("asdf", 3)); +} + +TEST(WrapString, StringLongerThanWrapNumberSeveralParts) +{ + WrappedStrings expected = {"12", "34", "56"}; + ASSERT_EQ(expected, WrapString("123456", 2)); +} + +TEST(WrapString, MultipleWordsLonger) +{ + WrappedStrings expected = {"1", "2"}; + ASSERT_EQ(expected, WrapString("1 2", 1)); +} + +TEST(WrapString, SpaceStringEnd) +{ + WrappedStrings expected = {"1", "2"}; + ASSERT_EQ(expected, WrapString("1 2", 2)); +} + +TEST(WrapString, StringWrappedBySeveralWhitespace) +{ + WrappedStrings expected = {"12", "34"}; + ASSERT_EQ(expected, WrapString("12 34", 3)); +} + +TEST(WrapString, StringWrappedWithSeveralWhitespaceInEnd) +{ + WrappedStrings expected = {"12", "34"}; + ASSERT_EQ(expected, WrapString("12 34", 4)); +} + +TEST(WrapString, StringWrappedWithSeveralWhitespaceInBegin) +{ + WrappedStrings expected = {"1234", "56"}; + ASSERT_EQ(expected, WrapString("1234 56", 4)); +} + +TEST(WrapString, StringLongerThanWrappedWithWhitespace) +{ + WrappedStrings expected = {"12", "34"}; + ASSERT_EQ(expected, WrapString("12 34", 4)); +} + +TEST(WrapString, StringFromSpec) +{ + WrappedStrings expected = {"When pos is specified, the", + "search only includes sequences", + "of characters that begin at or", + "before position pos, ignoring", + "any possible match beginning", + "after pos."}; + ASSERT_EQ(expected, WrapString("When pos is specified, the search only includes sequences of characters that begin at or before position pos, ignoring any possible match beginning after pos.", 30)); +} diff --git a/tdd_intro/homework/05_coffee/05_coffee.pro b/tdd_intro/homework/06_coffee/05_coffee.pro similarity index 100% rename from tdd_intro/homework/05_coffee/05_coffee.pro rename to tdd_intro/homework/06_coffee/05_coffee.pro diff --git a/tdd_intro/homework/05_coffee/test.cpp b/tdd_intro/homework/06_coffee/test.cpp similarity index 100% rename from tdd_intro/homework/05_coffee/test.cpp rename to tdd_intro/homework/06_coffee/test.cpp diff --git a/tdd_intro/homework/homework.pro b/tdd_intro/homework/homework.pro index 5824b2ae..cf6c01b9 100644 --- a/tdd_intro/homework/homework.pro +++ b/tdd_intro/homework/homework.pro @@ -5,4 +5,5 @@ SUBDIRS += \ 02_ternary_numbers \ 03_bank_ocr \ 04_weather_client \ - 05_coffee + 05_word_wrapp \ + 06_coffee