From ee359b730241c217a4ea3d0d903530e9ea8ace30 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Fri, 29 Nov 2019 11:39:40 +0100 Subject: [PATCH] + starting with unit tests for pixel area + more Unit tests for color handling --- .clang-tidy | 2 +- src/Color.cpp | 33 +++++++++++++----- src/Color.hpp | 8 ++--- src/vkvm.cpp | 29 ++++++++++------ test/area_test.cpp | 24 +++++++++++++ test/color_test.cpp | 82 +++++++++++++++++++++++++++++--------------- test/public_test.cpp | 10 ------ 7 files changed, 126 insertions(+), 62 deletions(-) create mode 100644 test/area_test.cpp delete mode 100644 test/public_test.cpp diff --git a/.clang-tidy b/.clang-tidy index f404c2e..11c1ab7 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,3 +1,3 @@ -Checks: '*,-llvm-header-guard,-fuchsia*,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-bounds-constant-array-index,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-reinterpret-cast,-hicpp-signed-bitwise,-bugprone-exception-escape,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-cstyle-cast,-hicpp-member-init,-google-readability-namespace-comments,-llvm-namespace-comment,-cppcoreguidelines-pro-type-vararg,-hicpp-vararg,-modernize-use-trailing-return-type' +Checks: '*,-llvm-header-guard,-fuchsia*,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-bounds-constant-array-index,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-reinterpret-cast,-hicpp-signed-bitwise,-bugprone-exception-escape,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-cstyle-cast,-hicpp-member-init,-google-readability-namespace-comments,-llvm-namespace-comment,-cppcoreguidelines-pro-type-vararg,-hicpp-vararg,-modernize-use-trailing-return-type,-readability-magic-numbers,-cppcoreguidelines-avoid-magic-numbers' WarningsAsErrors: 'true' HeaderFilterRegex: '.*,-catch.hpp' \ No newline at end of file diff --git a/src/Color.cpp b/src/Color.cpp index 8f03829..158e888 100644 --- a/src/Color.cpp +++ b/src/Color.cpp @@ -2,16 +2,21 @@ namespace vkvm { + constexpr int maxValue = 255; + Color::Color() noexcept : red(0), green(0), blue(0) {} - Color::Color(unsigned char red, unsigned char green, unsigned char blue) noexcept - : red(red), green(green), blue(blue) {} + Color::Color(int red, int green, int blue) noexcept { + setRed(red); + setGreen(green); + setBlue(blue); + } Color::Color(unsigned int hex) noexcept { - red = (unsigned char) ((hex >> 16 & 0xFF));//NOLINT - green = (unsigned char) ((hex >> 8u & 0xFF));//NOLINT - blue = (unsigned char) ((hex & 0xFF));//NOLINT + red = (unsigned char) ((hex >> 16 & 0xFF)); + green = (unsigned char) ((hex >> 8u & 0xFF)); + blue = (unsigned char) ((hex & 0xFF)); } auto Color::getRed() -> unsigned char { @@ -26,15 +31,27 @@ namespace vkvm { return blue; } - auto Color::setRed(unsigned char value) -> void { + auto Color::setRed(int value) -> void { + if(value > maxValue) { + value = maxValue; + } + red = value; } - auto Color::setGreen(unsigned char value) -> void { + auto Color::setGreen(int value) -> void { + if(value > maxValue) { + value = maxValue; + } + green = value; } - auto Color::setBlue(unsigned char value) -> void { + auto Color::setBlue(int value) -> void { + if(value > maxValue) { + value = maxValue; + } + blue = value; } diff --git a/src/Color.hpp b/src/Color.hpp index 8b3cc99..29c379e 100644 --- a/src/Color.hpp +++ b/src/Color.hpp @@ -18,7 +18,7 @@ namespace vkvm { public: Color() noexcept; - Color(unsigned char red, unsigned char green, unsigned char blue) noexcept; + Color(int red, int green, int blue) noexcept; explicit Color(unsigned int hex) noexcept; @@ -28,11 +28,11 @@ namespace vkvm { auto getBlue() -> unsigned char; - auto setRed(unsigned char value) -> void; + auto setRed(int value) -> void; - auto setGreen(unsigned char value) -> void; + auto setGreen(int value) -> void; - auto setBlue(unsigned char value) -> void; + auto setBlue(int value) -> void; bool operator==(const Color &other) const ; diff --git a/src/vkvm.cpp b/src/vkvm.cpp index df48721..0f6f160 100644 --- a/src/vkvm.cpp +++ b/src/vkvm.cpp @@ -6,11 +6,11 @@ #include namespace vkvm { - // NOLINT + auto initialize(int pid) -> void { impl.sharedMemoryPid = pid; - impl.sharedMemoryKey = 12345;// NOLINT - impl.sharedMemorySize = 8000;// NOLINT + impl.sharedMemoryKey = 12345; + impl.sharedMemorySize = 8000; initSemaphore(); setDefaultValues(); } @@ -20,15 +20,15 @@ namespace vkvm { if(getSharedMemory() != nullptr) { //set default values setMode(GraphicMode::RGB); - setCharactersPerRow(60);// NOLINT - setCharactersPerColumn(20);// NOLINT - setHeight(600);// NOLINT - setWidth(800);// NOLINT - setMousePosition(42, 42);// NOLINT + setCharactersPerRow(60); + setCharactersPerColumn(20); + setHeight(600); + setWidth(800); + setMousePosition(42, 42); setBackgroundColor(black); setForegroundColor(white); - setRedrawInterval(20);// NOLINT - setTimerInterruptInterval(10);// NOLINT + setRedrawInterval(20); + setTimerInterruptInterval(10); setFont(FontType(3,"",0,0)); } impl.localMemoryWarn = true; @@ -64,6 +64,9 @@ namespace vkvm { } auto setPixel(int x, int y, Color color) -> bool { + if(x > getWidth() || y > getHeight()) { + return false; + } lockSharedMemory(); auto reg = getRegisters(); const int bitsPerPixel = 8; @@ -111,10 +114,14 @@ namespace vkvm { } } unlockSharedMemory(); - return false; + return true; } auto getPixel(int x, int y) -> Color { + if(x > getWidth() || y > getHeight()) { + return getBackgroundColor(); + } + Color color = Color(0,0,0); auto reg = getRegisters(); const int bitsPerPixel = 8; diff --git a/test/area_test.cpp b/test/area_test.cpp new file mode 100644 index 0000000..6f5854c --- /dev/null +++ b/test/area_test.cpp @@ -0,0 +1,24 @@ +#include "../src/vkvm.hpp" +#include + +TEST_CASE("AREA") { + vkvm::initialize(0); + vkvm::setMode(vkvm::RGB); + vkvm::setWidth(200); + vkvm::setHeight(200); + + SECTION("inside bounds") { + REQUIRE(vkvm::setPixel(100, 100, vkvm::white)); + REQUIRE(vkvm::getPixel(100, 100) == vkvm::white); + } + + SECTION("Out of bounds") { + + REQUIRE(!vkvm::setPixel(400, 400, vkvm::white)); + REQUIRE(vkvm::getPixel(400, 400) == vkvm::black); + + vkvm::setBackgroundColor(vkvm::blue); + + REQUIRE(vkvm::getPixel(400, 400) == vkvm::blue); + } +} \ No newline at end of file diff --git a/test/color_test.cpp b/test/color_test.cpp index 32154ff..4afffb0 100644 --- a/test/color_test.cpp +++ b/test/color_test.cpp @@ -4,45 +4,71 @@ TEST_CASE("Colors") { vkvm::initialize(0); - vkvm::setPixel(10, 10, vkvm::black);//NOLINT - vkvm::setPixel(11, 11, vkvm::white);//NOLINT - vkvm::setPixel(12, 12, vkvm::green);//NOLINT - vkvm::setPixel(13, 13, vkvm::red);//NOLINT - vkvm::setPixel(14, 14, vkvm::blue);//NOLINT - vkvm::setPixel(15, 15, vkvm::Color(66, 77, 88)); //NOLINT - vkvm::setPixel(16, 16, vkvm::Color(0xFFFFFF));//NOLINT + vkvm::setWidth(400); + vkvm::setHeight(400); + vkvm::setMode(vkvm::RGB); + REQUIRE(vkvm::setPixel(10, 10, vkvm::black)); + REQUIRE(vkvm::setPixel(11, 11, vkvm::white)); + REQUIRE(vkvm::setPixel(12, 12, vkvm::green)); + REQUIRE(vkvm::setPixel(13, 13, vkvm::red)); + REQUIRE(vkvm::setPixel(14, 14, vkvm::blue)); + REQUIRE(vkvm::setPixel(15, 15, vkvm::Color(66, 77, 88))); + REQUIRE(vkvm::setPixel(16, 16, vkvm::Color(0xFFFFFF))); + REQUIRE(vkvm::setPixel(17, 17, vkvm::Color(500, 500, 500))); + REQUIRE(vkvm::setPixel(18, 18, vkvm::Color(0x000000))); SECTION("RGB") { - REQUIRE(vkvm::getPixel(10, 10) == vkvm::black);//NOLINT - REQUIRE(vkvm::getPixel(11, 11) == vkvm::white);//NOLINT - REQUIRE(vkvm::getPixel(12, 12) == vkvm::green);//NOLINT - REQUIRE(vkvm::getPixel(13, 13) == vkvm::red);//NOLINT - REQUIRE(vkvm::getPixel(14, 14) == vkvm::blue);//NOLINT - REQUIRE(vkvm::getPixel(15, 15) == vkvm::Color(66, 77, 88));//NOLINT - REQUIRE(vkvm::getPixel(16, 16) == vkvm::white);//NOLINT + REQUIRE(vkvm::getPixel(10, 10) == vkvm::black); + REQUIRE(vkvm::getPixel(11, 11) == vkvm::white); + REQUIRE(vkvm::getPixel(12, 12) == vkvm::green); + REQUIRE(vkvm::getPixel(13, 13) == vkvm::red); + REQUIRE(vkvm::getPixel(14, 14) == vkvm::blue); + REQUIRE(vkvm::getPixel(15, 15) == vkvm::Color(66, 77, 88)); + REQUIRE(vkvm::getPixel(16, 16) == vkvm::white); + REQUIRE(vkvm::getPixel(17, 17) == vkvm::white); + REQUIRE(vkvm::getPixel(18, 18) == vkvm::black); } SECTION("TwoColors") { vkvm::setMode(vkvm::TwoColors); - REQUIRE(vkvm::getPixel(10, 10) == vkvm::black);//NOLINT - REQUIRE(vkvm::getPixel(11, 11) == vkvm::white);//NOLINT - REQUIRE(vkvm::getPixel(12, 12) == vkvm::black);//NOLINT - REQUIRE(vkvm::getPixel(13, 13) == vkvm::black);//NOLINT - REQUIRE(vkvm::getPixel(14, 14) == vkvm::black);//NOLINT - REQUIRE(vkvm::getPixel(15, 15) == vkvm::black);//NOLINT - REQUIRE(vkvm::getPixel(16, 16) == vkvm::white);//NOLINT + REQUIRE(vkvm::getPixel(10, 10) == vkvm::black); + REQUIRE(vkvm::getPixel(11, 11) == vkvm::white); + REQUIRE(vkvm::getPixel(12, 12) == vkvm::black); + REQUIRE(vkvm::getPixel(13, 13) == vkvm::black); + REQUIRE(vkvm::getPixel(14, 14) == vkvm::black); + REQUIRE(vkvm::getPixel(15, 15) == vkvm::black); + REQUIRE(vkvm::getPixel(16, 16) == vkvm::white); + REQUIRE(vkvm::getPixel(17, 17) == vkvm::white); + REQUIRE(vkvm::getPixel(18, 18) == vkvm::black); + + + //different foreground/background color + vkvm::setBackgroundColor(vkvm::blue); + vkvm::setForegroundColor(vkvm::red); + + REQUIRE(vkvm::getPixel(10, 10) == vkvm::blue); + REQUIRE(vkvm::getPixel(11, 11) == vkvm::red); + REQUIRE(vkvm::getPixel(12, 12) == vkvm::blue); + REQUIRE(vkvm::getPixel(13, 13) == vkvm::blue); + REQUIRE(vkvm::getPixel(14, 14) == vkvm::blue); + REQUIRE(vkvm::getPixel(15, 15) == vkvm::blue); + REQUIRE(vkvm::getPixel(16, 16) == vkvm::red); + REQUIRE(vkvm::getPixel(17, 17) == vkvm::red); + REQUIRE(vkvm::getPixel(18, 18) == vkvm::blue); } SECTION("Gray256") { vkvm::setMode(vkvm::Gray_256); - REQUIRE(vkvm::getPixel(10, 10) == vkvm::black);//NOLINT - REQUIRE(vkvm::getPixel(11, 11) == vkvm::white);//NOLINT - REQUIRE(vkvm::getPixel(12, 12) == vkvm::Color(85, 85, 85));//NOLINT - REQUIRE(vkvm::getPixel(13, 13) == vkvm::Color(85, 85, 85));//NOLINT - REQUIRE(vkvm::getPixel(14, 14) == vkvm::Color(85, 85, 85));//NOLINT - REQUIRE(vkvm::getPixel(15, 15) == vkvm::Color(77, 77, 77));//NOLINT - REQUIRE(vkvm::getPixel(16, 16) == vkvm::white);//NOLINT + REQUIRE(vkvm::getPixel(10, 10) == vkvm::black); + REQUIRE(vkvm::getPixel(11, 11) == vkvm::white); + REQUIRE(vkvm::getPixel(12, 12) == vkvm::Color(85, 85, 85)); + REQUIRE(vkvm::getPixel(13, 13) == vkvm::Color(85, 85, 85)); + REQUIRE(vkvm::getPixel(14, 14) == vkvm::Color(85, 85, 85)); + REQUIRE(vkvm::getPixel(15, 15) == vkvm::Color(77, 77, 77)); + REQUIRE(vkvm::getPixel(16, 16) == vkvm::white); + REQUIRE(vkvm::getPixel(17, 17) == vkvm::white); + REQUIRE(vkvm::getPixel(18, 18) == vkvm::black); } } diff --git a/test/public_test.cpp b/test/public_test.cpp deleted file mode 100644 index 05f9467..0000000 --- a/test/public_test.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "catch2/catch.hpp" -#include "../src/vkvm.hpp" - -TEST_CASE("add works") { - vkvm::initialize(0); - vkvm::setText("Hello World"); - SECTION("equals") { - REQUIRE(vkvm::getText() == "Hello World"); - } -} \ No newline at end of file