Software /
code /
prosody
Comparison
spec/util_ringbuffer_spec.lua @ 11200:bf8f2da84007
Merge 0.11->trunk
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 05 Nov 2020 22:31:25 +0100 |
parent | 10960:f84e0e2faae2 |
comparison
equal
deleted
inserted
replaced
11199:6c7c50a4de32 | 11200:bf8f2da84007 |
---|---|
1 local rb = require "util.ringbuffer"; | |
2 describe("util.ringbuffer", function () | |
3 describe("#new", function () | |
4 it("has a constructor", function () | |
5 assert.Function(rb.new); | |
6 end); | |
7 it("can be created", function () | |
8 assert.truthy(rb.new()); | |
9 end); | |
10 it("won't create an empty buffer", function () | |
11 assert.has_error(function () | |
12 rb.new(0); | |
13 end); | |
14 end); | |
15 it("won't create a negatively sized buffer", function () | |
16 assert.has_error(function () | |
17 rb.new(-1); | |
18 end); | |
19 end); | |
20 end); | |
21 describe(":write", function () | |
22 local b = rb.new(); | |
23 it("works", function () | |
24 assert.truthy(b:write("hi")); | |
25 end); | |
26 end); | |
27 | |
28 describe(":discard", function () | |
29 local b = rb.new(); | |
30 it("works", function () | |
31 assert.truthy(b:write("hello world")); | |
32 assert.truthy(b:discard(6)); | |
33 assert.equal(5, #b); | |
34 assert.equal("world", b:read(5)); | |
35 end); | |
36 end); | |
37 | |
38 describe(":sub", function () | |
39 -- Helper function to compare buffer:sub() with string:sub() | |
40 local function test_sub(b, x, y) | |
41 local s = b:read(#b, true); | |
42 local string_result, buffer_result = s:sub(x, y), b:sub(x, y); | |
43 assert.equals(string_result, buffer_result, ("buffer:sub(%d, %s) does not match string:sub()"):format(x, y and ("%d"):format(y) or "nil")); | |
44 end | |
45 | |
46 it("works", function () | |
47 local b = rb.new(); | |
48 assert.truthy(b:write("hello world")); | |
49 assert.equals("hello", b:sub(1, 5)); | |
50 end); | |
51 | |
52 it("supports optional end parameter", function () | |
53 local b = rb.new(); | |
54 assert.truthy(b:write("hello world")); | |
55 assert.equals("hello world", b:sub(1)); | |
56 assert.equals("world", b:sub(-5)); | |
57 end); | |
58 | |
59 it("is equivalent to string:sub", function () | |
60 local b = rb.new(6); | |
61 assert.truthy(b:write("foobar")); | |
62 b:read(3); | |
63 b:write("foo"); | |
64 for i = -13, 13 do | |
65 for j = -13, 13 do | |
66 test_sub(b, i, j); | |
67 end | |
68 end | |
69 end); | |
70 end); | |
71 | |
72 describe(":byte", function () | |
73 -- Helper function to compare buffer:byte() with string:byte() | |
74 local function test_byte(b, x, y) | |
75 local s = b:read(#b, true); | |
76 local string_result, buffer_result = {s:byte(x, y)}, {b:byte(x, y)}; | |
77 assert.same(string_result, buffer_result, ("buffer:byte(%d, %s) does not match string:byte()"):format(x, y and ("%d"):format(y) or "nil")); | |
78 end | |
79 | |
80 it("is equivalent to string:byte", function () | |
81 local b = rb.new(6); | |
82 assert.truthy(b:write("foobar")); | |
83 b:read(3); | |
84 b:write("foo"); | |
85 test_byte(b, 1); | |
86 test_byte(b, 3); | |
87 test_byte(b, -1); | |
88 test_byte(b, -3); | |
89 for i = -13, 13 do | |
90 for j = -13, 13 do | |
91 test_byte(b, i, j); | |
92 end | |
93 end | |
94 end); | |
95 | |
96 it("works with characters > 127", function () | |
97 local b = rb.new(); | |
98 b:write(string.char(0, 140)); | |
99 local r = { b:byte(1, 2) }; | |
100 assert.same({ 0, 140 }, r); | |
101 end); | |
102 end); | |
103 end); |