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/04_weather_client/test.cpp b/tdd_intro/homework/04_weather_client/test.cpp index 6b04a27c..6bb01810 100644 --- a/tdd_intro/homework/04_weather_client/test.cpp +++ b/tdd_intro/homework/04_weather_client/test.cpp @@ -1,40 +1,348 @@ /* Fake Weather Client -You are going to develop a program that gets the statistics about weather in the cities using information from a certain server. Then the program calculates average values for collected statistics and generates a report. - -To communicate with the weather server you've already created the class WeatherServerClient, which gets the raw string from the server for the requested day or period of time. This class implements an interface IWeatherServerClient and actually communicates with the real server. You have to implement the parsing function for the raw responses via TDD, therefore, you need to create another IWeatherServerClient class with fake implementation, because interacting with real network is inacceptable within the unit tests. - -The server answers with text of this format: - 31.08.2018;03:00;20;181:5.1 - 31.08.2018;09:00;23;204:4.9 - 31.08.2018;15:00;33;193:4.3 - 31.08.2018;21:00;46;179:4.5 - Where each line represents the time of the day and contains the next information: ";