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