mirror of
https://github.com/odin-lang/Odin.git
synced 2026-04-06 06:38:20 +00:00
Add tests for utf8_to_utf16_alloc, utf8_to_wstring_alloc
This commit is contained in:
@@ -4,6 +4,7 @@ package test_core_sys_windows
|
||||
import "base:intrinsics"
|
||||
import "core:testing"
|
||||
import win32 "core:sys/windows"
|
||||
import runtime "base:runtime"
|
||||
|
||||
UTF16_Vector :: struct {
|
||||
wstr: win32.wstring,
|
||||
@@ -142,3 +143,112 @@ utf8_to_wstring_buf_test :: proc(t : ^testing.T) {
|
||||
// Buffer too short.
|
||||
testing.expect(t, result == nil)
|
||||
}
|
||||
|
||||
// Custom allocator proc that always returns dirty (non-zeroed) memory.
|
||||
dirty_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int,
|
||||
location: runtime.Source_Code_Location = #caller_location) -> ([]byte, runtime.Allocator_Error) {
|
||||
real_allocator := cast(^runtime.Allocator)allocator_data
|
||||
bytes, error := real_allocator.procedure(real_allocator.data, mode,
|
||||
size, alignment,
|
||||
old_memory, old_size,
|
||||
location)
|
||||
if error == .None {
|
||||
for i in 0 ..< len(bytes) {
|
||||
// This will yield a 0 byte on overflow, but that does not matter in this test suite.
|
||||
bytes[i] = cast(byte)(i + 1)
|
||||
}
|
||||
}
|
||||
return bytes, error
|
||||
}
|
||||
|
||||
@(test)
|
||||
utf8_to_utf16_alloc_test :: proc(t : ^testing.T) {
|
||||
// We want to ensure that everything works with dirty
|
||||
// (non-zeroed) memory returned from the allocator.
|
||||
real_allocator := context.temp_allocator
|
||||
allocator := runtime.Allocator {
|
||||
procedure = dirty_allocator_proc,
|
||||
data = cast(rawptr)&real_allocator,
|
||||
}
|
||||
|
||||
// Test the dirty allocator.
|
||||
allocator_test_slice := make([]u8, 100, allocator)
|
||||
testing.expect_value(t, len(allocator_test_slice), 100)
|
||||
for i in 0 ..< len(allocator_test_slice) {
|
||||
testing.expect_value(t, allocator_test_slice[i], cast(u8)(i + 1))
|
||||
}
|
||||
|
||||
result : []u16
|
||||
|
||||
result = win32.utf8_to_utf16_alloc("Hello\x00, World!", allocator)
|
||||
testing.expect_value(t, len(result), 14)
|
||||
testing.expect_value(t, result[4], 'o')
|
||||
testing.expect_value(t, result[5], 0)
|
||||
testing.expect_value(t, result[6], ',')
|
||||
testing.expect_value(t, result[13], '!')
|
||||
|
||||
result = win32.utf8_to_utf16_alloc("H\x00\x00", allocator)
|
||||
testing.expect_value(t, len(result), 3)
|
||||
testing.expect_value(t, result[1], 0)
|
||||
testing.expect_value(t, result[2], 0)
|
||||
|
||||
result = win32.utf8_to_utf16_alloc("你好,世界!", allocator)
|
||||
testing.expect_value(t, len(result), 6)
|
||||
testing.expect_value(t, result[0], 0x4F60)
|
||||
testing.expect_value(t, result[1], 0x597D)
|
||||
testing.expect_value(t, result[2], 0xFF0C)
|
||||
testing.expect_value(t, result[3], 0x4E16)
|
||||
testing.expect_value(t, result[4], 0x754C)
|
||||
testing.expect_value(t, result[5], 0xFF01)
|
||||
|
||||
result = win32.utf8_to_utf16_alloc("", allocator)
|
||||
// Valid, but indistinguishable from an error.
|
||||
testing.expect_value(t, len(result), 0)
|
||||
}
|
||||
|
||||
@(test)
|
||||
utf8_to_wstring_alloc_test :: proc(t : ^testing.T) {
|
||||
// We want to ensure that everything works with dirty
|
||||
// (non-zeroed) memory returned from the allocator.
|
||||
backing_allocator := context.temp_allocator
|
||||
allocator := runtime.Allocator {
|
||||
procedure = dirty_allocator_proc,
|
||||
data = cast(rawptr)&backing_allocator,
|
||||
}
|
||||
|
||||
result : win32.wstring
|
||||
buf : [^]u16
|
||||
|
||||
result = win32.utf8_to_wstring_alloc("Hello\x00, World!", allocator)
|
||||
buf = transmute([^]u16)result
|
||||
testing.expect(t, result != nil)
|
||||
testing.expect_value(t, buf[4], 'o')
|
||||
testing.expect_value(t, buf[5], 0)
|
||||
testing.expect_value(t, buf[6], ',')
|
||||
testing.expect_value(t, buf[13], '!')
|
||||
testing.expect_value(t, buf[14], 0)
|
||||
|
||||
result = win32.utf8_to_wstring_alloc("H\x00\x00", allocator)
|
||||
buf = transmute([^]u16)result
|
||||
testing.expect(t, result != nil)
|
||||
testing.expect_value(t, buf[1], 0)
|
||||
|
||||
result = win32.utf8_to_wstring_alloc("你好,世界!", allocator)
|
||||
buf = transmute([^]u16)result
|
||||
testing.expect(t, result != nil)
|
||||
testing.expect_value(t, buf[0], 0x4F60)
|
||||
testing.expect_value(t, buf[1], 0x597D)
|
||||
testing.expect_value(t, buf[2], 0xFF0C)
|
||||
testing.expect_value(t, buf[3], 0x4E16)
|
||||
testing.expect_value(t, buf[4], 0x754C)
|
||||
testing.expect_value(t, buf[5], 0xFF01)
|
||||
testing.expect_value(t, buf[6], 0)
|
||||
|
||||
result = win32.utf8_to_wstring_alloc("", allocator)
|
||||
buf = transmute([^]u16)result
|
||||
// Valid, and distinguishable from an error.
|
||||
testing.expect(t, result != nil)
|
||||
testing.expect_value(t, buf[0], 0)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user