summaryrefslogtreecommitdiff
path: root/src/unicode_tests.zig
blob: ae177a91896a4fc104b4b70bbc60b0425dfc19d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
const dbg_print = false;

test "Unicode normalization tests" {
    var arena = heap.ArenaAllocator.init(testing.allocator);
    defer arena.deinit();
    var allocator = arena.allocator();

    const n = try Normalize.init(allocator);
    defer n.deinit(allocator);

    var file = try fs.cwd().openFile("data/unicode/NormalizationTest.txt", .{});
    defer file.close();
    var buf_reader = io.bufferedReader(file.reader());
    var input_stream = buf_reader.reader();

    var buf: [4096]u8 = undefined;
    var cp_buf: [4]u8 = undefined;

    var line_iter: IterRead = .{ .read = &input_stream };

    while (try line_iter.next(&buf)) |line| {
        // Iterate over fields.
        var fields = mem.splitScalar(u8, line, ';');
        var field_index: usize = 0;
        var input: []u8 = undefined;
        defer allocator.free(input);

        while (fields.next()) |field| : (field_index += 1) {
            if (field_index == 0) {
                var i_buf = std.ArrayList(u8).init(allocator);
                defer i_buf.deinit();

                var i_fields = mem.splitScalar(u8, field, ' ');
                while (i_fields.next()) |s| {
                    const icp = try fmt.parseInt(u21, s, 16);
                    const len = try unicode.utf8Encode(icp, &cp_buf);
                    try i_buf.appendSlice(cp_buf[0..len]);
                }

                input = try i_buf.toOwnedSlice();
            } else if (field_index == 1) {
                if (dbg_print) debug.print("\n*** {s} ***\n", .{line});
                // NFC, time to test.
                var w_buf = std.ArrayList(u8).init(allocator);
                defer w_buf.deinit();

                var w_fields = mem.splitScalar(u8, field, ' ');
                while (w_fields.next()) |s| {
                    const wcp = try fmt.parseInt(u21, s, 16);
                    const len = try unicode.utf8Encode(wcp, &cp_buf);
                    try w_buf.appendSlice(cp_buf[0..len]);
                }

                const want = w_buf.items;
                var got = try n.nfc(allocator, input);
                defer got.deinit(allocator);

                try testing.expectEqualStrings(want, got.slice);
            } else if (field_index == 2) {
                // NFD, time to test.
                var w_buf = std.ArrayList(u8).init(allocator);
                defer w_buf.deinit();

                var w_fields = mem.splitScalar(u8, field, ' ');
                while (w_fields.next()) |s| {
                    const wcp = try fmt.parseInt(u21, s, 16);
                    const len = try unicode.utf8Encode(wcp, &cp_buf);
                    try w_buf.appendSlice(cp_buf[0..len]);
                }

                const want = w_buf.items;
                var got = try n.nfd(allocator, input);
                defer got.deinit(allocator);

                try testing.expectEqualStrings(want, got.slice);
            } else if (field_index == 3) {
                // NFKC, time to test.
                var w_buf = std.ArrayList(u8).init(allocator);
                defer w_buf.deinit();

                var w_fields = mem.splitScalar(u8, field, ' ');
                while (w_fields.next()) |s| {
                    const wcp = try fmt.parseInt(u21, s, 16);
                    const len = try unicode.utf8Encode(wcp, &cp_buf);
                    try w_buf.appendSlice(cp_buf[0..len]);
                }

                const want = w_buf.items;
                var got = try n.nfkc(allocator, input);
                defer got.deinit(allocator);

                try testing.expectEqualStrings(want, got.slice);
            } else if (field_index == 4) {
                // NFKD, time to test.
                var w_buf = std.ArrayList(u8).init(allocator);
                defer w_buf.deinit();

                var w_fields = mem.splitScalar(u8, field, ' ');
                while (w_fields.next()) |s| {
                    const wcp = try fmt.parseInt(u21, s, 16);
                    const len = try unicode.utf8Encode(wcp, &cp_buf);
                    try w_buf.appendSlice(cp_buf[0..len]);
                }

                const want = w_buf.items;
                const got = try n.nfkd(allocator, input);
                defer got.deinit(allocator);

                try testing.expectEqualStrings(want, got.slice);
            } else {
                continue;
            }
        }
    }
}

