Add tests to I/O module
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
71d5d696bd
commit
f68fbe16c7
49
src/io.zig
49
src/io.zig
|
@ -7,7 +7,28 @@ const std = @import("std");
|
|||
///
|
||||
pub const Writer = struct {
|
||||
context: *anyopaque,
|
||||
operation: fn (*anyopaque, []const u8) usize,
|
||||
writeContext: fn (*anyopaque, []const u8) usize,
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
pub const Radix = enum {
|
||||
binary,
|
||||
tinary,
|
||||
quaternary,
|
||||
quinary,
|
||||
senary,
|
||||
septenary,
|
||||
octal,
|
||||
nonary,
|
||||
decimal,
|
||||
undecimal,
|
||||
duodecimal,
|
||||
tridecimal,
|
||||
tetradecimal,
|
||||
pentadecimal,
|
||||
hexadecimal,
|
||||
};
|
||||
|
||||
///
|
||||
/// Wraps and returns a reference to `write_context` of type `WriteContext` and its associated
|
||||
|
@ -21,7 +42,7 @@ pub const Writer = struct {
|
|||
return .{
|
||||
.context = write_context,
|
||||
|
||||
.operation = struct {
|
||||
.writeContext = struct {
|
||||
fn write(context: *anyopaque, buffer: []const u8) usize {
|
||||
return writeContext(@ptrCast(*WriteContext,
|
||||
@alignCast(@alignOf(WriteContext), context)), buffer);
|
||||
|
@ -35,7 +56,7 @@ pub const Writer = struct {
|
|||
/// were successfully written.
|
||||
///
|
||||
pub fn write(writer: Writer, buffer: []const u8) usize {
|
||||
return writer.operation(writer.context, buffer);
|
||||
return writer.writeContext(writer.context, buffer);
|
||||
}
|
||||
|
||||
///
|
||||
|
@ -43,7 +64,7 @@ pub const Writer = struct {
|
|||
/// otherwise `false`.
|
||||
///
|
||||
pub fn writeByte(writer: Writer, byte: u8) bool {
|
||||
return (writer.operation(writer.context,
|
||||
return (writer.writeContext(writer.context,
|
||||
@ptrCast([*]const u8, &byte)[0 .. 1]) != 0);
|
||||
}
|
||||
|
||||
|
@ -51,10 +72,10 @@ pub const Writer = struct {
|
|||
/// Writes `value` as a ASCII / UTF-8 encoded integer to `writer`, returning `true` if the full
|
||||
/// sequence was successfully written, otherwise `false`.
|
||||
///
|
||||
/// The `base` argument identifies which base system to encode `value` as, with `10` being
|
||||
/// The `radix` argument identifies which base system to encode `value` as, with `10` being
|
||||
/// decimal, `16` being hexadecimal, `8` being octal`, so on and so forth.
|
||||
///
|
||||
pub fn writeInt(writer: Writer, value: anytype, base: u4) bool {
|
||||
pub fn writeInt(writer: Writer, radix: Radix, value: anytype) bool {
|
||||
const Int = @TypeOf(value);
|
||||
const type_info = @typeInfo(Int);
|
||||
|
||||
|
@ -62,6 +83,7 @@ pub const Writer = struct {
|
|||
|
||||
if (value == 0) return writer.writeByte('0');
|
||||
|
||||
// TODO: Unhardcode this as it will break with large ints.
|
||||
var buffer = std.mem.zeroes([28]u8);
|
||||
var buffer_count = @as(usize, 0);
|
||||
var n1 = value;
|
||||
|
@ -74,6 +96,8 @@ pub const Writer = struct {
|
|||
}
|
||||
|
||||
while (n1 != 0) {
|
||||
const base = @enumToInt(radix);
|
||||
|
||||
buffer[buffer_count] = @intCast(u8, (n1 % base) + '0');
|
||||
n1 = (n1 / base);
|
||||
buffer_count += 1;
|
||||
|
@ -106,3 +130,16 @@ pub const null_writer = Writer{
|
|||
}
|
||||
}.write,
|
||||
};
|
||||
|
||||
test {
|
||||
const testing = std.testing;
|
||||
|
||||
{
|
||||
const sequence = "foo";
|
||||
|
||||
testing.expectEqual(null_writer.write(sequence), sequence.len);
|
||||
}
|
||||
|
||||
testing.expect(null_writer.writeByte(0));
|
||||
testing.expect(null_writer.writeInt(.decimal, 420));
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ pub fn main() anyerror!void {
|
|||
}
|
||||
|
||||
test {
|
||||
_ = @import("./mem.zig");
|
||||
|
||||
}
|
||||
|
||||
fn run(event_loop: *sys.EventLoop, graphics: *sys.GraphicsContext) anyerror!void {
|
||||
|
@ -27,7 +27,7 @@ fn run(event_loop: *sys.EventLoop, graphics: *sys.GraphicsContext) anyerror!void
|
|||
const file_access = try event_loop.open(.readonly,
|
||||
try sys.FileSystem.data.joinedPath(&.{"data", "ona.lua"}));
|
||||
|
||||
defer _ = event_loop.close(file_access);
|
||||
defer event_loop.close(file_access);
|
||||
|
||||
const file_size = try file_access.size(event_loop);
|
||||
const allocator = gpa.allocator();
|
||||
|
|
Loading…
Reference in New Issue