Add tests for new safe to_cstring

This commit is contained in:
Feoramund
2025-02-27 18:48:38 -05:00
parent b2e3b34ce0
commit 6054b921db
+43
View File
@@ -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)
}
}