2019-11-04 12:05:29 -08:00
|
|
|
// FIFO of fixed size items
|
|
|
|
// Usually used for e.g. byte buffers
|
|
|
|
|
|
|
|
const std = @import("std");
|
|
|
|
const math = std.math;
|
|
|
|
const mem = std.mem;
|
|
|
|
const Allocator = mem.Allocator;
|
|
|
|
const debug = std.debug;
|
|
|
|
const assert = debug.assert;
|
|
|
|
const testing = std.testing;
|
|
|
|
|
2019-11-10 06:25:38 -08:00
|
|
|
pub const LinearFifoBufferType = union(enum) {
|
2019-11-10 06:23:21 -08:00
|
|
|
/// The buffer is internal to the fifo; it is of the specified size.
|
|
|
|
Static: usize,
|
|
|
|
|
|
|
|
/// The buffer is passed as a slice to the initialiser.
|
|
|
|
Slice,
|
|
|
|
|
|
|
|
/// The buffer is managed dynamically using a `mem.Allocator`.
|
|
|
|
Dynamic,
|
|
|
|
};
|
|
|
|
|
2019-11-10 06:25:38 -08:00
|
|
|
pub fn LinearFifo(
|
2019-11-10 06:23:21 -08:00
|
|
|
comptime T: type,
|
2019-11-10 06:25:38 -08:00
|
|
|
comptime buffer_type: LinearFifoBufferType,
|
2019-11-10 06:23:21 -08:00
|
|
|
) type {
|
2019-11-10 06:32:52 -08:00
|
|
|
const autoalign = false;
|
|
|
|
|
2019-11-10 06:23:21 -08:00
|
|
|
const powers_of_two = switch (buffer_type) {
|
|
|
|
.Static => std.math.isPowerOfTwo(buffer_type.Static),
|
|
|
|
.Slice => false, // Any size slice could be passed in
|
|
|
|
.Dynamic => true, // This could be configurable in future
|
|
|
|
};
|
2019-11-10 05:26:40 -08:00
|
|
|
|
2019-11-04 12:05:29 -08:00
|
|
|
return struct {
|
2019-11-10 06:23:21 -08:00
|
|
|
allocator: if (buffer_type == .Dynamic) *Allocator else void,
|
|
|
|
buf: if (buffer_type == .Static) [buffer_type.Static]T else []T,
|
2019-11-04 12:05:29 -08:00
|
|
|
head: usize,
|
|
|
|
count: usize,
|
|
|
|
|
|
|
|
const Self = @This();
|
|
|
|
|
2019-11-10 06:23:21 -08:00
|
|
|
// Type of Self argument for slice operations.
|
|
|
|
// If buffer is inline (Static) then we need to ensure we haven't
|
|
|
|
// returned a slice into a copy on the stack
|
|
|
|
const SliceSelfArg = if (buffer_type == .Static) *Self else Self;
|
|
|
|
|
|
|
|
pub usingnamespace switch (buffer_type) {
|
|
|
|
.Static => struct {
|
|
|
|
pub fn init() Self {
|
|
|
|
return .{
|
|
|
|
.allocator = {},
|
|
|
|
.buf = undefined,
|
|
|
|
.head = 0,
|
|
|
|
.count = 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.Slice => struct {
|
|
|
|
pub fn init(buf: []T) Self {
|
|
|
|
return .{
|
|
|
|
.allocator = {},
|
|
|
|
.buf = buf,
|
|
|
|
.head = 0,
|
|
|
|
.count = 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.Dynamic => struct {
|
|
|
|
pub fn init(allocator: *Allocator) Self {
|
|
|
|
return .{
|
|
|
|
.allocator = allocator,
|
2019-11-27 00:30:39 -08:00
|
|
|
.buf = &[_]T{},
|
2019-11-10 06:23:21 -08:00
|
|
|
.head = 0,
|
|
|
|
.count = 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2019-11-04 12:05:29 -08:00
|
|
|
|
2019-11-22 22:57:17 -08:00
|
|
|
pub fn deinit(self: Self) void {
|
2019-11-10 06:23:21 -08:00
|
|
|
if (buffer_type == .Dynamic) self.allocator.free(self.buf);
|
2019-11-04 12:05:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn realign(self: *Self) void {
|
|
|
|
if (self.buf.len - self.head >= self.count) {
|
|
|
|
// this copy overlaps
|
|
|
|
mem.copy(T, self.buf[0..self.count], self.buf[self.head..][0..self.count]);
|
|
|
|
self.head = 0;
|
|
|
|
} else {
|
|
|
|
var tmp: [mem.page_size / 2 / @sizeOf(T)]T = undefined;
|
|
|
|
|
|
|
|
while (self.head != 0) {
|
|
|
|
const n = math.min(self.head, tmp.len);
|
|
|
|
const m = self.buf.len - n;
|
|
|
|
mem.copy(T, tmp[0..n], self.buf[0..n]);
|
|
|
|
// this middle copy overlaps; the others here don't
|
|
|
|
mem.copy(T, self.buf[0..m], self.buf[n..][0..m]);
|
|
|
|
mem.copy(T, self.buf[m..], tmp[0..n]);
|
|
|
|
self.head -= n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{ // set unused area to undefined
|
2020-02-21 10:46:53 -08:00
|
|
|
const unused = mem.sliceAsBytes(self.buf[self.count..]);
|
2019-11-04 12:05:29 -08:00
|
|
|
@memset(unused.ptr, undefined, unused.len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reduce allocated capacity to `size`.
|
|
|
|
pub fn shrink(self: *Self, size: usize) void {
|
|
|
|
assert(size >= self.count);
|
2019-11-10 06:23:21 -08:00
|
|
|
if (buffer_type == .Dynamic) {
|
|
|
|
self.realign();
|
|
|
|
self.buf = self.allocator.realloc(self.buf, size) catch |e| switch (e) {
|
|
|
|
error.OutOfMemory => return, // no problem, capacity is still correct then.
|
|
|
|
};
|
|
|
|
}
|
2019-11-04 12:05:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Ensure that the buffer can fit at least `size` items
|
2019-11-10 05:26:40 -08:00
|
|
|
pub fn ensureCapacity(self: *Self, size: usize) !void {
|
2019-11-04 12:05:29 -08:00
|
|
|
if (self.buf.len >= size) return;
|
2019-11-10 06:23:21 -08:00
|
|
|
if (buffer_type == .Dynamic) {
|
|
|
|
self.realign();
|
|
|
|
const new_size = if (powers_of_two) math.ceilPowerOfTwo(usize, size) catch return error.OutOfMemory else size;
|
|
|
|
self.buf = try self.allocator.realloc(self.buf, new_size);
|
|
|
|
} else {
|
|
|
|
return error.OutOfMemory;
|
|
|
|
}
|
2019-11-04 12:05:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Makes sure at least `size` items are unused
|
|
|
|
pub fn ensureUnusedCapacity(self: *Self, size: usize) error{OutOfMemory}!void {
|
|
|
|
if (self.writableLength() >= size) return;
|
|
|
|
|
|
|
|
return try self.ensureCapacity(math.add(usize, self.count, size) catch return error.OutOfMemory);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns number of items currently in fifo
|
|
|
|
pub fn readableLength(self: Self) usize {
|
|
|
|
return self.count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a writable slice from the 'read' end of the fifo
|
2019-11-10 06:23:21 -08:00
|
|
|
fn readableSliceMut(self: SliceSelfArg, offset: usize) []T {
|
2019-11-27 00:30:39 -08:00
|
|
|
if (offset > self.count) return &[_]T{};
|
2019-11-04 12:05:29 -08:00
|
|
|
|
2019-11-22 18:13:47 -08:00
|
|
|
var start = self.head + offset;
|
2019-11-04 12:05:29 -08:00
|
|
|
if (start >= self.buf.len) {
|
2019-11-22 18:13:47 -08:00
|
|
|
start -= self.buf.len;
|
2019-11-25 10:51:09 -08:00
|
|
|
return self.buf[start .. self.count - offset];
|
2019-11-04 12:05:29 -08:00
|
|
|
} else {
|
2019-11-22 18:13:47 -08:00
|
|
|
const end = math.min(self.head + self.count, self.buf.len);
|
|
|
|
return self.buf[start..end];
|
2019-11-04 12:05:29 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a readable slice from `offset`
|
2019-11-10 06:23:21 -08:00
|
|
|
pub fn readableSlice(self: SliceSelfArg, offset: usize) []const T {
|
2019-11-04 12:05:29 -08:00
|
|
|
return self.readableSliceMut(offset);
|
|
|
|
}
|
|
|
|
|
2020-03-30 21:32:31 -07:00
|
|
|
/// Discard first `count` items in the fifo
|
2019-11-04 12:05:29 -08:00
|
|
|
pub fn discard(self: *Self, count: usize) void {
|
|
|
|
assert(count <= self.count);
|
|
|
|
{ // set old range to undefined. Note: may be wrapped around
|
|
|
|
const slice = self.readableSliceMut(0);
|
|
|
|
if (slice.len >= count) {
|
2020-02-21 10:46:53 -08:00
|
|
|
const unused = mem.sliceAsBytes(slice[0..count]);
|
2019-11-04 12:05:29 -08:00
|
|
|
@memset(unused.ptr, undefined, unused.len);
|
|
|
|
} else {
|
2020-02-21 10:46:53 -08:00
|
|
|
const unused = mem.sliceAsBytes(slice[0..]);
|
2019-11-04 12:05:29 -08:00
|
|
|
@memset(unused.ptr, undefined, unused.len);
|
2020-02-21 10:46:53 -08:00
|
|
|
const unused2 = mem.sliceAsBytes(self.readableSliceMut(slice.len)[0 .. count - slice.len]);
|
2019-11-04 12:05:29 -08:00
|
|
|
@memset(unused2.ptr, undefined, unused2.len);
|
|
|
|
}
|
|
|
|
}
|
2019-11-10 05:26:40 -08:00
|
|
|
if (autoalign and self.count == count) {
|
2019-11-04 12:05:29 -08:00
|
|
|
self.head = 0;
|
2019-11-10 05:26:40 -08:00
|
|
|
self.count = 0;
|
|
|
|
} else {
|
|
|
|
var head = self.head + count;
|
|
|
|
if (powers_of_two) {
|
|
|
|
head &= self.buf.len - 1;
|
|
|
|
} else {
|
|
|
|
head %= self.buf.len;
|
|
|
|
}
|
|
|
|
self.head = head;
|
|
|
|
self.count -= count;
|
|
|
|
}
|
2019-11-04 12:05:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Read the next item from the fifo
|
2020-05-14 10:20:27 -07:00
|
|
|
pub fn readItem(self: *Self) ?T {
|
|
|
|
if (self.count == 0) return null;
|
2019-11-04 12:05:29 -08:00
|
|
|
|
|
|
|
const c = self.buf[self.head];
|
|
|
|
self.discard(1);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2020-03-30 21:32:31 -07:00
|
|
|
/// Read data from the fifo into `dst`, returns number of items copied.
|
2019-11-10 07:46:40 -08:00
|
|
|
pub fn read(self: *Self, dst: []T) usize {
|
2019-11-04 12:05:29 -08:00
|
|
|
var dst_left = dst;
|
|
|
|
|
|
|
|
while (dst_left.len > 0) {
|
|
|
|
const slice = self.readableSlice(0);
|
|
|
|
if (slice.len == 0) break;
|
|
|
|
const n = math.min(slice.len, dst_left.len);
|
|
|
|
mem.copy(T, dst_left, slice[0..n]);
|
|
|
|
self.discard(n);
|
|
|
|
dst_left = dst_left[n..];
|
|
|
|
}
|
|
|
|
|
2019-11-10 07:46:40 -08:00
|
|
|
return dst.len - dst_left.len;
|
2019-11-04 12:05:29 -08:00
|
|
|
}
|
|
|
|
|
2020-04-02 03:14:15 -07:00
|
|
|
/// Same as `read` except it returns an error union
|
2020-06-08 21:34:50 -07:00
|
|
|
/// The purpose of this function existing is to match `std.io.Reader` API.
|
2020-04-02 03:14:15 -07:00
|
|
|
fn readFn(self: *Self, dest: []u8) error{}!usize {
|
|
|
|
return self.read(dest);
|
|
|
|
}
|
|
|
|
|
2020-06-08 21:34:50 -07:00
|
|
|
pub fn reader(self: *Self) std.io.Reader(*Self, error{}, readFn) {
|
|
|
|
return .{ .context = self };
|
|
|
|
}
|
|
|
|
/// Deprecated: `use reader`
|
2020-04-02 03:14:15 -07:00
|
|
|
pub fn inStream(self: *Self) std.io.InStream(*Self, error{}, readFn) {
|
|
|
|
return .{ .context = self };
|
|
|
|
}
|
|
|
|
|
2020-03-30 21:32:31 -07:00
|
|
|
/// Returns number of items available in fifo
|
2019-11-04 12:05:29 -08:00
|
|
|
pub fn writableLength(self: Self) usize {
|
|
|
|
return self.buf.len - self.count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the first section of writable buffer
|
|
|
|
/// Note that this may be of length 0
|
2019-11-10 06:23:21 -08:00
|
|
|
pub fn writableSlice(self: SliceSelfArg, offset: usize) []T {
|
2019-11-27 00:30:39 -08:00
|
|
|
if (offset > self.buf.len) return &[_]T{};
|
2019-11-04 12:05:29 -08:00
|
|
|
|
|
|
|
const tail = self.head + offset + self.count;
|
|
|
|
if (tail < self.buf.len) {
|
|
|
|
return self.buf[tail..];
|
|
|
|
} else {
|
|
|
|
return self.buf[tail - self.buf.len ..][0 .. self.writableLength() - offset];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 21:32:31 -07:00
|
|
|
/// Returns a writable buffer of at least `size` items, allocating memory as needed.
|
2019-11-04 12:05:29 -08:00
|
|
|
/// Use `fifo.update` once you've written data to it.
|
2020-03-30 21:32:31 -07:00
|
|
|
pub fn writableWithSize(self: *Self, size: usize) ![]T {
|
2019-11-04 12:05:29 -08:00
|
|
|
try self.ensureUnusedCapacity(size);
|
|
|
|
|
|
|
|
// try to avoid realigning buffer
|
|
|
|
var slice = self.writableSlice(0);
|
|
|
|
if (slice.len < size) {
|
|
|
|
self.realign();
|
|
|
|
slice = self.writableSlice(0);
|
|
|
|
}
|
|
|
|
return slice;
|
|
|
|
}
|
|
|
|
|
2020-03-30 21:32:31 -07:00
|
|
|
/// Update the tail location of the buffer (usually follows use of writable/writableWithSize)
|
2019-11-04 12:05:29 -08:00
|
|
|
pub fn update(self: *Self, count: usize) void {
|
|
|
|
assert(self.count + count <= self.buf.len);
|
|
|
|
self.count += count;
|
|
|
|
}
|
|
|
|
|
2019-11-08 17:11:12 -08:00
|
|
|
/// Appends the data in `src` to the fifo.
|
|
|
|
/// You must have ensured there is enough space.
|
2019-11-04 12:05:29 -08:00
|
|
|
pub fn writeAssumeCapacity(self: *Self, src: []const T) void {
|
|
|
|
assert(self.writableLength() >= src.len);
|
|
|
|
|
|
|
|
var src_left = src;
|
|
|
|
while (src_left.len > 0) {
|
|
|
|
const writable_slice = self.writableSlice(0);
|
|
|
|
assert(writable_slice.len != 0);
|
|
|
|
const n = math.min(writable_slice.len, src_left.len);
|
|
|
|
mem.copy(T, writable_slice, src_left[0..n]);
|
|
|
|
self.update(n);
|
|
|
|
src_left = src_left[n..];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 03:15:06 -08:00
|
|
|
/// Write a single item to the fifo
|
|
|
|
pub fn writeItem(self: *Self, item: T) !void {
|
|
|
|
try self.ensureUnusedCapacity(1);
|
2020-05-14 10:20:27 -07:00
|
|
|
return self.writeItemAssumeCapacity(item);
|
|
|
|
}
|
2019-11-16 03:15:06 -08:00
|
|
|
|
2020-05-14 10:20:27 -07:00
|
|
|
pub fn writeItemAssumeCapacity(self: *Self, item: T) void {
|
2019-11-16 03:15:06 -08:00
|
|
|
var tail = self.head + self.count;
|
|
|
|
if (powers_of_two) {
|
|
|
|
tail &= self.buf.len - 1;
|
|
|
|
} else {
|
|
|
|
tail %= self.buf.len;
|
|
|
|
}
|
2020-03-30 21:32:31 -07:00
|
|
|
self.buf[tail] = item;
|
2019-11-16 03:15:06 -08:00
|
|
|
self.update(1);
|
|
|
|
}
|
|
|
|
|
2019-11-04 12:05:29 -08:00
|
|
|
/// Appends the data in `src` to the fifo.
|
|
|
|
/// Allocates more memory as necessary
|
|
|
|
pub fn write(self: *Self, src: []const T) !void {
|
|
|
|
try self.ensureUnusedCapacity(src.len);
|
|
|
|
|
|
|
|
return self.writeAssumeCapacity(src);
|
|
|
|
}
|
|
|
|
|
2020-04-02 02:55:59 -07:00
|
|
|
/// Same as `write` except it returns the number of bytes written, which is always the same
|
|
|
|
/// as `bytes.len`. The purpose of this function existing is to match `std.io.OutStream` API.
|
|
|
|
fn appendWrite(self: *Self, bytes: []const u8) error{OutOfMemory}!usize {
|
|
|
|
try self.write(bytes);
|
|
|
|
return bytes.len;
|
|
|
|
}
|
2020-03-06 14:52:46 -08:00
|
|
|
|
2020-04-02 02:55:59 -07:00
|
|
|
pub fn outStream(self: *Self) std.io.OutStream(*Self, error{OutOfMemory}, appendWrite) {
|
|
|
|
return .{ .context = self };
|
|
|
|
}
|
2019-11-04 12:05:29 -08:00
|
|
|
|
2019-11-22 23:42:22 -08:00
|
|
|
/// Make `count` items available before the current read location
|
|
|
|
fn rewind(self: *Self, count: usize) void {
|
|
|
|
assert(self.writableLength() >= count);
|
2019-11-04 12:05:29 -08:00
|
|
|
|
2019-11-22 23:42:22 -08:00
|
|
|
var head = self.head + (self.buf.len - count);
|
2019-11-10 05:26:40 -08:00
|
|
|
if (powers_of_two) {
|
|
|
|
head &= self.buf.len - 1;
|
|
|
|
} else {
|
|
|
|
head %= self.buf.len;
|
|
|
|
}
|
|
|
|
self.head = head;
|
2019-11-22 23:42:22 -08:00
|
|
|
self.count += count;
|
2019-11-04 12:05:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Place data back into the read stream
|
|
|
|
pub fn unget(self: *Self, src: []const T) !void {
|
|
|
|
try self.ensureUnusedCapacity(src.len);
|
|
|
|
|
|
|
|
self.rewind(src.len);
|
|
|
|
|
|
|
|
const slice = self.readableSliceMut(0);
|
2019-11-10 08:03:57 -08:00
|
|
|
if (src.len < slice.len) {
|
|
|
|
mem.copy(T, slice, src);
|
|
|
|
} else {
|
|
|
|
mem.copy(T, slice, src[0..slice.len]);
|
|
|
|
const slice2 = self.readableSliceMut(slice.len);
|
|
|
|
mem.copy(T, slice2, src[slice.len..]);
|
|
|
|
}
|
2019-11-04 12:05:29 -08:00
|
|
|
}
|
|
|
|
|
2020-05-14 10:20:27 -07:00
|
|
|
/// Returns the item at `offset`.
|
|
|
|
/// Asserts offset is within bounds.
|
|
|
|
pub fn peekItem(self: Self, offset: usize) T {
|
|
|
|
assert(offset < self.count);
|
2019-11-04 12:05:29 -08:00
|
|
|
|
2019-11-10 05:26:40 -08:00
|
|
|
var index = self.head + offset;
|
|
|
|
if (powers_of_two) {
|
|
|
|
index &= self.buf.len - 1;
|
|
|
|
} else {
|
|
|
|
index %= self.buf.len;
|
|
|
|
}
|
|
|
|
return self.buf[index];
|
2019-11-04 12:05:29 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-11-10 06:25:38 -08:00
|
|
|
test "LinearFifo(u8, .Dynamic)" {
|
2020-01-29 19:22:01 -08:00
|
|
|
var fifo = LinearFifo(u8, .Dynamic).init(testing.allocator);
|
2019-11-04 12:05:29 -08:00
|
|
|
defer fifo.deinit();
|
|
|
|
|
|
|
|
try fifo.write("HELLO");
|
2019-11-06 20:25:57 -08:00
|
|
|
testing.expectEqual(@as(usize, 5), fifo.readableLength());
|
2019-11-04 12:05:29 -08:00
|
|
|
testing.expectEqualSlices(u8, "HELLO", fifo.readableSlice(0));
|
|
|
|
|
|
|
|
{
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < 5) : (i += 1) {
|
2020-05-14 10:20:27 -07:00
|
|
|
try fifo.write(&[_]u8{fifo.peekItem(i)});
|
2019-11-04 12:05:29 -08:00
|
|
|
}
|
2019-11-06 20:25:57 -08:00
|
|
|
testing.expectEqual(@as(usize, 10), fifo.readableLength());
|
2019-11-04 12:05:29 -08:00
|
|
|
testing.expectEqualSlices(u8, "HELLOHELLO", fifo.readableSlice(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-05-14 10:20:27 -07:00
|
|
|
testing.expectEqual(@as(u8, 'H'), fifo.readItem().?);
|
|
|
|
testing.expectEqual(@as(u8, 'E'), fifo.readItem().?);
|
|
|
|
testing.expectEqual(@as(u8, 'L'), fifo.readItem().?);
|
|
|
|
testing.expectEqual(@as(u8, 'L'), fifo.readItem().?);
|
|
|
|
testing.expectEqual(@as(u8, 'O'), fifo.readItem().?);
|
2019-11-04 12:05:29 -08:00
|
|
|
}
|
2019-11-06 20:25:57 -08:00
|
|
|
testing.expectEqual(@as(usize, 5), fifo.readableLength());
|
2019-11-04 12:05:29 -08:00
|
|
|
|
|
|
|
{ // Writes that wrap around
|
2019-11-06 20:25:57 -08:00
|
|
|
testing.expectEqual(@as(usize, 11), fifo.writableLength());
|
|
|
|
testing.expectEqual(@as(usize, 6), fifo.writableSlice(0).len);
|
2019-11-04 12:05:29 -08:00
|
|
|
fifo.writeAssumeCapacity("6<chars<11");
|
|
|
|
testing.expectEqualSlices(u8, "HELLO6<char", fifo.readableSlice(0));
|
|
|
|
testing.expectEqualSlices(u8, "s<11", fifo.readableSlice(11));
|
|
|
|
fifo.discard(11);
|
|
|
|
testing.expectEqualSlices(u8, "s<11", fifo.readableSlice(0));
|
|
|
|
fifo.discard(4);
|
2019-11-06 20:25:57 -08:00
|
|
|
testing.expectEqual(@as(usize, 0), fifo.readableLength());
|
2019-11-04 12:05:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-03-30 21:32:31 -07:00
|
|
|
const buf = try fifo.writableWithSize(12);
|
2019-11-06 20:25:57 -08:00
|
|
|
testing.expectEqual(@as(usize, 12), buf.len);
|
2019-11-04 12:05:29 -08:00
|
|
|
var i: u8 = 0;
|
|
|
|
while (i < 10) : (i += 1) {
|
|
|
|
buf[i] = i + 'a';
|
|
|
|
}
|
|
|
|
fifo.update(10);
|
|
|
|
testing.expectEqualSlices(u8, "abcdefghij", fifo.readableSlice(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
try fifo.unget("prependedstring");
|
|
|
|
var result: [30]u8 = undefined;
|
2019-11-10 07:46:40 -08:00
|
|
|
testing.expectEqualSlices(u8, "prependedstringabcdefghij", result[0..fifo.read(&result)]);
|
2019-11-10 08:03:57 -08:00
|
|
|
try fifo.unget("b");
|
|
|
|
try fifo.unget("a");
|
|
|
|
testing.expectEqualSlices(u8, "ab", result[0..fifo.read(&result)]);
|
2019-11-04 12:05:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fifo.shrink(0);
|
|
|
|
|
|
|
|
{
|
2020-03-13 08:55:50 -07:00
|
|
|
try fifo.outStream().print("{}, {}!", .{ "Hello", "World" });
|
2019-11-04 12:05:29 -08:00
|
|
|
var result: [30]u8 = undefined;
|
2019-11-10 07:46:40 -08:00
|
|
|
testing.expectEqualSlices(u8, "Hello, World!", result[0..fifo.read(&result)]);
|
2019-11-06 20:25:57 -08:00
|
|
|
testing.expectEqual(@as(usize, 0), fifo.readableLength());
|
2019-11-04 12:05:29 -08:00
|
|
|
}
|
2020-04-02 03:14:15 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
try fifo.outStream().writeAll("This is a test");
|
|
|
|
var result: [30]u8 = undefined;
|
2020-06-08 21:34:50 -07:00
|
|
|
testing.expectEqualSlices(u8, "This", (try fifo.reader().readUntilDelimiterOrEof(&result, ' ')).?);
|
|
|
|
testing.expectEqualSlices(u8, "is", (try fifo.reader().readUntilDelimiterOrEof(&result, ' ')).?);
|
|
|
|
testing.expectEqualSlices(u8, "a", (try fifo.reader().readUntilDelimiterOrEof(&result, ' ')).?);
|
|
|
|
testing.expectEqualSlices(u8, "test", (try fifo.reader().readUntilDelimiterOrEof(&result, ' ')).?);
|
2020-04-02 03:14:15 -07:00
|
|
|
}
|
2019-11-04 12:05:29 -08:00
|
|
|
}
|
2019-11-10 05:36:58 -08:00
|
|
|
|
2019-11-10 06:25:38 -08:00
|
|
|
test "LinearFifo" {
|
2019-11-10 05:36:58 -08:00
|
|
|
inline for ([_]type{ u1, u8, u16, u64 }) |T| {
|
2019-11-10 06:25:38 -08:00
|
|
|
inline for ([_]LinearFifoBufferType{ LinearFifoBufferType{ .Static = 32 }, .Slice, .Dynamic }) |bt| {
|
|
|
|
const FifoType = LinearFifo(T, bt);
|
2019-11-10 06:23:21 -08:00
|
|
|
var buf: if (bt == .Slice) [32]T else void = undefined;
|
|
|
|
var fifo = switch (bt) {
|
|
|
|
.Static => FifoType.init(),
|
|
|
|
.Slice => FifoType.init(buf[0..]),
|
2020-01-29 19:22:01 -08:00
|
|
|
.Dynamic => FifoType.init(testing.allocator),
|
2019-11-10 06:23:21 -08:00
|
|
|
};
|
|
|
|
defer fifo.deinit();
|
|
|
|
|
2019-11-27 00:30:39 -08:00
|
|
|
try fifo.write(&[_]T{ 0, 1, 1, 0, 1 });
|
2019-11-10 06:23:21 -08:00
|
|
|
testing.expectEqual(@as(usize, 5), fifo.readableLength());
|
|
|
|
|
|
|
|
{
|
2020-05-14 10:20:27 -07:00
|
|
|
testing.expectEqual(@as(T, 0), fifo.readItem().?);
|
|
|
|
testing.expectEqual(@as(T, 1), fifo.readItem().?);
|
|
|
|
testing.expectEqual(@as(T, 1), fifo.readItem().?);
|
|
|
|
testing.expectEqual(@as(T, 0), fifo.readItem().?);
|
|
|
|
testing.expectEqual(@as(T, 1), fifo.readItem().?);
|
2020-03-30 21:32:31 -07:00
|
|
|
testing.expectEqual(@as(usize, 0), fifo.readableLength());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
try fifo.writeItem(1);
|
|
|
|
try fifo.writeItem(1);
|
|
|
|
try fifo.writeItem(1);
|
|
|
|
testing.expectEqual(@as(usize, 3), fifo.readableLength());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
var readBuf: [3]T = undefined;
|
|
|
|
const n = fifo.read(&readBuf);
|
|
|
|
testing.expectEqual(@as(usize, 3), n); // NOTE: It should be the number of items.
|
2019-11-10 06:23:21 -08:00
|
|
|
}
|
2019-11-10 05:36:58 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|