From 6054b921db819246f9a107f4236a6338fda000f0 Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Thu, 27 Feb 2025 18:48:38 -0500 Subject: [PATCH] Add tests for new safe `to_cstring` --- tests/core/strings/test_core_strings.odin | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/core/strings/test_core_strings.odin b/tests/core/strings/test_core_strings.odin index 44c79ce736b..7b5f7fbb4fe 100644 --- a/tests/core/strings/test_core_strings.odin +++ b/tests/core/strings/test_core_strings.odin @@ -1,5 +1,6 @@ package test_core_strings +import "core:mem" import "core:strings" import "core:testing" import "base:runtime" @@ -175,3 +176,45 @@ test_substring :: proc(t: ^testing.T) { testing.expectf(t, sub == tc.sub, "expected %v[%v:%v] to return sub: %v, got: %v", tc.s, tc.start, tc.end, tc.sub, sub) } } + +@test +test_builder_to_cstring_with_nil_allocator :: proc(t: ^testing.T) { + b := strings.builder_make_none(mem.nil_allocator()) + + cstr, err := strings.to_cstring(&b) + testing.expect_value(t, cstr, nil) + testing.expect_value(t, err, mem.Allocator_Error.Out_Of_Memory) +} + +@test +test_builder_to_cstring :: proc(t: ^testing.T) { + buf: [8]byte + a: mem.Arena + mem.arena_init(&a, buf[:]) + + b := strings.builder_make_none(mem.arena_allocator(&a)) + + { + cstr, err := strings.to_cstring(&b) + testing.expectf(t, cstr != nil, "expected cstr to not be nil, got %v", cstr) + testing.expect_value(t, err, nil) + } + + n := strings.write_byte(&b, 'a') + testing.expect(t, n == 1) + + { + cstr, err := strings.to_cstring(&b) + testing.expectf(t, cstr != nil, "expected cstr to not be nil, got %v", cstr) + testing.expect_value(t, err, nil) + } + + n = strings.write_string(&b, "aaaaaaa") + testing.expect(t, n == 7) + + { + cstr, err := strings.to_cstring(&b) + testing.expect(t, cstr == nil) + testing.expect(t, err == .Out_Of_Memory) + } +}