2025-12-10 20:39:31 +01:00
# include "test.h"
# include "unit/file.h"
# include <cosms-core/file.h>
2025-12-09 22:07:16 +01:00
# include <string.h>
# include <stdlib.h>
2025-12-10 20:39:31 +01:00
void cosms_core_file_read_test ( ) {
2025-12-11 00:11:23 +01:00
COSMS_CORE_TEST_HEADER ( " file read " ) ;
2025-12-09 22:07:16 +01:00
const char * current_test_name = " reading small file " ;
2025-12-11 00:11:23 +01:00
COSMS_CORE_TEST_START ( current_test_name ) ;
2025-12-09 22:07:16 +01:00
unsigned long long file_size ;
char * file_buffer ;
2025-12-10 20:43:16 +01:00
CosmsCoreFileError error = cosms_core_file_read ( " tests/data/small-file.txt " , & file_buffer , & file_size ) ;
2025-12-11 00:11:23 +01:00
if ( cosms_core_test_assert ( current_test_name , error = = COSMS_CORE_FILE_OK , cosms_core_file_error_string ( error ) ) ) {
2025-12-09 22:07:16 +01:00
const char * expected_result = " Hello, World! " ;
2025-12-11 00:11:23 +01:00
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 ) ;
2025-12-09 22:07:16 +01:00
}
}
free ( file_buffer ) ;
current_test_name = " reading large file " ;
2025-12-11 00:11:23 +01:00
COSMS_CORE_TEST_START ( current_test_name ) ;
2025-12-09 22:07:16 +01:00
file_size = 0 ;
file_buffer = NULL ;
error = cosms_core_file_read ( " tests/data/large-file.txt " , & file_buffer , & file_size ) ;
2025-12-11 00:11:23 +01:00
if ( cosms_core_test_assert ( current_test_name , error = = COSMS_CORE_FILE_OK , cosms_core_file_error_string ( error ) ) ) {
2025-12-09 22:07:16 +01:00
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 ' ;
2025-12-11 00:11:23 +01:00
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 ) ;
2025-12-09 22:07:16 +01:00
}
free ( expected_result ) ;
}
free ( file_buffer ) ;
current_test_name = " reading non existing file " ;
2025-12-11 00:11:23 +01:00
COSMS_CORE_TEST_START ( current_test_name ) ;
2025-12-09 22:07:16 +01:00
file_size = 0 ;
file_buffer = NULL ;
error = cosms_core_file_read ( " non-existing-file.cosms " , & file_buffer , & file_size ) ;
2025-12-11 00:11:23 +01:00
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 ) ;
2025-12-09 22:07:16 +01:00
}
free ( file_buffer ) ;
2025-12-10 20:39:31 +01:00
}
void cosms_core_file_write_test ( ) {
2025-12-11 00:11:23 +01:00
COSMS_CORE_TEST_HEADER ( " file write " ) ;
2025-12-10 20:39:31 +01:00
const char * current_test_name = " writting small file " ;
2025-12-11 00:11:23 +01:00
COSMS_CORE_TEST_START ( current_test_name ) ;
2025-12-10 20:39:31 +01:00
char * file_buffer = " Hello, World! " ;
2025-12-11 00:11:23 +01:00
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 ) ) ) {
2025-12-10 20:39:31 +01:00
char * file_content ;
error = cosms_core_file_read ( " tests/data/small-write-file.txt " , & file_content , NULL ) ;
2025-12-11 00:11:23 +01:00
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 ) ;
2025-12-10 20:39:31 +01:00
}
}
free ( file_content ) ;
}
current_test_name = " writting big file " ;
2025-12-11 00:11:23 +01:00
COSMS_CORE_TEST_START ( current_test_name ) ;
2025-12-10 20:39:31 +01:00
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 ) ;
2025-12-11 00:11:23 +01:00
if ( cosms_core_test_assert ( current_test_name , error = = COSMS_CORE_FILE_OK , cosms_core_file_error_string ( error ) ) ) {
2025-12-10 20:39:31 +01:00
char * file_content ;
error = cosms_core_file_read ( " tests/data/large-write-file.txt " , & file_content , NULL ) ;
2025-12-11 00:11:23 +01:00
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 ) ;
2025-12-10 20:39:31 +01:00
}
}
free ( file_content ) ;
}
free ( large_file_buffer ) ;
current_test_name = " appending file " ;
2025-12-11 00:11:23 +01:00
COSMS_CORE_TEST_START ( current_test_name ) ;
2025-12-10 20:39:31 +01:00
2025-12-11 00:11:23 +01:00
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 ) ) ) {
2025-12-10 20:39:31 +01:00
char * file_content ;
error = cosms_core_file_read ( " tests/data/small-write-file.txt " , & file_content , NULL ) ;
2025-12-11 00:11:23 +01:00
if ( cosms_core_test_assert ( current_test_name , error = = COSMS_CORE_FILE_OK , cosms_core_file_error_string ( error ) ) ) {
2025-12-10 20:39:31 +01:00
const char * expected_result = " Hello, World!Hello, World! " ;
2025-12-11 00:11:23 +01:00
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 ) ;
2025-12-10 20:39:31 +01:00
}
}
2025-12-11 00:11:23 +01:00
2025-12-10 20:39:31 +01:00
free ( file_content ) ;
}
current_test_name = " appending to non existing file " ;
2025-12-11 00:11:23 +01:00
COSMS_CORE_TEST_START ( current_test_name ) ;
2025-12-10 20:39:31 +01:00
2025-12-11 00:11:23 +01:00
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 ) ) ) {
2025-12-10 20:39:31 +01:00
char * file_content ;
2025-12-11 00:11:23 +01:00
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 ) ;
2025-12-10 20:39:31 +01:00
}
}
free ( file_content ) ;
}
2025-12-11 22:38:30 +01:00
}
void cosms_core_file_delete_test ( ) {
COSMS_CORE_TEST_HEADER ( " file delete " ) ;
const char * current_test_name = " deleting small file " ;
COSMS_CORE_TEST_START ( current_test_name ) ;
CosmsCoreFileError error = cosms_core_file_delete ( " tests/data/small-write-file.txt " ) ;
if ( cosms_core_test_assert ( current_test_name , error = = COSMS_CORE_FILE_OK , cosms_core_file_error_string ( error ) ) ) {
char * file_content = NULL ;
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_NOT_FOUND , cosms_core_file_error_string ( error ) ) ) {
COSMS_CORE_TEST_SUCCESS ( current_test_name ) ;
}
if ( file_content ! = NULL ) {
free ( file_content ) ;
}
}
current_test_name = " deleting large file " ;
COSMS_CORE_TEST_START ( current_test_name ) ;
error = cosms_core_file_delete ( " tests/data/append-write-file.txt " ) ;
if ( cosms_core_test_assert ( current_test_name , error = = COSMS_CORE_FILE_OK , cosms_core_file_error_string ( error ) ) ) {
char * file_content = NULL ;
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_NOT_FOUND , cosms_core_file_error_string ( error ) ) ) {
COSMS_CORE_TEST_SUCCESS ( current_test_name ) ;
}
if ( file_content ! = NULL ) {
free ( file_content ) ;
}
}
current_test_name = " deleting non existing file " ;
COSMS_CORE_TEST_START ( current_test_name ) ;
error = cosms_core_file_delete ( " non-existing-file.cosms " ) ;
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 ) ;
}
2025-12-09 22:07:16 +01:00
}