Compare commits

...
Sign in to create a new pull request.

22 commits

Author SHA1 Message Date
Mineplay5780
cf4755145b refactor(file): changed way read and write works and temporary removed some tests 2025-12-17 23:45:55 +01:00
Mineplay5780
42cc01d79f fix(tests): added missing test for opening file 2025-12-17 23:00:42 +01:00
Mineplay5780
f3823efb63 fix(test): added closing of file for size tests 2025-12-17 22:58:02 +01:00
Mineplay5780
63b48db601 feat(test): added some tests for opening, closing and getting size of file 2025-12-17 21:16:00 +01:00
Mineplay5780
79e9bf2a8c feat(file): implemented getting file size 2025-12-17 20:40:01 +01:00
Mineplay5780
84b4f0dc59 refactor(file): removed boolean override for mode in write function 2025-12-17 20:36:35 +01:00
Mineplay5780
5498b060b8 feat(file): added manually opening and closing files 2025-12-17 20:31:33 +01:00
Mineplay5780
7b44871cb2 refactor(tests): removed success and changed it for error value 2025-12-17 18:53:20 +01:00
Mineplay5780
69439c6178 feat(test): implemented new method of making tests 2025-12-14 17:58:09 +01:00
64535d0288 fix(file): fixed tests and large file reading on linux 2025-12-13 18:36:48 +01:00
Mineplay5780
77f1518abc feat(file): added delete file function 2025-12-11 22:38:30 +01:00
Mineplay5780
3f0299bef1 feat(tests): added assert function for tests 2025-12-11 00:11:23 +01:00
Mineplay5780
febf56912e refactor(file): added missing core prefix to file error enum 2025-12-10 20:43:16 +01:00
Mineplay5780
e276388f7b feat(file): implemented write file tests 2025-12-10 20:39:31 +01:00
Mineplay5780
ae9b1ed2b9 feat(test): improved testing system and implemented read file tests 2025-12-09 22:07:16 +01:00
Mineplay5780
87fb7556c3 feat(file): made function to convert error codes to string 2025-12-08 23:32:43 +01:00
Mineplay5780
6a5a80b3f5 fix(file): corrected typing for linux implementation of read and write file 2025-12-08 23:18:35 +01:00
Mineplay5780
d44d460cad feat(file): implemented file reading and writting for windows 2025-12-08 23:04:51 +01:00
66ab9fbbdf feat(file): implemented write file for linux 2025-12-05 20:05:14 +01:00
531a4d03cb feat(file): implemented read file for linux 2025-12-05 18:53:35 +01:00
59928a9a55 feat(file): added enum for error handling 2025-12-05 17:16:59 +01:00
5ea177d928 docs(file): added license documentation to the file source files 2025-12-05 16:57:14 +01:00
11 changed files with 809 additions and 13 deletions

4
.gitignore vendored
View file

@ -65,5 +65,7 @@ compile_commands.json
CTestTestfile.cmake CTestTestfile.cmake
_deps _deps
CMakeUserPresets.json CMakeUserPresets.json
build build
# clangd
.cache/

View file

@ -11,10 +11,11 @@ target_include_directories(cosms-core PUBLIC ${PROJECT_SOURCE_DIR}/include)
if (MSVC) if (MSVC)
target_compile_options(cosms-core PRIVATE /W4) target_compile_options(cosms-core PRIVATE /W4)
else() else()
target_compile_options(cosms-core PRIVATE -wall -Wextra -pedantic) target_compile_options(cosms-core PRIVATE -Wall -Wextra -pedantic)
endif() endif()
add_executable(cosms-core-test ${PROJECT_SOURCE_DIR}/tests/main.c) file(GLOB_RECURSE SRC_TEST_FILES "${PROJECT_SOURCE_DIR}/tests/*.c")
add_executable(cosms-core-test ${SRC_TEST_FILES})
target_include_directories(cosms-core-test PRIVATE ${PROJECT_SOURCE_DIR}/tests) target_include_directories(cosms-core-test PRIVATE ${PROJECT_SOURCE_DIR}/tests)
target_link_libraries(cosms-core-test cosms-core) target_link_libraries(cosms-core-test cosms-core)

