| 1 | const std = @import("std"); |
| 2 | const Io = std.Io; |
| 3 | |
| 4 | const _NAME = @import(".NAME"); |
| 5 | |
| 6 | pub fn main(init: std.process.Init) !void { |
| 7 | // Prints to stderr, unbuffered, ignoring potential errors. |
| 8 | std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); |
| 9 | |
| 10 | // This is appropriate for anything that lives as long as the process. |
| 11 | const arena: std.mem.Allocator = init.arena.allocator(); |
| 12 | |
| 13 | // Accessing command line arguments: |
| 14 | const args = try init.minimal.args.toSlice(arena); |
| 15 | for (args) |arg| { |
| 16 | std.log.info("arg: {s}", .{arg}); |
| 17 | } |
| 18 | |
| 19 | // In order to do I/O operations need an `Io` instance. |
| 20 | const io = init.io; |
| 21 | |
| 22 | // Stdout is for the actual output of your application, for example if you |
| 23 | // are implementing gzip, then only the compressed bytes should be sent to |
| 24 | // stdout, not any debugging messages. |
| 25 | var stdout_buffer: [1024]u8 = undefined; |
| 26 | var stdout_file_writer: Io.File.Writer = .init(.stdout(), io, &stdout_buffer); |
| 27 | const stdout_writer = &stdout_file_writer.interface; |
| 28 | |
| 29 | try _NAME.printAnotherMessage(stdout_writer); |
| 30 | |
| 31 | try stdout_writer.flush(); // Don't forget to flush! |
| 32 | } |
| 33 | |
| 34 | test "simple test" { |
| 35 | const gpa = std.testing.allocator; |
| 36 | var list: std.ArrayList(i32) = .empty; |
| 37 | defer list.deinit(gpa); // Try commenting this out and see if zig detects the memory leak! |
| 38 | try list.append(gpa, 42); |
| 39 | try std.testing.expectEqual(@as(i32, 42), list.pop()); |
| 40 | } |
| 41 | |
| 42 | test "fuzz example" { |
| 43 | try std.testing.fuzz({}, testOne, .{}); |
| 44 | } |
| 45 | |
| 46 | fn testOne(context: void, smith: *std.testing.Smith) !void { |
| 47 | _ = context; |
| 48 | // Try command `zig build test --fuzz -Doptimize=ReleaseFast` to see if it manages to fail this test case! |
| 49 | |
| 50 | const gpa = std.testing.allocator; |
| 51 | var list: std.ArrayList(u8) = .empty; |
| 52 | defer list.deinit(gpa); |
| 53 | while (!smith.eos()) switch (smith.value(enum { add_data, dup_data })) { |
| 54 | .add_data => { |
| 55 | const slice = try list.addManyAsSlice(gpa, smith.value(u4)); |
| 56 | smith.bytes(slice); |
| 57 | }, |
| 58 | .dup_data => { |
| 59 | if (list.items.len == 0) continue; |
| 60 | if (list.items.len > std.math.maxInt(u32)) return error.SkipZigTest; |
| 61 | const len = smith.valueRangeAtMost(u32, 1, @min(32, list.items.len)); |
| 62 | const off = smith.valueRangeAtMost(u32, 0, @intCast(list.items.len - len)); |
| 63 | try list.appendSlice(gpa, list.items[off..][0..len]); |
| 64 | try std.testing.expectEqualSlices( |
| 65 | u8, |
| 66 | list.items[off..][0..len], |
| 67 | list.items[list.items.len - len ..], |
| 68 | ); |
| 69 | }, |
| 70 | }; |
| 71 | } |