// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! The UCA conformance suite, UTS #10 section 12.2. //! //! The files are built so that each line sorts at or after the line before //! it. Section 12.2 spells out the procedure: read a line, parse the //! sequence before the semicolon into a string, compare it against the //! previous line at identical strength using S3.10, and stop with an error //! if the order is wrong. //! //! Two of the four files are read, the `_SHORT` ones -- they hold the same //! cases as the long ones without the comments. `NON_IGNORABLE` and //! `SHIFTED` are the two `Alternate` values that section 12.2 says the files //! correspond to, so between them they pin both. //! //! The suite deliberately includes ill-formed strings with surrogate code //! points. Section 12.2 allows an implementation that does not weight them //! as reserved code points to filter those lines out; this one does weight //! them that way -- section 10.1.1's second approach -- so nothing is //! filtered and a failure on such a line is a real failure. //! //! This is the acceptance criterion for the library rather than a smoke //! test. Until it passes end to end, the library does not implement UTS #10. //! //! CLDR ships the same four files for its own root collation, with a `_CLDR` //! infix, because the CLDR root is a tailoring of DUCET and therefore orders //! things differently; `zig build test-conformance-cldr` runs those against //! `collate.cldr_root`. //! //! Usage: `conformance [ducet|cldr]`, or `zig build //! test-conformance` / `zig build test-conformance-cldr`. const std = @import("std"); const collate = @import("collate"); const Case = struct { file: []const u8, alternate: collate.Alternate, }; const Suite = struct { table: collate.Table, cases: []const Case, }; const ducet_suite: Suite = .{ .table = collate.ducet, .cases = &.{ .{ .file = "CollationTest_NON_IGNORABLE_SHORT.txt", .alternate = .non_ignorable }, .{ .file = "CollationTest_SHIFTED_SHORT.txt", .alternate = .shifted }, }, }; const cldr_suite: Suite = .{ .table = collate.cldr_root, .cases = &.{ .{ .file = "CollationTest_CLDR_NON_IGNORABLE_SHORT.txt", .alternate = .non_ignorable }, .{ .file = "CollationTest_CLDR_SHIFTED_SHORT.txt", .alternate = .shifted }, }, }; pub fn main(init: std.process.Init.Minimal) !u8 { var gpa_state: std.heap.DebugAllocator(.{}) = .init; defer _ = gpa_state.deinit(); const gpa = gpa_state.allocator(); var threaded: std.Io.Threaded = .init(gpa, .{}); defer threaded.deinit(); const io = threaded.io(); var arena_state: std.heap.ArenaAllocator = .init(gpa); defer arena_state.deinit(); const arena = arena_state.allocator(); const args = try init.args.toSlice(arena); if (args.len != 2 and args.len != 3) { std.debug.print("usage: conformance [ducet|cldr]\n", .{}); return error.WrongNumberOfArguments; } const suite: Suite = if (args.len == 3 and std.mem.eql(u8, args[2], "cldr")) cldr_suite else ducet_suite; if (suite.table.stage1.len == 0) { std.debug.print( "that table was not compiled in; build with -Dembed-ducet or -Dembed-cldr\n", .{}, ); return error.NoTable; } var stdout_buffer: [4096]u8 = undefined; var stdout = std.Io.File.stdout().writer(io, &stdout_buffer); const out = &stdout.interface; var failed = false; for (suite.cases) |case| { // The file and its path come from the general allocator and are // freed here rather than from the arena: the arena holds `args`, // which the next iteration still needs. const path = try std.fs.path.join(gpa, &.{ args[1], case.file }); defer gpa.free(path); const text = try std.Io.Dir.cwd().readFileAlloc(io, path, gpa, .limited(64 << 20)); defer gpa.free(text); const result = try run(gpa, out, suite.table, case, text); if (result.failures != 0) failed = true; } try out.flush(); return if (failed) 1 else 0; } const Result = struct { lines: usize, failures: usize, skipped: usize = 0 }; fn run( gpa: std.mem.Allocator, out: *std.Io.Writer, table: collate.Table, case: Case, text: []const u8, ) !Result { // Identical strength, which is what section 12.2 asks for: the suite // contains pairs that are equal at every weight level and are ordered // only by S3.10. const collator: collate.Collator = .{ .table = table, .options = .{ .strength = .identical, .alternate = case.alternate }, }; var builder: collate.Builder = .empty; defer builder.deinit(gpa); var previous_key: std.ArrayList(u8) = .empty; defer previous_key.deinit(gpa); var previous_line: std.ArrayList(u8) = .empty; defer previous_line.deinit(gpa); var cps: std.ArrayList(u21) = .empty; defer cps.deinit(gpa); var nfd: std.ArrayList(u21) = .empty; defer nfd.deinit(gpa); var result: Result = .{ .lines = 0, .failures = 0, .skipped = 0 }; var lines = std.mem.splitScalar(u8, text, '\n'); while (lines.next()) |raw| { const line = trim(raw); if (line.len == 0 or line[0] == '#' or line[0] == '@') continue; const body = if (std.mem.findScalar(u8, line, ';')) |i| line[0..i] else line; cps.clearRetainingCapacity(); var fields = std.mem.tokenizeAny(u8, body, " \t"); while (fields.next()) |field| { try cps.append(gpa, try std.fmt.parseInt(u21, field, 16)); } if (cps.items.len == 0) continue; // Section 9.3.3: a restricted table has no entries outside its // repertoire, so a line that reaches outside it is not a claim the // table makes. Skipping such lines still tests something real, and // something quite strong: the file is one totally ordered list, so // every subsequence of it is ordered too, and the lines that remain // must come out in the same order they do under the full table. That // is exactly what restricting is supposed to leave alone. if (table.repertoire.len != 0) { // The test is on the *decomposition*, because that is what gets // looked up: step 1 replaces a canonically decomposable code // point before anything reaches the table, so a line written with // one outside the repertoire is still answerable when what it // decomposes to is inside. Filtering on the line as written would // skip those, and they are worth checking. nfd.clearRetainingCapacity(); try collate.nfd.append(&nfd, gpa, cps.items); var reaches_outside = false; for (nfd.items) |cp| { if (!table.inRepertoire(cp)) reaches_outside = true; } if (reaches_outside) { result.skipped += 1; continue; } } result.lines += 1; const key = try builder.sortKeyAllocCodePoints(gpa, collator, cps.items); defer gpa.free(key); if (result.lines > 1 and collate.orderKeys(previous_key.items, key) == .gt) { result.failures += 1; // The first few are the diagnosis; the rest is noise. if (result.failures <= 10) { try out.print( "{s}: line {d} sorts before the line above it\n above: {s}\n here: {s}\n", .{ case.file, result.lines, previous_line.items, trim(body) }, ); } } previous_key.clearRetainingCapacity(); try previous_key.appendSlice(gpa, key); previous_line.clearRetainingCapacity(); try previous_line.appendSlice(gpa, trim(body)); } if (result.skipped == 0) { try out.print("{s}: {d} cases, {d} out of order\n", .{ case.file, result.lines, result.failures, }); } else { try out.print( "{s}: {d} cases, {d} out of order, {d} skipped as outside the repertoire\n", .{ case.file, result.lines, result.failures, result.skipped }, ); } return result; } fn trim(s: []const u8) []const u8 { return std.mem.trim(u8, s, " \t\r"); }