View file

@ -1,7 +1,63 @@
/*
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
*
* This software is licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License in the file LICENSE or at
* http://www.apache.org/licenses/LICENSE-2.0
*/
#ifndef COSMS_CORE_FILE #ifndef COSMS_CORE_FILE
#define COSMS_CORE_FILE #define COSMS_CORE_FILE
void cosms_core_file_read(char *path); #if defined(_WIN32)
void cosms_core_file_write(char *path); #include <windows.h>
#endif
typedef enum {
COSMS_CORE_FILE_OK = 0,
COSMS_CORE_FILE_NOT_FOUND = -1,
COSMS_CORE_FILE_NO_ACCESS = -2,
COSMS_CORE_FILE_LIMIT_REACHED = -3,
COSMS_CORE_FILE_COULD_NOT_READ_SIZE = -4,
COSMS_CORE_FILE_UNKOWN_ERROR = -5,
COSMS_CORE_FILE_FAILED_TO_ALLOCATE = -6,
COSMS_CORE_FILE_FAILED_TO_READ = -7,
COSMS_CORE_FILE_FAILED_TO_WRITE = -8,
COSMS_CORE_FILE_STILL_OPEN = -9,
COSMS_CORE_FILE_INVALID_FILE = -10,
COSMS_CORE_FILE_INVALID_OPERATION = -11
} CosmsCoreFileError;
#define COSMS_CORE_FILE_LARGE 0x7FFFFFFF
#define COSMS_CORE_FILE_MODE_READ (1u << 0)
#define COSMS_CORE_FILE_MODE_WRITE (1u << 1)
#define COSMS_CORE_FILE_MODE_APPEND (1u << 2)
#define COSMS_CORE_FILE_MODE_CREATE (1u << 3)
struct cosms_core_file {
int mode;
#if defined(__GNUC__)
int native_file;
#elif defined(_WIN32)
HANDLE native_file;
#endif
};
CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char *path, int mode);
CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file);
CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsigned long long *size);
CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char **buffer, unsigned int bytes_to_read, unsigned int *bytes_read);
CosmsCoreFileError cosms_core_file_read_all(struct cosms_core_file *file, char **buffer, unsigned long long *bytes_read);
CosmsCoreFileError cosms_core_file_write(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_write, unsigned int *bytes_written);
CosmsCoreFileError cosms_core_file_write_all(struct cosms_core_file *file, char *buffer, unsigned long long bytes_to_write);
CosmsCoreFileError cosms_core_file_delete(const char *path);
const char *cosms_core_file_error_string(CosmsCoreFileError error);
#endif #endif

View file

