Annotate

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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10973
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local dbuffer = require "util.dbuffer";
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 describe("util.dbuffer", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 describe("#new", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 it("has a constructor", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 assert.Function(dbuffer.new);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 it("can be created", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 assert.truthy(dbuffer.new());
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 it("won't create an empty buffer", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 assert.falsy(dbuffer.new(0));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 it("won't create a negatively sized buffer", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 assert.falsy(dbuffer.new(-1));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 describe(":write", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local b = dbuffer.new();
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 it("works", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 assert.truthy(b:write("hi"));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 describe(":discard", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local b = dbuffer.new();
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 it("works", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 assert.truthy(b:write("hello world"));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 assert.truthy(b:discard(6));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 assert.equal(5, #b);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 assert.equal("world", b:read(5));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 describe(":sub", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 -- Helper function to compare buffer:sub() with string:sub()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 local s = "hello world";
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 local function test_sub(b, x, y)
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 local string_result, buffer_result = s:sub(x, y), b:sub(x, y);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
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"));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 it("works", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local b = dbuffer.new();
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 assert.truthy(b:write("hello world"));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 assert.equals("hello", b:sub(1, 5));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 it("supports optional end parameter", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 local b = dbuffer.new();
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 assert.truthy(b:write("hello world"));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 assert.equals("hello world", b:sub(1));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 assert.equals("world", b:sub(-5));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 it("is equivalent to string:sub", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 local b = dbuffer.new(11);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 assert.truthy(b:write(s));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 for i = -13, 13 do
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 for j = -13, 13 do
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 test_sub(b, i, j);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 describe(":byte", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 -- Helper function to compare buffer:byte() with string:byte()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 local s = "hello world"
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 local function test_byte(b, x, y)
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 local string_result, buffer_result = {s:byte(x, y)}, {b:byte(x, y)};
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
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"));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 it("is equivalent to string:byte", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 local b = dbuffer.new(11);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 assert.truthy(b:write(s));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 test_byte(b, 1);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 test_byte(b, 3);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 test_byte(b, -1);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 test_byte(b, -3);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 for i = -13, 13 do
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 for j = -13, 13 do
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 test_byte(b, i, j);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 end
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 end
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 it("works with characters > 127", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 local b = dbuffer.new();
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 b:write(string.char(0, 140));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 local r = { b:byte(1, 2) };
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 assert.same({ 0, 140 }, r);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 end);