Skip to content

Commit

Permalink
Add tests for new safe to_cstring
Browse files Browse the repository at this point in the history
  • Loading branch information
Feoramund committed Feb 27, 2025
1 parent d9b6d10 commit 41b5f9a
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions tests/core/strings/test_core_strings.odin
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package test_core_strings

import "core:mem"
import "core:strings"
import "core:testing"
import "base:runtime"
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 41b5f9a

Please sign in to comment.