aboutsummaryrefslogtreecommitdiffstats
path: root/spec/util_ringbuffer_spec.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2020-06-04 15:19:20 +0100
committerMatthew Wild <mwild1@gmail.com>2020-06-04 15:19:20 +0100
commit14a436817d5faaa1942abb6263a43ca3847f5c5c (patch)
tree1c2e42f58c80fe9a0523b58df382fc30fc6505df /spec/util_ringbuffer_spec.lua
parenta6c4ce73ef53b432b88950450bf479938807e0ae (diff)
downloadprosody-14a436817d5faaa1942abb6263a43ca3847f5c5c.tar.gz
prosody-14a436817d5faaa1942abb6263a43ca3847f5c5c.zip
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Diffstat (limited to 'spec/util_ringbuffer_spec.lua')
-rw-r--r--spec/util_ringbuffer_spec.lua58
1 files changed, 58 insertions, 0 deletions
diff --git a/spec/util_ringbuffer_spec.lua b/spec/util_ringbuffer_spec.lua
index 9512bfd4..72656899 100644
--- a/spec/util_ringbuffer_spec.lua
+++ b/spec/util_ringbuffer_spec.lua
@@ -24,4 +24,62 @@ describe("util.ringbuffer", function ()
assert.truthy(b:write("hi"));
end);
end);
+ describe(":sub", function ()
+ -- Helper function to compare buffer:sub() with string:sub()
+ local function test_sub(b, x, y)
+ local s = b:read(#b, true);
+ 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 = rb.new();
+ b:write("hello world");
+ assert.equals("hello", b:sub(1, 5));
+ end);
+
+ it("supports optional end parameter", function ()
+ local b = rb.new();
+ 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 = rb.new(6);
+ b:write("foobar");
+ b:read(3);
+ b:write("foo");
+ 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 function test_byte(b, x, y)
+ local s = b:read(#b, true);
+ 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 = rb.new(6);
+ b:write("foobar");
+ b:read(3);
+ b:write("foo");
+ 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);
+ end);
end);