Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
0e34d7e
Red
alexchernysh92 Nov 13, 2018
98552d9
Green + refactoring
alexchernysh92 Nov 13, 2018
fb09975
Red
alexchernysh92 Nov 13, 2018
99cdcdd
Green
alexchernysh92 Nov 13, 2018
a90cf74
Refactoring
alexchernysh92 Nov 13, 2018
9ff8ab3
Red
alexchernysh92 Nov 13, 2018
3cd4607
Green
alexchernysh92 Nov 13, 2018
9b15365
Refactoring
alexchernysh92 Nov 14, 2018
c8073c7
Red
alexchernysh92 Nov 14, 2018
944f4c7
Green
alexchernysh92 Nov 14, 2018
a004cfc
Added some test
alexchernysh92 Nov 14, 2018
afdf9fb
Added some test
alexchernysh92 Nov 14, 2018
456014f
Refactoring
alexchernysh92 Nov 14, 2018
f417ef6
Red
alexchernysh92 Nov 14, 2018
6f1e44a
Green
alexchernysh92 Nov 14, 2018
d4daf34
Red
alexchernysh92 Nov 14, 2018
c8c79c7
Green
alexchernysh92 Nov 14, 2018
23dea11
Refactoring
alexchernysh92 Nov 14, 2018
73f188e
Red
alexchernysh92 Nov 14, 2018
858047c
Green
alexchernysh92 Nov 14, 2018
967c443
Red
alexchernysh92 Nov 14, 2018
550d7fd
Refactoring
alexchernysh92 Nov 14, 2018
1a58753
Red + Green
alexchernysh92 Nov 14, 2018
ef3ee18
Red
alexchernysh92 Nov 14, 2018
a220bac
Green
alexchernysh92 Nov 14, 2018
3068f7d
Red
alexchernysh92 Nov 14, 2018
f072293
Green
alexchernysh92 Nov 14, 2018
272870e
Added test
alexchernysh92 Nov 14, 2018
a97542a
Red
alexchernysh92 Nov 14, 2018
14f8f04
Green
alexchernysh92 Nov 14, 2018
7fa9305
AcceptanceTest
alexchernysh92 Nov 14, 2018
a719696
Refactoring
alexchernysh92 Nov 14, 2018
532681f
First red test
alexchernysh92 Nov 22, 2018
70c89b5
green
alexchernysh92 Nov 22, 2018
e0859a2
Refactoring + red test
alexchernysh92 Nov 22, 2018
73da9dc
Green
alexchernysh92 Nov 22, 2018
d834675
Red
alexchernysh92 Nov 22, 2018
82e6d50
Green
alexchernysh92 Nov 22, 2018
34862d7
Refactoring
alexchernysh92 Nov 22, 2018
6ec87e2
Red
alexchernysh92 Nov 22, 2018
ca60ec4
Green
alexchernysh92 Nov 22, 2018
8a3099d
Refactoring + next Redtest
alexchernysh92 Nov 22, 2018
fb6d2f2
Green
alexchernysh92 Nov 22, 2018
6c1fbe7
Red
alexchernysh92 Nov 22, 2018
1be25c4
Green
alexchernysh92 Nov 22, 2018
3e857a5
Refactoring
alexchernysh92 Nov 22, 2018
f121f21
Red
alexchernysh92 Nov 22, 2018
c512957
Green
alexchernysh92 Nov 22, 2018
441f249
Merge pull request #3 from Alexandr1992/BankOCR
Nov 22, 2018
b2c20c7
Merge pull request #4 from Alexandr1992/BankOCR
Nov 29, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions tdd_intro/homework/02_ternary_numbers/test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <gtest/gtest.h>
#include <cmath>
#include <string>

