Software /
code /
prosody
Comparison
spec/util_ringbuffer_spec.lua @ 10901:5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 04 Jun 2020 15:19:20 +0100 |
parent | 10899:8048255ae61e |
child | 10949:8b5b35baf370 |
comparison
equal
deleted
inserted
replaced
10900:9e6d979dd603 | 10901:5e33926f4b43 |
---|---|
22 local b = rb.new(); | 22 local b = rb.new(); |
23 it("works", function () | 23 it("works", function () |
24 assert.truthy(b:write("hi")); | 24 assert.truthy(b:write("hi")); |
25 end); | 25 end); |
26 end); | 26 end); |
27 describe(":sub", function () | |
28 -- Helper function to compare buffer:sub() with string:sub() | |
29 local function test_sub(b, x, y) | |
30 local s = b:read(#b, true); | |
31 local string_result, buffer_result = s:sub(x, y), b:sub(x, y); | |
32 assert.equals(string_result, buffer_result, ("buffer:sub(%d, %s) does not match string:sub()"):format(x, y and ("%d"):format(y) or "nil")); | |
33 end | |
34 | |
35 it("works", function () | |
36 local b = rb.new(); | |
37 b:write("hello world"); | |
38 assert.equals("hello", b:sub(1, 5)); | |
39 end); | |
40 | |
41 it("supports optional end parameter", function () | |
42 local b = rb.new(); | |
43 b:write("hello world"); | |
44 assert.equals("hello world", b:sub(1)); | |
45 assert.equals("world", b:sub(-5)); | |
46 end); | |
47 | |
48 it("is equivalent to string:sub", function () | |
49 local b = rb.new(6); | |
50 b:write("foobar"); | |
51 b:read(3); | |
52 b:write("foo"); | |
53 for i = -13, 13 do | |
54 for j = -13, 13 do | |
55 test_sub(b, i, j); | |
56 end | |
57 end | |
58 end); | |
59 end); | |
60 | |
61 describe(":byte", function () | |
62 -- Helper function to compare buffer:byte() with string:byte() | |
63 local function test_byte(b, x, y) | |
64 local s = b:read(#b, true); | |
65 local string_result, buffer_result = {s:byte(x, y)}, {b:byte(x, y)}; | |
66 assert.same(string_result, buffer_result, ("buffer:byte(%d, %s) does not match string:byte()"):format(x, y and ("%d"):format(y) or "nil")); | |
67 end | |
68 | |
69 it("is equivalent to string:byte", function () | |
70 local b = rb.new(6); | |
71 b:write("foobar"); | |
72 b:read(3); | |
73 b:write("foo"); | |
74 test_byte(b, 1); | |
75 test_byte(b, 3); | |
76 test_byte(b, -1); | |
77 test_byte(b, -3); | |
78 for i = -13, 13 do | |
79 for j = -13, 13 do | |
80 test_byte(b, i, j); | |
81 end | |
82 end | |
83 end); | |
84 end); | |
27 end); | 85 end); |