Skip to content

Commit

Permalink
core: ci skip: separate scratch file
Browse files Browse the repository at this point in the history
  • Loading branch information
flowerinthenight committed Sep 21, 2024
1 parent 37e04e1 commit 7dd147c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 111 deletions.
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn build(b: *std.Build) void {
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

const exe_unit_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.root_source_file = b.path("src/scratch.zig"),
.target = target,
.optimize = optimize,
.filter = b.option([]const u8, "test-filter", "Filter strings for test"),
Expand Down
123 changes: 13 additions & 110 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,30 @@ const Args = struct {
args: []u8,
};

const pdata = struct {
ev1: *std.Thread.ResetEvent,
ev2: *std.Thread.ResetEvent,
};

fn waiter(p: *pdata) void {
for (0..2) |i| {
dbg("{d} start wait1\n", .{i});
p.ev1.wait();
dbg("{d} end wait1, call reset\n", .{i});
p.ev1.reset();

dbg("{d} start wait2\n", .{i});
p.ev2.wait();
dbg("{d} end wait2, call reset\n", .{i});
p.ev2.reset();
}
}

fn testWaiter() !void {
var ev1 = std.Thread.ResetEvent{};
var ev2 = std.Thread.ResetEvent{};
var data = pdata{ .ev1 = &ev1, .ev2 = &ev2 };

const t = try std.Thread.spawn(.{}, waiter, .{&data});
t.detach();

std.time.sleep(std.time.ns_per_s * 5);
ev1.set();
ev2.set();
std.time.sleep(std.time.ns_per_s * 5);
ev1.set();
ev2.set();
std.time.sleep(std.time.ns_per_s * 5);
}

const UserData = struct {
dummy: u32,
};

fn callback(allocator: std.mem.Allocator, data: ?*UserData, addr: []const u8) !void {
defer allocator.free(addr);
var tm = try std.time.Timer.start();
defer {
allocator.free(addr);
log.info("callback took {any}", .{std.fmt.fmtDuration(tm.read())});
}

_ = data;
log.info("callback: leader={s}", .{addr});
}

const Fleet = zgroup.Fleet(UserData);

// Expected cmdline args:
//
// [0] = bin
// [1] = name
// [2] = member ip:port
// [3] = join ip:port
//
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var arena = std.heap.ArenaAllocator.init(gpa.allocator());
Expand All @@ -80,12 +56,6 @@ pub fn main() !void {
try hm.put(i, .{ .args = key });
}

// Expected:
// [0] = bin
// [1] = name
// [2] = member ip:port
// [3] = join ip:port

if (hm.count() < 4) {
log.err("invalid args", .{});
return;
Expand Down Expand Up @@ -155,70 +125,3 @@ pub fn main() !void {
// }
}
}

test "backoff" {
// Try referencing external dep in test block.
const bo = backoff.Backoff{};
dbg("val={any}\n", .{bo.initial});
}

test "atomic" {
var tm = try std.time.Timer.start();
var v: u64 = 0;
@atomicStore(u64, &v, 1, AtomicOrder.seq_cst);
_ = @atomicLoad(u64, &v, AtomicOrder.seq_cst);
// print("load={d}\n", .{a});
_ = @atomicRmw(u64, &v, AtomicRmwOp.Add, 1e9, AtomicOrder.seq_cst);
_ = @atomicLoad(u64, &v, AtomicOrder.seq_cst);
// print("add={d}\n", .{b});
dbg("took {any}\n", .{std.fmt.fmtDuration(tm.read())});
}

test "block" {
const flag = false;
section: {
if (flag) {
dbg("early return\n", .{});
break :section;
}

dbg("final return\n", .{});
}
}

test "tuple" {
var tuple: std.meta.Tuple(&.{ u32, bool }) = .{ 100, true };
dbg("{any}\n", .{tuple.len});
tuple[0] = 200;
dbg("{any}, {d}\n", .{ tuple.len, tuple[0] });
}

test "dupe" {
const alloc = std.testing.allocator;
const m1 = try std.fmt.allocPrint(alloc, "zig is the man", .{});
defer alloc.free(m1);
const dup1 = try alloc.dupe(u8, m1);
defer alloc.free(dup1);
const dup2 = try alloc.dupe(u8, m1);
defer alloc.free(dup2);
dbg("{s},{d}\n", .{ dup1, dup1.len });
dbg("{s},{d}\n", .{ dup2, dup2.len });
}

test "view" {
const en = enum(u4) {
change,
do,
start,
};

const e: en = .start;
dbg("size={d}\n", .{@sizeOf(@TypeOf(e))});
const ee: en = @enumFromInt(2);
dbg("int={any}\n", .{ee});

const val = 17293822569102704642; // 2
dbg("cmd={x}\n", .{(val & 0xf000000000000000) >> 60});
dbg("val={x}\n", .{val & 0x0fffffffffffffff});
dbg("{x}\n", .{0xffffffffffffffff & (0b11 << 62)});
}

0 comments on commit 7dd147c

Please sign in to comment.