From 11d5bda4a56e3d48ac7888fb70c879c3d56a719c Mon Sep 17 00:00:00 2001 From: Patricia Aas Date: Mon, 30 Nov 2020 17:06:49 +0100 Subject: [PATCH] Add a solution for lcd --- lcd/solution/lcd.cpp | 68 +++++++++++++++++++++++ lcd/solution/lcd_tests.cpp | 110 +++++++++++++++++++++++++++++++++++++ lcd/solution/main.cpp | 12 ++++ 3 files changed, 190 insertions(+) create mode 100644 lcd/solution/lcd.cpp create mode 100644 lcd/solution/lcd_tests.cpp create mode 100644 lcd/solution/main.cpp diff --git a/lcd/solution/lcd.cpp b/lcd/solution/lcd.cpp new file mode 100644 index 0000000..9dc79a0 --- /dev/null +++ b/lcd/solution/lcd.cpp @@ -0,0 +1,68 @@ +#include "lcd.hpp" + +lcd_grid lcd(const std::string & s1, + const std::string & s2, + const std::string & s3) { + lcd_grid result; + result.push_back(s1); + result.push_back(s2); + result.push_back(s3); + return result; +} + +const lcd_grid digits[] = + { + lcd(" _ ", + "| |", + "|_|" + ), + lcd(" ", + " |", + " |" + ), + lcd(" _ ", + " _|", + "|_ " + ), + lcd(" _ ", + " _|", + " _|" + ), + lcd(" ", + "|_|", + " |" + ), + lcd(" _ ", + "|_ ", + " _|" + ), + lcd(" _ ", + "|_ ", + "|_|" + ), + lcd(" _ ", + " |", + " |" + ), + lcd(" _ ", + "|_|", + "|_|" + ), + lcd(" _ ", + "|_|", + " |" + ), + }; + +lcd_grid lcd(int value) { + if (value < 10) + return digits[value]; + else { + lcd_grid lhs = lcd(value / 10); + lcd_grid rhs = digits[value % 10]; + return lcd( + lhs[0] + ' ' + rhs[0], + lhs[1] + ' ' + rhs[1], + lhs[2] + ' ' + rhs[2]); + } +} diff --git a/lcd/solution/lcd_tests.cpp b/lcd/solution/lcd_tests.cpp new file mode 100644 index 0000000..e1e55e9 --- /dev/null +++ b/lcd/solution/lcd_tests.cpp @@ -0,0 +1,110 @@ +#include + +#include "lcd.hpp" +#include + +std::string to_string(const lcd_grid & grid) { + std::stringstream output; + for (const auto & str: grid) + output << str; + return output.str(); +} + +void lcd_spec(int value, const lcd_grid & grid) { + std::string expected = to_string(grid), + actual = to_string(lcd(value)); + if (expected != actual) { + std::cerr + << "lcd(" << value << ")\n" + << "expected==\n" + << expected << '\n' + << "actual==\n" + << actual << std::endl; + std::exit(EXIT_FAILURE); + } +} + +TEST(LcdTest, Zero) { + lcd_spec(0, lcd( + " _ ", + "| |", + "|_|" + )); +} + +TEST(LcdTest, One) { + lcd_spec(1, lcd( + " ", + " |", + " |" + )); +} + +TEST(LcdTest, Two) { + lcd_spec(2, lcd( + " _ ", + " _|", + "|_ " + )); +} + +TEST(LcdTest, Three) { + lcd_spec(3, lcd( + " _ ", + " _|", + " _|" + )); +} + +TEST(LcdTest, Four) { + lcd_spec(4, lcd( + " ", + "|_|", + " |" + )); +} + +TEST(LcdTest, Twelve) { + lcd_spec(12, lcd( + " _ ", + " | _|", + " | |_ " + )); +} + +TEST(LcdTest, TwentyFive) { + lcd_spec(25, lcd( + " _ _ ", + " _| |_ ", + "|_ _|" + )); +} + +TEST(LcdTest, SixtyFour) { + lcd_spec(64, lcd( + " _ ", + "|_ |_|", + "|_| |" + )); +} + +TEST(LcdTest, SeventyFour) { + lcd_spec(74, lcd( + " _ ", + " | |_|", + " | |" + )); +} + +TEST(LcdTest, EightyNine) { + lcd_spec(89, lcd( + " _ _ ", + "|_| |_|", + "|_| |" + )); +} + +int main(int argc, char * argv[]) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/lcd/solution/main.cpp b/lcd/solution/main.cpp new file mode 100644 index 0000000..9955995 --- /dev/null +++ b/lcd/solution/main.cpp @@ -0,0 +1,12 @@ +#include +#include "lcd.hpp" + +static const int MAX = 100; + +int main() { + for (int i = 0; i < MAX; i++) { + auto grid = lcd(i); + for (const auto & line: grid) + std::cout << line << "\n"; + } +}