Comparison

spec/util_dbuffer_spec.lua @ 10973:39991e40d1dc

util.dbuffer: dynamic string buffer Similar to util.ringbuffer (and shares almost identical API). Differences: - size limit is optional and dynamic - does not allocate a fixed buffer of max_size bytes - focus on simply storing references to existing string objects where possible, avoiding unnecessary allocations - references are still stored in a ring buffer to enable use as a fast FIFO Optional second parameter to new() provides the number of ring buffer segments. On Lua 5.2 on my laptop, a segment is ~19 bytes. If the ring buffer fills up, the next write will compact all strings into a single item.
author Matthew Wild <mwild1@gmail.com>
date Fri, 26 Jun 2020 16:41:31 +0100
child 10977:29b9b679bdbc
comparison
equal deleted inserted replaced
10972:b3773b1b90a1 10973:39991e40d1dc
1 local dbuffer = require "util.dbuffer";
2 describe("util.dbuffer", function ()
3 describe("#new", function ()
4 it("has a constructor", function ()
5 assert.Function(dbuffer.new);
6 end);
7 it("can be created", function ()
8 assert.truthy(dbuffer.new());
9 end);
10 it("won't create an empty buffer", function ()
11 assert.falsy(dbuffer.new(0));
12 end);
13 it("won't create a negatively sized buffer", function ()
14 assert.falsy(dbuffer.new(-1));
15 end);
16 end);
17 describe(":write", function ()
18 local b = dbuffer.new();
19 it("works", function ()
20 assert.truthy(b:write("hi"));
21 end);
22 end);
23
24 describe(":discard", function ()
25 local b = dbuffer.new();
26 it("works", function ()
27 assert.truthy(b:write("hello world"));
28 assert.truthy(b:discard(6));
29 assert.equal(5, #b);
30 assert.equal("world", b:read(5));
31 end);
32 end);
33
34 describe(":sub", function ()
35 -- Helper function to compare buffer:sub() with string:sub()
36 local s = "hello world";
37 local function test_sub(b, x, y)
38 local string_result, buffer_result = s:sub(x, y), b:sub(x, y);
39 assert.equals(string_result, buffer_result, ("buffer:sub(%d, %s) does not match string:sub()"):format(x, y and ("%d"):format(y) or "nil"));
40 end
41
42 it("works", function ()
43 local b = dbuffer.new();
44 assert.truthy(b:write("hello world"));
45 assert.equals("hello", b:sub(1, 5));
46 end);
47
48 it("supports optional end parameter", function ()
49 local b = dbuffer.new();
50 assert.truthy(b:write("hello world"));
51 assert.equals("hello world", b:sub(1));
52 assert.equals("world", b:sub(-5));
53 end);
54
55 it("is equivalent to string:sub", function ()
56 local b = dbuffer.new(11);
57 assert.truthy(b:write(s));
58 for i = -13, 13 do
59 for j = -13, 13 do
60 test_sub(b, i, j);
61 end
62 end
63 end);
64 end);
65
66 describe(":byte", function ()
67 -- Helper function to compare buffer:byte() with string:byte()
68 local s = "hello world"
69 local function test_byte(b, x, y)
70 local string_result, buffer_result = {s:byte(x, y)}, {b:byte(x, y)};
71 assert.same(string_result, buffer_result, ("buffer:byte(%d, %s) does not match string:byte()"):format(x, y and ("%d"):format(y) or "nil"));
72 end
73
74 it("is equivalent to string:byte", function ()
75 local b = dbuffer.new(11);
76 assert.truthy(b:write(s));
77 test_byte(b, 1);
78 test_byte(b, 3);
79 test_byte(b, -1);
80 test_byte(b, -3);
81 for i = -13, 13 do
82 for j = -13, 13 do
83 test_byte(b, i, j);
84 end
85 end
86 end);
87
88 it("works with characters > 127", function ()
89 local b = dbuffer.new();
90 b:write(string.char(0, 140));
91 local r = { b:byte(1, 2) };
92 assert.same({ 0, 140 }, r);
93 end);
94 end);
95 end);