package tests import "core:strings" import "core:testing" import "gram:chunks" // ----------------------------------------- // Single file tests @(test) single_asset_single_chunk_read_from_path :: 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, "single_asset", "hello world!") pack(&ts, t, 750) delete_path(&ts, t, "") create_file(&ts, t, "single_asset", "hello world from path!") // Act _, read := read_asset(&ts, t, "single_asset") // Assert testing.expect_value(t, read, "hello world from path!") } @(test) single_asset_single_chunk_read_from_chunk :: 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, "single_asset", "hello world!") pack(&ts, t, 750) delete_path(&ts, t, "") // Act _, read := read_asset(&ts, t, "single_asset") // Assert testing.expect_value(t, read, "hello world!") } @(test) single_asset_multiple_chunks_read_from_chunk :: proc(t: ^testing.T) { // Arrange ts := test_state_init(t, #procedure) defer test_state_destroy(&ts, t) delete_path(&ts, t, "") content := strings.repeat("abcde", 400) defer delete(content) create_file(&ts, t, "single_asset", content) pack(&ts, t, 750) delete_path(&ts, t, "") // Act _, read := read_asset(&ts, t, "single_asset") // Assert testing.expect_value(t, read, content) } // ----------------------------------------- // Multi file tests @(test) multiple_assets_single_chunk_read_from_path :: 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, "a", "hello a!") create_file(&ts, t, "b", "hello b!") create_file(&ts, t, "c", "hello c!") pack(&ts, t, 1750) delete_path(&ts, t, "") create_file(&ts, t, "a", "hello a from path!") create_file(&ts, t, "b", "hello b from path!") create_file(&ts, t, "c", "hello c from path!") // Act _, a := read_asset(&ts, t, "a") _, b := read_asset(&ts, t, "b") _, c := read_asset(&ts, t, "c") // Assert testing.expect_value(t, a, "hello a from path!") testing.expect_value(t, b, "hello b from path!") testing.expect_value(t, c, "hello c from path!") } @(test) multiple_assets_single_chunk_read_from_chunk :: 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, "a", "hello a!") create_file(&ts, t, "b", "hello b!") create_file(&ts, t, "c", "hello c!") pack(&ts, t, 1750) delete_path(&ts, t, "") // Act _, a := read_asset(&ts, t, "a") _, b := read_asset(&ts, t, "b") _, c := read_asset(&ts, t, "c") // Assert testing.expect_value(t, a, "hello a!") testing.expect_value(t, b, "hello b!") testing.expect_value(t, c, "hello c!") } @(test) multiple_assets_multiple_chunks_read_from_chunk :: proc(t: ^testing.T) { // Arrange ts := test_state_init(t, #procedure) defer test_state_destroy(&ts, t) delete_path(&ts, t, "") content_a := strings.repeat("a", 700) defer delete(content_a) content_b := strings.repeat("b", 700) defer delete(content_b) content_c := strings.repeat("c", 700) defer delete(content_c) create_file(&ts, t, "a", content_a) create_file(&ts, t, "b", content_b) create_file(&ts, t, "c", content_c) pack(&ts, t, 1750) delete_path(&ts, t, "") // Act _, a := read_asset(&ts, t, "a") _, b := read_asset(&ts, t, "b") _, c := read_asset(&ts, t, "c") // Assert testing.expect_value(t, a, content_a) testing.expect_value(t, b, content_b) testing.expect_value(t, c, content_c) } // ----------------------------------------- // Nested path tests @(test) single_asset_single_chunk_nested_path_read_from_path :: 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, "subdir/nested_asset", "hello nested!") pack(&ts, t, 750) delete_path(&ts, t, "") create_file(&ts, t, "subdir/nested_asset", "hello nested from path!") // Act _, read := read_asset(&ts, t, "subdir/nested_asset") // Assert testing.expect_value(t, read, "hello nested from path!") } @(test) single_asset_single_chunk_nested_path_read_from_chunk :: 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, "subdir/nested_asset", "hello nested!") pack(&ts, t, 750) delete_path(&ts, t, "") // Act _, read := read_asset(&ts, t, "subdir/nested_asset") // Assert testing.expect_value(t, read, "hello nested!") } @(test) single_asset_multiple_chunks_nested_path_read_from_chunk :: proc( t: ^testing.T, ) { // Arrange ts := test_state_init(t, #procedure) defer test_state_destroy(&ts, t) delete_path(&ts, t, "") content := strings.repeat("abcde", 50) defer delete(content) create_file(&ts, t, "subdir/nested_asset", content) pack(&ts, t, 750) delete_path(&ts, t, "") // Act _, read := read_asset(&ts, t, "subdir/nested_asset") // Assert testing.expect_value(t, read, content) } // ----------------------------------------- // Asset exists tests @(test) asset_in_path_not_in_chunk_read_matching_file :: 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", "hello from chunk!") pack(&ts, t, 750) delete_path(&ts, t, "") create_file(&ts, t, "target", "hello from path!") // Act _, read := read_asset(&ts, t, "target") // Assert testing.expect_value(t, read, "hello from path!") } @(test) asset_in_chunk_not_in_path_read_matching_file :: 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 _, read := read_asset(&ts, t, "target") // Assert testing.expect_value(t, read, "hello from chunk!") } @(test) asset_in_chunk_and_in_path_read_matching_file :: 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, "") create_file(&ts, t, "target", "hello from path!") // Act _, read := read_asset(&ts, t, "target") // Assert testing.expect_value(t, read, "hello from path!") } @(test) asset_in_path_not_in_chunk_read_non_matching_file :: proc(t: ^testing.T) { // Arrange 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_asset(&ts, t, "non_matching") // Assert testing.expect(t, !exists) } @(test) asset_in_chunk_not_in_path_read_non_matching_file :: 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 exists, _ := read_asset(&ts, t, "non_matching") // Assert testing.expect(t, !exists) } @(test) asset_in_chunk_and_in_path_read_non_matching_file :: 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, "") create_file(&ts, t, "target", "hello from path!") // Act 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), ) }