general.odin 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package win32_tests
  2. import "core:sys/win32"
  3. import "core:testing"
  4. utf16_to_utf8 :: proc(t: ^testing.T, str: []u16, comparison: string, expected_result: bool, loc := #caller_location) {
  5. result := win32.utf16_to_utf8(str[:]);
  6. testing.expect(t, (result == comparison) == expected_result, "Incorrect utf16_to_utf8 conversion", loc);
  7. }
  8. wstring_to_utf8 :: proc(t: ^testing.T, str: []u16, comparison: string, expected_result: bool, loc := #caller_location) {
  9. result := win32.wstring_to_utf8(nil if len(str) == 0 else cast(win32.Wstring)&str[0], -1);
  10. testing.expect(t, (result == comparison) == expected_result, "Incorrect wstring_to_utf8 conversion", loc);
  11. }
  12. @test
  13. test_utf :: proc(t: ^testing.T) {
  14. utf16_to_utf8(t, []u16{}, "", true);
  15. utf16_to_utf8(t, []u16{0}, "", true);
  16. utf16_to_utf8(t, []u16{0, 't', 'e', 's', 't'}, "", true);
  17. utf16_to_utf8(t, []u16{0, 't', 'e', 's', 't', 0}, "", true);
  18. utf16_to_utf8(t, []u16{'t', 'e', 's', 't'}, "test", true);
  19. utf16_to_utf8(t, []u16{'t', 'e', 's', 't', 0}, "test", true);
  20. utf16_to_utf8(t, []u16{'t', 'e', 0, 's', 't'}, "te", true);
  21. utf16_to_utf8(t, []u16{'t', 'e', 0, 's', 't', 0}, "te", true);
  22. wstring_to_utf8(t, []u16{}, "", true);
  23. wstring_to_utf8(t, []u16{0}, "", true);
  24. wstring_to_utf8(t, []u16{0, 't', 'e', 's', 't'}, "", true);
  25. wstring_to_utf8(t, []u16{0, 't', 'e', 's', 't', 0}, "", true);
  26. wstring_to_utf8(t, []u16{'t', 'e', 's', 't', 0}, "test", true);
  27. wstring_to_utf8(t, []u16{'t', 'e', 0, 's', 't'}, "te", true);
  28. wstring_to_utf8(t, []u16{'t', 'e', 0, 's', 't', 0}, "te", true);
  29. // WARNING: Passing a non-zero-terminated string to wstring_to_utf8 is dangerous,
  30. // as it will go out of bounds looking for a zero.
  31. // It will "fail" or "succeed" by having a zero just after the end of the input string or not.
  32. wstring_to_utf8(t, []u16{'t', 'e', 's', 't'}, "test", false);
  33. wstring_to_utf8(t, []u16{'t', 'e', 's', 't', 0}[:4], "test", true);
  34. wstring_to_utf8(t, []u16{'t', 'e', 's', 't', 'q'}[:4], "test", false);
  35. }