@ -1,11 +1,417 @@
#include <cosms-core/file.h> /*
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
*
* This software is licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License in the file LICENSE or at
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include "cosms-core/file.h"
#include <stdio.h> #include <stdlib.h>
#include <stdint.h>
#include <limits.h>
void cosms_core_file_read(char *path) { #if defined(__GNUC__)
printf("Hello, World!"); #define _FILE_OFFSET_BITS_ 64
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#endif
CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char *path, int mode) {
file->mode = mode;
#if defined(__GNUC__)
int flags = 0;
if (mode & (COSMS_CORE_FILE_MODE_READ | COSMS_CORE_FILE_MODE_WRITE)) {
flags |= O_RDWR;
} else if (mode & COSMS_CORE_FILE_MODE_READ) {
flags |= O_RDONLY;
} else if (mode & COSMS_CORE_FILE_MODE_WRITE) {
flags |= (O_WRONLY | O_TRUNC);
} else if (mode & COSMS_CORE_FILE_MODE_APPEND) {
flags |= (O_WRONLY | O_APPEND);
} }
void cosms_core_file_write(char *path) { if (mode & COSMS_CORE_FILE_MODE_CREATE) {
printf("Hello, World"); flags |= O_CREAT;
}
struct stat file_stat;
if (stat(path, &file_stat) != 0) {
return COSMS_CORE_FILE_COULD_NOT_READ_SIZE;
}
if (file_stat.st_size > COSMS_CORE_FILE_LARGE) {
flags |= O_LARGEFILE;
}
file->native_file = open(path, flags);
if (file->native_file == -1) {
switch(errno) {
case ENOENT:
return COSMS_CORE_FILE_NOT_FOUND;
case EACCES:
return COSMS_CORE_FILE_NO_ACCESS;
case EMFILE:
case ENFILE:
return COSMS_CORE_FILE_LIMIT_REACHED;
default:
return COSMS_CORE_FILE_UNKOWN_ERROR;
}
}
#elif defined(_WIN32)
DWORD access_flags = 0;
DWORD create_flag = OPEN_EXISTING;
if (mode & COSMS_CORE_FILE_MODE_READ) {
access_flags |= GENERIC_READ;
}
if (mode & COSMS_CORE_FILE_MODE_WRITE) {
access_flags |= GENERIC_WRITE;
}
if (mode & COSMS_CORE_FILE_MODE_APPEND) {
access_flags |= GENERIC_WRITE;
create_flag = OPEN_ALWAYS;
}
if (mode & COSMS_CORE_FILE_MODE_CREATE) {
create_flag = CREATE_ALWAYS;
}
file->native_file = CreateFile(path, access_flags, 0, NULL, create_flag, FILE_ATTRIBUTE_NORMAL, NULL);
if (file->native_file == INVALID_HANDLE_VALUE) {
switch(GetLastError()) {
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
return COSMS_CORE_FILE_NOT_FOUND;
case ERROR_ACCESS_DENIED:
case ERROR_SHARING_VIOLATION:
case ERROR_LOCK_VIOLATION:
return COSMS_CORE_FILE_NO_ACCESS;
case ERROR_TOO_MANY_OPEN_FILES:
return COSMS_CORE_FILE_LIMIT_REACHED;
default:
return COSMS_CORE_FILE_UNKOWN_ERROR;
}
}
#endif
return COSMS_CORE_FILE_OK;
}
CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file) {
#if defined(__GNUC__)
int error = close(file->native_file);
if (error == -1) {
if (errno == EBADF) {
return COSMS_CORE_FILE_INVALID_FILE;
}
return COSMS_CORE_FILE_UNKOWN_ERROR;
}
#elif defined(_WIN32)
BOOL error = CloseHandle(file->native_file);
if (error == 0) {
if (GetLastError() == ERROR_INVALID_HANDLE) {
return COSMS_CORE_FILE_INVALID_FILE;
}
return COSMS_CORE_FILE_UNKOWN_ERROR;
}
#endif
return COSMS_CORE_FILE_OK;
}
CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsigned long long *size) {
#if defined(__GNUC__)
struct stat file_stat;
if (fstat(file->native_file, &file_stat) != 0) {
if (errno == EBADF) {
return COSMS_CORE_FILE_INVALID_FILE;
}
return COSMS_CORE_FILE_COULD_NOT_READ_SIZE;
}
(*size) = file_stat.st_size;
#elif defined(_WIN32)
DWORD high_size;
DWORD low_size = GetFileSize(file->native_file, &high_size);
if (low_size == INVALID_FILE_SIZE) {
if (GetLastError() == ERROR_INVALID_HANDLE) {
return COSMS_CORE_FILE_INVALID_FILE;
}
return COSMS_CORE_FILE_COULD_NOT_READ_SIZE;
}
(*size) = ((unsigned long long)high_size << 32) | low_size;
#endif
return COSMS_CORE_FILE_OK;
}
CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char **buffer, unsigned int bytes_to_read, unsigned int *bytes_read) {
if ((file->mode & COSMS_CORE_FILE_MODE_READ) == 0) {
return COSMS_CORE_FILE_INVALID_OPERATION;
}
#if defined(__GNUC__)
int read_bytes = read(file->native_file, *buffer, bytes_to_read);
if (read_bytes == -1) {
return COSMS_CORE_FILE_FAILED_TO_READ;
}
if (bytes_read != NULL) {
(*bytes_read) = read_bytes;
}
#elif defined(_WIN32)
if (ReadFile(file->native_file, *buffer, bytes_to_read, (LPDWORD)bytes_read, NULL) == 0) {
return COSMS_CORE_FILE_FAILED_TO_READ;
}
#endif
buffer[bytes_to_read] = '\0';
return COSMS_CORE_FILE_OK;
}
CosmsCoreFileError cosms_core_file_read_all(struct cosms_core_file *file, char **buffer, unsigned long long *bytes_read) {
if ((file->mode & COSMS_CORE_FILE_MODE_READ) == 0) {
return COSMS_CORE_FILE_INVALID_OPERATION;
}
#if defined(__GNUC__)
struct stat file_stat;
if (fstat(file->native_file, &file_stat) != 0) {
return COSMS_CORE_FILE_COULD_NOT_READ_SIZE;
}
if (bytes_read != NULL) {
(*bytes_read) = file_stat.st_size;
}
(*buffer) = (char*)malloc((file_stat.st_size + 1) * sizeof(char));
if ((*buffer) == NULL) {
return COSMS_CORE_FILE_FAILED_TO_ALLOCATE;
}
unsigned long long remaining_bytes = file_stat.st_size;
while (remaining_bytes != 0) {
unsigned int current_bytes_to_read;
if (remaining_bytes > UINT_MAX) {
current_bytes_to_read = UINT_MAX;
} else {
current_bytes_to_read = (unsigned int)remaining_bytes;
}
int read_bytes = read(file, (*buffer) + (file_stat.st_size - remaining_bytes), current_bytes_to_read);
if (read_bytes == -1) {
free(*buffer);
return COSMS_CORE_FILE_FAILED_TO_READ;
}
remaining_bytes -= read_bytes;
}
(*buffer)[file_stat.st_size] = '\0';
#elif defined(_WIN32)
DWORD high_size;
DWORD low_size = GetFileSize(file->native_file, &high_size);
if (low_size == INVALID_FILE_SIZE && GetLastError() != NO_ERROR) {
return COSMS_CORE_FILE_COULD_NOT_READ_SIZE;
}
unsigned long long file_size = ((unsigned long long)high_size << 32) | low_size;
if (bytes_read != NULL) {
(*bytes_read) = file_size;
}
(*buffer) = (char*)malloc((file_size + 1) * sizeof(char));
if ((*buffer) == NULL) {
return COSMS_CORE_FILE_FAILED_TO_ALLOCATE;
}
unsigned long long remaining_bytes = file_size;
while (remaining_bytes != 0) {
DWORD current_bytes_to_read, read_bytes;
if (remaining_bytes > UINT_MAX) {
current_bytes_to_read = UINT_MAX;
} else {
current_bytes_to_read = (DWORD)remaining_bytes;
}
if (ReadFile(file->native_file, (*buffer) + (file_size - remaining_bytes), current_bytes_to_read, &read_bytes, NULL) == 0) {
free(*buffer);
return COSMS_CORE_FILE_FAILED_TO_READ;
}
remaining_bytes -= read_bytes;
}
(*buffer)[file_size] = '\0';
#endif
return COSMS_CORE_FILE_OK;
}
CosmsCoreFileError cosms_core_file_write(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_write, unsigned int *bytes_written) {
if ((file->mode & COSMS_CORE_FILE_MODE_WRITE) == 0) {
return COSMS_CORE_FILE_INVALID_OPERATION;
}
#if defined(__GNUC__)
int written_bytes = write(file->native_file, buffer, bytes_to_write);
if (written_bytes == -1) {
return COSMS_CORE_FILE_FAILED_TO_WRITE;
}
if (bytes_written != NULL) {
(*bytes_written) = written_bytes;
}
#elif defined(_WIN32)
if (WriteFile(file->native_file, buffer, bytes_to_write, (LPDWORD)bytes_written, NULL) == 0) {
return COSMS_CORE_FILE_FAILED_TO_WRITE;
}
#endif
return COSMS_CORE_FILE_OK;
}
CosmsCoreFileError cosms_core_file_write_all(struct cosms_core_file *file, char *buffer, unsigned long long bytes_to_write) {
if ((file->mode & COSMS_CORE_FILE_MODE_WRITE) == 0) {
return COSMS_CORE_FILE_INVALID_OPERATION;
}
#if defined(__GNUC__)
unsigned long long remaining_bytes = bytes_to_write;
while (remaining_bytes != 0) {
unsigned int current_bytes_to_write;
if (remaining_bytes > UINT_MAX) {
current_bytes_to_write = UINT_MAX;
} else {
current_bytes_to_write = (unsigned int)remaining_bytes;
}
int written_bytes = write(file, content + (bytes_to_write - remaining_bytes), current_bytes_to_write);
if (written_bytes == -1) {
return COSMS_CORE_FILE_FAILED_TO_WRITE;
}
remaining_bytes -= written_bytes;
}
#elif defined(_WIN32)
unsigned long long remaining_bytes = bytes_to_write;
while (remaining_bytes != 0) {
DWORD current_bytes_to_write, written_bytes;
if (remaining_bytes > UINT_MAX) {
current_bytes_to_write = UINT_MAX;
} else {
current_bytes_to_write = (DWORD)remaining_bytes;
}
if (WriteFile(file, buffer + (bytes_to_write - remaining_bytes), current_bytes_to_write, &written_bytes, NULL) == 0) {
return COSMS_CORE_FILE_FAILED_TO_WRITE;
}
remaining_bytes -= written_bytes;
}
#endif
return COSMS_CORE_FILE_OK;
}
CosmsCoreFileError cosms_core_file_delete(const char *path) {
#if defined(__GNUC__)
if (unlink(path) == -1) {
switch (errno) {
case ENOENT:
return COSMS_CORE_FILE_NOT_FOUND;
case EACCES:
return COSMS_CORE_FILE_NO_ACCESS;
case EBUSY:
return COSMS_CORE_FILE_STILL_OPEN;
default:
return COSMS_CORE_FILE_UNKOWN_ERROR;
}
}
#elif defined(_WIN32)
if (!DeleteFile(path)) {
switch(GetLastError()) {
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
return COSMS_CORE_FILE_NOT_FOUND;
case ERROR_ACCESS_DENIED:
return COSMS_CORE_FILE_NO_ACCESS;
case ERROR_LOCK_VIOLATION:
case ERROR_SHARING_VIOLATION:
return COSMS_CORE_FILE_STILL_OPEN;
default:
return COSMS_CORE_FILE_UNKOWN_ERROR;
}
}
#endif
return COSMS_CORE_FILE_OK;
}
const char *cosms_core_file_error_string(CosmsCoreFileError error) {
switch(error) {
case COSMS_CORE_FILE_OK:
return "cosms-file everything ok";
case COSMS_CORE_FILE_NOT_FOUND:
return "cosms-file path not found";
case COSMS_CORE_FILE_NO_ACCESS:
return "cosms-file no access";
case COSMS_CORE_FILE_LIMIT_REACHED:
return "cosms-file to many open files";
case COSMS_CORE_FILE_COULD_NOT_READ_SIZE:
return "cosms-file failed to read size";
case COSMS_CORE_FILE_UNKOWN_ERROR:
return "cosms-file unkown error occured";
case COSMS_CORE_FILE_FAILED_TO_ALLOCATE:
return "cosms-file failed to allocate memory";
case COSMS_CORE_FILE_FAILED_TO_READ:
return "cosms-file failed to read content of file";
case COSMS_CORE_FILE_FAILED_TO_WRITE:
return "cosms-file failed to write content to file";
case COSMS_CORE_FILE_STILL_OPEN:
return "cosms-file still open";
case COSMS_CORE_FILE_INVALID_FILE:
return "cosms-file invalid native file given";
case COSMS_CORE_FILE_INVALID_OPERATION:
return "cosms-file invalid operation for current file";
default:
return NULL;
}
} }

