2024-06-28 03:28:23 +02:00
2024-06-28 18:50:30 +02:00
void log_heap ( ) {
os_spinlock_lock ( heap_lock ) ;
printf ( " \n HEAP: \n " ) ;
Heap_Block * block = heap_head ;
while ( block ! = 0 ) {
printf ( " \t BLOCK @ 0x%I64x, %llu bytes \n " , ( u64 ) block , block - > size ) ;
Heap_Free_Node * node = block - > free_head ;
u64 total_free = 0 ;
while ( node ! = 0 ) {
printf ( " \t \t FREE NODE @ 0x%I64x, %llu bytes \n " , ( u64 ) node , node - > size ) ;
total_free + = node - > size ;
node = node - > next ;
}
printf ( " \t TOTAL FREE: %llu \n \n " , total_free ) ;
block = block - > next ;
}
os_spinlock_unlock ( heap_lock ) ;
}
2024-06-28 03:28:23 +02:00
void test_allocator ( bool do_log_heap ) {
2024-07-01 13:10:06 +02:00
Allocator heap = get_heap_allocator ( ) ;
2024-06-28 03:28:23 +02:00
// Basic allocation and free
2024-07-01 13:10:06 +02:00
int * a = ( int * ) alloc ( heap , sizeof ( int ) ) ;
int * b = ( int * ) alloc ( heap , sizeof ( int ) ) ;
int * c = ( int * ) alloc ( heap , sizeof ( int ) ) ;
2024-06-28 03:28:23 +02:00
* a = 69 ;
* b = 420 ;
* c = 1337 ;
assert ( * a = = 69 , " Test failed: Memory corrupted " ) ;
assert ( * b = = 420 , " Test failed: Memory corrupted " ) ;
assert ( * c = = 1337 , " Test failed: Memory corrupted " ) ;
// Test growing memory
os_grow_program_memory ( 1024 * 1024 * 1000 ) ;
assert ( * a = = 69 , " Test failed: Memory corrupted " ) ;
assert ( * b = = 420 , " Test failed: Memory corrupted " ) ;
assert ( * c = = 1337 , " Test failed: Memory corrupted " ) ;
// Allocate and free large block
2024-07-01 13:10:06 +02:00
void * large_block = alloc ( heap , 1024 * 1024 * 100 ) ;
dealloc ( heap , large_block ) ;
2024-06-28 03:28:23 +02:00
// Allocate multiple small blocks
void * blocks [ 100 ] ;
for ( int i = 0 ; i < 100 ; + + i ) {
2024-07-01 13:10:06 +02:00
blocks [ i ] = alloc ( heap , 128 ) ;
2024-06-28 03:28:23 +02:00
assert ( blocks [ i ] ! = NULL , " Failed to allocate small block " ) ;
}
for ( int i = 0 ; i < 100 ; + + i ) {
2024-07-01 13:10:06 +02:00
dealloc ( heap , blocks [ i ] ) ;
2024-06-28 03:28:23 +02:00
}
// Stress test with various sizes
for ( int i = 1 ; i < = 1000 ; + + i ) {
2024-07-01 13:10:06 +02:00
void * p = alloc ( heap , i * 64 ) ;
2024-06-28 03:28:23 +02:00
assert ( p ! = NULL , " Failed to allocate varying size block " ) ;
2024-07-01 13:10:06 +02:00
dealloc ( heap , p ) ;
2024-06-28 03:28:23 +02:00
}
// Free in reverse order
for ( int i = 0 ; i < 100 ; + + i ) {
2024-07-01 13:10:06 +02:00
blocks [ i ] = alloc ( heap , 128 ) ;
2024-06-28 03:28:23 +02:00
assert ( blocks [ i ] ! = NULL , " Failed to allocate small block " ) ;
}
for ( int i = 99 ; i > = 0 ; - - i ) {
2024-07-01 13:10:06 +02:00
dealloc ( heap , blocks [ i ] ) ;
2024-06-28 03:28:23 +02:00
}
// Test memory integrity with various allocation patterns
int * nums [ 10 ] ;
for ( int i = 0 ; i < 10 ; + + i ) {
2024-07-01 13:10:06 +02:00
nums [ i ] = ( int * ) alloc ( heap , sizeof ( int ) * 10 ) ;
2024-06-28 03:28:23 +02:00
for ( int j = 0 ; j < 10 ; + + j ) {
nums [ i ] [ j ] = i * 10 + j ;
}
}
for ( int i = 0 ; i < 10 ; + + i ) {
for ( int j = 0 ; j < 10 ; + + j ) {
assert ( nums [ i ] [ j ] = = i * 10 + j , " Memory corruption detected " ) ;
}
2024-07-01 13:10:06 +02:00
dealloc ( heap , nums [ i ] ) ;
2024-06-28 03:28:23 +02:00
}
2024-06-28 18:50:30 +02:00
reset_temporary_storage ( ) ;
2024-06-28 03:28:23 +02:00
2024-07-01 13:10:06 +02:00
int * foo = ( int * ) alloc ( temp , 72 ) ;
2024-06-28 03:28:23 +02:00
* foo = 1337 ;
2024-07-01 13:10:06 +02:00
void * bar = alloc ( temp , 69 ) ;
2024-06-28 19:10:29 +02:00
( void ) bar ;
2024-07-01 13:10:06 +02:00
void * baz = alloc ( temp , 420 ) ;
2024-06-28 19:10:29 +02:00
( void ) baz ;
2024-06-28 03:28:23 +02:00
assert ( * foo = = 1337 , " Temp memory corruptada " ) ;
int * old_foo = foo ;
reset_temporary_storage ( ) ;
2024-07-01 13:10:06 +02:00
foo = ( int * ) alloc ( temp , 72 ) ;
2024-06-28 03:28:23 +02:00
assert ( old_foo = = foo , " Temp allocator goof " ) ;
// Repeated Allocation and Free
for ( int i = 0 ; i < 10000 ; + + i ) {
2024-07-01 13:10:06 +02:00
void * temp = alloc ( heap , 128 ) ;
2024-06-28 03:28:23 +02:00
assert ( temp ! = NULL & & " Repeated allocation failed " ) ;
2024-07-01 13:10:06 +02:00
dealloc ( heap , temp ) ;
2024-06-28 03:28:23 +02:00
}
// Mixed Size Allocations
void * mixed_blocks [ 200 ] ;
for ( int i = 0 ; i < 200 ; + + i ) {
if ( i % 2 = = 0 ) {
2024-07-01 13:10:06 +02:00
mixed_blocks [ i ] = alloc ( heap , 128 ) ;
2024-06-28 03:28:23 +02:00
} else {
2024-07-01 13:10:06 +02:00
mixed_blocks [ i ] = alloc ( heap , 1024 * 1024 ) ; // 1MB blocks
2024-06-28 03:28:23 +02:00
}
assert ( mixed_blocks [ i ] ! = NULL & & " Mixed size allocation failed " ) ;
}
for ( int i = 0 ; i < 200 ; + + i ) {
if ( i % 2 = = 0 ) {
2024-07-01 13:10:06 +02:00
dealloc ( heap , mixed_blocks [ i ] ) ;
2024-06-28 03:28:23 +02:00
}
}
for ( int i = 0 ; i < 200 ; + + i ) {
if ( i % 2 ! = 0 ) {
2024-07-01 13:10:06 +02:00
dealloc ( heap , mixed_blocks [ i ] ) ;
2024-06-28 03:28:23 +02:00
}
}
// Fragmentation Stress Test
for ( int i = 0 ; i < 50 ; + + i ) {
2024-07-01 13:10:06 +02:00
blocks [ i ] = alloc ( heap , 256 ) ;
2024-06-28 03:28:23 +02:00
assert ( blocks [ i ] ! = NULL & & " Failed to allocate small block for fragmentation test " ) ;
}
for ( int i = 0 ; i < 50 ; i + = 2 ) {
2024-07-01 13:10:06 +02:00
dealloc ( heap , blocks [ i ] ) ;
2024-06-28 03:28:23 +02:00
}
for ( int i = 50 ; i < 100 ; + + i ) {
2024-07-01 13:10:06 +02:00
blocks [ i ] = alloc ( heap , 128 ) ;
2024-06-28 03:28:23 +02:00
assert ( blocks [ i ] ! = NULL & & " Failed to allocate small block in fragmented heap " ) ;
}
for ( int i = 50 ; i < 100 ; + + i ) {
2024-07-01 13:10:06 +02:00
dealloc ( heap , blocks [ i ] ) ;
2024-06-28 03:28:23 +02:00
}
for ( int i = 1 ; i < 50 ; i + = 2 ) {
2024-07-01 13:10:06 +02:00
dealloc ( heap , blocks [ i ] ) ;
2024-06-28 03:28:23 +02:00
}
if ( do_log_heap ) log_heap ( ) ;
}
void test_thread_proc1 ( Thread * t ) {
os_sleep ( 5 ) ;
printf ( " Hello from thread %llu \n " , t - > id ) ;
os_sleep ( 5 ) ;
printf ( " Hello from thread %llu \n " , t - > id ) ;
os_sleep ( 5 ) ;
printf ( " Hello from thread %llu \n " , t - > id ) ;
os_sleep ( 5 ) ;
printf ( " Hello from thread %llu \n " , t - > id ) ;
os_sleep ( 5 ) ;
printf ( " Hello from thread %llu \n " , t - > id ) ;
}
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
2024-06-28 03:28:23 +02:00
void test_threads ( ) {
2024-07-01 13:10:06 +02:00
Thread * t = os_make_thread ( test_thread_proc1 , get_heap_allocator ( ) ) ;
2024-06-28 03:28:23 +02:00
os_start_thread ( t ) ;
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
os_sleep ( 20 ) ;
2024-06-28 03:28:23 +02:00
printf ( " This should be printed in middle of thread execution \n " ) ;
os_join_thread ( t ) ;
printf ( " Thread is joined \n " ) ;
Mutex_Handle m = os_make_mutex ( ) ;
os_lock_mutex ( m ) ;
os_unlock_mutex ( m ) ;
}
void test_allocator_threaded ( Thread * t ) {
2024-07-01 13:10:06 +02:00
Allocator heap = get_heap_allocator ( ) ;
2024-06-28 03:28:23 +02:00
for ( int i = 0 ; i < 1000 ; + + i ) {
2024-07-01 13:10:06 +02:00
void * temp = alloc ( heap , 128 ) ;
2024-06-28 03:28:23 +02:00
assert ( temp ! = NULL & & " Repeated allocation failed " ) ;
2024-07-01 13:10:06 +02:00
dealloc ( heap , temp ) ;
2024-06-28 03:28:23 +02:00
}
void * mixed_blocks [ 40 ] ;
for ( int i = 0 ; i < 40 ; + + i ) {
if ( i % 2 = = 0 ) {
2024-07-01 13:10:06 +02:00
mixed_blocks [ i ] = alloc ( heap , 128 ) ;
2024-06-28 03:28:23 +02:00
} else {
2024-07-01 13:10:06 +02:00
mixed_blocks [ i ] = alloc ( heap , 1024 * 1024 ) ; // 1MB blocks
2024-06-28 03:28:23 +02:00
}
assert ( mixed_blocks [ i ] ! = NULL & & " Mixed size allocation failed " ) ;
}
for ( int i = 0 ; i < 40 ; + + i ) {
if ( i % 2 = = 0 ) {
2024-07-01 13:10:06 +02:00
dealloc ( heap , mixed_blocks [ i ] ) ;
2024-06-28 03:28:23 +02:00
}
}
for ( int i = 0 ; i < 40 ; + + i ) {
if ( i % 2 ! = 0 ) {
2024-07-01 13:10:06 +02:00
dealloc ( heap , mixed_blocks [ i ] ) ;
2024-06-28 03:28:23 +02:00
}
}
}
2024-06-28 12:07:02 +02:00
void test_strings ( ) {
2024-06-28 18:50:30 +02:00
// Test length_of_null_terminated_string
assert ( length_of_null_terminated_string ( " Test " ) = = 4 , " Failed: length_of_null_terminated_string " ) ;
assert ( length_of_null_terminated_string ( " " ) = = 0 , " Failed: length_of_null_terminated_string " ) ;
2024-07-01 13:10:06 +02:00
Allocator heap = get_heap_allocator ( ) ;
2024-06-28 18:50:30 +02:00
// Test alloc_string and dealloc_string
2024-07-01 13:10:06 +02:00
string alloc_str = alloc_string ( heap , 10 ) ;
2024-06-28 18:50:30 +02:00
assert ( alloc_str . data ! = NULL , " Failed: alloc_string " ) ;
assert ( alloc_str . count = = 10 , " Failed: alloc_string " ) ;
2024-07-01 13:10:06 +02:00
dealloc_string ( heap , alloc_str ) ;
2024-06-28 18:50:30 +02:00
// Test string_concat
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
string str1 = fixed_string ( " Hello, " ) ;
string str2 = fixed_string ( " World! " ) ;
2024-07-01 13:10:06 +02:00
string concat_str = string_concat ( str1 , str2 , heap ) ;
2024-06-28 18:50:30 +02:00
assert ( concat_str . count = = str1 . count + str2 . count , " Failed: string_concat " ) ;
assert ( memcmp ( concat_str . data , " Hello, World! " , concat_str . count ) = = 0 , " Failed: string_concat " ) ;
2024-07-01 13:10:06 +02:00
dealloc_string ( heap , concat_str ) ;
2024-06-28 18:50:30 +02:00
// Test convert_to_null_terminated_string
2024-07-01 13:10:06 +02:00
char * cstr = convert_to_null_terminated_string ( str1 , heap ) ;
2024-06-28 18:50:30 +02:00
assert ( strcmp ( cstr , " Hello, " ) = = 0 , " Failed: convert_to_null_terminated_string " ) ;
2024-07-01 13:10:06 +02:00
dealloc ( heap , cstr ) ;
2024-06-28 18:50:30 +02:00
// Test temp_convert_to_null_terminated_string
cstr = temp_convert_to_null_terminated_string ( str2 ) ;
assert ( strcmp ( cstr , " World! " ) = = 0 , " Failed: temp_convert_to_null_terminated_string " ) ;
// Test sprint
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
string format_str = fixed_string ( " Number: %d " ) ;
2024-07-01 13:10:06 +02:00
string formatted_str = sprint ( heap , format_str , 42 ) ;
char * formatted_cstr = convert_to_null_terminated_string ( formatted_str , heap ) ;
2024-06-28 18:50:30 +02:00
assert ( strcmp ( formatted_cstr , " Number: 42 " ) = = 0 , " Failed: sprint " ) ;
2024-07-01 13:10:06 +02:00
dealloc ( heap , formatted_str . data ) ;
dealloc ( heap , formatted_cstr ) ;
2024-06-28 18:50:30 +02:00
// Test tprint
string temp_formatted_str = tprint ( format_str , 100 ) ;
formatted_cstr = temp_convert_to_null_terminated_string ( temp_formatted_str ) ;
assert ( strcmp ( formatted_cstr , " Number: 100 " ) = = 0 , " Failed: tprint " ) ;
// Test print and printf (visual inspection)
printf ( " Expected output: Hello, World! \n " ) ;
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
print ( " Hello, %s! \n " , fixed_string ( " World " ) ) ;
2024-06-28 18:50:30 +02:00
printf ( " Expected output: Number: 1234 \n " ) ;
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
print ( fixed_string ( " Number: %d \n " ) , 1234 ) ;
2024-06-28 18:50:30 +02:00
printf ( " Expected output: Number: 1234 \n " ) ;
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
print ( fixed_string ( " Number: %d \n " ) , 1234 ) ;
2024-06-28 18:50:30 +02:00
printf ( " Expected output: Mixed values: 42 and 3.14 \n " ) ;
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
print ( fixed_string ( " Mixed values: %d and %.2f \n " ) , 42 , 3.14 ) ;
2024-06-28 18:50:30 +02:00
// This should fail assert and print descriptive error
//printf("Expected output (printf): Hello, World!\n");
//printf("Hello, %cs!\n", 5);
// This should fail assert and print descriptive error
// printf("Expected output (printf): Hello, World!\n");
// printf("Hello, %s!\n", "World");
printf ( " Expected output (printf): Hello, World! \n " ) ;
printf ( " Hello, %s! \n " , cstr ( " World " ) ) ;
printf ( " Expected output (printf): Number: 5678 \n " ) ;
printf ( " Number: %d \n " , 5678 ) ;
printf ( " Expected output (printf): Mixed values: 99 and 2.71 \n " ) ;
printf ( " Mixed values: %d and %.2f \n " , 99 , 2.71 ) ;
// Test handling of empty strings
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
string empty_str = fixed_string ( " " ) ;
2024-07-01 13:10:06 +02:00
string concat_empty_str = string_concat ( empty_str , empty_str , heap ) ;
2024-06-28 18:50:30 +02:00
assert ( concat_empty_str . count = = 0 , " Failed: string_concat with empty strings " ) ;
2024-07-01 13:10:06 +02:00
dealloc_string ( heap , concat_empty_str ) ;
2024-06-28 18:50:30 +02:00
// Test very large strings (performance test)
2024-07-01 13:10:06 +02:00
string large_str1 = alloc_string ( heap , 1024 * 1024 ) ;
string large_str2 = alloc_string ( heap , 1024 * 1024 ) ;
string large_concat_str = string_concat ( large_str1 , large_str2 , heap ) ;
2024-06-28 18:50:30 +02:00
assert ( large_concat_str . count = = 2 * 1024 * 1024 , " Failed: large string_concat " ) ;
2024-07-01 13:10:06 +02:00
dealloc_string ( heap , large_str1 ) ;
dealloc_string ( heap , large_str2 ) ;
dealloc_string ( heap , large_concat_str ) ;
2024-06-28 18:50:30 +02:00
// Test string with special characters
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
string special_char_str = fixed_string ( " Special chars: \n \t \r " ) ;
2024-07-01 13:10:06 +02:00
cstr = convert_to_null_terminated_string ( special_char_str , heap ) ;
2024-06-28 18:50:30 +02:00
assert ( strcmp ( cstr , " Special chars: \n \t \r " ) = = 0 , " Failed: special character string " ) ;
2024-07-01 13:10:06 +02:00
dealloc ( heap , cstr ) ;
2024-06-28 18:50:30 +02:00
string a = tprintf ( " Hello, %cs! \n " , " balls " ) ;
string balls1 = string_view ( a , 7 , 5 ) ;
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
string balls2 = fixed_string ( " balls " ) ;
2024-06-28 18:50:30 +02:00
assert ( strings_match ( balls1 , balls2 ) , " String match failed " ) ;
assert ( ! strings_match ( balls1 , a ) , " String match failed " ) ;
2024-06-28 12:07:02 +02:00
}
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
void test_file_io ( ) {
# if TARGET_OS == WINDOWS
// Test win32_fixed_utf8_to_null_terminated_wide
string utf8_str = fixed_string ( " Test " ) ;
2024-07-01 13:10:06 +02:00
u16 * wide_str = win32_fixed_utf8_to_null_terminated_wide ( utf8_str , get_heap_allocator ( ) ) ;
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
assert ( wide_str ! = NULL , " Failed: win32_fixed_utf8_to_null_terminated_wide " ) ;
assert ( wide_str [ 4 ] = = 0 , " Failed: win32_fixed_utf8_to_null_terminated_wide " ) ;
2024-07-01 13:10:06 +02:00
dealloc ( get_heap_allocator ( ) , wide_str ) ;
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
// Test temp_win32_fixed_utf8_to_null_terminated_wide
wide_str = temp_win32_fixed_utf8_to_null_terminated_wide ( utf8_str ) ;
assert ( wide_str ! = NULL , " Failed: temp_win32_fixed_utf8_to_null_terminated_wide " ) ;
assert ( wide_str [ 4 ] = = 0 , " Failed: temp_win32_fixed_utf8_to_null_terminated_wide " ) ;
# endif
2024-06-28 18:50:30 +02:00
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
File file = OS_INVALID_FILE ;
os_file_close ( file ) ;
// Test os_file_open and os_file_close
file = os_file_open ( fixed_string ( " test.txt " ) , O_WRITE | O_CREATE ) ;
assert ( file ! = OS_INVALID_FILE , " Failed: os_file_open (write/create) " ) ;
os_file_close ( file ) ;
// Test os_file_write_string and os_file_read
string hello_world_write = fixed_string ( " Hello, World! " ) ;
file = os_file_open ( fixed_string ( " test.txt " ) , O_WRITE | O_CREATE ) ;
assert ( file ! = OS_INVALID_FILE , " Failed: os_file_open (write/create) " ) ;
bool write_result = os_file_write_string ( file , hello_world_write ) ;
assert ( write_result , " Failed: os_file_write_string " ) ;
os_file_close ( file ) ;
file = os_file_open ( fixed_string ( " test.txt " ) , O_READ ) ;
assert ( file ! = OS_INVALID_FILE , " Failed: os_file_open (read) " ) ;
string hello_world_read = talloc_string ( hello_world_write . count ) ;
bool read_result = os_file_read ( file , hello_world_read . data , hello_world_read . count , & hello_world_read . count ) ;
assert ( read_result , " Failed: os_file_read %d " , GetLastError ( ) ) ;
assert ( strings_match ( hello_world_read , hello_world_write ) , " Failed: os_file_read write/read mismatch " ) ;
os_file_close ( file ) ;
// Test os_file_write_bytes
file = os_file_open ( fixed_string ( " test_bytes.txt " ) , O_WRITE | O_CREATE ) ;
assert ( file ! = OS_INVALID_FILE , " Failed: os_file_open (write/create) " ) ;
int int_data = 42 ;
write_result = os_file_write_bytes ( file , & int_data , sizeof ( int ) ) ;
assert ( write_result , " Failed: os_file_write_bytes " ) ;
os_file_close ( file ) ;
// Test os_read_entire_file and os_write_entire_file
string write_data = fixed_string ( " Entire file test " ) ;
bool write_entire_result = os_write_entire_file ( fixed_string ( " entire_test.txt " ) , write_data ) ;
assert ( write_entire_result , " Failed: os_write_entire_file " ) ;
2024-07-01 13:10:06 +02:00
Allocator heap = get_heap_allocator ( ) ;
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
string read_data ;
2024-07-01 13:10:06 +02:00
bool read_entire_result = os_read_entire_file ( fixed_string ( " entire_test.txt " ) , & read_data , heap ) ;
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
assert ( read_entire_result , " Failed: os_read_entire_file " ) ;
assert ( strings_match ( read_data , write_data ) , " Failed: os_read_entire_file write/read mismatch " ) ;
assert ( memcmp ( read_data . data , write_data . data , write_data . count ) = = 0 , " Failed: os_read_entire_file (content mismatch) " ) ;
2024-07-01 13:10:06 +02:00
dealloc ( heap , read_data . data ) ;
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
// Test fprint
File balls = os_file_open ( fixed_string ( " balls.txt " ) , O_WRITE | O_CREATE ) ;
assert ( balls ! = OS_INVALID_FILE , " Failed: Could not create balls.txt " ) ;
fprint ( balls , " Hello, %cs! " , " Balls " ) ;
os_file_close ( balls ) ;
string hello_balls ;
2024-07-01 13:10:06 +02:00
read_entire_result = os_read_entire_file ( fixed_string ( " balls.txt " ) , & hello_balls , heap ) ;
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
assert ( read_entire_result , " Failed: could not read balls.txt " ) ;
assert ( strings_match ( hello_balls , fixed_string ( " Hello, Balls! " ) ) , " Failed: balls read/write mismatch. Expected 'Hello, Balls!', got '%s' " , hello_balls ) ;
2024-07-01 02:14:08 +02:00
u64 integers [ 4096 ] ;
for ( u64 i = 0 ; i < 4096 ; i + + ) {
integers [ i ] = get_random ( ) ;
}
string integers_data ;
integers_data . data = ( u8 * ) integers ;
integers_data . count = 4096 * sizeof ( u64 ) ;
bool ok = os_write_entire_file ( fxstr ( " integers " ) , integers_data ) ;
assert ( ok , " write integers fail " ) ;
string integers_read ;
2024-07-01 13:10:06 +02:00
ok = os_read_entire_file ( fxstr ( " integers " ) , & integers_read , heap ) ;
2024-07-01 02:14:08 +02:00
assert ( ok , " read integers fail " ) ;
u64 * new_integers = ( u64 * ) integers_data . data ;
assert ( integers_read . count = = integers_data . count , " Failed: big file read/write mismatch. Read was %d and written was %d " , integers_read . count , integers_data . count ) ;
assert ( strings_match ( integers_data , integers_read ) , " Failed: big file read/write mismatch " ) ;
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
// Clean up test files
bool delete_ok = false ;
delete_ok = os_file_delete ( fixed_string ( " test.txt " ) ) ;
assert ( delete_ok , " Failed: could not delete test.txt " ) ;
delete_ok = os_file_delete ( fixed_string ( " test_bytes.txt " ) ) ;
assert ( delete_ok , " Failed: could not delete test_bytes.txt " ) ;
delete_ok = os_file_delete ( fixed_string ( " entire_test.txt " ) ) ;
assert ( delete_ok , " Failed: could not delete entire_test.txt " ) ;
delete_ok = os_file_delete ( fixed_string ( " balls.txt " ) ) ;
assert ( delete_ok , " Failed: could not delete balls.txt " ) ;
2024-07-01 13:10:06 +02:00
delete_ok = os_file_delete ( fixed_string ( " integers.txt " ) ) ;
assert ( delete_ok , " Failed: could not delete integers.txt " ) ;
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
}
2024-06-28 18:50:30 +02:00
2024-06-28 03:28:23 +02:00
void oogabooga_run_tests ( ) {
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
printf ( " Testing allocator... " ) ;
2024-06-28 03:28:23 +02:00
test_allocator ( true ) ;
printf ( " OK! \n " ) ;
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
printf ( " Testing threads... " ) ;
2024-06-28 03:28:23 +02:00
test_threads ( ) ;
printf ( " OK! \n " ) ;
2024-06-28 12:07:02 +02:00
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
printf ( " Testing strings... " ) ;
2024-06-28 12:07:02 +02:00
test_strings ( ) ;
printf ( " OK! \n " ) ;
2024-06-28 03:28:23 +02:00
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
printf ( " Thread bombing allocator... " ) ;
2024-06-28 03:28:23 +02:00
Thread * threads [ 100 ] ;
for ( int i = 0 ; i < 100 ; i + + ) {
2024-07-01 13:10:06 +02:00
threads [ i ] = os_make_thread ( test_allocator_threaded , get_heap_allocator ( ) ) ;
2024-06-28 03:28:23 +02:00
os_start_thread ( threads [ i ] ) ;
}
for ( int i = 0 ; i < 100 ; i + + ) {
os_join_thread ( threads [ i ] ) ;
}
printf ( " OK! \n " ) ;
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
printf ( " Testing file IO... " ) ;
test_file_io ( ) ;
printf ( " OK! \n " ) ;
2024-06-28 03:28:23 +02:00
}