aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2020-06-26 16:41:31 +0100
committerMatthew Wild <mwild1@gmail.com>2020-06-26 16:41:31 +0100
commitfb8e10737dfe14aaaa5fbc0295044f1f57dc9ad0 (patch)
tree3bb3b0ab4e6ff550147a36520624d181983af194 /spec
parent0f8937fc27f535fe33480e4db3f0ccb8b7af50ce (diff)
downloadprosody-fb8e10737dfe14aaaa5fbc0295044f1f57dc9ad0.tar.gz
prosody-fb8e10737dfe14aaaa5fbc0295044f1f57dc9ad0.zip
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.
Diffstat (limited to 'spec')
-rw-r--r--spec/util_dbuffer_spec.lua95
1 files changed, 95 insertions, 0 deletions
diff --git a/spec/util_dbuffer_spec.lua b/spec/util_dbuffer_spec.lua
new file mode 100644
index 00000000..854f3125
--- /dev/null
+++ b/spec/util_dbuffer_spec.lua
@@ -0,0 +1,95 @@
+local dbuffer = require "util.dbuffer";
+describe("util.dbuffer", function ()
+ describe("#new", function ()
+ it("has a constructor", function ()
+ assert.Function(dbuffer.new);
+ end);
+ it("can be created", function ()
+ assert.truthy(dbuffer.new());
+ end);
+ it("won't create an empty buffer", function ()
+ assert.falsy(dbuffer.new(0));
+ end);
+ it("won't create a negatively sized buffer", function ()
+ assert.falsy(dbuffer.new(-1));
+ end);
+ end);
+ describe(":write", function ()
+ local b = dbuffer.new();
+ it("works", function ()
+ assert.truthy(b:write("hi"));
+ end);
+ end);
+
+ describe(":discard", function ()
+ local b = dbuffer.new();
+ it("works", function ()
+ assert.truthy(b:write("hello world"));
+ assert.truthy(b:discard(6));
+ assert.equal(5, #b);
+ assert.equal("world", b:read(5));
+ end);
+ end);
+
+ describe(":sub", function ()
+ -- Helper function to compare buffer:sub() with string:sub()
+ local s = "hello world";
+ local function test_sub(b, x, y)
+ local string_result, buffer_result = s:sub(x, y), b:sub(x, y);
+ assert.equals(string_result, buffer_result, ("buffer:sub(%d, %s) does not match string:sub()"):format(x, y and ("%d"):format(y) or "nil"));
+ end
+
+ it("works", function ()
+ local b = dbuffer.new();
+ assert.truthy(b:write("hello world"));
+ assert.equals("hello", b:sub(1, 5));
+ end);
+
+ it("supports optional end parameter", function ()
+ local b = dbuffer.new();
+ assert.truthy(b:write("hello world"));
+ assert.equals("hello world", b:sub(1));
+ assert.equals("world", b:sub(-5));
+ end);
+
+ it("is equivalent to string:sub", function ()
+ local b = dbuffer.new(11);
+ assert.truthy(b:write(s));
+ for i = -13, 13 do
+ for j = -13, 13 do
+ test_sub(b, i, j);
+ end
+ end
+ end);
+ end);
+
+ describe(":byte", function ()
+ -- Helper function to compare buffer:byte() with string:byte()
+ local s = "hello world"
+ local function test_byte(b, x, y)
+ local string_result, buffer_result = {s:byte(x, y)}, {b:byte(x, y)};
+ assert.same(string_result, buffer_result, ("buffer:byte(%d, %s) does not match string:byte()"):format(x, y and ("%d"):format(y) or "nil"));
+ end
+
+ it("is equivalent to string:byte", function ()
+ local b = dbuffer.new(11);
+ assert.truthy(b:write(s));
+ test_byte(b, 1);
+ test_byte(b, 3);
+ test_byte(b, -1);
+ test_byte(b, -3);
+ for i = -13, 13 do
+ for j = -13, 13 do
+ test_byte(b, i, j);
+ end
+ end
+ end);
+
+ it("works with characters > 127", function ()
+ local b = dbuffer.new();
+ b:write(string.char(0, 140));
+ local r = { b:byte(1, 2) };
+ assert.same({ 0, 140 }, r);
+ end);
+ end);
+end);