diff options
| author | vassvik <mvassvik@gmail.com> | 2020-06-13 18:04:19 +0200 |
|---|---|---|
| committer | vassvik <mvassvik@gmail.com> | 2020-06-13 18:04:19 +0200 |
| commit | 9cccb20f49ff885ed5ea16f326011467ca6cf263 (patch) | |
| tree | d0d31d5adcf9ef08ef118f8ba58b1ff1eedbc7b9 | |
| parent | 6985d72fdad8516633958b98a093238cf8382411 (diff) | |
Add some tests to test utf16_to_utf8 and wstring_to_utf8
| -rw-r--r-- | core/sys/win32/tests/general.odin | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/core/sys/win32/tests/general.odin b/core/sys/win32/tests/general.odin new file mode 100644 index 000000000..80cf33a9c --- /dev/null +++ b/core/sys/win32/tests/general.odin @@ -0,0 +1,44 @@ +package win32_tests + +import "core:fmt" +import "core:sys/win32" + +main :: proc(){ + test_utf16_to_utf8 :: proc(str: []u16, comparison: string, expected_result: bool, loc := #caller_location) { + result := win32.utf16_to_utf8(str[:]); + fmt.assertf((result == comparison) == expected_result, + "Incorrect utf16_to_utf8 conversion: %q %s %q\nloc = %#v\n", + result, "!=" if expected_result else "==", comparison, loc); + } + + test_utf16_to_utf8([]u16{}, "", true); + test_utf16_to_utf8([]u16{0}, "", true); + test_utf16_to_utf8([]u16{0, 't', 'e', 's', 't'}, "", true); + test_utf16_to_utf8([]u16{0, 't', 'e', 's', 't', 0}, "", true); + test_utf16_to_utf8([]u16{'t', 'e', 's', 't'}, "test", true); + test_utf16_to_utf8([]u16{'t', 'e', 's', 't', 0}, "test", true); + test_utf16_to_utf8([]u16{'t', 'e', 0, 's', 't'}, "te", true); + test_utf16_to_utf8([]u16{'t', 'e', 0, 's', 't', 0}, "te", true); + + test_wstring_to_utf8 :: proc(str: []u16, comparison: string, expected_result: bool, loc := #caller_location) { + result := win32.wstring_to_utf8(nil if len(str) == 0 else cast(win32.Wstring)&str[0], -1); + fmt.assertf((result == comparison) == expected_result, + "Incorrect wstring_to_utf8 conversion: %q %s %q\nloc = %#v\n", + result, "!=" if expected_result else "==", comparison, loc); + } + + test_wstring_to_utf8([]u16{}, "", true); + test_wstring_to_utf8([]u16{0}, "", true); + test_wstring_to_utf8([]u16{0, 't', 'e', 's', 't'}, "", true); + test_wstring_to_utf8([]u16{0, 't', 'e', 's', 't', 0}, "", true); + test_wstring_to_utf8([]u16{'t', 'e', 's', 't', 0}, "test", true); + test_wstring_to_utf8([]u16{'t', 'e', 0, 's', 't'}, "te", true); + test_wstring_to_utf8([]u16{'t', 'e', 0, 's', 't', 0}, "te", true); + + // WARNING: Passing a non-zero-terminated string to wstring_to_utf8 is dangerous, + // as it will go out of bounds looking for a zero. + // It will "fail" or "succeed" by having a zero just after the end of the input string or not. + test_wstring_to_utf8([]u16{'t', 'e', 's', 't'}, "test", false); + test_wstring_to_utf8([]u16{'t', 'e', 's', 't', 0}[:4], "test", true); + test_wstring_to_utf8([]u16{'t', 'e', 's', 't', 'q'}[:4], "test", false); +} |