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
291 changes: 241 additions & 50 deletions tdd_intro/homework/05_bank_ocr/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,90 +98,281 @@ Example input and output
// - parse
// parse several lines

using Digit = std::vector<std::string>;
using DigitView = std::vector<std::string>;
class Digit
{
public:
Digit(const std::initializer_list<std::string>& lst)
{
DigitView view;
for (const auto& line : lst) { view.push_back(line); }
Init(view);
}
Digit(const DigitView& view)
{
Init(view);
}

const Digit s_1({" ",
" |",
" |"});
const DigitView& View() const { return m_view; }

const Digit s_2({" _ ",
" _|",
"|_ "});
static constexpr unsigned char Height() { return 3; }
static constexpr unsigned char Width() { return 3; }

std::vector<Digit> s_digits({
private:
void Init(const DigitView& view)
{
if (view.size() != Height())
{
throw std::runtime_error("Invalid digit view height");
}

});
for (const auto& line : view)
{
if (line.size() != Width())
{
throw std::runtime_error("Invalid digit view width");
}
}
m_view = view;
}

bool CheckMatrixDimension(const Digit& digit)
private:
DigitView m_view;
};

const Digit s_0 = { " _ ",
"| |",
"|_|"
};
const Digit s_1 = { " ",
" |",
" |"
};
const Digit s_2 = { " _ ",
" _|",
"|_ "
};
const Digit s_3 = { " _ ",
" _|",
" _|"
};
const Digit s_4 = { " ",
"|_|",
" |"
};
const Digit s_5 = { " _ ",
"|_ ",
" _|"
};
const Digit s_6 = { " _ ",
"|_ ",
"|_|"
};
const Digit s_7 = { " _ ",
" |",
" |"
};
const Digit s_8 = { " _ ",
"|_|",
"|_|"
};
const Digit s_9 = { " _ ",
"|_|",
" _|"
};

bool DigitsAreEqual(const Digit& left, const Digit& right)
{
const size_t prefferedSize = 3;

if (digit.size() != prefferedSize)
for (unsigned char line = 0; line < Digit::Height(); ++line)
{
return false;
if (left.View().at(line) != right.View().at(line))
{
return false;
}
}
return true;
}

for (const std::string& line : digit)
using Number = unsigned char;
Number DigitToNumber(const Digit& digit)
{
static const std::vector<const Digit*> s_possibleDigits = { &s_0, &s_1, &s_2, &s_3, &s_4, &s_5, &s_6, &s_7, &s_8, &s_9 };
for (Number n = 0; s_possibleDigits.size(); ++n)
{
if (line.size() != prefferedSize)
if (DigitsAreEqual(digit, *s_possibleDigits.at(n)))
{
return false;
return n;
}
}

return true;
throw std::runtime_error("Digit could not be parsed");
}

std::string ParseDigit(const Digit& digit)
using DigitsDisplayView = std::vector<std::string>;
class DigitsDisplay
{
if (digit == Digit({" ",
" |",
" |"}))
public:
DigitsDisplay(const std::initializer_list<std::string>& lst)
{
return "1";
if (lst.size() != Digit::Height())
{
throw std::runtime_error("Invalid digit display view height");
}

for (const auto& line : lst)
{
if (line.size() != Width())
{
throw std::runtime_error("Invalid digit display view width");
}
m_view.push_back(line);
}
}
else if (digit == Digit({" _ ",
" _|",
"|_ "}))

const DigitsDisplayView& View() const { return m_view; }

static constexpr unsigned char NumbersCount() { return 9; }
static constexpr unsigned char Height() { return Digit::Height(); }
static constexpr unsigned char Width() { return Digit::Width() * NumbersCount(); }

private:
DigitsDisplayView m_view;
};

std::string ParseDisplay(const DigitsDisplay& display)
{
DigitView digitView(display.Height());
std::string res;
for (unsigned char digitPos = 0; digitPos < display.Width(); digitPos += Digit::Width())
{
return "2";
for (unsigned char height = 0; height < display.Height(); ++height)
{
digitView.at(height) = display.View().at(height).substr(digitPos, Digit::Width());
}
res += std::to_string(DigitToNumber(Digit(digitView)));
}
return res;
}

return "8";
TEST(BankOCRTests, DigitToNumber_0)
{
EXPECT_EQ(0, DigitToNumber(Digit { " _ ",
"| |",
"|_|"
}
));
}

TEST(BankOCRTests, DigitToNumber_1)
{
EXPECT_EQ(1, DigitToNumber(Digit { " ",
" |",
" |"
}
));
}

TEST(BankOCRTests, Check_Matrix_dimension_true)
TEST(BankOCRTests, DigitToNumber_2)
{
Digit digit = {" ", " ", " "};
EXPECT_TRUE(CheckMatrixDimension(digit));
EXPECT_EQ(2, DigitToNumber(Digit { " _ ",
" _|",
"|_ "
}
));
}