test "Segmentation GraphemeIterator" {
    const allocator = std.testing.allocator;
    var file = try std.fs.cwd().openFile("data/unicode/auxiliary/GraphemeBreakTest.txt", .{});
    defer file.close();
    var buf_reader = std.io.bufferedReader(file.reader());
    var input_stream = buf_reader.reader();

    const graph = try Graphemes.init(allocator);
    defer graph.deinit(allocator);

    var buf: [4096]u8 = undefined;
    var line_iter: IterRead = .{ .read = &input_stream };

    while (try line_iter.next(&buf)) |raw| {
        // Clean up.
        var line = std.mem.trimLeft(u8, raw, "÷ ");
        if (std.mem.indexOf(u8, line, " ÷\t")) |final| {
            line = line[0..final];
        }
        // Iterate over fields.
        var want = std.ArrayList(Grapheme).init(allocator);
        defer want.deinit();

        var all_bytes = std.ArrayList(u8).init(allocator);
        defer all_bytes.deinit();

        var graphemes = std.mem.splitSequence(u8, line, " ÷ ");
        var bytes_index: uoffset = 0;

        while (graphemes.next()) |field| {
            var code_points = std.mem.splitScalar(u8, field, ' ');
            var cp_buf: [4]u8 = undefined;
            var cp_index: uoffset = 0;
            var gc_len: u8 = 0;

            while (code_points.next()) |code_point| {
                if (std.mem.eql(u8, code_point, "×")) continue;
                const cp: u21 = try std.fmt.parseInt(u21, code_point, 16);
                const len = try unicode.utf8Encode(cp, &cp_buf);
                try all_bytes.appendSlice(cp_buf[0..len]);
                cp_index += len;
                gc_len += len;
            }

            try want.append(Grapheme{ .len = gc_len, .offset = bytes_index });
            bytes_index += cp_index;
        }

        const this_str = all_bytes.items;

        {
            var iter = graph.iterator(this_str);

            // Check.
            for (want.items, 1..) |want_gc, idx| {
                const got_gc = (iter.next()).?;
                try std.testing.expectEqualStrings(
                    want_gc.bytes(this_str),
                    got_gc.bytes(this_str),
                );
                for (got_gc.offset..got_gc.offset + got_gc.len) |i| {
                    const this_gc = graph.graphemeAtIndex(this_str, i);
                    std.testing.expectEqualSlices(
                        u8,
                        got_gc.bytes(this_str),
                        this_gc.bytes(this_str),
                    ) catch |err| {
                        debug.print("Wrong grapheme on line {d} #{d} offset {d}\n", .{ line_iter.line, idx, i });
                        return err;
                    };
                }
                var after_iter = graph.iterateAfterGrapheme(this_str, got_gc);
                if (after_iter.next()) |next_gc| {
                    if (iter.peek()) |next_peek| {
                        std.testing.expectEqualSlices(
                            u8,
                            next_gc.bytes(this_str),
                            next_peek.bytes(this_str),
                        ) catch |err| {
                            debug.print("Peeks differ on line {d} #{d} \n", .{ line_iter.line, idx });
                            return err;
                        };
                    } else {
                        debug.print("Mismatch: peek missing, next found, line {d} #{d}\n", .{ line_iter.line, idx });
                        try testing.expect(false);
                    }
                } else {
                    try testing.expectEqual(null, iter.peek());
                }
            }
        }
        {
            var iter = graph.reverseIterator(this_str);

            // Check.
            var i: usize = want.items.len;
            while (i > 0) {
                i -= 1;
                const want_gc = want.items[i];
                const got_gc = iter.prev() orelse {
                    std.debug.print(
                        "line {d} grapheme {d}: expected {any} found null\n",
                        .{ line_iter.line, i, want_gc },
                    );
                    return error.TestExpectedEqual;
                };
                std.testing.expectEqualStrings(
                    want_gc.bytes(this_str),
                    got_gc.bytes(this_str),
                ) catch |err| {
                    std.debug.print(
                        "line {d} grapheme {d}: expected {any} found {any}\n",
                        .{ line_iter.line, i, want_gc, got_gc },
                    );
                    return err;
                };
                var before_iter = graph.iterateBeforeGrapheme(this_str, got_gc);
                if (before_iter.prev()) |prev_gc| {
                    if (iter.peek()) |prev_peek| {
                        std.testing.expectEqualSlices(
                            u8,
                            prev_gc.bytes(this_str),
                            prev_peek.bytes(this_str),
                        ) catch |err| {
                            debug.print("Peeks differ on line {d} #{d} \n", .{ line_iter.line, i });
                            return err;
                        };
                    } else {
                        debug.print("Mismatch: peek missing, prev found, line {d} #{d}\n", .{ line_iter.line, i });
                        try testing.expect(false);
                    }
                } else {
                    try testing.expectEqual(null, iter.peek());
                }
            }
        }
    }
}

