Add LCD code

This commit is contained in:
Patricia Aas 2020-11-29 19:11:19 +01:00
parent b718e9e54f
commit 23bc01fa38
8 changed files with 112 additions and 22 deletions

View file

@ -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)

View file

@ -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

View file

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

View file

@ -1,8 +1,12 @@
#ifndef LCD_LCD_HPP
#define LCD_LCD_HPP
#pragma once
class lcd {
#include <string>
#include <vector>
};
typedef std::vector<std::string> lcd_grid;
#endif //LCD_LCD_HPP
lcd_grid lcd(int value);
lcd_grid lcd(std::string s1,
std::string s2,
std::string s3);

View file

@ -1,3 +1,12 @@
int main(int argc, char * argv[]) {
return 0;
#include <iostream>
#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";
}
}

View file

@ -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:)

46
lcd/test/lcd_tests.cpp Normal file
View file

@ -0,0 +1,46 @@
#include <gtest/gtest.h>
#include "lcd.hpp"
#include <iostream>
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();
}

View file

@ -1,10 +0,0 @@
#include <gtest/gtest.h>
TEST(LcdTest, Initial) {
EXPECT_EQ(1, 2);
}
int main(int argc, char* argv[]) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}