diff --git a/tdd_intro/homework/01_word_count/test.cpp b/tdd_intro/homework/01_word_count/test.cpp index 4d36c9c..fe0d40d 100644 --- a/tdd_intro/homework/01_word_count/test.cpp +++ b/tdd_intro/homework/01_word_count/test.cpp @@ -13,10 +13,71 @@ free: 1 #include #include #include +#include using MyMap = std::map; MyMap WordsCount(const std::string& str) { + if (str.empty()) + { + return MyMap(); + } + MyMap result; + + std::istringstream stream(str); + std::string token; + + while(std::getline(stream, token, ' ')) + { + if (!token.empty()) + { + ++result[token]; + } + } + + return result; +} + + +TEST(WordsCount, empty_line) +{ + EXPECT_TRUE(WordsCount("").empty()); +} + +TEST(WordsCount, single_word) +{ + const std::string singleWord = "singleWord"; + MyMap expected {std::make_pair(singleWord, 1)}; + EXPECT_EQ(expected, WordsCount(singleWord)); +} + +TEST(WordsCount, two_different_words) +{ + MyMap expected {std::make_pair("word_1", 1), + std::make_pair("word_2", 1)}; + EXPECT_EQ(expected, WordsCount("word_1 word_2")); +} + +TEST(WordsCount, two_same_words) +{ + MyMap expected {std::make_pair("word_1", 2)}; + EXPECT_EQ(expected, WordsCount("word_1 word_1")); +} + +TEST(WordsCount, two_different_words_with_double_space) +{ + MyMap expected {std::make_pair("word_1", 1), + std::make_pair("word_2", 1)}; + EXPECT_EQ(expected, WordsCount("word_1 word_2")); +} + +TEST(WordsCount, acceptance) +{ + MyMap expected {std::make_pair("olly", 2), + std::make_pair("in", 1), + std::make_pair("come", 1), + std::make_pair("free", 1)}; + EXPECT_EQ(expected, WordsCount("olly olly in come free")); } diff --git a/tdd_intro/homework/02_leap_year/test.cpp b/tdd_intro/homework/02_leap_year/test.cpp index 4f186c8..30da571 100644 --- a/tdd_intro/homework/02_leap_year/test.cpp +++ b/tdd_intro/homework/02_leap_year/test.cpp @@ -13,3 +13,31 @@ If your language provides a method in the standard library that does this look-u */ #include + +bool IsLeapYear(int year) +{ + return year % 400 == 0 || year % 4 == 0 && year % 100 != 0; +} + +TEST(IsYearLeap, one_return_false) +{ + EXPECT_FALSE(IsLeapYear(1)); +} + +TEST(IsYearLeap, hundred_return_false) +{ + EXPECT_FALSE(IsLeapYear(100)); +} + +TEST(IsYearLeap, four_hundred_return_true) +{ + EXPECT_TRUE(IsLeapYear(400)); +} + +TEST(IsYearLeap, acceptance) +{ + EXPECT_FALSE(IsLeapYear(1900)); + EXPECT_TRUE(IsLeapYear(1996)); + EXPECT_FALSE(IsLeapYear(1997)); + EXPECT_TRUE(IsLeapYear(2000)); +} diff --git a/tdd_intro/homework/02_trinary_numbers/test.cpp b/tdd_intro/homework/02_trinary_numbers/test.cpp index c35967c..85c0aea 100644 --- a/tdd_intro/homework/02_trinary_numbers/test.cpp +++ b/tdd_intro/homework/02_trinary_numbers/test.cpp @@ -16,3 +16,75 @@ The last place in a trinary 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 CharToTernaryDigit(char ch) +{ + if (ch >= '0' && ch < '3') + { + return ch - '0'; + } + + throw std::runtime_error("non ternary"); +} + +int StringToTernaryNumber(const std::string& number) +{ + int result = 0; + const int numberOfDigits = number.size() - 1; + try + { + for (int i = numberOfDigits; i >= 0; --i) + { + if (!number.empty()) + { + result += CharToTernaryDigit(number[i]) * (std::pow(3, numberOfDigits - i)); + } + } + } + catch (const std::runtime_error&) + { + result = 0; + } + return result; +} + +TEST(CharToTernaryDigit, input_zero) +{ + EXPECT_EQ(0, CharToTernaryDigit('0')); +} + +TEST(CharToTernaryDigit, input_four_throw) +{ + EXPECT_THROW(CharToTernaryDigit('4'), std::runtime_error); +} + +TEST(CharToTernaryDigit, input_0) +{ + EXPECT_THROW(CharToTernaryDigit('/'), std::runtime_error); +} + +TEST(StringToTernaryNumber, input_zero) +{ + EXPECT_EQ(0, StringToTernaryNumber("0")); +} + +TEST(StringToTernaryNumber, input_empty_string) +{ + EXPECT_EQ(0, StringToTernaryNumber("")); +} + +TEST(StringToTernaryNumber, input_10) +{ + EXPECT_EQ(3, StringToTernaryNumber("10")); +} + +TEST(StringToTernaryNumber, input_invalid_number) +{ + EXPECT_EQ(0, StringToTernaryNumber("103")); +} + +TEST(StringToTernaryNumber, acceptance) +{ + EXPECT_EQ(302, StringToTernaryNumber("102012")); +}