test "Segmentation Word Iterator" {
    const allocator = std.testing.allocator;
    var file = try std.fs.cwd().openFile("data/unicode/auxiliary/WordBreakTest.txt", .{});
    defer file.close();
    var buf_reader = std.io.bufferedReader(file.reader());
    var input_stream = buf_reader.reader();

    const wb = try Words.init(allocator);
    defer wb.deinit(allocator);

    var buf: [4096]u8 = undefined;
    var line_iter: IterRead = .{ .read = &input_stream };

    while (try line_iter.next(&buf)) |raw| {
        // Clean up.
        var line = std.mem.trimLeft(u8, raw, "÷ ");
        if (std.mem.indexOf(u8, line, " ÷\t")) |final| {
            line = line[0..final];
        }
        // Iterate over fields.
        var want = std.ArrayList(Word).init(allocator);
        defer want.deinit();

        var all_bytes = std.ArrayList(u8).init(allocator);
        defer all_bytes.deinit();

        var words = std.mem.splitSequence(u8, line, " ÷ ");
        var bytes_index: uoffset = 0;

        while (words.next()) |field| {
            var code_points = std.mem.splitScalar(u8, field, ' ');
            var cp_buf: [4]u8 = undefined;
            var cp_index: uoffset = 0;
            var gc_len: u8 = 0;

            while (code_points.next()) |code_point| {
                if (std.mem.eql(u8, code_point, "×")) continue;
                const cp: u21 = try std.fmt.parseInt(u21, code_point, 16);
                const len = try unicode.utf8Encode(cp, &cp_buf);
                try all_bytes.appendSlice(cp_buf[0..len]);
                cp_index += len;
                gc_len += len;
            }

            try want.append(Word{ .len = gc_len, .offset = bytes_index });
            bytes_index += cp_index;
        }
        const this_str = all_bytes.items;

        {
            var iter = wb.iterator(this_str);
            var peeked: ?Word = iter.peek();

            // Check.
            for (want.items, 1..) |want_word, idx| {
                const got_word = (iter.next()).?;
                std.testing.expectEqualStrings(
                    want_word.bytes(this_str),
                    got_word.bytes(this_str),
                ) catch |err| {
                    debug.print("Error on line {d}, #{d}\n", .{ line_iter.line, idx });
                    return err;
                };
                std.testing.expectEqualStrings(
                    peeked.?.bytes(this_str),
                    got_word.bytes(this_str),
                ) catch |err| {
                    debug.print("Peek != word on line {d} #{d}\n", .{ line_iter.line, idx });
                    return err;
                };
                var r_iter = iter.reverseIterator();
                const if_r_word = r_iter.prev();
                if (if_r_word) |r_word| {
                    std.testing.expectEqualStrings(
                        want_word.bytes(this_str),
                        r_word.bytes(this_str),
                    ) catch |err| {
                        debug.print("Reversal Error on line {d}, #{d}\n", .{ line_iter.line, idx });
                        return err;
                    };
                } else {
                    try testing.expect(false);
                }
                var peek_iter = wb.iterateAfterWord(this_str, got_word);
                const peek_1 = peek_iter.next();
                if (peek_1) |p1| {
                    const peek_2 = iter.peek();
                    if (peek_2) |p2| {
                        std.testing.expectEqualSlices(
                            u8,
                            p1.bytes(this_str),
                            p2.bytes(this_str),
                        ) catch |err| {
                            debug.print("Bad peek on line {d} #{d} offset {d}\n", .{ line_iter.line, idx + 1, idx });
                            return err;
                        };
                    } else {
                        try testing.expect(false);
                    }
                } else {
                    try testing.expectEqual(null, iter.peek());
                }
                for (got_word.offset..got_word.offset + got_word.len) |i| {
                    const this_word = wb.wordAtIndex(this_str, i);
                    std.testing.expectEqualSlices(
                        u8,
                        got_word.bytes(this_str),
                        this_word.bytes(this_str),
                    ) catch |err| {
                        debug.print("Wrong word on line {d} #{d} offset {d}\n", .{ line_iter.line, idx, i });
                        return err;
                    };
                }
                peeked = iter.peek();
            }
        }
        {
            var r_iter = wb.reverseIterator(this_str);
            var peeked: ?Word = r_iter.peek();
            var idx = want.items.len - 1;

            while (true) : (idx -= 1) {
                const want_word = want.items[idx];
                const got_word = r_iter.prev().?;
                std.testing.expectEqualSlices(
                    u8,
                    want_word.bytes(this_str),
                    got_word.bytes(this_str),
                ) catch |err| {
                    debug.print("Error on line {d}, #{d}\n", .{ line_iter.line, idx + 1 });
                    return err;
                };
                std.testing.expectEqualStrings(
                    peeked.?.bytes(this_str),
                    got_word.bytes(this_str),
                ) catch |err| {
                    debug.print("Peek != word on line {d} #{d}\n", .{ line_iter.line, idx + 1 });
                    return err;
                };
                var f_iter = r_iter.forwardIterator();
                const if_f_word = f_iter.next();
                if (if_f_word) |f_word| {
                    std.testing.expectEqualStrings(
                        want_word.bytes(this_str),
                        f_word.bytes(this_str),
                    ) catch |err| {
                        debug.print("Reversal Error on line {d}, #{d}\n", .{ line_iter.line, idx });
                        return err;
                    };
                } else {
                    try testing.expect(false);
                }
                var peek_iter = wb.iterateBeforeWord(this_str, got_word);
                const peek_1 = peek_iter.prev();
                if (peek_1) |p1| {
                    const peek_2 = r_iter.peek();
                    if (peek_2) |p2| {
                        std.testing.expectEqualSlices(
                            u8,
                            p1.bytes(this_str),
                            p2.bytes(this_str),
                        ) catch |err| {
                            debug.print("Bad peek on line {d} #{d} offset {d}\n", .{ line_iter.line, idx + 1, idx });
                            return err;
                        };
                    } else {
                        try testing.expect(false);
                    }
                } else {
                    try testing.expectEqual(null, r_iter.peek());
                }
                for (got_word.offset..got_word.offset + got_word.len) |i| {
                    const this_word = wb.wordAtIndex(this_str, i);
                    std.testing.expectEqualSlices(
                        u8,
                        got_word.bytes(this_str),
                        this_word.bytes(this_str),
                    ) catch |err| {
                        debug.print("Wrong word on line {d} #{d} offset {d}\n", .{ line_iter.line, idx + 1, i });
                        return err;
                    };
                }
                peeked = r_iter.peek();
                if (idx == 0) break;
            }
        }
    }
}

const IterRead = struct {
    read: *Reader,
    line: usize = 0,

    pub fn next(iter: *IterRead, buf: []u8) !?[]const u8 {
        defer iter.line += 1;
        const maybe_line = try iter.read.readUntilDelimiterOrEof(buf, '#');
        if (maybe_line) |this_line| {
            try iter.read.skipUntilDelimiterOrEof('\n');
            if (this_line.len == 0 or this_line[0] == '@') {
                // comment, next line
                return iter.next(buf);
            } else {
                return this_line;
            }
        } else {
            return null;
        }
    }
};

const std = @import("std");
const fmt = std.fmt;
const fs = std.fs;
const io = std.io;
const Reader = io.BufferedReader(4096, fs.File.Reader).Reader;
const heap = std.heap;
const mem = std.mem;
const debug = std.debug;
const testing = std.testing;
const unicode = std.unicode;

const uoffset = @FieldType(Word, "offset");

const Grapheme = @import("Graphemes").Grapheme;
const Graphemes = @import("Graphemes");
const GraphemeIterator = @import("Graphemes").Iterator;
const Normalize = @import("Normalize");

const Words = @import("Words");
const Word = Words.Word;