Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
31 changes: 31 additions & 0 deletions tdd_intro/homework/01_leap_year/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,34 @@ If your language provides a method in the standard library that does this look-u
*/

#include <gtest/gtest.h>

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));
}
106 changes: 106 additions & 0 deletions tdd_intro/homework/02_ternary_numbers/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,109 @@ 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));
}

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<int>(symbol - '0');
}
TEST(ConvertToInt, two_for_2)
{
ASSERT_EQ(2, ConvertToInt('2'));
}

int TernaryNumberValue(std::string number)
{
if(number.empty())
return 0;



int sum = 0;
for(size_t i = 0, size = number.size(); i < size; ++i)
{
size_t digitNumber = size - i -1;

char symbol = number.at(i);
if(!IsTernaryDigit(symbol))
{
return 0;
}
int value = TernaryDigitValue(ConvertToInt(symbol), static_cast<int>(digitNumber));
sum+=value;
}
return sum;
}

TEST(TernaryNumberValue, zero_for_word)
{
ASSERT_EQ(0, TernaryNumberValue("word"));
}

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"));
}
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"));
}
70 changes: 70 additions & 0 deletions tdd_intro/homework/03_bank_ocr/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,73 @@ 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)
{
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),
number.lines[2].substr(lineOffset, g_digitLen)

};
return d;
}

TEST(GetDigit, NineForAllNinesAnd0)
{
ASSERT_TRUE(IsDigitsEqual(s_digit9, GetDigit(s_displayAll9, 0)));
}

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