From bca6a988897be8cd41fa8d965062d6465490a6ce Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Thu, 12 May 2022 22:33:35 -0700 Subject: [PATCH] Add tests for ciutf8 --- src/util.zig | 58 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/src/util.zig b/src/util.zig index ee3f79e..c2b51e9 100644 --- a/src/util.zig +++ b/src/util.zig @@ -183,14 +183,52 @@ pub const ciutf8 = struct { return it_a == null and it_b == null; } - - pub fn lowerInPlace(str: []u8) void { - const view = View.init(str) catch return; - - var iter = view.iterator(); - var it = iter.nextCodepointSlice(); - while (it != null) : (it = iter.nextCodepointSlice()) { - if (isAscii(it.?[0])) it.?[0] = toLower(it.?[0]); - } - } }; + +test "case insensitive eql with utf-8 chars" { + const t = std.testing; + try t.expectEqual(true, ciutf8.eql("abc 💯 def", "aBc 💯 DEF")); + try t.expectEqual(false, ciutf8.eql("xyz 💯 ijk", "aBc 💯 DEF")); + try t.expectEqual(false, ciutf8.eql("abc 💯 def", "aBc x DEF")); + try t.expectEqual(true, ciutf8.eql("💯", "💯")); + try t.expectEqual(false, ciutf8.eql("💯", "a")); + try t.expectEqual(false, ciutf8.eql("💯", "💯 continues")); + try t.expectEqual(false, ciutf8.eql("💯 fsdfs", "💯")); + try t.expectEqual(false, ciutf8.eql("💯", "")); + try t.expectEqual(false, ciutf8.eql("", "💯")); + + try t.expectEqual(true, ciutf8.eql("abc x def", "aBc x DEF")); + try t.expectEqual(false, ciutf8.eql("xyz x ijk", "aBc x DEF")); + try t.expectEqual(true, ciutf8.eql("x", "x")); + try t.expectEqual(false, ciutf8.eql("x", "a")); + try t.expectEqual(false, ciutf8.eql("x", "x continues")); + try t.expectEqual(false, ciutf8.eql("x fsdfs", "x")); + try t.expectEqual(false, ciutf8.eql("x", "")); + try t.expectEqual(false, ciutf8.eql("", "x")); + + try t.expectEqual(true, ciutf8.eql("", "")); +} + +test "case insensitive hash with utf-8 chars" { + const t = std.testing; + try t.expect(ciutf8.hash("abc 💯 def") == ciutf8.hash("aBc 💯 DEF")); + try t.expect(ciutf8.hash("xyz 💯 ijk") != ciutf8.hash("aBc 💯 DEF")); + try t.expect(ciutf8.hash("abc 💯 def") != ciutf8.hash("aBc x DEF")); + try t.expect(ciutf8.hash("💯") == ciutf8.hash("💯")); + try t.expect(ciutf8.hash("💯") != ciutf8.hash("a")); + try t.expect(ciutf8.hash("💯") != ciutf8.hash("💯 continues")); + try t.expect(ciutf8.hash("💯 fsdfs") != ciutf8.hash("💯")); + try t.expect(ciutf8.hash("💯") != ciutf8.hash("")); + try t.expect(ciutf8.hash("") != ciutf8.hash("💯")); + + try t.expect(ciutf8.hash("abc x def") == ciutf8.hash("aBc x DEF")); + try t.expect(ciutf8.hash("xyz x ijk") != ciutf8.hash("aBc x DEF")); + try t.expect(ciutf8.hash("x") == ciutf8.hash("x")); + try t.expect(ciutf8.hash("x") != ciutf8.hash("a")); + try t.expect(ciutf8.hash("x") != ciutf8.hash("x continues")); + try t.expect(ciutf8.hash("x fsdfs") != ciutf8.hash("x")); + try t.expect(ciutf8.hash("x") != ciutf8.hash("")); + try t.expect(ciutf8.hash("") != ciutf8.hash("x")); + + try t.expect(ciutf8.hash("") == ciutf8.hash("")); +}