Add even more tests and some impl improvements from their results

This commit is contained in:
Riyyi
2026-08-25 21:12:18 +02:00
parent 6e81c87501
commit ddaa3846f2
7 changed files with 292 additions and 58 deletions
+188 -29
View File
@@ -3,6 +3,11 @@ package tests
import "core:strings"
import "core:testing"
import "src:chunks"
// -----------------------------------------
// Single file tests
@(test)
single_asset_single_chunk_read_from_path :: proc(t: ^testing.T) {
// Arrange
@@ -11,14 +16,14 @@ single_asset_single_chunk_read_from_path :: proc(t: ^testing.T) {
defer test_state_destroy(&ts, t)
delete_path(&ts, t, "")
create_file(&ts, t, "single_tiny_file", "hello world!")
create_file(&ts, t, "single_asset", "hello world!")
pack(&ts, t, 750)
delete_path(&ts, t, "")
create_file(&ts, t, "single_tiny_file", "hello world from path!")
create_file(&ts, t, "single_asset", "hello world from path!")
// Act
_, read := read(&ts, t, "single_tiny_file")
_, read := read_asset(&ts, t, "single_asset")
// Assert
@@ -33,13 +38,13 @@ single_asset_single_chunk_read_from_chunk :: proc(t: ^testing.T) {
defer test_state_destroy(&ts, t)
delete_path(&ts, t, "")
create_file(&ts, t, "single_tiny_file", "hello world!")
create_file(&ts, t, "single_asset", "hello world!")
pack(&ts, t, 750)
delete_path(&ts, t, "")
// Act
_, read := read(&ts, t, "single_tiny_file")
_, read := read_asset(&ts, t, "single_asset")
// Assert
@@ -56,13 +61,13 @@ single_asset_multiple_chunks_read_from_chunk :: proc(t: ^testing.T) {
delete_path(&ts, t, "")
content := strings.repeat("abcde", 400)
defer delete(content)
create_file(&ts, t, "multi_chunk_file", content)
create_file(&ts, t, "single_asset", content)
pack(&ts, t, 750)
delete_path(&ts, t, "")
// Act
_, read := read(&ts, t, "multi_chunk_file")
_, read := read_asset(&ts, t, "single_asset")
// Assert
@@ -70,6 +75,7 @@ single_asset_multiple_chunks_read_from_chunk :: proc(t: ^testing.T) {
}
// -----------------------------------------
// Multi file tests
@(test)
multiple_assets_single_chunk_read_from_path :: proc(t: ^testing.T) {
@@ -90,9 +96,9 @@ multiple_assets_single_chunk_read_from_path :: proc(t: ^testing.T) {
// Act
_, a := read(&ts, t, "a")
_, b := read(&ts, t, "b")
_, c := read(&ts, t, "c")
_, a := read_asset(&ts, t, "a")
_, b := read_asset(&ts, t, "b")
_, c := read_asset(&ts, t, "c")
// Assert
@@ -117,9 +123,9 @@ multiple_assets_single_chunk_read_from_chunk :: proc(t: ^testing.T) {
// Act
_, a := read(&ts, t, "a")
_, b := read(&ts, t, "b")
_, c := read(&ts, t, "c")
_, a := read_asset(&ts, t, "a")
_, b := read_asset(&ts, t, "b")
_, c := read_asset(&ts, t, "c")
// Assert
@@ -150,9 +156,9 @@ multiple_assets_multiple_chunks_read_from_chunk :: proc(t: ^testing.T) {
// Act
_, a := read(&ts, t, "a")
_, b := read(&ts, t, "b")
_, c := read(&ts, t, "c")
_, a := read_asset(&ts, t, "a")
_, b := read_asset(&ts, t, "b")
_, c := read_asset(&ts, t, "c")
// Assert
@@ -162,6 +168,7 @@ multiple_assets_multiple_chunks_read_from_chunk :: proc(t: ^testing.T) {
}
// -----------------------------------------
// Nested path tests
@(test)
single_asset_single_chunk_nested_path_read_from_path :: proc(t: ^testing.T) {
@@ -171,14 +178,14 @@ single_asset_single_chunk_nested_path_read_from_path :: proc(t: ^testing.T) {
defer test_state_destroy(&ts, t)
delete_path(&ts, t, "")
create_file(&ts, t, "subdir/nested_file", "hello nested!")
create_file(&ts, t, "subdir/nested_asset", "hello nested!")
pack(&ts, t, 750)
delete_path(&ts, t, "")
create_file(&ts, t, "subdir/nested_file", "hello nested from path!")
create_file(&ts, t, "subdir/nested_asset", "hello nested from path!")
// Act
_, read := read(&ts, t, "subdir/nested_file")
_, read := read_asset(&ts, t, "subdir/nested_asset")
// Assert
@@ -193,13 +200,13 @@ single_asset_single_chunk_nested_path_read_from_chunk :: proc(t: ^testing.T) {
defer test_state_destroy(&ts, t)
delete_path(&ts, t, "")
create_file(&ts, t, "subdir/nested_file", "hello nested!")
create_file(&ts, t, "subdir/nested_asset", "hello nested!")
pack(&ts, t, 750)
delete_path(&ts, t, "")
// Act
_, read := read(&ts, t, "subdir/nested_file")
_, read := read_asset(&ts, t, "subdir/nested_asset")
// Assert
@@ -218,13 +225,13 @@ single_asset_multiple_chunks_nested_path_read_from_chunk :: proc(
delete_path(&ts, t, "")
content := strings.repeat("abcde", 50)
defer delete(content)
create_file(&ts, t, "subdir/nested_file", content)
create_file(&ts, t, "subdir/nested_asset", content)
pack(&ts, t, 750)
delete_path(&ts, t, "")
// Act
_, read := read(&ts, t, "subdir/nested_file")
_, read := read_asset(&ts, t, "subdir/nested_asset")
// Assert
@@ -232,6 +239,7 @@ single_asset_multiple_chunks_nested_path_read_from_chunk :: proc(
}
// -----------------------------------------
// Asset exists tests
@(test)
asset_in_path_not_in_chunk_read_matching_file :: proc(t: ^testing.T) {
@@ -248,7 +256,7 @@ asset_in_path_not_in_chunk_read_matching_file :: proc(t: ^testing.T) {
// Act
_, read := read(&ts, t, "target")
_, read := read_asset(&ts, t, "target")
// Assert
@@ -269,7 +277,7 @@ asset_in_chunk_not_in_path_read_matching_file :: proc(t: ^testing.T) {
// Act
_, read := read(&ts, t, "target")
_, read := read_asset(&ts, t, "target")
// Assert
@@ -291,7 +299,7 @@ asset_in_chunk_and_in_path_read_matching_file :: proc(t: ^testing.T) {
// Act
_, read := read(&ts, t, "target")
_, read := read_asset(&ts, t, "target")
// Assert
@@ -305,13 +313,14 @@ asset_in_path_not_in_chunk_read_non_matching_file :: proc(t: ^testing.T) {
ts := test_state_init(t, #procedure)
defer test_state_destroy(&ts, t)
// Empty asset dir
pack(&ts, t, 750)
delete_path(&ts, t, "")
create_file(&ts, t, "target", "hello from path!")
// Act
exists, _ := read(&ts, t, "non_matching")
exists, _ := read_asset(&ts, t, "non_matching")
// Assert
@@ -332,7 +341,7 @@ asset_in_chunk_not_in_path_read_non_matching_file :: proc(t: ^testing.T) {
// Act
exists, _ := read(&ts, t, "non_matching")
exists, _ := read_asset(&ts, t, "non_matching")
// Assert
@@ -354,9 +363,159 @@ asset_in_chunk_and_in_path_read_non_matching_file :: proc(t: ^testing.T) {
// Act
exists, _ := read(&ts, t, "non_matching")
exists, _ := read_asset(&ts, t, "non_matching")
// Assert
testing.expect(t, !exists)
}
// -----------------------------------------
// Error return tests
@(test)
chunk_size_too_small :: proc(t: ^testing.T) {
// Arrange
ts := test_state_init(t, #procedure)
defer test_state_destroy(&ts, t)
delete_path(&ts, t, "")
create_file(&ts, t, "chunk_too_small", "hello world!")
// Act
wa_err, _ := pack(&ts, t, 100, 0)
// Assert
testing.expect(
t,
wa_err == chunks.Write_Error.Chunk_Size_Too_Small,
chunks.error_string(wa_err),
)
}
@(test)
non_chunk_file_read :: proc(t: ^testing.T) {
// Arrange
ts := test_state_init(t, #procedure)
defer test_state_destroy(&ts, t)
create_file(
&ts,
t,
"CHUNK0",
"this is definitely not a chunk file, nope",
true,
)
// Act
err := reader_init_raw(&ts, t)
// Assert
testing.expect(
t,
err == chunks.Read_Error.File_Not_A_Chunk,
chunks.error_string(err),
)
}
@(test)
compression_unimplemented :: proc(t: ^testing.T) {
// Arrange
ts := test_state_init(t, #procedure)
defer test_state_destroy(&ts, t)
delete_path(&ts, t, "")
create_file(&ts, t, "tiny_file", "hello world!")
wa_err, wm_err := pack(&ts, t, 750, 1)
testing.expect(t, wa_err == nil, chunks.error_string(wa_err))
testing.expect(t, wm_err == nil, chunks.error_string(wm_err))
// Act
err := reader_init_raw(&ts, t)
// Assert
testing.expect(
t,
err == chunks.Read_Error.Unimplemented,
chunks.error_string(err),
)
}
@(test)
read_non_existent_asset :: proc(t: ^testing.T) {
// Arrange
ts := test_state_init(t, #procedure)
defer test_state_destroy(&ts, t)
delete_path(&ts, t, "")
create_file(&ts, t, "target", "hello from chunk!")
pack(&ts, t, 750)
delete_path(&ts, t, "")
// Act
_, err := read_asset_raw(&ts, t, "non_matching")
// Assert
testing.expect(
t,
err == chunks.Read_Error.Asset_Not_Exist,
chunks.error_string(err),
)
}
@(test)
path_too_large :: proc(t: ^testing.T) {
// Arrange
ts := test_state_init(t, #procedure)
defer test_state_destroy(&ts, t)
delete_path(&ts, t, "")
dir_segment := strings.repeat("a", 100)
defer delete(dir_segment)
// Nested to avoid OS filename limit of 255
nested_path := strings.concatenate(
{
dir_segment,
"/",
dir_segment,
"/",
dir_segment,
"/",
dir_segment,
"/",
dir_segment,
"/",
"path_too_large",
},
)
defer delete(nested_path)
create_file(&ts, t, nested_path, "hello nested!")
// Act
_, wm_err := pack(&ts, t, 750, 0)
// Assert
testing.expect(
t,
wm_err == chunks.Write_Error.Path_Too_Large,
chunks.error_string(wm_err),
)
}