| 1 | // zig run this file inside the test_parsing/ directory of this repo: https://github.com/nst/JSONTestSuite |
| 2 | |
| 3 | const std = @import("std"); |
| 4 | const Io = std.Io; |
| 5 | |
| 6 | pub fn main(init: std.process.Init) !void { |
| 7 | const allocator = init.arena.allocator(); |
| 8 | const io = init.io; |
| 9 | |
| 10 | var stdout_buffer: [2000]u8 = undefined; |
| 11 | var stdout_writer = Io.File.stdout().writerStreaming(io, &stdout_buffer); |
| 12 | const output = &stdout_writer.interface; |
| 13 | try output.writeAll( |
| 14 | \\//! This file was generated by _generate_JSONTestSuite.zig |
| 15 | \\//! These test cases are sourced from: https://github.com/nst/JSONTestSuite |
| 16 | \\const ok = @import("./test.zig").ok; |
| 17 | \\const err = @import("./test.zig").err; |
| 18 | \\const any = @import("./test.zig").any; |
| 19 | \\ |
| 20 | \\ |
| 21 | ); |
| 22 | |
| 23 | var names = std.array_list.Managed([]const u8).init(allocator); |
| 24 | var cwd = try Io.Dir.cwd().openDir(io, ".", .{ .iterate = true }); |
| 25 | var it = cwd.iterate(); |
| 26 | while (try it.next(io)) |entry| { |
| 27 | try names.append(try allocator.dupe(u8, entry.name)); |
| 28 | } |
| 29 | std.mem.sort([]const u8, names.items, {}, (struct { |
| 30 | fn lessThan(_: void, a: []const u8, b: []const u8) bool { |
| 31 | return std.mem.lessThan(u8, a, b); |
| 32 | } |
| 33 | }).lessThan); |
| 34 | |
| 35 | for (names.items) |name| { |
| 36 | const contents = try Io.Dir.cwd().readFileAlloc(io, name, allocator, .limited(300000)); |
| 37 | try output.writeAll("test "); |
| 38 | try writeString(output, name); |
| 39 | try output.writeAll(" {\n try "); |
| 40 | switch (name[0]) { |
| 41 | 'y' => try output.writeAll("ok"), |
| 42 | 'n' => try output.writeAll("err"), |
| 43 | 'i' => try output.writeAll("any"), |
| 44 | else => unreachable, |
| 45 | } |
| 46 | try output.writeByte('('); |
| 47 | try writeString(output, contents); |
| 48 | try output.writeAll(");\n}\n"); |
| 49 | } |
| 50 | |
| 51 | try output.flush(); |
| 52 | } |
| 53 | |
| 54 | const i_structure_500_nested_arrays = &(@as([500]u8, @splat('[')) ++ @as([500]u8, @splat(']'))); |
| 55 | const n_structure_100000_opening_arrays: *const [100000]u8 = &@splat('['); |
| 56 | const n_structure_open_array_object = str: { |
| 57 | const part = "[{\"\":"; |
| 58 | const buf: [50000][part.len]u8 = @splat(part.*); |
| 59 | const s: []const u8 = @ptrCast(&buf); |
| 60 | break :str s ++ "\n"; |
| 61 | }; |
| 62 | |
| 63 | fn writeString(writer: anytype, s: []const u8) !void { |
| 64 | if (s.len > 200) { |
| 65 | // There are a few of these we can compress with Zig expressions. |
| 66 | if (std.mem.eql(u8, s, i_structure_500_nested_arrays)) { |
| 67 | return writer.writeAll("&@as([500]u8, @splat('[')) ++ &@as([500]u8, @splat(']'))"); |
| 68 | } else if (std.mem.eql(u8, s, n_structure_100000_opening_arrays)) { |
| 69 | return writer.writeAll("&@as([100000]u8, @splat('['))"); |
| 70 | } else if (std.mem.eql(u8, s, n_structure_open_array_object)) { |
| 71 | return writer.writeAll( |
| 72 | \\str: { |
| 73 | \\ const part = "[{\"\":"; |
| 74 | \\ const buf: [50000][part.len]u8 = @splat(part.*); |
| 75 | \\ const s: []const u8 = @ptrCast(&buf); |
| 76 | \\ break :str s ++ "\n"; |
| 77 | \\ } |
| 78 | ); |
| 79 | } else { |
| 80 | @panic("unhandled long string literal"); |
| 81 | } |
| 82 | } |
| 83 | try writer.writeByte('"'); |
| 84 | for (s) |b| { |
| 85 | switch (b) { |
| 86 | 0...('\n' - 1), |
| 87 | ('\n' + 1)...0x1f, |
| 88 | 0x7f...0xff, |
| 89 | => try writer.print("\\x{x:0>2}", .{b}), |
| 90 | '\n' => try writer.writeAll("\\n"), |
| 91 | '"' => try writer.writeAll("\\\""), |
| 92 | '\\' => try writer.writeAll("\\\\"), |
| 93 | else => try writer.writeByte(b), |
| 94 | } |
| 95 | } |
| 96 | try writer.writeByte('"'); |
| 97 | } |