86
src/main.zig
86
src/main.zig
@@ -1,80 +1,24 @@
|
||||
const std = @import("std");
|
||||
const json = @import("json");
|
||||
|
||||
fn concat(ally: std.mem.Allocator, u: []const u8, v: []const u8) ![]const u8 {
|
||||
const res = try ally.alloc(u8, u.len + v.len);
|
||||
@memcpy(res[0..u.len], u);
|
||||
@memcpy(res[u.len .. u.len + v.len], v);
|
||||
return res;
|
||||
}
|
||||
|
||||
const Shape = union(enum) {
|
||||
rect: struct { w: f32, h: f32 },
|
||||
square: struct { s: f32 },
|
||||
circle: struct { r: f32 },
|
||||
};
|
||||
|
||||
fn show(shape: Shape) void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
const all = gpa.allocator();
|
||||
const text = json.toSlice(all, shape) catch unreachable;
|
||||
defer all.free(text);
|
||||
std.debug.print("shape: {s}\n", .{text});
|
||||
// switch (shape) {
|
||||
// Shape.rect => |r| std.debug.print("rect: {} x {}\n", .{ r.w, r.h }),
|
||||
// Shape.square => |s| std.debug.print("square: {}\n", .{s.s}),
|
||||
// Shape.circle => |c| std.debug.print("circle: {}\n", .{c.r}),
|
||||
// }
|
||||
}
|
||||
|
||||
const Point = struct {
|
||||
x: i32,
|
||||
y: i32,
|
||||
};
|
||||
|
||||
pub fn main() !void {
|
||||
var big_buf: [1 << 20]u8 = undefined; // 1 MB
|
||||
var fixed = std.heap.FixedBufferAllocator.init(&big_buf);
|
||||
const ally = fixed.allocator();
|
||||
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
|
||||
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
|
||||
|
||||
var s: Shape = Shape{ .circle = .{ .r = 99 } };
|
||||
show(s);
|
||||
s = Shape{ .rect = .{ .w = 1, .h = 2 } };
|
||||
show(s);
|
||||
// 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.
|
||||
const stdout_file = std.io.getStdOut().writer();
|
||||
var bw = std.io.bufferedWriter(stdout_file);
|
||||
const stdout = bw.writer();
|
||||
|
||||
const val = Point{ .x = 1, .y = 2 };
|
||||
const ser = try json.toSlice(ally, val);
|
||||
defer ally.free(ser);
|
||||
try stdout.print("Run `zig build test` to run the tests.\n", .{});
|
||||
|
||||
std.debug.print("{s}\n", .{ser});
|
||||
|
||||
const deser = try json.fromSlice(ally, Point, ser);
|
||||
std.debug.print("{any}\n", .{deser});
|
||||
|
||||
var hash_map = std.StringArrayHashMap([]const u8).init(ally);
|
||||
defer hash_map.deinit();
|
||||
|
||||
try hash_map.put("foo", "bar");
|
||||
try hash_map.put("fizz", "buzz");
|
||||
try hash_map.put("qux", "quxx");
|
||||
|
||||
const x = try concat(ally, hash_map.get("fizz").?, hash_map.get("foo").?);
|
||||
defer ally.free(x);
|
||||
|
||||
std.debug.print("x={s}\n", .{x});
|
||||
|
||||
var iter = hash_map.iterator();
|
||||
while (iter.next()) |it| {
|
||||
std.debug.print("{s} {s}\n", .{ it.key_ptr.*, it.value_ptr.* });
|
||||
}
|
||||
try bw.flush(); // don't forget to flush!
|
||||
}
|
||||
|
||||
test concat {
|
||||
const x = "hello";
|
||||
const y = "world";
|
||||
|
||||
const z = try concat(std.testing.allocator, x, y);
|
||||
defer std.testing.allocator.free(z);
|
||||
|
||||
try std.testing.expectEqualStrings(z, "helloworld");
|
||||
test "simple test" {
|
||||
var list = std.ArrayList(i32).init(std.testing.allocator);
|
||||
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
|
||||
try list.append(42);
|
||||
try std.testing.expectEqual(@as(i32, 42), list.pop());
|
||||
}
|
||||
|
10
src/root.zig
Normal file
10
src/root.zig
Normal file
@@ -0,0 +1,10 @@
|
||||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
|
||||
export fn add(a: i32, b: i32) i32 {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
test "basic add functionality" {
|
||||
try testing.expect(add(3, 7) == 10);
|
||||
}
|
Reference in New Issue
Block a user