diff --git a/.gitignore b/.gitignore index 259148fa..3e94e61a 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,9 @@ *.exe *.out *.app + +# Build dirs +build-* + +# .user files +*.user \ No newline at end of file diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 6bdc8367..bafa8d47 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -17,3 +17,78 @@ such: 1 #include #include +std::vector Split(const std::string& sentence, const std::string& delimetr) +{ + std::vector result; + + std::string::size_type pre_pos = 0; + for (;;) + { + std::string::size_type delimetr_pos = sentence.find(delimetr, pre_pos); + result.push_back(sentence.substr(pre_pos, delimetr_pos - pre_pos)); + if (delimetr_pos == std::string::npos) + { + break; + } + pre_pos = delimetr_pos + 1; + } + + return result; +} +std::map CountWords(const std::string& sentence) +{ + std::map result; + std::vector words = Split(sentence, " "); + for (const std::string& word : words) + { + if (result.find(word) != result.end()) + { + result[word]++; + } + else + { + result.insert(std::make_pair(word, 1)); + } + } + return result; +} + +TEST(SplitTest, ReturnsNWordsForNWordsSentenceSentence) +{ + EXPECT_EQ(std::vector({"word"}), Split("word", " ")); + EXPECT_EQ(std::vector({"word_a", "word_b"}), Split("word_a word_b", " ")); + EXPECT_EQ(std::vector({"word_a", "word_b", "word_c"}), Split("word_a word_b word_c", " ")); + EXPECT_EQ(std::vector({"word_a", "word_b", "word_a"}), Split("word_a word_b word_a", " ")); +} + +TEST(CountWordsTest, ReturnsSameWordMapForOneWord) +{ + std::map expected({ { "word", 1 } }); + EXPECT_EQ(expected, CountWords("word")); +} +TEST(CountWordsTest, CountsDifferentWords) +{ + std::map expected({ { "word_a", 1 }, { "word_b", 1 } }); + EXPECT_EQ(expected, CountWords("word_a word_b")); +} +TEST(CountWordsTest, CountSameWords) +{ + std::map expected({ { "word_a", 2 } }); + EXPECT_EQ(expected, CountWords("word_a word_a")); +} +TEST(CountWordsTest, Acceptance) +{ + std::map expected({ + { "olly", 3 }, + { "in", 2 }, + { "come", 1 }, + { "free", 1 }, + { "please", 2 }, + { "let", 1 }, + { "it", 1 }, + { "be", 1 }, + { "such", 1 }, + { "manner", 1 }, + }); + EXPECT_EQ(expected, CountWords("olly olly in come free please please let it be in such manner olly")); +} diff --git a/tdd_intro/homework/01_leap_year/test.cpp b/tdd_intro/homework/01_leap_year/test.cpp index 4f186c8b..474d2005 100644 --- a/tdd_intro/homework/01_leap_year/test.cpp +++ b/tdd_intro/homework/01_leap_year/test.cpp @@ -13,3 +13,36 @@ If your language provides a method in the standard library that does this look-u */ #include + +bool isDivisibleBy(size_t numb, size_t by) +{ + return numb % by == 0; +} + +bool isLeapYear(size_t year) +{ + return isDivisibleBy(year, 4) + && (!isDivisibleBy(year, 100) + || isDivisibleBy(year, 400)); +} + +TEST(LeapYear, TheYearsDivisibleBy4AreLeap) +{ + EXPECT_TRUE(isLeapYear(2016)); + EXPECT_TRUE(isLeapYear(2020)); +} +TEST(LeapYear, TheYearsDivisibleBy100AreNotLeap) +{ + EXPECT_FALSE(isLeapYear(1900)); + EXPECT_FALSE(isLeapYear(1800)); +} +TEST(LeapYear, TheYearsDivisibleBy400AreLeap) +{ + EXPECT_TRUE(isLeapYear(2000)); + EXPECT_TRUE(isLeapYear(1600)); +} +TEST(LeapYear, AnyOtherYearsArentNotLeapYear) +{ + EXPECT_FALSE(isLeapYear(2017)); + EXPECT_FALSE(isLeapYear(1235)); +}