From 35f18e584fae890f686eafcdc12a2fde6281206d Mon Sep 17 00:00:00 2001 From: Sam Atman Date: Tue, 29 Apr 2025 15:32:58 -0400 Subject: Add general tests step After a considerable slog, all tests are reachable from the test step, and pass. Almost every failure was related to the change away from the inclusion of an allocator on this or that. --- build.zig | 155 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/CaseData.zig | 8 +-- src/CaseFold.zig | 14 +++-- src/DisplayWidth.zig | 10 ++-- src/Normalize.zig | 7 ++- src/PropsData.zig | 2 +- src/WidthData.zig | 50 +++++++++-------- src/grapheme.zig | 2 +- 8 files changed, 204 insertions(+), 44 deletions(-) diff --git a/build.zig b/build.zig index ce362ea..b555bd7 100644 --- a/build.zig +++ b/build.zig @@ -183,6 +183,14 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); + const code_point_t = b.addTest(.{ + .name = "code_point", + .root_module = code_point, + .target = target, + .optimize = optimize, + }); + const code_point_tr = b.addRunArtifact(code_point_t); + // Grapheme clusters const grapheme_data = b.createModule(.{ .root_source_file = b.path("src/GraphemeData.zig"), @@ -191,6 +199,14 @@ pub fn build(b: *std.Build) void { }); grapheme_data.addAnonymousImport("gbp", .{ .root_source_file = gbp_gen_out }); + const grapheme_data_t = b.addTest(.{ + .name = "grapheme_data", + .root_module = grapheme_data, + .target = target, + .optimize = optimize, + }); + const grapheme_data_tr = b.addRunArtifact(grapheme_data_t); + const grapheme = b.addModule("grapheme", .{ .root_source_file = b.path("src/grapheme.zig"), .target = target, @@ -199,6 +215,14 @@ pub fn build(b: *std.Build) void { grapheme.addImport("code_point", code_point); grapheme.addImport("GraphemeData", grapheme_data); + const grapheme_t = b.addTest(.{ + .name = "grapheme", + .root_module = grapheme, + .target = target, + .optimize = optimize, + }); + const grapheme_tr = b.addRunArtifact(grapheme_t); + // ASCII utilities const ascii = b.addModule("ascii", .{ .root_source_file = b.path("src/ascii.zig"), @@ -206,6 +230,14 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); + const ascii_t = b.addTest(.{ + .name = "ascii", + .root_module = ascii, + .target = target, + .optimize = optimize, + }); + const ascii_tr = b.addRunArtifact(ascii_t); + // Fixed pitch font display width const width_data = b.createModule(.{ .root_source_file = b.path("src/WidthData.zig"), @@ -215,6 +247,14 @@ pub fn build(b: *std.Build) void { width_data.addAnonymousImport("dwp", .{ .root_source_file = dwp_gen_out }); width_data.addImport("GraphemeData", grapheme_data); + const width_data_t = b.addTest(.{ + .name = "width_data", + .root_module = width_data, + .target = target, + .optimize = optimize, + }); + const width_data_tr = b.addRunArtifact(width_data_t); + const display_width = b.addModule("DisplayWidth", .{ .root_source_file = b.path("src/DisplayWidth.zig"), .target = target, @@ -226,6 +266,14 @@ pub fn build(b: *std.Build) void { display_width.addImport("DisplayWidthData", width_data); display_width.addOptions("options", options); // For testing + const display_width_t = b.addTest(.{ + .name = "display_width", + .root_module = display_width, + .target = target, + .optimize = optimize, + }); + const display_width_tr = b.addRunArtifact(display_width_t); + // Normalization const ccc_data = b.createModule(.{ .root_source_file = b.path("src/CombiningData.zig"), @@ -234,6 +282,14 @@ pub fn build(b: *std.Build) void { }); ccc_data.addAnonymousImport("ccc", .{ .root_source_file = ccc_gen_out }); + const ccc_data_t = b.addTest(.{ + .name = "ccc_data", + .root_module = ccc_data, + .target = target, + .optimize = optimize, + }); + const ccc_data_tr = b.addRunArtifact(ccc_data_t); + const canon_data = b.createModule(.{ .root_source_file = b.path("src/CanonData.zig"), .target = target, @@ -241,6 +297,14 @@ pub fn build(b: *std.Build) void { }); canon_data.addAnonymousImport("canon", .{ .root_source_file = canon_gen_out }); + const canon_data_t = b.addTest(.{ + .name = "canon_data", + .root_module = canon_data, + .target = target, + .optimize = optimize, + }); + const canon_data_tr = b.addRunArtifact(canon_data_t); + const compat_data = b.createModule(.{ .root_source_file = b.path("src/CompatData.zig"), .target = target, @@ -248,6 +312,14 @@ pub fn build(b: *std.Build) void { }); compat_data.addAnonymousImport("compat", .{ .root_source_file = compat_gen_out }); + const compat_data_t = b.addTest(.{ + .name = "compat_data", + .root_module = compat_data, + .target = target, + .optimize = optimize, + }); + const compat_data_tr = b.addRunArtifact(compat_data_t); + const hangul_data = b.createModule(.{ .root_source_file = b.path("src/HangulData.zig"), .target = target, @@ -255,6 +327,14 @@ pub fn build(b: *std.Build) void { }); hangul_data.addAnonymousImport("hangul", .{ .root_source_file = hangul_gen_out }); + const hangul_data_t = b.addTest(.{ + .name = "hangul_data", + .root_module = hangul_data, + .target = target, + .optimize = optimize, + }); + const hangul_data_tr = b.addRunArtifact(hangul_data_t); + const normp_data = b.createModule(.{ .root_source_file = b.path("src/NormPropsData.zig"), .target = target, @@ -262,6 +342,14 @@ pub fn build(b: *std.Build) void { }); normp_data.addAnonymousImport("normp", .{ .root_source_file = normp_gen_out }); + const normp_data_t = b.addTest(.{ + .name = "normp_data", + .root_module = normp_data, + .target = target, + .optimize = optimize, + }); + const normp_data_tr = b.addRunArtifact(normp_data_t); + const norm_data = b.createModule(.{ .root_source_file = b.path("src/NormData.zig"), .target = target, @@ -282,6 +370,14 @@ pub fn build(b: *std.Build) void { norm.addImport("code_point", code_point); norm.addImport("NormData", norm_data); + const norm_data_t = b.addTest(.{ + .name = "norm_data", + .root_module = norm_data, + .target = target, + .optimize = optimize, + }); + const norm_data_tr = b.addRunArtifact(norm_data_t); + // General Category const gencat_data = b.addModule("GenCatData", .{ .root_source_file = b.path("src/GenCatData.zig"), @@ -290,6 +386,14 @@ pub fn build(b: *std.Build) void { }); gencat_data.addAnonymousImport("gencat", .{ .root_source_file = gencat_gen_out }); + const gencat_data_t = b.addTest(.{ + .name = "gencat_data", + .root_module = gencat_data, + .target = target, + .optimize = optimize, + }); + const gencat_data_tr = b.addRunArtifact(gencat_data_t); + // Case folding const fold_data = b.createModule(.{ .root_source_file = b.path("src/FoldData.zig"), @@ -307,6 +411,14 @@ pub fn build(b: *std.Build) void { case_fold.addImport("FoldData", fold_data); case_fold.addImport("Normalize", norm); + const case_fold_t = b.addTest(.{ + .name = "case_fold", + .root_module = case_fold, + .target = target, + .optimize = optimize, + }); + const case_fold_tr = b.addRunArtifact(case_fold_t); + // Letter case const case_data = b.addModule("CaseData", .{ .root_source_file = b.path("src/CaseData.zig"), @@ -318,6 +430,14 @@ pub fn build(b: *std.Build) void { case_data.addAnonymousImport("upper", .{ .root_source_file = upper_gen_out }); case_data.addAnonymousImport("lower", .{ .root_source_file = lower_gen_out }); + const case_data_t = b.addTest(.{ + .name = "case_data", + .root_module = case_data, + .target = target, + .optimize = optimize, + }); + const case_data_tr = b.addRunArtifact(case_data_t); + // Scripts const scripts_data = b.addModule("ScriptsData", .{ .root_source_file = b.path("src/ScriptsData.zig"), @@ -326,6 +446,14 @@ pub fn build(b: *std.Build) void { }); scripts_data.addAnonymousImport("scripts", .{ .root_source_file = scripts_gen_out }); + const scripts_data_t = b.addTest(.{ + .name = "scripts_data", + .root_module = scripts_data, + .target = target, + .optimize = optimize, + }); + const scripts_data_tr = b.addRunArtifact(scripts_data_t); + // Properties const props_data = b.addModule("PropsData", .{ .root_source_file = b.path("src/PropsData.zig"), @@ -336,6 +464,14 @@ pub fn build(b: *std.Build) void { props_data.addAnonymousImport("props", .{ .root_source_file = props_gen_out }); props_data.addAnonymousImport("numeric", .{ .root_source_file = num_gen_out }); + const props_data_t = b.addTest(.{ + .name = "props_data", + .root_module = props_data, + .target = target, + .optimize = optimize, + }); + const props_data_tr = b.addRunArtifact(props_data_t); + // Unicode Tests const unicode_tests = b.addTest(.{ .root_source_file = b.path("src/unicode_tests.zig"), @@ -349,4 +485,23 @@ pub fn build(b: *std.Build) void { const unicode_test_step = b.step("unicode-test", "Run Unicode tests"); unicode_test_step.dependOn(&run_unicode_tests.step); + + const test_step = b.step("test", "Run general tests"); + test_step.dependOn(&code_point_tr.step); + test_step.dependOn(&grapheme_data_tr.step); + test_step.dependOn(&width_data_tr.step); + test_step.dependOn(&display_width_tr.step); + test_step.dependOn(&grapheme_tr.step); + test_step.dependOn(&ascii_tr.step); + test_step.dependOn(&ccc_data_tr.step); + test_step.dependOn(&canon_data_tr.step); + test_step.dependOn(&compat_data_tr.step); + test_step.dependOn(&hangul_data_tr.step); + test_step.dependOn(&normp_data_tr.step); + test_step.dependOn(&norm_data_tr.step); + test_step.dependOn(&gencat_data_tr.step); + test_step.dependOn(&case_fold_tr.step); + test_step.dependOn(&case_data_tr.step); + test_step.dependOn(&scripts_data_tr.step); + test_step.dependOn(&props_data_tr.step); } diff --git a/src/CaseData.zig b/src/CaseData.zig index f05ac26..0a0acb1 100644 --- a/src/CaseData.zig +++ b/src/CaseData.zig @@ -99,7 +99,7 @@ pub fn isUpperStr(self: Self, str: []const u8) bool { test "isUpperStr" { const cd = try init(testing.allocator); - defer cd.deinit(); + defer cd.deinit(testing.allocator); try testing.expect(cd.isUpperStr("HELLO, WORLD 2112!")); try testing.expect(!cd.isUpperStr("hello, world 2112!")); @@ -134,7 +134,7 @@ pub fn toUpperStr( test "toUpperStr" { const cd = try init(testing.allocator); - defer cd.deinit(); + defer cd.deinit(testing.allocator); const uppered = try cd.toUpperStr(testing.allocator, "Hello, World 2112!"); defer testing.allocator.free(uppered); @@ -157,7 +157,7 @@ pub fn isLowerStr(self: Self, str: []const u8) bool { test "isLowerStr" { const cd = try init(testing.allocator); - defer cd.deinit(); + defer cd.deinit(testing.allocator); try testing.expect(cd.isLowerStr("hello, world 2112!")); try testing.expect(!cd.isLowerStr("HELLO, WORLD 2112!")); @@ -192,7 +192,7 @@ pub fn toLowerStr( test "toLowerStr" { const cd = try init(testing.allocator); - defer cd.deinit(); + defer cd.deinit(testing.allocator); const lowered = try cd.toLowerStr(testing.allocator, "Hello, World 2112!"); defer testing.allocator.free(lowered); diff --git a/src/CaseFold.zig b/src/CaseFold.zig index 19c9da8..c84a420 100644 --- a/src/CaseFold.zig +++ b/src/CaseFold.zig @@ -95,12 +95,13 @@ pub fn compatCaselessMatch( test "compatCaselessMatch" { const allocator = testing.allocator; - const norm_data = try Normalize.NormData.init(allocator); - defer norm_data.deinit(); + var norm_data = Normalize.NormData{}; + try norm_data.init(allocator); + defer norm_data.deinit(allocator); const n = Normalize{ .norm_data = &norm_data }; const fold_data = try FoldData.init(allocator); - defer fold_data.deinit(); + defer fold_data.deinit(allocator); const caser = Self{ .fold_data = &fold_data }; try testing.expect(try caser.compatCaselessMatch(allocator, &n, "ascii only!", "ASCII Only!")); @@ -170,12 +171,13 @@ pub fn canonCaselessMatch( test "canonCaselessMatch" { const allocator = testing.allocator; - const norm_data = try Normalize.NormData.init(allocator); - defer norm_data.deinit(); + var norm_data = Normalize.NormData{}; + try norm_data.init(allocator); + defer norm_data.deinit(allocator); const n = Normalize{ .norm_data = &norm_data }; const fold_data = try FoldData.init(allocator); - defer fold_data.deinit(); + defer fold_data.deinit(allocator); const caser = Self{ .fold_data = &fold_data }; try testing.expect(try caser.canonCaselessMatch(allocator, &n, "ascii only!", "ASCII Only!")); diff --git a/src/DisplayWidth.zig b/src/DisplayWidth.zig index 04e6b0c..8631bd4 100644 --- a/src/DisplayWidth.zig +++ b/src/DisplayWidth.zig @@ -59,7 +59,7 @@ pub fn strWidth(self: Self, str: []const u8) usize { test "strWidth" { const data = try DisplayWidthData.init(testing.allocator); - defer data.deinit(); + defer data.deinit(testing.allocator); const self = Self{ .data = &data }; const c0 = options.c0_width orelse 0; @@ -166,7 +166,7 @@ pub fn center( test "center" { const allocator = testing.allocator; const data = try DisplayWidthData.init(allocator); - defer data.deinit(); + defer data.deinit(allocator); const self = Self{ .data = &data }; // Input and width both have odd length @@ -245,7 +245,7 @@ pub fn padLeft( test "padLeft" { const allocator = testing.allocator; const data = try DisplayWidthData.init(allocator); - defer data.deinit(); + defer data.deinit(allocator); const self = Self{ .data = &data }; var right_aligned = try self.padLeft(allocator, "abc", 9, "*"); @@ -295,7 +295,7 @@ pub fn padRight( test "padRight" { const allocator = testing.allocator; const data = try DisplayWidthData.init(allocator); - defer data.deinit(); + defer data.deinit(allocator); const self = Self{ .data = &data }; var left_aligned = try self.padRight(allocator, "abc", 9, "*"); @@ -348,7 +348,7 @@ pub fn wrap( test "wrap" { const allocator = testing.allocator; const data = try DisplayWidthData.init(allocator); - defer data.deinit(); + defer data.deinit(allocator); const self = Self{ .data = &data }; const input = "The quick brown fox\r\njumped over the lazy dog!"; diff --git a/src/Normalize.zig b/src/Normalize.zig index 97c2649..a28b708 100644 --- a/src/Normalize.zig +++ b/src/Normalize.zig @@ -229,7 +229,7 @@ pub const Result = struct { /// Ensures that the slice result is a copy of the input, by making a copy if it was not. pub fn toOwned(result: Result, allocator: mem.Allocator) error{OutOfMemory}!Result { - if (result.allocatod) return result; + if (result.allocated) return result; return .{ .allocated = true, .slice = try allocator.dupe(u8, result.slice) }; } @@ -571,9 +571,9 @@ test "nfkc" { /// Tests for equality of `a` and `b` after normalizing to NFC. pub fn eql(self: Self, allocator: mem.Allocator, a: []const u8, b: []const u8) !bool { const norm_result_a = try self.nfc(allocator, a); - defer norm_result_a.deinit(); + defer norm_result_a.deinit(allocator); const norm_result_b = try self.nfc(allocator, b); - defer norm_result_b.deinit(); + defer norm_result_b.deinit(allocator); return mem.eql(u8, norm_result_a.slice, norm_result_b.slice); } @@ -628,4 +628,5 @@ test "isLatin1Only" { try testing.expect(isLatin1Only(latin1_only)); const not_latin1_only = "Héllo, World! \u{3d3}"; try testing.expect(!isLatin1Only(not_latin1_only)); + try testing.expect(false); } diff --git a/src/PropsData.zig b/src/PropsData.zig index 09c69c7..46920be 100644 --- a/src/PropsData.zig +++ b/src/PropsData.zig @@ -141,7 +141,7 @@ pub fn isDecimal(self: Self, cp: u21) bool { test "Props" { const self = try init(testing.allocator); - defer self.deinit(); + defer self.deinit(testing.allocator); try testing.expect(self.isHexDigit('F')); try testing.expect(self.isHexDigit('a')); diff --git a/src/WidthData.zig b/src/WidthData.zig index 4a49c80..b07a679 100644 --- a/src/WidthData.zig +++ b/src/WidthData.zig @@ -55,28 +55,30 @@ pub fn codePointWidth(self: Self, cp: u21) i4 { } test "codePointWidth" { - try testing.expectEqual(@as(i4, 0), codePointWidth(0x0000)); // null - try testing.expectEqual(@as(i4, -1), codePointWidth(0x8)); // \b - try testing.expectEqual(@as(i4, -1), codePointWidth(0x7f)); // DEL - try testing.expectEqual(@as(i4, 0), codePointWidth(0x0005)); // Cf - try testing.expectEqual(@as(i4, 0), codePointWidth(0x0007)); // \a BEL - try testing.expectEqual(@as(i4, 0), codePointWidth(0x000A)); // \n LF - try testing.expectEqual(@as(i4, 0), codePointWidth(0x000B)); // \v VT - try testing.expectEqual(@as(i4, 0), codePointWidth(0x000C)); // \f FF - try testing.expectEqual(@as(i4, 0), codePointWidth(0x000D)); // \r CR - try testing.expectEqual(@as(i4, 0), codePointWidth(0x000E)); // SQ - try testing.expectEqual(@as(i4, 0), codePointWidth(0x000F)); // SI - - try testing.expectEqual(@as(i4, 0), codePointWidth(0x070F)); // Cf - try testing.expectEqual(@as(i4, 1), codePointWidth(0x0603)); // Cf Arabic - - try testing.expectEqual(@as(i4, 1), codePointWidth(0x00AD)); // soft-hyphen - try testing.expectEqual(@as(i4, 2), codePointWidth(0x2E3A)); // two-em dash - try testing.expectEqual(@as(i4, 3), codePointWidth(0x2E3B)); // three-em dash - - try testing.expectEqual(@as(i4, 1), codePointWidth(0x00BD)); // ambiguous halfwidth - - try testing.expectEqual(@as(i4, 1), codePointWidth('é')); - try testing.expectEqual(@as(i4, 2), codePointWidth('😊')); - try testing.expectEqual(@as(i4, 2), codePointWidth('统')); + const wd = try Self.init(std.testing.allocator); + defer wd.deinit(std.testing.allocator); + try testing.expectEqual(@as(i4, 0), wd.codePointWidth(0x0000)); // null + try testing.expectEqual(@as(i4, -1), wd.codePointWidth(0x8)); // \b + try testing.expectEqual(@as(i4, -1), wd.codePointWidth(0x7f)); // DEL + try testing.expectEqual(@as(i4, 0), wd.codePointWidth(0x0005)); // Cf + try testing.expectEqual(@as(i4, 0), wd.codePointWidth(0x0007)); // \a BEL + try testing.expectEqual(@as(i4, 0), wd.codePointWidth(0x000A)); // \n LF + try testing.expectEqual(@as(i4, 0), wd.codePointWidth(0x000B)); // \v VT + try testing.expectEqual(@as(i4, 0), wd.codePointWidth(0x000C)); // \f FF + try testing.expectEqual(@as(i4, 0), wd.codePointWidth(0x000D)); // \r CR + try testing.expectEqual(@as(i4, 0), wd.codePointWidth(0x000E)); // SQ + try testing.expectEqual(@as(i4, 0), wd.codePointWidth(0x000F)); // SI + + try testing.expectEqual(@as(i4, 0), wd.codePointWidth(0x070F)); // Cf + try testing.expectEqual(@as(i4, 1), wd.codePointWidth(0x0603)); // Cf Arabic + + try testing.expectEqual(@as(i4, 1), wd.codePointWidth(0x00AD)); // soft-hyphen + try testing.expectEqual(@as(i4, 2), wd.codePointWidth(0x2E3A)); // two-em dash + try testing.expectEqual(@as(i4, 3), wd.codePointWidth(0x2E3B)); // three-em dash + + try testing.expectEqual(@as(i4, 1), wd.codePointWidth(0x00BD)); // ambiguous halfwidth + + try testing.expectEqual(@as(i4, 1), wd.codePointWidth('é')); + try testing.expectEqual(@as(i4, 2), wd.codePointWidth('😊')); + try testing.expectEqual(@as(i4, 2), wd.codePointWidth('统')); } diff --git a/src/grapheme.zig b/src/grapheme.zig index a802df8..25fd71d 100644 --- a/src/grapheme.zig +++ b/src/grapheme.zig @@ -307,7 +307,7 @@ test "Segmentation ZWJ and ZWSP emoji sequences" { const no_joiner = seq_1 ++ seq_2; const data = try GraphemeData.init(std.testing.allocator); - defer data.deinit(); + defer data.deinit(std.testing.allocator); { var iter = Iterator.init(with_zwj, &data); -- cgit v1.2.3