1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- #include <stdio.h>
- #include <threads.h>
- #include "../Tests.h"
- #include "../src/ErrorSimulator.h"
- #include "core/ReadLine.h"
- static void sleepMillis(int millis) {
- struct timespec t = {.tv_nsec = millis * 1000000};
- thrd_sleep(&t, nullptr);
- }
- static void testStringError(int line, const char* s) {
- char buffer[256];
- for(int i = 0; i < 200; i++) {
- if(readLine(buffer, sizeof(buffer))) {
- break;
- }
- sleepMillis(10);
- }
- if(!testString(__FILE__, line, s, buffer)) {
- const char* p = buffer;
- printf("Invalid sequence: ");
- while(*p != 0) {
- printf("%u ", (unsigned int)*p & 0xFF);
- p++;
- }
- putchar('\n');
- p = s;
- printf("Correct sequence: ");
- while(*p != 0) {
- printf("%u ", (unsigned int)*p & 0xFF);
- p++;
- }
- putchar('\n');
- }
- }
- void testReadLine(void) {
- #ifdef ERROR_SIMULATOR
- failMutexInit = true;
- if(!TEST_TRUE(startReadLine())) {
- return;
- }
- failMutexInit = false;
- failThreadInit = true;
- if(!TEST_TRUE(startReadLine())) {
- return;
- }
- failThreadInit = false;
- failMutexLock = true;
- failMutexUnlock = true;
- #endif
- if(!TEST_FALSE(startReadLine())) {
- return;
- }
- testStringError(__LINE__, "wusi");
- #ifdef ERROR_SIMULATOR
- failMutexLock = false;
- failMutexUnlock = false;
- #endif
- testStringError(__LINE__, "gusi");
- testStringError(__LINE__, "abc");
- testStringError(__LINE__, "abc");
- testStringError(__LINE__, "abd");
- testStringError(__LINE__, "abö");
- testStringError(__LINE__, "ghi");
- testStringError(__LINE__, "abcghi");
- testStringError(__LINE__, "abcghi");
- testStringError(__LINE__, "abö");
- testStringError(__LINE__, "abac");
- testStringError(
- __LINE__,
- "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
- "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
- "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
- "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbb");
- testStringError(__LINE__, "abäo");
- testStringError(__LINE__, "bäöo");
- #ifdef ERROR_SIMULATOR
- failThreadJoin = true;
- stopReadLine();
- failThreadJoin = false;
- #endif
- stopReadLine();
- }
|