Software /
code /
prosody
Annotate
spec/util_dbuffer_spec.lua @ 13007:534c055ec378
util.session: Add 'since' property with timestamp of session creation
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 29 Mar 2023 13:01:28 +0100 |
parent | 12764:bf6d2f9fad4d |
rev | line source |
---|---|
11105 | 1 local dbuffer = require "util.dbuffer"; |
2 describe("util.dbuffer", function () | |
3 describe("#new", function () | |
4 it("has a constructor", function () | |
5 assert.Function(dbuffer.new); | |
6 end); | |
7 it("can be created", function () | |
8 assert.truthy(dbuffer.new()); | |
12764
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
9 assert.truthy(dbuffer.new(1)); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
10 assert.truthy(dbuffer.new(1024)); |
11105 | 11 end); |
12 it("won't create an empty buffer", function () | |
13 assert.falsy(dbuffer.new(0)); | |
14 end); | |
15 it("won't create a negatively sized buffer", function () | |
16 assert.falsy(dbuffer.new(-1)); | |
17 end); | |
18 end); | |
19 describe(":write", function () | |
12764
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
20 local b = dbuffer.new(10, 3); |
11105 | 21 it("works", function () |
22 assert.truthy(b:write("hi")); | |
23 end); | |
12764
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
24 it("fails when the buffer is full", function () |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
25 local ret = b:write(" there world, this is a long piece of data"); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
26 assert.is_falsy(ret); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
27 end); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
28 it("works when max_chunks is reached", function () |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
29 -- Chunks are an optimization, dbuffer should collapse chunks when needed |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
30 for _ = 1, 8 do |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
31 assert.truthy(b:write("!")); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
32 end |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
33 assert.falsy(b:write("!")); -- Length reached |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
34 end); |
11105 | 35 end); |
36 | |
37 describe(":read", function () | |
38 it("supports optional bytes parameter", function () | |
39 -- should return the frontmost chunk | |
40 local b = dbuffer.new(); | |
41 assert.truthy(b:write("hello")); | |
42 assert.truthy(b:write(" ")); | |
43 assert.truthy(b:write("world")); | |
44 assert.equal("h", b:read(1)); | |
45 | |
46 assert.equal("ello", b:read()); | |
47 assert.equal(" ", b:read()); | |
48 assert.equal("world", b:read()); | |
49 end); | |
12764
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
50 it("fails when there is not enough data in the buffer", function () |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
51 local b = dbuffer.new(12); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
52 b:write("hello"); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
53 b:write(" "); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
54 b:write("world"); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
55 assert.is_falsy(b:read(12)); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
56 assert.is_falsy(b:read(13)); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
57 end); |
11105 | 58 end); |
59 | |
11636
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
60 describe(":read_until", function () |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
61 it("works", function () |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
62 local b = dbuffer.new(); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
63 b:write("hello\n"); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
64 b:write("world"); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
65 b:write("\n"); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
66 b:write("\n\n"); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
67 b:write("stuff"); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
68 b:write("more\nand more"); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
69 |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
70 assert.equal(nil, b:read_until(".")); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
71 assert.equal(nil, b:read_until("%")); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
72 assert.equal("hello\n", b:read_until("\n")); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
73 assert.equal("world\n", b:read_until("\n")); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
74 assert.equal("\n", b:read_until("\n")); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
75 assert.equal("\n", b:read_until("\n")); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
76 assert.equal("stu", b:read(3)); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
77 assert.equal("ffmore\n", b:read_until("\n")); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
78 assert.equal(nil, b:read_until("\n")); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
79 assert.equal("and more", b:read_chunk()); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
80 end); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
81 end); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
82 |
11105 | 83 describe(":discard", function () |
84 local b = dbuffer.new(); | |
85 it("works", function () | |
86 assert.truthy(b:write("hello world")); | |
87 assert.truthy(b:discard(6)); | |
88 assert.equal(5, b:length()); | |
11156
a8ef69f7fc35
util.dbuffer: Expose length as :len() method, like strings
Kim Alvefur <zash@zash.se>
parents:
11105
diff
changeset
|
89 assert.equal(5, b:len()); |
11105 | 90 assert.equal("world", b:read(5)); |
91 end); | |
12764
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
92 it("works across chunks", function () |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
93 assert.truthy(b:write("hello")); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
94 assert.truthy(b:write(" ")); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
95 assert.truthy(b:write("world")); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
96 assert.truthy(b:discard(3)); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
97 assert.equal(8, b:length()); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
98 assert.truthy(b:discard(3)); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
99 assert.equal(5, b:length()); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
100 assert.equal("world", b:read(5)); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
101 end); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
102 it("can discard the entire buffer", function () |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
103 assert.equal(b:len(), 0); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
104 assert.truthy(b:write("hello world")); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
105 assert.truthy(b:discard(11)); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
106 assert.equal(0, b:len()); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
107 assert.truthy(b:write("hello world")); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
108 assert.truthy(b:discard(12)); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
109 assert.equal(0, b:len()); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
110 assert.truthy(b:write("hello world")); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
111 assert.truthy(b:discard(128)); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
112 assert.equal(0, b:len()); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
113 end); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
114 it("works on an empty buffer", function () |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
115 assert.truthy(dbuffer.new():discard()); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
116 assert.truthy(dbuffer.new():discard(0)); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
117 assert.truthy(dbuffer.new():discard(1)); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
118 end); |
11105 | 119 end); |
120 | |
121 describe(":collapse()", function () | |
12764
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
122 it("works", function () |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
123 local b = dbuffer.new(); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
124 b:write("hello"); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
125 b:write(" "); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
126 b:write("world"); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
127 b:collapse(6); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
128 local ret, bytes = b:read_chunk(); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
129 assert.equal("hello ", ret); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
130 assert.equal(6, bytes); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
131 end); |
11105 | 132 it("works on an empty buffer", function () |
133 local b = dbuffer.new(); | |
134 b:collapse(); | |
135 end); | |
136 end); | |
137 | |
138 describe(":sub", function () | |
139 -- Helper function to compare buffer:sub() with string:sub() | |
140 local s = "hello world"; | |
141 local function test_sub(b, x, y) | |
142 local string_result, buffer_result = s:sub(x, y), b:sub(x, y); | |
143 assert.equals(string_result, buffer_result, ("buffer:sub(%d, %s) does not match string:sub()"):format(x, y and ("%d"):format(y) or "nil")); | |
144 end | |
145 | |
146 it("works", function () | |
147 local b = dbuffer.new(); | |
148 assert.truthy(b:write("hello world")); | |
149 assert.equals("hello", b:sub(1, 5)); | |
150 end); | |
151 | |
152 it("works after discard", function () | |
153 local b = dbuffer.new(256); | |
154 assert.truthy(b:write("foobar")); | |
155 assert.equals("foobar", b:sub(1, 6)); | |
156 assert.truthy(b:discard(3)); -- consume "foo" | |
157 assert.equals("bar", b:sub(1, 3)); | |
158 end); | |
159 | |
160 it("supports optional end parameter", function () | |
161 local b = dbuffer.new(); | |
162 assert.truthy(b:write("hello world")); | |
163 assert.equals("hello world", b:sub(1)); | |
164 assert.equals("world", b:sub(-5)); | |
165 end); | |
166 | |
167 it("is equivalent to string:sub", function () | |
168 local b = dbuffer.new(11); | |
169 assert.truthy(b:write(s)); | |
170 for i = -13, 13 do | |
171 for j = -13, 13 do | |
172 test_sub(b, i, j); | |
173 end | |
174 end | |
175 end); | |
12764
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
176 |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
177 it("works on an empty buffer", function () |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
178 local b = dbuffer.new(); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
179 assert.equal("", b:sub(1, 12)); |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
180 end); |
11105 | 181 end); |
182 | |
183 describe(":byte", function () | |
184 -- Helper function to compare buffer:byte() with string:byte() | |
185 local s = "hello world" | |
186 local function test_byte(b, x, y) | |
187 local string_result, buffer_result = {s:byte(x, y)}, {b:byte(x, y)}; | |
12764
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
188 assert.same( |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
189 string_result, |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
190 buffer_result, |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
191 ("buffer:byte(%s, %s) does not match string:byte()"):format(x and ("%d"):format(x) or "nil", y and ("%d"):format(y) or "nil") |
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
192 ); |
11105 | 193 end |
194 | |
195 it("is equivalent to string:byte", function () | |
196 local b = dbuffer.new(11); | |
197 assert.truthy(b:write(s)); | |
198 test_byte(b, 1); | |
199 test_byte(b, 3); | |
200 test_byte(b, -1); | |
201 test_byte(b, -3); | |
12764
bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
11637
diff
changeset
|
202 test_byte(b, nil, 5); |
11105 | 203 for i = -13, 13 do |
204 for j = -13, 13 do | |
205 test_byte(b, i, j); | |
206 end | |
207 end | |
208 end); | |
209 | |
210 it("works with characters > 127", function () | |
211 local b = dbuffer.new(); | |
212 b:write(string.char(0, 140)); | |
213 local r = { b:byte(1, 2) }; | |
214 assert.same({ 0, 140 }, r); | |
215 end); | |
216 | |
217 it("works on an empty buffer", function () | |
218 local b = dbuffer.new(); | |
219 assert.equal("", b:sub(1,1)); | |
220 end); | |
221 end); | |
222 end); |