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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@
*.exe
*.out
*.app

# Build dirs
build-*

# .user files
*.user
75 changes: 75 additions & 0 deletions tdd_intro/demo/02_word_count/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,78 @@ such: 1
#include <string>
#include <map>

std::vector<std::string> Split(const std::string& sentence, const std::string& delimetr)
{
std::vector<std::string> 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<std::string, size_t> CountWords(const std::string& sentence)
{
std::map<std::string, size_t> result;
std::vector<std::string> words = Split(sentence, " ");
for (const std::string& word : words)
{
if (result.find(word) != result.end())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно упростить за счёт оператора [] в std::map:

result[word] += 1;

Он сделает вставку, если элемента нет, а в противном случае возьмет существующий

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А он проинициализирует вставленное значение нулем?)

{
result[word]++;
}
else
{
result.insert(std::make_pair(word, 1));
}
}
return result;
}

TEST(SplitTest, ReturnsNWordsForNWordsSentenceSentence)
{
EXPECT_EQ(std::vector<std::string>({"word"}), Split("word", " "));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

этот экспект должен идти отдельно, поскольку изолирует отдельный кейс отсутствия делимитера

EXPECT_EQ(std::vector<std::string>({"word_a", "word_b"}), Split("word_a word_b", " "));
EXPECT_EQ(std::vector<std::string>({"word_a", "word_b", "word_c"}), Split("word_a word_b word_c", " "));
EXPECT_EQ(std::vector<std::string>({"word_a", "word_b", "word_a"}), Split("word_a word_b word_a", " "));
}

TEST(CountWordsTest, ReturnsSameWordMapForOneWord)
{
std::map<std::string, size_t> expected({ { "word", 1 } });
EXPECT_EQ(expected, CountWords("word"));
}
TEST(CountWordsTest, CountsDifferentWords)
{
std::map<std::string, size_t> expected({ { "word_a", 1 }, { "word_b", 1 } });
EXPECT_EQ(expected, CountWords("word_a word_b"));
}
TEST(CountWordsTest, CountSameWords)
{
std::map<std::string, size_t> expected({ { "word_a", 2 } });
EXPECT_EQ(expected, CountWords("word_a word_a"));
}
TEST(CountWordsTest, Acceptance)
{
std::map<std::string, size_t> 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"));
}
33 changes: 33 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,36 @@ If your language provides a method in the standard library that does this look-u
*/

#include <gtest/gtest.h>

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