From 5f20d316174a39dbcc095809024d40caa3d6383b Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Fri, 1 Jul 2022 22:20:08 -0400 Subject: [PATCH] Add more strToScalars tests --- crates/compiler/builtins/bitcode/src/str.zig | 59 ++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/crates/compiler/builtins/bitcode/src/str.zig b/crates/compiler/builtins/bitcode/src/str.zig index 3f47965056..1b43e751d4 100644 --- a/crates/compiler/builtins/bitcode/src/str.zig +++ b/crates/compiler/builtins/bitcode/src/str.zig @@ -618,6 +618,65 @@ test "strToScalars: Multiple 2-byte UTF-8 characters" { try expect(RocList.eql(actual, expected)); } +test "strToScalars: One 3-byte UTF-8 character" { + const str = RocStr.fromSlice("鹏"); + defer RocStr.deinit(str); + + const expected_array = [_]u32{ 40527 }; + const expected = RocList.fromSlice(u32, expected_array[0..expected_array.len]); + defer RocList.deinit(expected, u32); + + const actual = strToScalars(str); + defer RocList.deinit(actual, u32); + + try expect(RocList.eql(actual, expected)); +} + +test "strToScalars: Multiple 3-byte UTF-8 characters" { + const str = RocStr.fromSlice("鹏很有趣"); + defer RocStr.deinit(str); + + const expected_array = [_]u32{ 40527, 24456, 26377, 36259 }; + const expected = RocList.fromSlice(u32, expected_array[0..expected_array.len]); + defer RocList.deinit(expected, u32); + + const actual = strToScalars(str); + defer RocList.deinit(actual, u32); + + try expect(RocList.eql(actual, expected)); +} + +test "strToScalars: One 4-byte UTF-8 character" { + // from https://design215.com/toolbox/utf8-4byte-characters.php + const str = RocStr.fromSlice("𒀀"); + defer RocStr.deinit(str); + + const expected_array = [_]u32{ 73728 }; + const expected = RocList.fromSlice(u32, expected_array[0..expected_array.len]); + defer RocList.deinit(expected, u32); + + const actual = strToScalars(str); + defer RocList.deinit(actual, u32); + + try expect(RocList.eql(actual, expected)); +} + +test "strToScalars: Multiple 4-byte UTF-8 characters" { + // from https://design215.com/toolbox/utf8-4byte-characters.php + const str = RocStr.fromSlice("𒀀𒀁"); + defer RocStr.deinit(str); + + const expected_array = [_]u32{ 73728, 73729 }; + const expected = RocList.fromSlice(u32, expected_array[0..expected_array.len]); + defer RocList.deinit(expected, u32); + + const actual = strToScalars(str); + defer RocList.deinit(actual, u32); + + try expect(RocList.eql(actual, expected)); +} + + // Str.fromInt pub fn exportFromInt(comptime T: type, comptime name: []const u8) void { comptime var f = struct {