essence-os/desktop/api_tests.cpp

142 lines
3.3 KiB
C++

#ifdef API_TESTS_FOR_RUNNER
#define TEST(_callback) { .cName = #_callback }
typedef struct Test { const char *cName; } Test;
#else
#define ES_PRIVATE_APIS
#include <essence.h>
#include <shared/crc.h>
#define TEST(_callback) { .callback = _callback }
struct Test { bool (*callback)(); };
//////////////////////////////////////////////////////////////
bool BasicFileOperationsDoByteCount(size_t byteCount) {
uint8_t *buffer = (uint8_t *) EsHeapAllocate(byteCount, false);
for (uintptr_t i = 0; i < byteCount; i++) {
buffer[i] = EsRandomU8();
}
EsError error = EsFileWriteAll(EsLiteral("|Settings:/temp.dat"), buffer, byteCount);
if (error != ES_SUCCESS) {
EsPrint("Error %d writing file of size %d.\n", error, byteCount);
return false;
}
size_t readSize;
uint8_t *read = (uint8_t *) EsFileReadAll(EsLiteral("|Settings:/temp.dat"), &readSize, &error);
if (error != ES_SUCCESS) {
EsPrint("Error %d reading file of size %d.\n", error, byteCount);
return false;
}
if (readSize != byteCount) {
EsPrint("Read size mismatch: got %d, expected %d.\n", readSize, byteCount);
return false;
}
if (EsMemoryCompare(buffer, read, byteCount)) {
EsPrint("Read data mismatch.\n");
return false;
}
EsHeapFree(buffer);
EsHeapFree(read);
return true;
}
bool BasicFileOperations() {
for (uintptr_t i = 0; i < 24; i += 2) {
if (!BasicFileOperationsDoByteCount(1 << i)) {
return false;
}
}
for (uintptr_t i = 18; i > 0; i -= 3) {
if (!BasicFileOperationsDoByteCount(1 << i)) {
return false;
}
}
EsError error = EsPathDelete(EsLiteral("|Settings:/temp.dat"));
if (error != ES_SUCCESS) {
EsPrint("Error %d deleting file.\n", error);
return false;
}
EsFileReadAll(EsLiteral("|Settings:/temp.dat"), nullptr, &error);
if (error != ES_ERROR_FILE_DOES_NOT_EXIST) {
EsPrint("Checking file does not exist after deleting, instead got error %d.\n", error);
return false;
}
return true;
}
//////////////////////////////////////////////////////////////
#endif
const Test tests[] = {
TEST(BasicFileOperations),
};
#ifndef API_TESTS_FOR_RUNNER
void RunTests() {
size_t fileSize;
EsError error;
void *fileData = EsFileReadAll(EsLiteral("|Settings:/test.dat"), &fileSize, &error);
if (error == ES_ERROR_FILE_DOES_NOT_EXIST) {
return; // Not in test mode.
} else if (error != ES_SUCCESS) {
EsPrint("Could not read test.dat (error %d).\n", error);
} else if (fileSize != sizeof(uint32_t)) {
EsPrint("test.dat is the wrong size (got %d, expected %d).\n", fileSize, sizeof(uint32_t));
} else {
uint32_t index;
EsMemoryCopy(&index, fileData, sizeof(uint32_t));
EsHeapFree(fileData);
if (index >= sizeof(tests) / sizeof(tests[0])) {
EsPrint("Test index out of bounds.\n");
} else if (tests[index].callback()) {
EsPrint("[APITests-Success]\n");
} else {
EsPrint("[APITests-Failure]\n");
}
}
EsSyscall(ES_SYSCALL_SHUTDOWN, SHUTDOWN_ACTION_POWER_OFF, 0, 0, 0);
EsProcessTerminateCurrent();
}
void _start() {
_init();
while (true) {
EsMessage *message = EsMessageReceive();
if (message->type == ES_MSG_INSTANCE_CREATE) {
EsInstance *instance = EsInstanceCreate(message, EsLiteral("API Tests"));
EsApplicationStartupRequest request = EsInstanceGetStartupRequest(instance);
if (request.flags & ES_APPLICATION_STARTUP_BACKGROUND_SERVICE) {
RunTests();
}
}
}
}
#endif