From 1a6b8e0f2bbb8205655a94bec7a55789403c9f00 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Sun, 1 Dec 2019 19:56:35 +0100 Subject: [PATCH] + Tests for Text + Tests for Events + clearText --- src/Color.cpp | 6 +-- src/KeyCode.hpp | 3 +- src/SharedMemoryAccess.cpp | 11 ++--- src/internal.hpp | 1 + src/vkvm.cpp | 86 +++++++++++++++++++++----------------- src/vkvm.hpp | 7 ++++ test/color_test.cpp | 5 ++- test/event_test.cpp | 32 ++++++++++++++ test/text_test.cpp | 24 +++++++++++ 9 files changed, 122 insertions(+), 53 deletions(-) create mode 100644 test/event_test.cpp create mode 100644 test/text_test.cpp diff --git a/src/Color.cpp b/src/Color.cpp index 158e888..8b9629d 100644 --- a/src/Color.cpp +++ b/src/Color.cpp @@ -14,9 +14,9 @@ namespace vkvm { } Color::Color(unsigned int hex) noexcept { - red = (unsigned char) ((hex >> 16 & 0xFF)); - green = (unsigned char) ((hex >> 8u & 0xFF)); - blue = (unsigned char) ((hex & 0xFF)); + red = static_cast(hex >> 16 & 0xFF); + green = static_cast(hex >> 8U & 0xFF); + blue = static_cast((hex & 0xFF)); } auto Color::getRed() -> unsigned char { diff --git a/src/KeyCode.hpp b/src/KeyCode.hpp index d09dfdb..1a7c357 100644 --- a/src/KeyCode.hpp +++ b/src/KeyCode.hpp @@ -1,12 +1,11 @@ #ifndef LIBRARY_KEYCODE_HPP #define LIBRARY_KEYCODE_HPP -#include namespace vkvm { enum KeyCode { - Backspcce = 32, + Backspace = 32, Tab = 33, Enter = 37, ShiftLeft = 249, diff --git a/src/SharedMemoryAccess.cpp b/src/SharedMemoryAccess.cpp index 57392e3..da6dc26 100644 --- a/src/SharedMemoryAccess.cpp +++ b/src/SharedMemoryAccess.cpp @@ -1,25 +1,21 @@ #include -#include #include -#include #include #include #include #include #include -#include -#include "SharedMemoryAccess.hpp" /* header is important for the shmID. name could be different. maybe not needed cause: (shmget(memory_access_key, NULL, 0)) */ +#include "SharedMemoryAccess.hpp" #include "internal.hpp" #include "log.hpp" namespace vkvm { - constexpr int PERM = 0666; /* access rights */ + constexpr int PERMISSION = 0666; constexpr int LOCK = -1; constexpr int UNLOCK = 1; constexpr int SEM_KEY = 123458L; -//int memoryAccessKey; /* var type is int. but could be another type. */ //TODO: look after type in sharedmemory group int semId; struct sembuf semaphore; @@ -33,7 +29,7 @@ namespace vkvm { /* Alle Zugriffsrechte der Dateikreierungsmaske */ /* erlauben */ umask(0); - semId = semget(SEM_KEY, 1, IPC_CREAT | IPC_EXCL | PERM); + semId = semget(SEM_KEY, 1, IPC_CREAT | IPC_EXCL | PERMISSION); if (semId < 0) { return -1; } @@ -50,6 +46,7 @@ namespace vkvm { semaphore.sem_flg = SEM_UNDO; if (semop(semId, &semaphore, 1) == -1) { perror(" semop "); + log(LogLevel::CRITICAL, "could not operate on semaphore, exiting"); return -1; } return 1; diff --git a/src/internal.hpp b/src/internal.hpp index e1b5ea2..bbbe03b 100644 --- a/src/internal.hpp +++ b/src/internal.hpp @@ -61,6 +61,7 @@ constexpr int keyboardBufferSize = 16; std::vector> eventTable; bool localMemoryWarn = true; char *sharedMemory = nullptr; + int maxTextLength = 60; }; extern Impl impl; diff --git a/src/vkvm.cpp b/src/vkvm.cpp index 0f6f160..df7927b 100644 --- a/src/vkvm.cpp +++ b/src/vkvm.cpp @@ -17,7 +17,7 @@ namespace vkvm { auto setDefaultValues() -> void { impl.localMemoryWarn = false; - if(getSharedMemory() != nullptr) { + if (getSharedMemory() != nullptr) { //set default values setMode(GraphicMode::RGB); setCharactersPerRow(60); @@ -29,7 +29,7 @@ namespace vkvm { setForegroundColor(white); setRedrawInterval(20); setTimerInterruptInterval(10); - setFont(FontType(3,"",0,0)); + setFont(FontType(3, "", 0, 0)); } impl.localMemoryWarn = true; } @@ -40,17 +40,17 @@ namespace vkvm { lockSharedMemory(); - for(int i = 0; i < impl.interruptEntrysPerEventType;i++){ - auto &entry= ivt[type * impl.interruptEntrysPerEventType + i]; + for (int i = 0; i < impl.interruptEntrysPerEventType; i++) { + auto &entry = ivt[type * impl.interruptEntrysPerEventType + i]; if (entry.pid == 0) { entry.pid = getpid(); entry.signum = signum; impl.eventTable.push_back(handler); - onSignal(signum, [](int sig){ - if(sig >= SIGUSR1){ - if((sig - SIGUSR1) < impl.eventTable.size()){ + onSignal(signum, [](int sig) { + if (sig >= SIGUSR1) { + if ((sig - SIGUSR1) < impl.eventTable.size()) { impl.eventTable[sig - SIGUSR1](); } } @@ -64,22 +64,21 @@ namespace vkvm { } auto setPixel(int x, int y, Color color) -> bool { - if(x > getWidth() || y > getHeight()) { + if (x > getWidth() || y > getHeight()) { return false; } lockSharedMemory(); auto reg = getRegisters(); const int bitsPerPixel = 8; - switch(reg->graphicMode) { + switch (reg->graphicMode) { case Text: { int pixelIndex = (y * getWidth() + x); unsigned char *ptr = reinterpret_cast(getPixelArea()) + (pixelIndex / bitsPerPixel); - if(color == reg->foreground_color){ + if (color == reg->foreground_color) { //set bit to 1 ptr[0] |= (1 << (pixelIndex % bitsPerPixel)); - } - else{ + } else { //set bit to 0 ptr[0] &= ~(1 << (pixelIndex % bitsPerPixel)); } @@ -88,11 +87,10 @@ namespace vkvm { case TwoColors: { int pixelIndex = (y * getWidth() + x); unsigned char *ptr = reinterpret_cast(getPixelArea()) + (pixelIndex / bitsPerPixel); - if(color == reg->foreground_color){ + if (color == reg->foreground_color) { //set bit to 1 ptr[0] |= (1 << (pixelIndex % bitsPerPixel)); - } - else{ + } else { //set bit to 0 ptr[0] &= ~(1 << (pixelIndex % bitsPerPixel)); } @@ -118,23 +116,23 @@ namespace vkvm { } auto getPixel(int x, int y) -> Color { - if(x > getWidth() || y > getHeight()) { + if (x > getWidth() || y > getHeight()) { return getBackgroundColor(); } - Color color = Color(0,0,0); + Color color = Color(0, 0, 0); auto reg = getRegisters(); const int bitsPerPixel = 8; - switch(reg->graphicMode) { + switch (reg->graphicMode) { case Text: { int pixelIndex = (y * getWidth() + x); unsigned char *ptr = reinterpret_cast(getPixelArea()) + pixelIndex / bitsPerPixel; bool bit = static_cast(ptr[0] & (1 << (pixelIndex % bitsPerPixel))); - if(bit){ + if (bit) { color = reg->foreground_color; - }else{ - color =reg->background_color; + } else { + color = reg->background_color; } break; } @@ -142,10 +140,10 @@ namespace vkvm { int pixelIndex = (y * getWidth() + x); unsigned char *ptr = reinterpret_cast(getPixelArea()) + (pixelIndex / bitsPerPixel); bool bit = static_cast(ptr[0] & (1 << (pixelIndex % bitsPerPixel))); - if(bit){ + if (bit) { color = reg->foreground_color; - }else{ - color =reg->background_color; + } else { + color = reg->background_color; } break; } @@ -166,13 +164,12 @@ namespace vkvm { auto setText(std::string text) -> bool { lockSharedMemory(); char *ptr = getTextArea(); - for(int i = 0; i < static_cast(text.size());i++){ - if(i >= getCharactersPerColumn() * getCharactersPerRow()){ - break; + for (int i = 0; i < text.size(); i++) { + if (i < getCharactersPerColumn() * getCharactersPerRow() && i < impl.maxTextLength) { + ptr[i] = text[i]; } - ptr[i] = text[i]; } - if(text.size() < getCharactersPerColumn() * getCharactersPerRow()){ + if (text.size() < getCharactersPerColumn() * getCharactersPerRow()) { ptr[text.size()] = '\0'; } unlockSharedMemory(); @@ -180,7 +177,18 @@ namespace vkvm { } auto getText() -> std::string { - return std::string (getTextArea()); + return std::string(getTextArea()); + } + + auto clearText() -> bool { + lockSharedMemory(); + char *ptr = getTextArea(); + for (int i = 0; i < impl.maxTextLength; ++i) { + ptr[i] = '\0'; + } + + unlockSharedMemory(); + return true; } auto getLayoutVersion() -> LayoutVersion { @@ -218,29 +226,29 @@ namespace vkvm { auto setMode(GraphicMode newValue) -> void { lockSharedMemory(); auto reg = getRegisters(); - if(reg->graphicMode != newValue){ + if (reg->graphicMode != newValue) { std::vector pixels; int height = reg->height_pixels; int width = reg->width_pixels; pixels.resize(height * width); - for(int y = 0; y < height;y++){ - for(int x = 0;x < width;x++){ - pixels[y * width + x] = getPixel(x,y); + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + pixels[y * width + x] = getPixel(x, y); } } getRegisters()->graphicMode = newValue; unlockSharedMemory(); - for(int y = 0; y < height;y++){ - for(int x = 0;x < width;x++){ - setPixel(x,y, pixels[y * width + x]); + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + setPixel(x, y, pixels[y * width + x]); } } lockSharedMemory(); - }else{ + } else { reg->graphicMode = newValue; } unlockSharedMemory(); @@ -314,7 +322,7 @@ namespace vkvm { lockSharedMemory(); auto keyCode = KeyCode(0); auto reg = getRegisters(); - if(reg->keyboardBuffer_index_read != reg->keyboardBuffer_index_write) { + if (reg->keyboardBuffer_index_read != reg->keyboardBuffer_index_write) { keyCode = static_cast(reg->keyboardBuffer[reg->keyboardBuffer_index_read++]); if (reg->keyboardBuffer_index_read >= sizeof(reg->keyboardBuffer)) { reg->keyboardBuffer_index_read = 0; diff --git a/src/vkvm.hpp b/src/vkvm.hpp index ed70601..b882206 100644 --- a/src/vkvm.hpp +++ b/src/vkvm.hpp @@ -67,6 +67,13 @@ namespace vkvm { */ auto getText() -> std::string; + /** + * clear text area + * @return if text could be cleared, false if it could not be cleared. + */ + auto clearText() -> bool; + + //Control registers start here //all modes diff --git a/test/color_test.cpp b/test/color_test.cpp index 4afffb0..92fd426 100644 --- a/test/color_test.cpp +++ b/test/color_test.cpp @@ -4,8 +4,6 @@ TEST_CASE("Colors") { vkvm::initialize(0); - vkvm::setWidth(400); - vkvm::setHeight(400); vkvm::setMode(vkvm::RGB); REQUIRE(vkvm::setPixel(10, 10, vkvm::black)); REQUIRE(vkvm::setPixel(11, 11, vkvm::white)); @@ -46,6 +44,9 @@ TEST_CASE("Colors") { vkvm::setBackgroundColor(vkvm::blue); vkvm::setForegroundColor(vkvm::red); + REQUIRE(vkvm::getBackgroundColor() == vkvm::blue); + REQUIRE(vkvm::getForegroundColor() == vkvm::red); + REQUIRE(vkvm::getPixel(10, 10) == vkvm::blue); REQUIRE(vkvm::getPixel(11, 11) == vkvm::red); REQUIRE(vkvm::getPixel(12, 12) == vkvm::blue); diff --git a/test/event_test.cpp b/test/event_test.cpp new file mode 100644 index 0000000..eeb9c78 --- /dev/null +++ b/test/event_test.cpp @@ -0,0 +1,32 @@ +#include "../src/internal.hpp" +#include "../src/vkvm.hpp" +#include + +TEST_CASE("Event") { + vkvm::initialize(0); + + SECTION("Register") { + bool mouseMove = false; + bool timer = false; + bool timer2 = false; + vkvm::registerEvent(vkvm::MouseMove, [&]() { + mouseMove = true; + }); + vkvm::registerEvent(vkvm::Timer, [&]() { + timer = true; + }); + vkvm::registerEvent(vkvm::Timer, [&]() { + timer2 = true; + }); + + REQUIRE_FALSE(mouseMove); + vkvm::callEvent(vkvm::MouseMove); + REQUIRE(mouseMove); + + REQUIRE_FALSE(timer); + REQUIRE_FALSE(timer2); + vkvm::callEvent(vkvm::Timer); + REQUIRE(timer); + REQUIRE(timer2); + } +} diff --git a/test/text_test.cpp b/test/text_test.cpp new file mode 100644 index 0000000..f42b26b --- /dev/null +++ b/test/text_test.cpp @@ -0,0 +1,24 @@ +#include "../src/internal.hpp" +#include "../src/vkvm.hpp" +#include + +TEST_CASE("Text") { + vkvm::initialize(0); + + REQUIRE(vkvm::setText("Hello World, this is a test")); + REQUIRE(vkvm::getText() == "Hello World, this is a test"); + + REQUIRE(vkvm::setText("Hello World")); + REQUIRE(vkvm::getText() == "Hello World"); + + vkvm::setCharactersPerColumn(5); + vkvm::setCharactersPerRow(1); + + REQUIRE(vkvm::clearText()); + + REQUIRE(vkvm::getCharactersPerColumn() == 5); + REQUIRE(vkvm::getCharactersPerRow() == 1); + + REQUIRE(vkvm::setText("Hello World")); + REQUIRE(vkvm::getText() == "Hello"); +} \ No newline at end of file