mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-04-28 19:47:08 +03:00
52 lines
1.8 KiB
Zig
52 lines
1.8 KiB
Zig
const std = @import("std");
|
|
const Io = std.Io;
|
|
|
|
const _NAME = @import(".NAME");
|
|
|
|
pub fn main(init: std.process.Init) !void {
|
|
// Prints to stderr, unbuffered, ignoring potential errors.
|
|
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
|
|
|
|
// This is appropriate for anything that lives as long as the process.
|
|
const arena: std.mem.Allocator = init.arena.allocator();
|
|
|
|
// Accessing command line arguments:
|
|
const args = try init.minimal.args.toSlice(arena);
|
|
for (args) |arg| {
|
|
std.log.info("arg: {s}", .{arg});
|
|
}
|
|
|
|
// In order to do I/O operations need an `Io` instance.
|
|
const io = init.io;
|
|
|
|
// Stdout is for the actual output of your application, for example if you
|
|
// are implementing gzip, then only the compressed bytes should be sent to
|
|
// stdout, not any debugging messages.
|
|
var stdout_buffer: [1024]u8 = undefined;
|
|
var stdout_file_writer: Io.File.Writer = .init(.stdout(), io, &stdout_buffer);
|
|
const stdout_writer = &stdout_file_writer.interface;
|
|
|
|
try _NAME.printAnotherMessage(stdout_writer);
|
|
|
|
try stdout_writer.flush(); // Don't forget to flush!
|
|
}
|
|
|
|
test "simple test" {
|
|
const gpa = std.testing.allocator;
|
|
var list: std.ArrayList(i32) = .empty;
|
|
defer list.deinit(gpa); // Try commenting this out and see if zig detects the memory leak!
|
|
try list.append(gpa, 42);
|
|
try std.testing.expectEqual(@as(i32, 42), list.pop());
|
|
}
|
|
|
|
test "fuzz example" {
|
|
const Context = struct {
|
|
fn testOne(context: @This(), input: []const u8) anyerror!void {
|
|
_ = context;
|
|
// Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case!
|
|
try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input));
|
|
}
|
|
};
|
|
try std.testing.fuzz(Context{}, Context.testOne, .{});
|
|
}
|