Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions modules/rtt/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,22 @@ const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

_ = b.addModule("rtt", .{
.root_source_file = b.path("src/rtt.zig"),
.target = target,
.optimize = optimize,
});

const test_step = b.step("test", "Run unit tests");
const lib_tests = b.addTest(.{
.name = "rtt-tests",
.root_module = b.createModule(.{
.root_source_file = b.path("src/rtt.zig"),
.target = target,
.optimize = optimize,
}),
});
const run_lib_tests = b.addRunArtifact(lib_tests);
test_step.dependOn(&run_lib_tests.step);
}
87 changes: 86 additions & 1 deletion modules/rtt/src/rtt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,91 @@ const Header = extern struct {
}
};

test {
// Verify Down mode() returns correct mode after init
const LockOps = struct {
fn lock(_: *anyopaque) void {}
fn unlock(_: *anyopaque) void {}
};
const my_lock: AnyLock = .{ .context = undefined, .lock_fn = LockOps.lock, .unlock_fn = LockOps.unlock };
const TestDown = channel.Down(my_lock, struct {
inline fn barrier() void {}
}.barrier);
var buf: [64]u8 align(4) = undefined;
var d: TestDown = undefined;
d.init("test", &buf, .NoBlockSkip);
try std.testing.expectEqual(.NoBlockSkip, d.mode());
}

test {
// Verify Down set_mode changes mode
const LockOps = struct {
fn lock(_: *anyopaque) void {}
fn unlock(_: *anyopaque) void {}
};
const my_lock: AnyLock = .{ .context = undefined, .lock_fn = LockOps.lock, .unlock_fn = LockOps.unlock };
const TestDown = channel.Down(my_lock, struct {
inline fn barrier() void {}
}.barrier);
var buf: [64]u8 align(4) = undefined;
var d: TestDown = undefined;
d.init("test", &buf, .BlockIfFull);
try std.testing.expectEqual(.BlockIfFull, d.mode());
d.set_mode(.NoBlockTrim);
try std.testing.expectEqual(.NoBlockTrim, d.mode());
}

test {
// Verify Up channel Writer compiles and drain works
const LockOps = struct {
fn lock(_: *anyopaque) void {}
fn unlock(_: *anyopaque) void {}
};
const my_lock: AnyLock = .{ .context = undefined, .lock_fn = LockOps.lock, .unlock_fn = LockOps.unlock };
const TestUp = channel.Up(my_lock, struct {
inline fn barrier() void {}
}.barrier);
var buf: [128]u8 align(4) = undefined;
var u: TestUp = undefined;
u.init("test", &buf, .NoBlockTrim);
var wbuf: [32]u8 = undefined;
var w = u.writer(&wbuf);
_ = w.interface.write("hello") catch 0;
}

test {
// Verify Down channel Reader compiles
const LockOps = struct {
fn lock(_: *anyopaque) void {}
fn unlock(_: *anyopaque) void {}
};
const my_lock: AnyLock = .{ .context = undefined, .lock_fn = LockOps.lock, .unlock_fn = LockOps.unlock };
const TestDown = channel.Down(my_lock, struct {
inline fn barrier() void {}
}.barrier);
var buf: [64]u8 align(4) = undefined;
var d: TestDown = undefined;
d.init("test", &buf, .NoBlockSkip);
var rbuf: [32]u8 = undefined;
_ = d.reader(&rbuf);
}

test {
// Verify Up mode() returns correct mode
const LockOps = struct {
fn lock(_: *anyopaque) void {}
fn unlock(_: *anyopaque) void {}
};
const my_lock: AnyLock = .{ .context = undefined, .lock_fn = LockOps.lock, .unlock_fn = LockOps.unlock };
const TestUp = channel.Up(my_lock, struct {
inline fn barrier() void {}
}.barrier);
var buf: [128]u8 align(4) = undefined;
var u: TestUp = undefined;
u.init("test", &buf, .BlockIfFull);
try std.testing.expectEqual(.BlockIfFull, u.mode());
}

pub const channel = struct {
pub const Mode = enum(usize) {
NoBlockSkip = 0,
Expand Down Expand Up @@ -305,7 +390,7 @@ pub const channel = struct {
}

pub fn mode(self: *Self) Mode {
return @enumFromInt(self.mode & 3);
return @enumFromInt(self.flags & 3);
}

pub fn set_mode(self: *Self, mode_: Mode) void {
Expand Down
Loading