root.zig
1 //! By convention, root.zig is the root source file when making a library. 2 //! 3 //! This file exposes reusable functionality and tests. 4 //! It does NOT assume global stdout access. 5 //! Instead, IO is passed in explicitly (Zig 0.16 style). 6 7 const std = @import("std"); 8 9 const arithmetic = @cImport({ 10 @cInclude("arithmetic/arithmetic.h"); 11 }); 12 13 /// Prints a message using a buffered writer. 14 /// 15 /// Stdout is for the actual output of your application. 16 /// For example, if you are implementing gzip, then only the compressed 17 /// bytes should be sent to stdout, not debugging messages. 18 /// 19 /// We use a buffer to reduce syscalls and improve performance. 20 pub fn bufferedPrint(io: std.Io) !void { 21 var stdout_buffer: [1024]u8 = undefined; 22 23 // Create a buffered writer targeting stdout. 24 var stdout_writer = std.Io.File.stdout().writer(io, &stdout_buffer); 25 const stdout = &stdout_writer.interface; 26 27 try stdout.print("Run `zig build test` to run the tests.\n", .{}); 28 29 // Always flush buffered output! 30 try stdout.flush(); 31 } 32 33 /// Simple addition function to demonstrate exported library logic. 34 pub fn add(a: i32, b: i32) i32 { 35 return a + b; 36 } 37 38 pub fn add_c(x: i32, y: i32) i32 { 39 return arithmetic.add(x, y); 40 } 41 42 test "basic add functionality" { 43 try std.testing.expect(add(3, 7) == 10); 44 }