+ Tests for Text

+ Tests for Events
+ clearText
This commit is contained in:
Johannes Theiner 2019-12-01 19:56:35 +01:00
parent ee359b7302
commit 1a6b8e0f2b
9 changed files with 122 additions and 53 deletions

View File

@ -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<unsigned char>(hex >> 16 & 0xFF);
green = static_cast<unsigned char>(hex >> 8U & 0xFF);
blue = static_cast<unsigned char>((hex & 0xFF));
}
auto Color::getRed() -> unsigned char {

View File

@ -1,12 +1,11 @@
#ifndef LIBRARY_KEYCODE_HPP
#define LIBRARY_KEYCODE_HPP
#include <sys/types.h>
namespace vkvm {
enum KeyCode {
Backspcce = 32,
Backspace = 32,
Tab = 33,
Enter = 37,
ShiftLeft = 249,

View File

@ -1,25 +1,21 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#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;

View File

@ -61,6 +61,7 @@ constexpr int keyboardBufferSize = 16;
std::vector<std::function<void()>> eventTable;
bool localMemoryWarn = true;
char *sharedMemory = nullptr;
int maxTextLength = 60;
};
extern Impl impl;

View File

@ -78,8 +78,7 @@ namespace vkvm {
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));
}
@ -91,8 +90,7 @@ namespace vkvm {
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));
}
@ -166,12 +164,11 @@ namespace vkvm {
auto setText(std::string text) -> bool {
lockSharedMemory();
char *ptr = getTextArea();
for(int i = 0; i < static_cast<int>(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];
}
}
if (text.size() < getCharactersPerColumn() * getCharactersPerRow()) {
ptr[text.size()] = '\0';
}
@ -183,6 +180,17 @@ namespace vkvm {
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 {
return static_cast<LayoutVersion>(getRegisters()->layout_version);
}

View File

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

View File

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

32
test/event_test.cpp Normal file
View File

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

24
test/text_test.cpp Normal file
View File

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