diff --git a/lcd/CMakeLists.txt b/lcd/CMakeLists.txt index e0b6d41..a07af72 100644 --- a/lcd/CMakeLists.txt +++ b/lcd/CMakeLists.txt @@ -1,5 +1,7 @@ conan_cmake_run(CONANFILE conanfile.py BASIC_SETUP CMAKE_TARGETS BUILD missing) +include_directories(lib) + add_subdirectory(lib) add_subdirectory(src) add_subdirectory(test) diff --git a/lcd/README.md b/lcd/README.md index 39d1b56..9fd915e 100644 --- a/lcd/README.md +++ b/lcd/README.md @@ -1,2 +1,6 @@ [< Back](../README.md) # Mod(C++) - Lcd Exercise + +* Add tests in [lcd_tests.cpp](test/lcd_tests.cpp) and implementation in +[lcd.cpp](lib/lcd.cpp) to support printing all two digit numbers +* Set the MAX variable in [main.cpp](src/main.cpp) to 99 and print all the numbers diff --git a/lcd/lib/lcd.cpp b/lcd/lib/lcd.cpp index aeb840e..f17caea 100644 --- a/lcd/lib/lcd.cpp +++ b/lcd/lib/lcd.cpp @@ -1,5 +1,40 @@ -// -// Created by patricia on 11/29/2020. -// - #include "lcd.hpp" + +lcd_grid lcd(std::string s1, + std::string s2, + 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_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/lib/lcd.hpp b/lcd/lib/lcd.hpp index 6de216f..6bccce9 100644 --- a/lcd/lib/lcd.hpp +++ b/lcd/lib/lcd.hpp @@ -1,8 +1,12 @@ -#ifndef LCD_LCD_HPP -#define LCD_LCD_HPP +#pragma once -class lcd { +#include +#include -}; +typedef std::vector lcd_grid; -#endif //LCD_LCD_HPP +lcd_grid lcd(int value); + +lcd_grid lcd(std::string s1, + std::string s2, + std::string s3); diff --git a/lcd/src/main.cpp b/lcd/src/main.cpp index 82a68ed..544548b 100644 --- a/lcd/src/main.cpp +++ b/lcd/src/main.cpp @@ -1,3 +1,12 @@ -int main(int argc, char * argv[]) { - return 0; +#include +#include "lcd.hpp" + +static const int MAX = 3; + +int main() { + for (int i = 0; i < MAX; i++) { + auto grid = lcd(i); + for (const auto & line: grid) + std::cout << line << "\n"; + } } diff --git a/lcd/test/CMakeLists.txt b/lcd/test/CMakeLists.txt index cc24420..0f13f9e 100644 --- a/lcd/test/CMakeLists.txt +++ b/lcd/test/CMakeLists.txt @@ -3,7 +3,7 @@ find_package(GTest REQUIRED) include(GoogleTest) -add_executable(lcd_tests tests.cpp) +add_executable(lcd_tests lcd_tests.cpp) target_link_libraries(lcd_tests GTest::GTest liblcd) gtest_discover_tests(lcd_tests TEST_PREFIX lcd:) diff --git a/lcd/test/lcd_tests.cpp b/lcd/test/lcd_tests.cpp new file mode 100644 index 0000000..f173d61 --- /dev/null +++ b/lcd/test/lcd_tests.cpp @@ -0,0 +1,46 @@ +#include + +#include "lcd.hpp" +#include + +std::string to_string(lcd_grid grid) { + std::stringstream output; + for (const auto & str: grid) + output << str; + return output.str(); +} + +void lcd_spec(int value, 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, Twelve) { + lcd_spec(12, lcd( + " _ ", + " | _|", + " | |_ " + )); +} + +int main(int argc, char * argv[]) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/lcd/test/tests.cpp b/lcd/test/tests.cpp deleted file mode 100644 index 21a751a..0000000 --- a/lcd/test/tests.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include - -TEST(LcdTest, Initial) { - EXPECT_EQ(1, 2); -} - -int main(int argc, char* argv[]) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} \ No newline at end of file