feat(file): implemented write file tests
This commit is contained in:
parent
ae9b1ed2b9
commit
e276388f7b
4 changed files with 126 additions and 11 deletions
10
src/file.c
10
src/file.c
|
|
@ -46,7 +46,10 @@ CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned l
|
||||||
return COSMS_FILE_COULD_NOT_READ_SIZE;
|
return COSMS_FILE_COULD_NOT_READ_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
*size = file_stat.st_size;
|
if (size != NULL) {
|
||||||
|
*size = file_stat.st_size;
|
||||||
|
}
|
||||||
|
|
||||||
*content = (char*)malloc((file_stat.st_size + 1) * sizeof(char));
|
*content = (char*)malloc((file_stat.st_size + 1) * sizeof(char));
|
||||||
if (content == NULL) {
|
if (content == NULL) {
|
||||||
close(file);
|
close(file);
|
||||||
|
|
@ -104,7 +107,10 @@ CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned l
|
||||||
|
|
||||||
unsigned long long file_size = ((unsigned long long)high_size << 32) | low_size;
|
unsigned long long file_size = ((unsigned long long)high_size << 32) | low_size;
|
||||||
|
|
||||||
*size = file_size;
|
if (size != NULL) {
|
||||||
|
*size = file_size;
|
||||||
|
}
|
||||||
|
|
||||||
*content = (char*)malloc((file_size + 1) * sizeof(char));
|
*content = (char*)malloc((file_size + 1) * sizeof(char));
|
||||||
if (content == NULL) {
|
if (content == NULL) {
|
||||||
CloseHandle(file);
|
CloseHandle(file);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#include "unit/unit.h"
|
#include "unit/unit.h"
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
cosms_file_read_test();
|
cosms_core_file_read_test();
|
||||||
|
cosms_core_file_write_test();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
#include "file.h"
|
#include "test.h"
|
||||||
#include "../test.h"
|
#include "unit/file.h"
|
||||||
//#include <cosms-core/file.h>
|
|
||||||
#include "../../include/cosms-core/file.h"
|
#include <cosms-core/file.h>
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
void cosms_file_read_test() {
|
void cosms_core_file_read_test() {
|
||||||
COSMS_TEST_HEADER("file read");
|
COSMS_TEST_HEADER("file read");
|
||||||
|
|
||||||
const char *current_test_name = "reading small file";
|
const char *current_test_name = "reading small file";
|
||||||
|
|
@ -72,4 +72,111 @@ void cosms_file_read_test() {
|
||||||
}
|
}
|
||||||
|
|
||||||
free(file_buffer);
|
free(file_buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cosms_core_file_write_test() {
|
||||||
|
COSMS_TEST_HEADER("file write");
|
||||||
|
|
||||||
|
const char *current_test_name = "writting small file";
|
||||||
|
COSMS_TEST_START(current_test_name);
|
||||||
|
|
||||||
|
char *file_buffer = "Hello, World!";
|
||||||
|
CosmsFileError error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer) - 1, true);
|
||||||
|
if (error != COSMS_FILE_OK) {
|
||||||
|
COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error));
|
||||||
|
} else {
|
||||||
|
char *file_content;
|
||||||
|
error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL);
|
||||||
|
if (error != COSMS_FILE_OK) {
|
||||||
|
COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error));
|
||||||
|
} else {
|
||||||
|
if (strcmp(file_buffer, file_content)) {
|
||||||
|
COSMS_TEST_SUCCESS(current_test_name);
|
||||||
|
} else {
|
||||||
|
COSMS_TEST_FAIL(current_test_name, "file content not what was written");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(file_content);
|
||||||
|
}
|
||||||
|
|
||||||
|
current_test_name = "writting big file";
|
||||||
|
COSMS_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 (error != COSMS_FILE_OK) {
|
||||||
|
COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error));
|
||||||
|
} else {
|
||||||
|
char *file_content;
|
||||||
|
error = cosms_core_file_read("tests/data/large-write-file.txt", &file_content, NULL);
|
||||||
|
if (error != COSMS_FILE_OK) {
|
||||||
|
COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error));
|
||||||
|
} else {
|
||||||
|
if (strcmp(file_buffer, file_content)) {
|
||||||
|
COSMS_TEST_SUCCESS(current_test_name);
|
||||||
|
} else {
|
||||||
|
COSMS_TEST_FAIL(current_test_name, "file content not what was written");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(file_content);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(large_file_buffer);
|
||||||
|
|
||||||
|
current_test_name = "appending file";
|
||||||
|
COSMS_TEST_START(current_test_name);
|
||||||
|
|
||||||
|
error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer) - 1, false);
|
||||||
|
if (error != COSMS_FILE_OK) {
|
||||||
|
COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error));
|
||||||
|
} else {
|
||||||
|
char *file_content;
|
||||||
|
error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL);
|
||||||
|
if (error != COSMS_FILE_OK) {
|
||||||
|
COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error));
|
||||||
|
} else {
|
||||||
|
const char *expected_result = "Hello, World!Hello, World!";
|
||||||
|
if (strcmp(expected_result, file_content)) {
|
||||||
|
COSMS_TEST_SUCCESS(current_test_name);
|
||||||
|
} else {
|
||||||
|
COSMS_TEST_FAIL(current_test_name, "file content not what was written");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(file_content);
|
||||||
|
}
|
||||||
|
|
||||||
|
current_test_name = "appending to non existing file";
|
||||||
|
COSMS_TEST_START(current_test_name);
|
||||||
|
|
||||||
|
error = cosms_core_file_write("tests/data/append-write-file.txt", file_buffer, strlen(file_buffer) - 1, false);
|
||||||
|
if (error != COSMS_FILE_OK) {
|
||||||
|
COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error));
|
||||||
|
} else {
|
||||||
|
char *file_content;
|
||||||
|
error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL);
|
||||||
|
if (error != COSMS_FILE_OK) {
|
||||||
|
COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error));
|
||||||
|
} else {
|
||||||
|
if (strcmp(file_buffer, file_content)) {
|
||||||
|
COSMS_TEST_SUCCESS(current_test_name);
|
||||||
|
} else {
|
||||||
|
COSMS_TEST_FAIL(current_test_name, "file content not what was written");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(file_content);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
#ifndef COSMS_FILE_TEST
|
#ifndef COSMS_CORE_FILE_TEST
|
||||||
#define COSMS_FILE_TEST
|
#define COSMS_CORE_FILE_TEST
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
void cosms_file_read_test();
|
void cosms_core_file_read_test();
|
||||||
|
void cosms_core_file_write_test();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Loading…
Add table
Reference in a new issue