feat(tests): added assert function for tests
This commit is contained in:
parent
febf56912e
commit
3f0299bef1
5 changed files with 54 additions and 76 deletions
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-write-file.txt
Normal file
BIN
tests/data/large-write-file.txt
Normal file
Binary file not shown.
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!
|
||||
18
tests/test.h
18
tests/test.h
|
|
@ -2,11 +2,21 @@
|
|||
#define COSMS_TEST
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define COSMS_TEST_HEADER(test_module_name) printf("----------------------------------------\n%s\n----------------------------------------\n", test_module_name);
|
||||
#define COSMS_CORE_TEST_HEADER(test_module_name) printf("----------------------------------------\n%s\n----------------------------------------\n", test_module_name);
|
||||
|
||||
#define COSMS_TEST_START(test_name) printf("[*] running test %s...\n", test_name)
|
||||
#define COSMS_TEST_SUCCESS(test_name) printf("[+] test %s completed successfully!\n", test_name);
|
||||
#define COSMS_TEST_FAIL(test_name, error) printf("[-] test %s failed with error: %s\n", test_name, error)
|
||||
#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
|
||||
|
|
@ -7,36 +7,30 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
void cosms_core_file_read_test() {
|
||||
COSMS_TEST_HEADER("file read");
|
||||
COSMS_CORE_TEST_HEADER("file read");
|
||||
|
||||
const char *current_test_name = "reading small file";
|
||||
COSMS_TEST_START(current_test_name);
|
||||
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 (error != COSMS_CORE_FILE_OK) {
|
||||
COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error));
|
||||
} else {
|
||||
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 (file_size != strlen(expected_result) || strcmp(expected_result, file_buffer)) {
|
||||
COSMS_TEST_FAIL(current_test_name, "result does not match expected result");
|
||||
} else {
|
||||
COSMS_TEST_SUCCESS(current_test_name);
|
||||
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_TEST_START(current_test_name);
|
||||
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 (error != COSMS_CORE_FILE_OK) {
|
||||
COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error));
|
||||
} else {
|
||||
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) {
|
||||
|
|
@ -48,10 +42,8 @@ void cosms_core_file_read_test() {
|
|||
memset(expected_result, 'a', expected_size);
|
||||
expected_result[expected_size] = '\0';
|
||||
|
||||
if (file_size != expected_size || strcmp(expected_result, file_buffer)) {
|
||||
COSMS_TEST_FAIL(current_test_name, "result does not match expected result");
|
||||
} else {
|
||||
COSMS_TEST_SUCCESS(current_test_name);
|
||||
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);
|
||||
|
|
@ -60,40 +52,32 @@ void cosms_core_file_read_test() {
|
|||
free(file_buffer);
|
||||
|
||||
current_test_name = "reading non existing file";
|
||||
COSMS_TEST_START(current_test_name);
|
||||
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 (error == COSMS_CORE_FILE_NOT_FOUND) {
|
||||
COSMS_TEST_SUCCESS(current_test_name);
|
||||
} else {
|
||||
COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error));
|
||||
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_TEST_HEADER("file write");
|
||||
COSMS_CORE_TEST_HEADER("file write");
|
||||
|
||||
const char *current_test_name = "writting small file";
|
||||
COSMS_TEST_START(current_test_name);
|
||||
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) - 1, true);
|
||||
if (error != COSMS_CORE_FILE_OK) {
|
||||
COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error));
|
||||
} else {
|
||||
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 (error != COSMS_CORE_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");
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -101,7 +85,7 @@ void cosms_core_file_write_test() {
|
|||
}
|
||||
|
||||
current_test_name = "writting big file";
|
||||
COSMS_TEST_START(current_test_name);
|
||||
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));
|
||||
|
|
@ -115,18 +99,12 @@ void cosms_core_file_write_test() {
|
|||
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_CORE_FILE_OK) {
|
||||
COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error));
|
||||
} else {
|
||||
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 (error != COSMS_CORE_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");
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -136,44 +114,32 @@ void cosms_core_file_write_test() {
|
|||
free(large_file_buffer);
|
||||
|
||||
current_test_name = "appending file";
|
||||
COSMS_TEST_START(current_test_name);
|
||||
COSMS_CORE_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_CORE_FILE_OK) {
|
||||
COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error));
|
||||
} else {
|
||||
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 (error != COSMS_CORE_FILE_OK) {
|
||||
COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error));
|
||||
} else {
|
||||
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 (strcmp(expected_result, file_content)) {
|
||||
COSMS_TEST_SUCCESS(current_test_name);
|
||||
} else {
|
||||
COSMS_TEST_FAIL(current_test_name, "file content not what was written");
|
||||
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_TEST_START(current_test_name);
|
||||
COSMS_CORE_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_CORE_FILE_OK) {
|
||||
COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error));
|
||||
} else {
|
||||
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/small-write-file.txt", &file_content, NULL);
|
||||
if (error != COSMS_CORE_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");
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue