Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@
*.exe
*.out
*.app
build
.DS_Store
*.pro.user
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@
#include "gmock/internal/gmock-port.h"
#include "gtest/gtest.h"

namespace {

const testing::internal::RelationToSourceCopy kCopy;
const testing::internal::RelationToSourceReference kReference;

}

namespace testing {
namespace internal {

Expand Down
4 changes: 2 additions & 2 deletions tdd_intro/cleanroom/cleanroom.pro
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
TEMPLATE = subdirs

SUBDIRS += \
chatclient
#SUBDIRS += \
# chatclient
4 changes: 2 additions & 2 deletions tdd_intro/gtest.pri
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../3rd_party/gtest/release/ -lgtest
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../../3rd_party/gtest/debug/ -lgtest
else:unix:!macx: LIBS += -L$$OUT_PWD/../../3rd_party/gtest/ -lgtest
else:unix: LIBS += -L$$OUT_PWD/../../3rd_party/gtest/ -lgtest

INCLUDEPATH += $$PWD/3rd_party/gtest/googletest/include
DEPENDPATH += $$PWD/3rd_party/gtest/googletest/include
Expand All @@ -10,4 +10,4 @@ win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../3rd_
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../3rd_party/gtest/debug/libgtest.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../3rd_party/gtest/release/gtest.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../3rd_party/gtest/debug/gtest.lib
else:unix:!macx: PRE_TARGETDEPS += $$OUT_PWD/../../3rd_party/gtest/libgtest.a
else:unix: PRE_TARGETDEPS += $$OUT_PWD/../../3rd_party/gtest/libgtest.a
319 changes: 211 additions & 108 deletions tdd_intro/homework/03_bank_ocr/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,111 +87,214 @@ Example input and output
#include <gtest/gtest.h>
#include <string>

const unsigned short g_digitLen = 3;
const unsigned short g_linesInDigit = 3;
struct Digit
{
std::string lines[g_linesInDigit];
};

const unsigned short g_digitsOnDisplay = 9;
struct Display
{
std::string lines[g_linesInDigit];
};

const Digit s_digit0 = { " _ ",
"| |",
"|_|"
};
const Digit s_digit1 = { " ",
" |",
" |"
};
const Digit s_digit2 = { " _ ",
" _|",
"|_ "
};
const Digit s_digit3 = { " _ ",
" _|",
" _|"
};
const Digit s_digit4 = { " ",
"|_|",
" |"
};
const Digit s_digit5 = { " _ ",
"|_ ",
" _|"
};
const Digit s_digit6 = { " _ ",
"|_ ",
"|_|"
};
const Digit s_digit7 = { " _ ",
" |",
" |"
};
const Digit s_digit8 = { " _ ",
"|_|",
"|_|"
};
const Digit s_digit9 = { " _ ",
"|_|",
" _|"
};

const Display s_displayAll0 = { " _ _ _ _ _ _ _ _ _ ",
"| || || || || || || || || |",
"|_||_||_||_||_||_||_||_||_|"
};

const Display s_displayAll1 = { " ",
" | | | | | | | | |",
" | | | | | | | | |"
};

const Display s_displayAll2 = { " _ _ _ _ _ _ _ _ _ ",
" _| _| _| _| _| _| _| _| _|",
"|_ |_ |_ |_ |_ |_ |_ |_ |_ "
};

const Display s_displayAll3 = { " _ _ _ _ _ _ _ _ _ ",
" _| _| _| _| _| _| _| _| _|",
" _| _| _| _| _| _| _| _| _|"
};

const Display s_displayAll4 = { " ",
"|_||_||_||_||_||_||_||_||_|",
" | | | | | | | | |"
};

const Display s_displayAll5 = { " _ _ _ _ _ _ _ _ _ ",
"|_ |_ |_ |_ |_ |_ |_ |_ |_ ",
" _| _| _| _| _| _| _| _| _|"
};

const Display s_displayAll6 = { " _ _ _ _ _ _ _ _ _ ",
"|_ |_ |_ |_ |_ |_ |_ |_ |_ ",
"|_||_||_||_||_||_||_||_||_|"
};

const Display s_displayAll7 = { " _ _ _ _ _ _ _ _ _ ",
" | | | | | | | | |",
" | | | | | | | | |"
};

const Display s_displayAll8 = { " _ _ _ _ _ _ _ _ _ ",
"|_||_||_||_||_||_||_||_||_|",
"|_||_||_||_||_||_||_||_||_|"
};

const Display s_displayAll9 = { " _ _ _ _ _ _ _ _ _ ",
"|_||_||_||_||_||_||_||_||_|",
" _| _| _| _| _| _| _| _| _|"
};

const Display s_display123456789 = { " _ _ _ _ _ _ _ ",
" | _| _||_||_ |_ ||_||_|",
" ||_ _| | _||_| ||_| _|"
};
const size_t kCharsPerDigit = 3;
const size_t kLinesPerEntity = 3;

std::vector<std::string> SplitStringForLines(const std::string& string)
{
std::vector<std::string> lines;

std::string mutableString = string;
while (true)
{
size_t newLinePos = mutableString.find("\n");
lines.push_back(mutableString.substr(0, newLinePos));

if (newLinePos == std::string::npos)
{
break;
}

mutableString = mutableString.substr(newLinePos + 1);
}

return lines;
}

std::vector<std::string> ExtractDigitsFromEntity(const std::string& entity)
{
std::vector<std::string> lines = SplitStringForLines(entity);
if (lines.size() != kLinesPerEntity + 1) //1 for empty line in the end
{
throw std::runtime_error("Entity format is wrong. Not enough lines.");
}

lines.resize(kLinesPerEntity);

for (auto& line: lines)
{
if (line.size() % kCharsPerDigit != 0)
{
throw std::runtime_error("Entity format is wrong. Too small line.");
}
}

std::vector<std::string> digits;
for (size_t i = 0; i < lines[0].size(); i += kCharsPerDigit)
{
std::string digit;
for (auto& line: lines)
{
digit += line.substr(i, kCharsPerDigit);
}

digits.push_back(digit);
}

return digits;
}

std::string ParseNumbersFromEntity(const std::string& entity)
{
std::vector<std::string> digits = ExtractDigitsFromEntity(entity);
std::map<std::string, std::string> numberFromDigit = {{" _ "
"| |"
"|_|", "0"},
{" "
" |"
" |", "1"},
{" _ "
" _|"
"|_ ", "2"},
{" _ "
" _|"
" _|", "3"}};

std::string numbers;
for (auto& digit: digits)
{
numbers += numberFromDigit[digit];
}

return numbers;
}

TEST(SplitStringForLines, OneLineWithoutDelimiters)
{
std::vector<std::string> lines = {"a"};
EXPECT_EQ(lines, SplitStringForLines("a"));
}

TEST(SplitStringForLines, TwoLinesWithOneDelimiters)
{
std::vector<std::string> lines = {"a", "b"};
EXPECT_EQ(lines, SplitStringForLines("a\nb"));
}

TEST(SplitStringForLines, ThreeLinesWithTwoDelimiters)
{
std::vector<std::string> lines = {"a", "b", "c"};
EXPECT_EQ(lines, SplitStringForLines("a\nb\nc"));
}

TEST(SplitStringForLines, OneLineForEmptyLine)
{
std::vector<std::string> lines = {""};
EXPECT_EQ(lines, SplitStringForLines(""));
}

TEST(SplitStringForLines, TwoEmptyLinesForLineOnlyWithDelimiters)
{
std::vector<std::string> lines = {"", ""};
EXPECT_EQ(lines, SplitStringForLines("\n"));
}

TEST(ExtractDigitsFromEntity, OneDigitExtraction)
{
std::vector<std::string> digits = {" _ "
"| |"
"|_|"};

EXPECT_EQ(digits, ExtractDigitsFromEntity(" _ \n"
"| |\n"
"|_|\n"));
}

TEST(ExtractDigitsFromEntity, TwoDigitsExtraction)
{
std::vector<std::string> digits = {" _ "
"| |"
"|_|",
" "
" |"
" |"};

EXPECT_EQ(digits, ExtractDigitsFromEntity(" _ \n"
"| | |\n"
"|_| |\n"));
}

TEST(ExtractDigitsFromEntity, ThreeDigitsExtraction)
{
std::vector<std::string> digits = {" _ "
"| |"
"|_|",
" "
" |"
" |",
" _ "
" _|"
"|_ ",};

EXPECT_EQ(digits, ExtractDigitsFromEntity(" _ _ \n"
"| | | _|\n"
"|_| ||_ \n"));
}

TEST(ExtractDigitsFromEntity, NoDigitsForEmptyEntity)
{
std::vector<std::string> digits = {};
EXPECT_EQ(digits, ExtractDigitsFromEntity("\n"
"\n"
"\n"));
}

TEST(ExtractDigitsFromEntity, ExceptionForNotEnoughLines)
{
EXPECT_THROW(ExtractDigitsFromEntity(" \n"
" |\n"),
std::runtime_error);
}

TEST(ExtractDigitsFromEntity, ExceptionForTooSmallLines)
{
EXPECT_THROW(ExtractDigitsFromEntity("||\n"
"|||\n"
"|||\n"),
std::runtime_error);
}

TEST(ParseNumbersFromEntity, ZeroFromEntity)
{
EXPECT_EQ("0", ParseNumbersFromEntity(" _ \n"
"| |\n"
"|_|\n"));
}

TEST(ParseNumbersFromEntity, OneFromEntity)
{
EXPECT_EQ("1", ParseNumbersFromEntity(" \n"
" |\n"
" |\n"));
}

TEST(ParseNumbersFromEntity, TwoFromEntity)
{
EXPECT_EQ("2", ParseNumbersFromEntity(" _ \n"
" _|\n"
"|_ \n"));
}

TEST(ParseNumbersFromEntity, ThreeFromEntity)
{
EXPECT_EQ("3", ParseNumbersFromEntity(" _ \n"
" _|\n"
" _|\n"));
}

TEST(ParseNumbersFromEntity, ServeralNumbersFromEntity)
{
EXPECT_EQ("0123", ParseNumbersFromEntity(" _ _ _ \n"
"| | | _| _|\n"
"|_| ||_ _|\n"));
}
1 change: 1 addition & 0 deletions tdd_intro/homework/04_weather_client/04_weather_client.pro
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include(../../gmock.pri)
include(../../gtest.pri)

TEMPLATE = app
CONFIG += console c++11
Expand Down
2 changes: 2 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,8 @@ Each line means "<request>" : "<response>":
#include <gtest/gtest.h>
#include <gmock/gmock.h>

#include <cmath>

struct Weather
{
short temperature = 0;
Expand Down
1 change: 1 addition & 0 deletions tdd_intro/homework/06_coffee/06_coffee.pro
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include(../../gtest.pri)
include(../../gmock.pri)

TEMPLATE = app
Expand Down
4 changes: 2 additions & 2 deletions tdd_intro/homework/homework.pro
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ SUBDIRS += \
02_ternary_numbers \
03_bank_ocr \
04_weather_client \
05_word_wrapp \
06_coffee
05_word_wrapp
# 06_coffee