Compare commits
4 commits
87fb7556c3
...
3f0299bef1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3f0299bef1 | ||
|
|
febf56912e | ||
|
|
e276388f7b | ||
|
|
ae9b1ed2b9 |
13 changed files with 252 additions and 72 deletions
|
|
@ -14,7 +14,8 @@ else()
|
|||
target_compile_options(cosms-core PRIVATE -Wall -Wextra -pedantic)
|
||||
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_link_libraries(cosms-core-test cosms-core)
|
||||
|
|
|
|||
|
|
@ -12,20 +12,20 @@
|
|||
#include <stdbool.h>
|
||||
|
||||
typedef enum {
|
||||
COSMS_FILE_OK = 0,
|
||||
COSMS_FILE_NOT_FOUND = -1,
|
||||
COSMS_FILE_NO_ACCESS = -2,
|
||||
COSMS_FILE_LIMIT_REACHED = -3,
|
||||
COSMS_FILE_COULD_NOT_READ_SIZE = -4,
|
||||
COSMS_FILE_UNKOWN_ERROR = -5,
|
||||
COSMS_FILE_FAILED_TO_ALLOCATE = -6,
|
||||
COSMS_FILE_FAILED_TO_READ = -7,
|
||||
COSMS_FILE_FAILED_TO_WRITE = -8,
|
||||
} CosmsFileError;
|
||||
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,
|
||||
} CosmsCoreFileError;
|
||||
|
||||
CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned long long *size);
|
||||
CosmsFileError cosms_core_file_write(const char *path, const char *content, unsigned long long size, bool override);
|
||||
CosmsCoreFileError cosms_core_file_read(const char *path, char **content, unsigned long long *size);
|
||||
CosmsCoreFileError cosms_core_file_write(const char *path, const char *content, unsigned long long size, bool override);
|
||||
|
||||
const char *cosms_core_file_error_string(CosmsFileError error);
|
||||
const char *cosms_core_file_error_string(CosmsCoreFileError error);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
82
src/file.c
82
src/file.c
|
|
@ -20,37 +20,40 @@
|
|||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned long long *size) {
|
||||
CosmsCoreFileError cosms_core_file_read(const char *path, char **content, unsigned long long *size) {
|
||||
#if defined(__GNUC__)
|
||||
int file = open(path, O_RDONLY);
|
||||
if (file == -1) {
|
||||
switch(errno) {
|
||||
case ENOENT:
|
||||
return COSMS_FILE_NOT_FOUND;
|
||||
return COSMS_CORE_FILE_NOT_FOUND;
|
||||
|
||||
case EACCES:
|
||||
return COSMS_FILE_NO_ACCESS;
|
||||
return COSMS_CORE_FILE_NO_ACCESS;
|
||||
|
||||
case EMFILE:
|
||||
case ENFILE:
|
||||
return COSMS_FILE_LIMIT_REACHED;
|
||||
return COSMS_CORE_FILE_LIMIT_REACHED;
|
||||
|
||||
default:
|
||||
return COSMS_FILE_UNKOWN_ERROR;
|
||||
return COSMS_CORE_FILE_UNKOWN_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
struct stat file_stat;
|
||||
if (stat(path, &file_stat) != 0) {
|
||||
close(file);
|
||||
return COSMS_FILE_COULD_NOT_READ_SIZE;
|
||||
return COSMS_CORE_FILE_COULD_NOT_READ_SIZE;
|
||||
}
|
||||
|
||||
if (size != NULL) {
|
||||
*size = file_stat.st_size;
|
||||
}
|
||||
|
||||
*content = (char*)malloc((file_stat.st_size + 1) * sizeof(char));
|
||||
if (content == NULL) {
|
||||
close(file);
|
||||
return COSMS_FILE_FAILED_TO_ALLOCATE;
|
||||
return COSMS_CORE_FILE_FAILED_TO_ALLOCATE;
|
||||
}
|
||||
|
||||
unsigned long long remaining_bytes = file_stat.st_size;
|
||||
|
|
@ -66,7 +69,7 @@ CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned l
|
|||
if (read_bytes == -1) {
|
||||
free(*content);
|
||||
close(file);
|
||||
return COSMS_FILE_FAILED_TO_READ;
|
||||
return COSMS_CORE_FILE_FAILED_TO_READ;
|
||||
}
|
||||
|
||||
remaining_bytes -= read_bytes;
|
||||
|
|
@ -80,18 +83,18 @@ CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned l
|
|||
switch(GetLastError()) {
|
||||
case ERROR_FILE_NOT_FOUND:
|
||||
case ERROR_PATH_NOT_FOUND:
|
||||
return COSMS_FILE_NOT_FOUND;
|
||||
return COSMS_CORE_FILE_NOT_FOUND;
|
||||
|
||||
case ERROR_ACCESS_DENIED:
|
||||
case ERROR_SHARING_VIOLATION:
|
||||
case ERROR_LOCK_VIOLATION:
|
||||
return COSMS_FILE_NO_ACCESS;
|
||||
return COSMS_CORE_FILE_NO_ACCESS;
|
||||
|
||||
case ERROR_TOO_MANY_OPEN_FILES:
|
||||
return COSMS_FILE_LIMIT_REACHED;
|
||||
return COSMS_CORE_FILE_LIMIT_REACHED;
|
||||
|
||||
default:
|
||||
return COSMS_FILE_UNKOWN_ERROR;
|
||||
return COSMS_CORE_FILE_UNKOWN_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -99,16 +102,19 @@ CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned l
|
|||
DWORD low_size = GetFileSize(file, &high_size);
|
||||
if (low_size == INVALID_FILE_SIZE && GetLastError() != NO_ERROR) {
|
||||
CloseHandle(file);
|
||||
return COSMS_FILE_COULD_NOT_READ_SIZE;
|
||||
return COSMS_CORE_FILE_COULD_NOT_READ_SIZE;
|
||||
}
|
||||
|
||||
unsigned long long file_size = ((unsigned long long)high_size << 32) | low_size;
|
||||
|
||||
if (size != NULL) {
|
||||
*size = file_size;
|
||||
}
|
||||
|
||||
*content = (char*)malloc((file_size + 1) * sizeof(char));
|
||||
if (content == NULL) {
|
||||
CloseHandle(file);
|
||||
return COSMS_FILE_FAILED_TO_ALLOCATE;
|
||||
return COSMS_CORE_FILE_FAILED_TO_ALLOCATE;
|
||||
}
|
||||
|
||||
unsigned long long remaining_bytes = file_size;
|
||||
|
|
@ -123,7 +129,7 @@ CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned l
|
|||
if (!ReadFile(file, (*content) + (file_size - remaining_bytes), bytes_to_read, &read_bytes, NULL)) {
|
||||
free(*content);
|
||||
CloseHandle(file);
|
||||
return COSMS_FILE_FAILED_TO_READ;
|
||||
return COSMS_CORE_FILE_FAILED_TO_READ;
|
||||
}
|
||||
|
||||
remaining_bytes -= read_bytes;
|
||||
|
|
@ -133,10 +139,10 @@ CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned l
|
|||
CloseHandle(file);
|
||||
#endif
|
||||
|
||||
return COSMS_FILE_OK;
|
||||
return COSMS_CORE_FILE_OK;
|
||||
}
|
||||
|
||||
CosmsFileError cosms_core_file_write(const char *path, const char *content, unsigned long long size, bool override) {
|
||||
CosmsCoreFileError cosms_core_file_write(const char *path, const char *content, unsigned long long size, bool override) {
|
||||
#if defined(__GNUC__)
|
||||
int file;
|
||||
if (override) {
|
||||
|
|
@ -148,17 +154,17 @@ CosmsFileError cosms_core_file_write(const char *path, const char *content, unsi
|
|||
if (file == -1) {
|
||||
switch (errno) {
|
||||
case ENOENT:
|
||||
return COSMS_FILE_NOT_FOUND;
|
||||
return COSMS_CORE_FILE_NOT_FOUND;
|
||||
|
||||
case EACCES:
|
||||
return COSMS_FILE_NO_ACCESS;
|
||||
return COSMS_CORE_FILE_NO_ACCESS;
|
||||
|
||||
case EMFILE:
|
||||
case ENFILE:
|
||||
return COSMS_FILE_LIMIT_REACHED;
|
||||
return COSMS_CORE_FILE_LIMIT_REACHED;
|
||||
|
||||
default:
|
||||
return COSMS_FILE_UNKOWN_ERROR;
|
||||
return COSMS_CORE_FILE_UNKOWN_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -174,7 +180,7 @@ CosmsFileError cosms_core_file_write(const char *path, const char *content, unsi
|
|||
int written_bytes = write(file, content + (size - remaining_bytes), bytes_to_write);
|
||||
if (written_bytes == -1) {
|
||||
close(file);
|
||||
return COSMS_FILE_FAILED_TO_WRITE;
|
||||
return COSMS_CORE_FILE_FAILED_TO_WRITE;
|
||||
}
|
||||
|
||||
remaining_bytes -= written_bytes;
|
||||
|
|
@ -193,18 +199,18 @@ CosmsFileError cosms_core_file_write(const char *path, const char *content, unsi
|
|||
switch(GetLastError()) {
|
||||
case ERROR_FILE_NOT_FOUND:
|
||||
case ERROR_PATH_NOT_FOUND:
|
||||
return COSMS_FILE_NOT_FOUND;
|
||||
return COSMS_CORE_FILE_NOT_FOUND;
|
||||
|
||||
case ERROR_ACCESS_DENIED:
|
||||
case ERROR_SHARING_VIOLATION:
|
||||
case ERROR_LOCK_VIOLATION:
|
||||
return COSMS_FILE_NO_ACCESS;
|
||||
return COSMS_CORE_FILE_NO_ACCESS;
|
||||
|
||||
case ERROR_TOO_MANY_OPEN_FILES:
|
||||
return COSMS_FILE_LIMIT_REACHED;
|
||||
return COSMS_CORE_FILE_LIMIT_REACHED;
|
||||
|
||||
default:
|
||||
return COSMS_FILE_UNKOWN_ERROR;
|
||||
return COSMS_CORE_FILE_UNKOWN_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -219,7 +225,7 @@ CosmsFileError cosms_core_file_write(const char *path, const char *content, unsi
|
|||
|
||||
if (!WriteFile(file, content + (size - remaining_bytes), bytes_to_write, &written_bytes, NULL)) {
|
||||
CloseHandle(file);
|
||||
return COSMS_FILE_FAILED_TO_WRITE;
|
||||
return COSMS_CORE_FILE_FAILED_TO_WRITE;
|
||||
}
|
||||
|
||||
remaining_bytes -= written_bytes;
|
||||
|
|
@ -228,36 +234,36 @@ CosmsFileError cosms_core_file_write(const char *path, const char *content, unsi
|
|||
CloseHandle(file);
|
||||
#endif
|
||||
|
||||
return COSMS_FILE_OK;
|
||||
return COSMS_CORE_FILE_OK;
|
||||
}
|
||||
|
||||
const char *cosms_core_file_error_string(CosmsFileError error) {
|
||||
const char *cosms_core_file_error_string(CosmsCoreFileError error) {
|
||||
switch(error) {
|
||||
case COSMS_FILE_OK:
|
||||
case COSMS_CORE_FILE_OK:
|
||||
return "cosms-file everything ok";
|
||||
|
||||
case COSMS_FILE_NOT_FOUND:
|
||||
case COSMS_CORE_FILE_NOT_FOUND:
|
||||
return "cosms-file path not found";
|
||||
|
||||
case COSMS_FILE_NO_ACCESS:
|
||||
case COSMS_CORE_FILE_NO_ACCESS:
|
||||
return "cosms-file no access";
|
||||
|
||||
case COSMS_FILE_LIMIT_REACHED:
|
||||
case COSMS_CORE_FILE_LIMIT_REACHED:
|
||||
return "cosms-file to many open files";
|
||||
|
||||
case COSMS_FILE_COULD_NOT_READ_SIZE:
|
||||
case COSMS_CORE_FILE_COULD_NOT_READ_SIZE:
|
||||
return "cosms-file failed to read size";
|
||||
|
||||
case COSMS_FILE_UNKOWN_ERROR:
|
||||
case COSMS_CORE_FILE_UNKOWN_ERROR:
|
||||
return "cosms-file unkown error occured";
|
||||
|
||||
case COSMS_FILE_FAILED_TO_ALLOCATE:
|
||||
case COSMS_CORE_FILE_FAILED_TO_ALLOCATE:
|
||||
return "cosms-file failed to allocate memory";
|
||||
|
||||
case COSMS_FILE_FAILED_TO_READ:
|
||||
case COSMS_CORE_FILE_FAILED_TO_READ:
|
||||
return "cosms-file failed to read content of file";
|
||||
|
||||
case COSMS_FILE_FAILED_TO_WRITE:
|
||||
case COSMS_CORE_FILE_FAILED_TO_WRITE:
|
||||
return "cosms-file failed to write content to file";
|
||||
|
||||
default:
|
||||
|
|
|
|||
1
tests/data/append-write-file.txt
Normal file
1
tests/data/append-write-file.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
Hello, World!
|
||||
BIN
tests/data/large-file.txt
Normal file
BIN
tests/data/large-file.txt
Normal file
Binary file not shown.
BIN
tests/data/large-write-file.txt
Normal file
BIN
tests/data/large-write-file.txt
Normal file
Binary file not shown.
1
tests/data/small-file.txt
Normal file
1
tests/data/small-file.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
Hello, World!
|
||||
1
tests/data/small-write-file.txt
Normal file
1
tests/data/small-write-file.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
Hello, World!Hello, World!
|
||||
21
tests/main.c
21
tests/main.c
|
|
@ -1,22 +1,7 @@
|
|||
#include <cosms-core/file.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include "unit/unit.h"
|
||||
|
||||
int main(void) {
|
||||
unsigned long long buffer_size;
|
||||
char *buffer;
|
||||
CosmsFileError error = cosms_core_file_read("test.txt", &buffer, &buffer_size);
|
||||
if (error != COSMS_FILE_OK) {
|
||||
fprintf(stderr, "error(%d): %s\n", error, cosms_core_file_error_string(error));
|
||||
} else {
|
||||
printf("file read with content:\n%s\n", buffer);
|
||||
}
|
||||
|
||||
char *temp_buffer = "Hello, World!";
|
||||
error = cosms_core_file_write("write-file.txt", temp_buffer, 13, true);
|
||||
if (error != COSMS_FILE_OK) {
|
||||
fprintf(stderr, "error(%d): %s\n", error, cosms_core_file_error_string(error));
|
||||
}
|
||||
|
||||
cosms_core_file_read_test();
|
||||
cosms_core_file_write_test();
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
22
tests/test.h
Normal file
22
tests/test.h
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#ifndef COSMS_TEST
|
||||
#define COSMS_TEST
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define COSMS_CORE_TEST_HEADER(test_module_name) printf("----------------------------------------\n%s\n----------------------------------------\n", test_module_name);
|
||||
|
||||
#define COSMS_CORE_TEST_START(test_name) printf("[*] running test %s...\n", test_name)
|
||||
#define COSMS_CORE_TEST_SUCCESS(test_name) printf("[+] test %s completed successfully!\n", test_name);
|
||||
#define COSMS_CORE_TEST_FAIL(test_name, error) printf("[-] test %s failed with error: %s\n", test_name, error)
|
||||
|
||||
inline bool cosms_core_test_assert(const char *test_name, bool expression, const char *error_message) {
|
||||
if (!expression) {
|
||||
printf("[-] test %s failed with error: %s\n", test_name, error_message);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
||||
148
tests/unit/file.c
Normal file
148
tests/unit/file.c
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
#include "test.h"
|
||||
#include "unit/file.h"
|
||||
|
||||
#include <cosms-core/file.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void cosms_core_file_read_test() {
|
||||
COSMS_CORE_TEST_HEADER("file read");
|
||||
|
||||
const char *current_test_name = "reading small file";
|
||||
COSMS_CORE_TEST_START(current_test_name);
|
||||
|
||||
unsigned long long file_size;
|
||||
char *file_buffer;
|
||||
CosmsCoreFileError error = cosms_core_file_read("tests/data/small-file.txt", &file_buffer, &file_size);
|
||||
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) {
|
||||
const char *expected_result = "Hello, World!";
|
||||
if (cosms_core_test_assert(current_test_name, file_size == strlen(expected_result) && strcmp(expected_result, file_buffer) == 0, "result does not match expected result")) {
|
||||
COSMS_CORE_TEST_SUCCESS(current_test_name);
|
||||
}
|
||||
}
|
||||
|
||||
free(file_buffer);
|
||||
|
||||
current_test_name = "reading large file";
|
||||
COSMS_CORE_TEST_START(current_test_name);
|
||||
|
||||
file_size = 0;
|
||||
file_buffer = NULL;
|
||||
error = cosms_core_file_read("tests/data/large-file.txt", &file_buffer, &file_size);
|
||||
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) {
|
||||
unsigned long long expected_size = 5242880000ULL;
|
||||
char *expected_result = (char*)malloc((expected_size + 1) * sizeof(char));
|
||||
if (!expected_result) {
|
||||
printf("[-] failed to allocate memory for test aborting file read tests...\n");
|
||||
free(file_buffer);
|
||||
return;
|
||||
}
|
||||
|
||||
memset(expected_result, 'a', expected_size);
|
||||
expected_result[expected_size] = '\0';
|
||||
|
||||
if (cosms_core_test_assert(current_test_name, file_size == expected_size && strcmp(expected_result, file_buffer) == 0, "result does not match expected result")) {
|
||||
COSMS_CORE_TEST_SUCCESS(current_test_name);
|
||||
}
|
||||
|
||||
free(expected_result);
|
||||
}
|
||||
|
||||
free(file_buffer);
|
||||
|
||||
current_test_name = "reading non existing file";
|
||||
COSMS_CORE_TEST_START(current_test_name);
|
||||
|
||||
file_size = 0;
|
||||
file_buffer = NULL;
|
||||
error = cosms_core_file_read("non-existing-file.cosms", &file_buffer, &file_size);
|
||||
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_NOT_FOUND, cosms_core_file_error_string(error))) {
|
||||
COSMS_CORE_TEST_SUCCESS(current_test_name);
|
||||
}
|
||||
|
||||
free(file_buffer);
|
||||
}
|
||||
|
||||
void cosms_core_file_write_test() {
|
||||
COSMS_CORE_TEST_HEADER("file write");
|
||||
|
||||
const char *current_test_name = "writting small file";
|
||||
COSMS_CORE_TEST_START(current_test_name);
|
||||
|
||||
char *file_buffer = "Hello, World!";
|
||||
CosmsCoreFileError error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer), true);
|
||||
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) {
|
||||
char *file_content;
|
||||
error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL);
|
||||
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) {
|
||||
if (cosms_core_test_assert(current_test_name, strcmp(file_buffer, file_content) == 0, "file content not what was written")) {
|
||||
COSMS_CORE_TEST_SUCCESS(current_test_name);
|
||||
}
|
||||
}
|
||||
|
||||
free(file_content);
|
||||
}
|
||||
|
||||
current_test_name = "writting big file";
|
||||
COSMS_CORE_TEST_START(current_test_name);
|
||||
|
||||
unsigned long long large_file_size = 5242880000ULL;
|
||||
char *large_file_buffer = (char*)malloc((large_file_size + 1) * sizeof(char));
|
||||
if (!large_file_buffer) {
|
||||
printf("[-] failed to allocate memory for test aborting file read tests...\n");
|
||||
free(large_file_buffer);
|
||||
return;
|
||||
}
|
||||
|
||||
memset(large_file_buffer, 'a', large_file_size);
|
||||
large_file_buffer[large_file_size] = '\0';
|
||||
|
||||
error = cosms_core_file_write("tests/data/large-write-file.txt", large_file_buffer, large_file_size, true);
|
||||
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) {
|
||||
char *file_content;
|
||||
error = cosms_core_file_read("tests/data/large-write-file.txt", &file_content, NULL);
|
||||
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) {
|
||||
if (cosms_core_test_assert(current_test_name, strcmp(large_file_buffer, file_content) == 0, "file content not what was written")) {
|
||||
COSMS_CORE_TEST_SUCCESS(current_test_name);
|
||||
}
|
||||
}
|
||||
|
||||
free(file_content);
|
||||
}
|
||||
|
||||
free(large_file_buffer);
|
||||
|
||||
current_test_name = "appending file";
|
||||
COSMS_CORE_TEST_START(current_test_name);
|
||||
|
||||
error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer), false);
|
||||
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) {
|
||||
char *file_content;
|
||||
error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL);
|
||||
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) {
|
||||
const char *expected_result = "Hello, World!Hello, World!";
|
||||
if (cosms_core_test_assert(current_test_name, strcmp(expected_result, file_content) == 0, "file content not what was written")) {
|
||||
COSMS_CORE_TEST_SUCCESS(current_test_name);
|
||||
}
|
||||
}
|
||||
|
||||
free(file_content);
|
||||
}
|
||||
|
||||
current_test_name = "appending to non existing file";
|
||||
COSMS_CORE_TEST_START(current_test_name);
|
||||
|
||||
error = cosms_core_file_write("tests/data/append-write-file.txt", file_buffer, strlen(file_buffer), false);
|
||||
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) {
|
||||
char *file_content;
|
||||
error = cosms_core_file_read("tests/data/append-write-file.txt", &file_content, NULL);
|
||||
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) {
|
||||
if (cosms_core_test_assert(current_test_name, strcmp(file_buffer, file_content) == 0, "file content not what was written")) {
|
||||
COSMS_CORE_TEST_SUCCESS(current_test_name);
|
||||
}
|
||||
}
|
||||
|
||||
free(file_content);
|
||||
}
|
||||
}
|
||||
9
tests/unit/file.h
Normal file
9
tests/unit/file.h
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#ifndef COSMS_CORE_FILE_TEST
|
||||
#define COSMS_CORE_FILE_TEST
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
void cosms_core_file_read_test();
|
||||
void cosms_core_file_write_test();
|
||||
|
||||
#endif
|
||||
6
tests/unit/unit.h
Normal file
6
tests/unit/unit.h
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef COSMS_UNIT
|
||||
#define COSMS_UNIT
|
||||
|
||||
#include "file.h"
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Reference in a new issue