/*
Convert a ternary number, represented as a string (e.g. '102012'), to its decimal equivalent using first principles.
Expand All @@ -16,3 +18,72 @@ 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)
{
if(numerical > 2)
{
return 0;
}

double ternaryPow = std::pow(3, position);
return static_cast<size_t>(ternaryPow) * numerical;
}

size_t ConvertTernaryToDecimal(const std::string& ternary)
{
size_t result = 0;
size_t position = ternary.size();
for (const char ternarySymbChar: ternary)
{
int ternaryNumb = std::atoi(&ternarySymbChar);
result += ConvertTernaryToDecimal(static_cast<size_t>(ternaryNumb), --position);
}

return result;
}

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, TestConvertInvalidTernary)
{
EXPECT_EQ(0, ConvertTernaryToDecimal(4, 0));
EXPECT_EQ(0, ConvertTernaryToDecimal(5, 0));
EXPECT_EQ(0, ConvertTernaryToDecimal(20, 0));
}

TEST(TernaryNumber, TestConvertOneTernaryNumericalStrToDecimal)
{
EXPECT_EQ(0, ConvertTernaryToDecimal("0"));
EXPECT_EQ(1, ConvertTernaryToDecimal("1"));
EXPECT_EQ(2, ConvertTernaryToDecimal("2"));
EXPECT_EQ(0, ConvertTernaryToDecimal("3"));
}

TEST(TernaryNumber, TestConvertTernaryStrToDecimal)
{
EXPECT_EQ(302, ConvertTernaryToDecimal("102012"));
}

TEST(TernaryNumber, TestEmptyTernaryString)
{
EXPECT_EQ(0, ConvertTernaryToDecimal(""));
}

TEST(TernaryNumber, TestIncorrectTernaryNumberInString)
{
EXPECT_EQ(302, ConvertTernaryToDecimal("192612"));
EXPECT_EQ(302, ConvertTernaryToDecimal("1*2%12"));
}
128 changes: 128 additions & 0 deletions tdd_intro/homework/03_bank_ocr/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ struct Display
std::string lines[g_linesInDigit];
};

const std::map<std::string, char> s_digitsMap{
{ " _ | ||_|", '0'},
{ " | |", '1'},
{ " _ _||_ ", '2'},
{ " _ _| _|", '3'},
{ " |_| |", '4'},
{ " _ |_ _|", '5'},
{ " _ |_ |_|", '6'},
{ " _ | |", '7'},
{ " _ |_||_|", '8'},
{ " _ |_| _|", '9'}
};

const Digit s_digit0 = { " _ ",
"| |",
"|_|"
Expand Down Expand Up @@ -195,3 +208,118 @@ const Display s_display123456789 = { " _ _ _ _ _ _ _ ",
" | _| _||_||_ |_ ||_||_|",
" ||_ _| | _||_| ||_| _|"
};

char DetectDigit(const Digit& digit)
{
const std::string digitHash = digit.lines[0] + digit.lines[1] + digit.lines[2];

const auto& value = s_digitsMap.find(digitHash);
if (value != s_digitsMap.end())
{
return value->second;
}

throw std::runtime_error("Invalid digit format");
}

Digit ParseDigitFromDisplay(const Display& display, size_t digitPos)
{
if (digitPos > 9)
{
throw std::runtime_error("Invalid display offset");
}

Digit digit;
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);
}

return digit;
}


std::string ParseDigits(const Display& display)
{
std::string finalDigits;
for (size_t i = 0; i < g_digitsOnDisplay; ++i)
{
Digit digit = ParseDigitFromDisplay(display, i);
finalDigits += DetectDigit(digit);
}

return finalDigits;
}

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));
}

TEST(BankOcr, TestThrowExceptionWhenDetectInvalidDigits)
{
const static Digit invalidDigit = { " _ ",
"| |",
"| |"};

EXPECT_THROW(DetectDigit(invalidDigit), std::runtime_error);
}

TEST(BankOcr, TestParseFirstDigitFromDisplay0)
{
EXPECT_EQ('0', ParseDigits(s_displayAll0)[0]);
}

TEST(BankOcr, TestParseFirstDigitFromDisplay1)
{
EXPECT_EQ('2', ParseDigits(s_display123456789)[1]);
}

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]);
}

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);
}

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));

}
151 changes: 151 additions & 0 deletions tdd_intro/homework/04_weather_client/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ Each line means "<request>" : "<response>":
#include <gtest/gtest.h>
#include <gmock/gmock.h>

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;
Expand All @@ -60,6 +67,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<short>(GetLineDoble(responseStream));
weather.windDirection = static_cast<unsigned short>(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:
Expand All @@ -68,6 +103,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
{
Expand All @@ -79,3 +120,113 @@ class IWeatherClient
virtual double GetAverageWindDirection(IWeatherServer& server, const std::string& date) = 0;
virtual double GetMaximumWindSpeed(IWeatherServer& server, const std::string& date) = 0;
};


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)
{
std::set<double> 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
{
std::set<double> 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
{
throw std::runtime_error("not implemented");
}

virtual double GetMaximumWindSpeed(IWeatherServer& server, const std::string& date) override
{
throw std::runtime_error("not implemented");
}

private:
Weather GetWeatherByDate(IWeatherServer& server, const std::string& date)
{
std::string response = server.GetWeather(date);
return ParseWeather(response);
}
};

TEST(WeatherClient, TestParseTemperatureFromResponse)
{
Weather weather = ParseWeather("1;1;1");
EXPECT_EQ(1, weather.temperature);
}

TEST(WeatherClient, TestThrowWhenCannotParseTemperature)
{
EXPECT_THROW(ParseWeather("1"), std::runtime_error);
}

TEST(WeatherClient, TestParseWindDirectionFromResponse)
{
Weather weather = ParseWeather("1;1;1");
EXPECT_EQ(1, weather.windDirection);
}

TEST(WeatherClient, TestParseInvalidWindDirectionFromResponse)
{
EXPECT_THROW(ParseWeather("1;360;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);
}

//Test client

TEST(WeatherClient, TestGetMinimumTemperature_21pm)
{
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"));
}

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(30, client.GetMaximumTemperature(server, "31.08.2018"));
}