TEST(BankOCRTests, Check_Matrix_dimension_false)
TEST(BankOCRTests, DigitToNumber_Acceptance)
{
Digit digit = {" ", "! ", " "};
EXPECT_FALSE(CheckMatrixDimension(digit));
EXPECT_EQ(3, DigitToNumber(Digit { " _ ",
" _|",
" _|"
}
));
EXPECT_EQ(4, DigitToNumber(Digit { " ",
"|_|",
" |"
}
));
EXPECT_EQ(5, DigitToNumber(Digit { " _ ",
"|_ ",
" _|"
}
));
EXPECT_EQ(6, DigitToNumber(Digit { " _ ",
"|_ ",
"|_|"
}
));
EXPECT_EQ(7, DigitToNumber(Digit { " _ ",
" |",
" |"
}
));
EXPECT_EQ(8, DigitToNumber(Digit { " _ ",
"|_|",
"|_|"
}
));
EXPECT_EQ(9, DigitToNumber(Digit { " _ ",
"|_|",
" _|"
}
));
}

TEST(BankOCRTests, ParseDigit_1)
TEST(BankOCRTests, ParseDigits_000000000)
{
Digit digit = {" ",
" |",
" |"};
EXPECT_EQ("1", ParseDigit(digit));
DigitsDisplay nulls = { " _ _ _ _ _ _ _ _ _ ",
"| || || || || || || || || |",
"|_||_||_||_||_||_||_||_||_|"
};
EXPECT_STREQ("000000000", ParseDisplay(nulls).c_str());
}

TEST(BankOCRTests, ParseDigit_2)
TEST(BankOCRTests, ParseDigits_111111111)
{
Digit digit = {" _ ",
" _|",
"|_ "};
EXPECT_EQ("2", ParseDigit(digit));
DigitsDisplay ones = { " ",
" | | | | | | | | |",
" | | | | | | | | |"
};
EXPECT_STREQ("111111111", ParseDisplay(ones).c_str());
}

TEST(BankOCRTests, ParseDigit_8)
TEST(BankOCRTests, ParseDigits_Acceptance)
{
Digit digit = {" _ ",
"|_|",
"|_|"};
EXPECT_EQ("8", ParseDigit(digit));
EXPECT_STREQ("222222222", ParseDisplay({" _ _ _ _ _ _ _ _ _ ",
" _| _| _| _| _| _| _| _| _|",
"|_ |_ |_ |_ |_ |_ |_ |_ |_ "}
).c_str());
EXPECT_STREQ("333333333", ParseDisplay({" _ _ _ _ _ _ _ _ _ ",
" _| _| _| _| _| _| _| _| _|",
" _| _| _| _| _| _| _| _| _|"}
).c_str());
EXPECT_STREQ("444444444", ParseDisplay({" ",
"|_||_||_||_||_||_||_||_||_|",
" | | | | | | | | |"}
).c_str());
EXPECT_STREQ("555555555", ParseDisplay({" _ _ _ _ _ _ _ _ _ ",
"|_ |_ |_ |_ |_ |_ |_ |_ |_ ",
" _| _| _| _| _| _| _| _| _|"}
).c_str());
EXPECT_STREQ("666666666", ParseDisplay({" _ _ _ _ _ _ _ _ _ ",
"|_ |_ |_ |_ |_ |_ |_ |_ |_ ",
"|_||_||_||_||_||_||_||_||_|"}
).c_str());
EXPECT_STREQ("777777777", ParseDisplay({" _ _ _ _ _ _ _ _ _ ",
" | | | | | | | | |",
" | | | | | | | | |"}
).c_str());
EXPECT_STREQ("888888888", ParseDisplay({" _ _ _ _ _ _ _ _ _ ",
"|_||_||_||_||_||_||_||_||_|",
"|_||_||_||_||_||_||_||_||_|"}
).c_str());
EXPECT_STREQ("999999999", ParseDisplay({" _ _ _ _ _ _ _ _ _ ",
"|_||_||_||_||_||_||_||_||_|",
" _| _| _| _| _| _| _| _| _|"}
).c_str());
EXPECT_STREQ("123456789", ParseDisplay({" _ _ _ _ _ _ _ ",
" | _| _||_||_ |_ ||_||_|",
" ||_ _| | _||_| ||_| _|"}
).c_str());
}
16 changes: 8 additions & 8 deletions tdd_intro/homework/homework.pro
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
TEMPLATE = subdirs

SUBDIRS += \
01_bob \
02_leap_year \
03_anagram \
04_trinary_numbers \
05_word_wrapp \
#01_bob \
#02_leap_year \
#03_anagram \
#04_trinary_numbers \
#05_word_wrapp \
05_bank_ocr \
06_allergies \
07_filecopier \
08_timer
#06_allergies \
#07_filecopier \
#08_timer
Loading