BIN
tests/data/large-file.txt Normal file

Binary file not shown.

View file

@ -0,0 +1 @@
Hello, World!

View file

@ -1,6 +1,14 @@
#include <cosms-core/file.h> /*
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
*
* This software is licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License in the file LICENSE or at
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include "unit/unit.h"
int main(void) { int main(void) {
cosms_core_file_read("test.txt"); COSMS_CORE_TEST_RUN()
return 0; return 0;
} }

43
tests/test.h Normal file
View file

@ -0,0 +1,43 @@
#ifndef COSMS_CORE_TEST
#define COSMS_CORE_TEST
#include <stdio.h>
#include <stdbool.h>
#define COSMS_CORE_TEST_FUNCTION_NAME(name) cosms_core_##name##_test
#define COSMS_CORE_TEST_DEFINE(name) void cosms_core_##name##_test(void)
#define COSMS_CORE_TEST_TEST(name, implementation) void cosms_core_##name##_test(void) { \
printf("----------------------------------------\n%s\n----------------------------------------\n", #name); \
implementation \
}
#define COSMS_CORE_TEST_SUB_TEST(name, implementation) { \
const char *cosms_core_test_sub_test_error = NULL; \
printf("[*] running test %s...\n", #name); \
implementation \
if (cosms_core_test_sub_test_error == NULL) { \
printf("[+] test %s completed successfully!\n", #name); \
} else { \
printf("[-] test %s failed with error: %s\n", #name, cosms_core_test_sub_test_error); \
} \
}
#define COSMS_CORE_TEST_EXPORT(name, ...) static const cosms_core_test cosms_core_test_##name[] = { \
__VA_ARGS__ \
}
#define COSMS_CORE_TEST_START static const cosms_core_test *cosms_core_test_tests[] = {
#define COSMS_CORE_TEST_IMPORT(name) cosms_core_test_##name
#define COSMS_CORE_TEST_END };
#define COSMS_CORE_TEST_RUN() \
for (unsigned long long index = 0; index < sizeof(cosms_core_test_tests); index += 1) { \
for (unsigned long long test_index = 0; test_index < sizeof(cosms_core_test_tests[index]); test_index += 1) { \
cosms_core_test_tests[index][test_index](); \
} \
}
typedef void (*cosms_core_test)();
#endif

230
tests/unit/file.c Normal file
View file

@ -0,0 +1,230 @@
/*
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
*
* This software is licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License in the file LICENSE or at
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include "unit/file.h"
#include <cosms-core/file.h>
#include <string.h>
#include <stdlib.h>
COSMS_CORE_TEST_TEST(file_open,
COSMS_CORE_TEST_SUB_TEST(opening small file for reading,
struct cosms_core_file file;
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_READ);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(opening large file for reading,
struct cosms_core_file file;
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.txt", COSMS_CORE_FILE_MODE_READ);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(opening small file for writting,
struct cosms_core_file file;
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_WRITE);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(opening large file for writting,
struct cosms_core_file file;
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.txt", COSMS_CORE_FILE_MODE_WRITE);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(opening small file for appending,
struct cosms_core_file file;
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_APPEND);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(opening large file for appending,
struct cosms_core_file file;
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.txt", COSMS_CORE_FILE_MODE_APPEND);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(opening small file for reading and writting,
struct cosms_core_file file;
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_READ | COSMS_CORE_FILE_MODE_WRITE);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(opening large file for reading and writting,
struct cosms_core_file file;
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.txt", COSMS_CORE_FILE_MODE_READ | COSMS_CORE_FILE_MODE_WRITE);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(opening non-existing file for reading,
struct cosms_core_file file;
CosmsCoreFileError error = cosms_core_file_open(&file, "non-existing-file.cosms", COSMS_CORE_FILE_MODE_READ);
if (error != COSMS_CORE_FILE_NOT_FOUND) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
if (error == COSMS_CORE_FILE_OK) {
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
}
)
)
COSMS_CORE_TEST_TEST(file_close,
COSMS_CORE_TEST_SUB_TEST(closing file,
struct cosms_core_file file;
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_READ);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(closing non-existing file,
struct cosms_core_file file;
file.native_file = 0;
CosmsCoreFileError error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_INVALID_FILE) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
)
)
COSMS_CORE_TEST_TEST(file_size,
COSMS_CORE_TEST_SUB_TEST(reading small file size,
struct cosms_core_file file;
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_READ);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
unsigned long long size = 0;
error = cosms_core_file_get_size(&file, &size);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
if (size != 13) {
cosms_core_test_sub_test_error = "result does not match expected result";
}
}
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(reading large file size,
struct cosms_core_file file;
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.txt", COSMS_CORE_FILE_MODE_READ);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
unsigned long long size = 0;
error = cosms_core_file_get_size(&file, &size);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
if (size != 5242880000ULL) {
cosms_core_test_sub_test_error = "result does not match expected result";
}
}
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(reading non-existing file size,
struct cosms_core_file file;
file.native_file = 0;
unsigned long long size = 0;
CosmsCoreFileError error = cosms_core_file_get_size(&file, &size);
if (error != COSMS_CORE_FILE_INVALID_FILE) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
)
)
COSMS_CORE_TEST_TEST(file_read,
)
COSMS_CORE_TEST_TEST(file_write,
)
COSMS_CORE_TEST_TEST(file_delete,
)

30
tests/unit/file.h Normal file
View file

@ -0,0 +1,30 @@
/*
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
*
* This software is licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License in the file LICENSE or at
* http://www.apache.org/licenses/LICENSE-2.0
*/
#ifndef COSMS_CORE_FILE_TEST
#define COSMS_CORE_FILE_TEST
#include "test.h"
COSMS_CORE_TEST_DEFINE(file_open);
COSMS_CORE_TEST_DEFINE(file_close);
COSMS_CORE_TEST_DEFINE(file_size);
COSMS_CORE_TEST_DEFINE(file_read);
COSMS_CORE_TEST_DEFINE(file_write);
COSMS_CORE_TEST_DEFINE(file_delete);
COSMS_CORE_TEST_EXPORT(file,
COSMS_CORE_TEST_FUNCTION_NAME(file_open),
COSMS_CORE_TEST_FUNCTION_NAME(file_close),
COSMS_CORE_TEST_FUNCTION_NAME(file_size),
COSMS_CORE_TEST_FUNCTION_NAME(file_read),
COSMS_CORE_TEST_FUNCTION_NAME(file_write),
COSMS_CORE_TEST_FUNCTION_NAME(file_delete)
);
#endif

19
tests/unit/unit.h Normal file
View file

@ -0,0 +1,19 @@
/*
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
*
* This software is licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License in the file LICENSE or at
* http://www.apache.org/licenses/LICENSE-2.0
*/
#ifndef COSMS_UNIT
#define COSMS_UNIT
#include "test.h"
#include "file.h"
COSMS_CORE_TEST_START
COSMS_CORE_TEST_IMPORT(file)
COSMS_CORE_TEST_END
#endif