+ starting with unit tests for pixel area

+ more Unit tests for color handling
This commit is contained in:
Johannes Theiner 2019-11-29 11:39:40 +01:00
parent ab2e1daea2
commit ee359b7302
7 changed files with 126 additions and 62 deletions

View File

@ -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' WarningsAsErrors: 'true'
HeaderFilterRegex: '.*,-catch.hpp' HeaderFilterRegex: '.*,-catch.hpp'

View File

@ -2,16 +2,21 @@
namespace vkvm { namespace vkvm {
constexpr int maxValue = 255;
Color::Color() noexcept Color::Color() noexcept
: red(0), green(0), blue(0) {} : red(0), green(0), blue(0) {}
Color::Color(unsigned char red, unsigned char green, unsigned char blue) noexcept Color::Color(int red, int green, int blue) noexcept {
: red(red), green(green), blue(blue) {} setRed(red);
setGreen(green);
setBlue(blue);
}
Color::Color(unsigned int hex) noexcept { Color::Color(unsigned int hex) noexcept {
red = (unsigned char) ((hex >> 16 & 0xFF));//NOLINT red = (unsigned char) ((hex >> 16 & 0xFF));
green = (unsigned char) ((hex >> 8u & 0xFF));//NOLINT green = (unsigned char) ((hex >> 8u & 0xFF));
blue = (unsigned char) ((hex & 0xFF));//NOLINT blue = (unsigned char) ((hex & 0xFF));
} }
auto Color::getRed() -> unsigned char { auto Color::getRed() -> unsigned char {
@ -26,15 +31,27 @@ namespace vkvm {
return blue; return blue;
} }
auto Color::setRed(unsigned char value) -> void { auto Color::setRed(int value) -> void {
if(value > maxValue) {
value = maxValue;
}
red = value; red = value;
} }
auto Color::setGreen(unsigned char value) -> void { auto Color::setGreen(int value) -> void {
if(value > maxValue) {
value = maxValue;
}
green = value; green = value;
} }
auto Color::setBlue(unsigned char value) -> void { auto Color::setBlue(int value) -> void {
if(value > maxValue) {
value = maxValue;
}
blue = value; blue = value;
} }

View File

@ -18,7 +18,7 @@ namespace vkvm {
public: public:
Color() noexcept; 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; explicit Color(unsigned int hex) noexcept;
@ -28,11 +28,11 @@ namespace vkvm {
auto getBlue() -> unsigned char; 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 ; bool operator==(const Color &other) const ;

View File

@ -6,11 +6,11 @@
#include <unistd.h> #include <unistd.h>
namespace vkvm { namespace vkvm {
// NOLINT
auto initialize(int pid) -> void { auto initialize(int pid) -> void {
impl.sharedMemoryPid = pid; impl.sharedMemoryPid = pid;
impl.sharedMemoryKey = 12345;// NOLINT impl.sharedMemoryKey = 12345;
impl.sharedMemorySize = 8000;// NOLINT impl.sharedMemorySize = 8000;
initSemaphore(); initSemaphore();
setDefaultValues(); setDefaultValues();
} }
@ -20,15 +20,15 @@ namespace vkvm {
if(getSharedMemory() != nullptr) { if(getSharedMemory() != nullptr) {
//set default values //set default values
setMode(GraphicMode::RGB); setMode(GraphicMode::RGB);
setCharactersPerRow(60);// NOLINT setCharactersPerRow(60);
setCharactersPerColumn(20);// NOLINT setCharactersPerColumn(20);
setHeight(600);// NOLINT setHeight(600);
setWidth(800);// NOLINT setWidth(800);
setMousePosition(42, 42);// NOLINT setMousePosition(42, 42);
setBackgroundColor(black); setBackgroundColor(black);
setForegroundColor(white); setForegroundColor(white);
setRedrawInterval(20);// NOLINT setRedrawInterval(20);
setTimerInterruptInterval(10);// NOLINT setTimerInterruptInterval(10);
setFont(FontType(3,"",0,0)); setFont(FontType(3,"",0,0));
} }
impl.localMemoryWarn = true; impl.localMemoryWarn = true;
@ -64,6 +64,9 @@ namespace vkvm {
} }
auto setPixel(int x, int y, Color color) -> bool { auto setPixel(int x, int y, Color color) -> bool {
if(x > getWidth() || y > getHeight()) {
return false;
}
lockSharedMemory(); lockSharedMemory();
auto reg = getRegisters(); auto reg = getRegisters();
const int bitsPerPixel = 8; const int bitsPerPixel = 8;
@ -111,10 +114,14 @@ namespace vkvm {
} }
} }
unlockSharedMemory(); unlockSharedMemory();
return false; return true;
} }
auto getPixel(int x, int y) -> Color { auto getPixel(int x, int y) -> Color {
if(x > getWidth() || y > getHeight()) {
return getBackgroundColor();
}
Color color = Color(0,0,0); Color color = Color(0,0,0);
auto reg = getRegisters(); auto reg = getRegisters();
const int bitsPerPixel = 8; const int bitsPerPixel = 8;

24
test/area_test.cpp Normal file
View File

@ -0,0 +1,24 @@
#include "../src/vkvm.hpp"
#include <catch2/catch.hpp>
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);
}
}

View File

@ -4,45 +4,71 @@
TEST_CASE("Colors") { TEST_CASE("Colors") {
vkvm::initialize(0); vkvm::initialize(0);
vkvm::setPixel(10, 10, vkvm::black);//NOLINT vkvm::setWidth(400);
vkvm::setPixel(11, 11, vkvm::white);//NOLINT vkvm::setHeight(400);
vkvm::setPixel(12, 12, vkvm::green);//NOLINT vkvm::setMode(vkvm::RGB);
vkvm::setPixel(13, 13, vkvm::red);//NOLINT REQUIRE(vkvm::setPixel(10, 10, vkvm::black));
vkvm::setPixel(14, 14, vkvm::blue);//NOLINT REQUIRE(vkvm::setPixel(11, 11, vkvm::white));
vkvm::setPixel(15, 15, vkvm::Color(66, 77, 88)); //NOLINT REQUIRE(vkvm::setPixel(12, 12, vkvm::green));
vkvm::setPixel(16, 16, vkvm::Color(0xFFFFFF));//NOLINT 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") { SECTION("RGB") {
REQUIRE(vkvm::getPixel(10, 10) == vkvm::black);//NOLINT REQUIRE(vkvm::getPixel(10, 10) == vkvm::black);
REQUIRE(vkvm::getPixel(11, 11) == vkvm::white);//NOLINT REQUIRE(vkvm::getPixel(11, 11) == vkvm::white);
REQUIRE(vkvm::getPixel(12, 12) == vkvm::green);//NOLINT REQUIRE(vkvm::getPixel(12, 12) == vkvm::green);
REQUIRE(vkvm::getPixel(13, 13) == vkvm::red);//NOLINT REQUIRE(vkvm::getPixel(13, 13) == vkvm::red);
REQUIRE(vkvm::getPixel(14, 14) == vkvm::blue);//NOLINT REQUIRE(vkvm::getPixel(14, 14) == vkvm::blue);
REQUIRE(vkvm::getPixel(15, 15) == vkvm::Color(66, 77, 88));//NOLINT REQUIRE(vkvm::getPixel(15, 15) == vkvm::Color(66, 77, 88));
REQUIRE(vkvm::getPixel(16, 16) == vkvm::white);//NOLINT REQUIRE(vkvm::getPixel(16, 16) == vkvm::white);
REQUIRE(vkvm::getPixel(17, 17) == vkvm::white);
REQUIRE(vkvm::getPixel(18, 18) == vkvm::black);
} }
SECTION("TwoColors") { SECTION("TwoColors") {
vkvm::setMode(vkvm::TwoColors); vkvm::setMode(vkvm::TwoColors);
REQUIRE(vkvm::getPixel(10, 10) == vkvm::black);//NOLINT REQUIRE(vkvm::getPixel(10, 10) == vkvm::black);
REQUIRE(vkvm::getPixel(11, 11) == vkvm::white);//NOLINT REQUIRE(vkvm::getPixel(11, 11) == vkvm::white);
REQUIRE(vkvm::getPixel(12, 12) == vkvm::black);//NOLINT REQUIRE(vkvm::getPixel(12, 12) == vkvm::black);
REQUIRE(vkvm::getPixel(13, 13) == vkvm::black);//NOLINT REQUIRE(vkvm::getPixel(13, 13) == vkvm::black);
REQUIRE(vkvm::getPixel(14, 14) == vkvm::black);//NOLINT REQUIRE(vkvm::getPixel(14, 14) == vkvm::black);
REQUIRE(vkvm::getPixel(15, 15) == vkvm::black);//NOLINT REQUIRE(vkvm::getPixel(15, 15) == vkvm::black);
REQUIRE(vkvm::getPixel(16, 16) == vkvm::white);//NOLINT 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") { SECTION("Gray256") {
vkvm::setMode(vkvm::Gray_256); vkvm::setMode(vkvm::Gray_256);
REQUIRE(vkvm::getPixel(10, 10) == vkvm::black);//NOLINT REQUIRE(vkvm::getPixel(10, 10) == vkvm::black);
REQUIRE(vkvm::getPixel(11, 11) == vkvm::white);//NOLINT REQUIRE(vkvm::getPixel(11, 11) == vkvm::white);
REQUIRE(vkvm::getPixel(12, 12) == vkvm::Color(85, 85, 85));//NOLINT REQUIRE(vkvm::getPixel(12, 12) == vkvm::Color(85, 85, 85));
REQUIRE(vkvm::getPixel(13, 13) == vkvm::Color(85, 85, 85));//NOLINT REQUIRE(vkvm::getPixel(13, 13) == vkvm::Color(85, 85, 85));
REQUIRE(vkvm::getPixel(14, 14) == vkvm::Color(85, 85, 85));//NOLINT REQUIRE(vkvm::getPixel(14, 14) == vkvm::Color(85, 85, 85));
REQUIRE(vkvm::getPixel(15, 15) == vkvm::Color(77, 77, 77));//NOLINT REQUIRE(vkvm::getPixel(15, 15) == vkvm::Color(77, 77, 77));
REQUIRE(vkvm::getPixel(16, 16) == vkvm::white);//NOLINT REQUIRE(vkvm::getPixel(16, 16) == vkvm::white);
REQUIRE(vkvm::getPixel(17, 17) == vkvm::white);
REQUIRE(vkvm::getPixel(18, 18) == vkvm::black);
} }
} }

View File

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