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

View File

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

View File

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

View File

@ -6,11 +6,11 @@
#include <unistd.h>
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;

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

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