2
0
Эх сурвалжийг харах

Add tests for new safe `to_cstring`

Feoramund 6 сар өмнө
parent
commit
6054b921db

+ 43 - 0
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)
+	}
+}