Software / code / prosody
Comparison
spec/util_dbuffer_spec.lua @ 12764:bf6d2f9fad4d
util.dbuffer: Add a bunch of missing test cases
Found via mutation testing.
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Tue, 11 Oct 2022 11:38:32 +0100 |
| parent | 11637:19cddf92fcc2 |
comparison
equal
deleted
inserted
replaced
| 12763:d26eefe98d09 | 12764:bf6d2f9fad4d |
|---|---|
| 4 it("has a constructor", function () | 4 it("has a constructor", function () |
| 5 assert.Function(dbuffer.new); | 5 assert.Function(dbuffer.new); |
| 6 end); | 6 end); |
| 7 it("can be created", function () | 7 it("can be created", function () |
| 8 assert.truthy(dbuffer.new()); | 8 assert.truthy(dbuffer.new()); |
| 9 assert.truthy(dbuffer.new(1)); | |
| 10 assert.truthy(dbuffer.new(1024)); | |
| 9 end); | 11 end); |
| 10 it("won't create an empty buffer", function () | 12 it("won't create an empty buffer", function () |
| 11 assert.falsy(dbuffer.new(0)); | 13 assert.falsy(dbuffer.new(0)); |
| 12 end); | 14 end); |
| 13 it("won't create a negatively sized buffer", function () | 15 it("won't create a negatively sized buffer", function () |
| 14 assert.falsy(dbuffer.new(-1)); | 16 assert.falsy(dbuffer.new(-1)); |
| 15 end); | 17 end); |
| 16 end); | 18 end); |
| 17 describe(":write", function () | 19 describe(":write", function () |
| 18 local b = dbuffer.new(); | 20 local b = dbuffer.new(10, 3); |
| 19 it("works", function () | 21 it("works", function () |
| 20 assert.truthy(b:write("hi")); | 22 assert.truthy(b:write("hi")); |
| 23 end); | |
| 24 it("fails when the buffer is full", function () | |
| 25 local ret = b:write(" there world, this is a long piece of data"); | |
| 26 assert.is_falsy(ret); | |
| 27 end); | |
| 28 it("works when max_chunks is reached", function () | |
| 29 -- Chunks are an optimization, dbuffer should collapse chunks when needed | |
| 30 for _ = 1, 8 do | |
| 31 assert.truthy(b:write("!")); | |
| 32 end | |
| 33 assert.falsy(b:write("!")); -- Length reached | |
| 21 end); | 34 end); |
| 22 end); | 35 end); |
| 23 | 36 |
| 24 describe(":read", function () | 37 describe(":read", function () |
| 25 it("supports optional bytes parameter", function () | 38 it("supports optional bytes parameter", function () |
| 31 assert.equal("h", b:read(1)); | 44 assert.equal("h", b:read(1)); |
| 32 | 45 |
| 33 assert.equal("ello", b:read()); | 46 assert.equal("ello", b:read()); |
| 34 assert.equal(" ", b:read()); | 47 assert.equal(" ", b:read()); |
| 35 assert.equal("world", b:read()); | 48 assert.equal("world", b:read()); |
| 49 end); | |
| 50 it("fails when there is not enough data in the buffer", function () | |
| 51 local b = dbuffer.new(12); | |
| 52 b:write("hello"); | |
| 53 b:write(" "); | |
| 54 b:write("world"); | |
| 55 assert.is_falsy(b:read(12)); | |
| 56 assert.is_falsy(b:read(13)); | |
| 36 end); | 57 end); |
| 37 end); | 58 end); |
| 38 | 59 |
| 39 describe(":read_until", function () | 60 describe(":read_until", function () |
| 40 it("works", function () | 61 it("works", function () |
| 66 assert.truthy(b:discard(6)); | 87 assert.truthy(b:discard(6)); |
| 67 assert.equal(5, b:length()); | 88 assert.equal(5, b:length()); |
| 68 assert.equal(5, b:len()); | 89 assert.equal(5, b:len()); |
| 69 assert.equal("world", b:read(5)); | 90 assert.equal("world", b:read(5)); |
| 70 end); | 91 end); |
| 92 it("works across chunks", function () | |
| 93 assert.truthy(b:write("hello")); | |
| 94 assert.truthy(b:write(" ")); | |
| 95 assert.truthy(b:write("world")); | |
| 96 assert.truthy(b:discard(3)); | |
| 97 assert.equal(8, b:length()); | |
| 98 assert.truthy(b:discard(3)); | |
| 99 assert.equal(5, b:length()); | |
| 100 assert.equal("world", b:read(5)); | |
| 101 end); | |
| 102 it("can discard the entire buffer", function () | |
| 103 assert.equal(b:len(), 0); | |
| 104 assert.truthy(b:write("hello world")); | |
| 105 assert.truthy(b:discard(11)); | |
| 106 assert.equal(0, b:len()); | |
| 107 assert.truthy(b:write("hello world")); | |
| 108 assert.truthy(b:discard(12)); | |
| 109 assert.equal(0, b:len()); | |
| 110 assert.truthy(b:write("hello world")); | |
| 111 assert.truthy(b:discard(128)); | |
| 112 assert.equal(0, b:len()); | |
| 113 end); | |
| 114 it("works on an empty buffer", function () | |
| 115 assert.truthy(dbuffer.new():discard()); | |
| 116 assert.truthy(dbuffer.new():discard(0)); | |
| 117 assert.truthy(dbuffer.new():discard(1)); | |
| 118 end); | |
| 71 end); | 119 end); |
| 72 | 120 |
| 73 describe(":collapse()", function () | 121 describe(":collapse()", function () |
| 122 it("works", function () | |
| 123 local b = dbuffer.new(); | |
| 124 b:write("hello"); | |
| 125 b:write(" "); | |
| 126 b:write("world"); | |
| 127 b:collapse(6); | |
| 128 local ret, bytes = b:read_chunk(); | |
| 129 assert.equal("hello ", ret); | |
| 130 assert.equal(6, bytes); | |
| 131 end); | |
| 74 it("works on an empty buffer", function () | 132 it("works on an empty buffer", function () |
| 75 local b = dbuffer.new(); | 133 local b = dbuffer.new(); |
| 76 b:collapse(); | 134 b:collapse(); |
| 77 end); | 135 end); |
| 78 end); | 136 end); |
| 113 for j = -13, 13 do | 171 for j = -13, 13 do |
| 114 test_sub(b, i, j); | 172 test_sub(b, i, j); |
| 115 end | 173 end |
| 116 end | 174 end |
| 117 end); | 175 end); |
| 176 | |
| 177 it("works on an empty buffer", function () | |
| 178 local b = dbuffer.new(); | |
| 179 assert.equal("", b:sub(1, 12)); | |
| 180 end); | |
| 118 end); | 181 end); |
| 119 | 182 |
| 120 describe(":byte", function () | 183 describe(":byte", function () |
| 121 -- Helper function to compare buffer:byte() with string:byte() | 184 -- Helper function to compare buffer:byte() with string:byte() |
| 122 local s = "hello world" | 185 local s = "hello world" |
| 123 local function test_byte(b, x, y) | 186 local function test_byte(b, x, y) |
| 124 local string_result, buffer_result = {s:byte(x, y)}, {b:byte(x, y)}; | 187 local string_result, buffer_result = {s:byte(x, y)}, {b:byte(x, y)}; |
| 125 assert.same(string_result, buffer_result, ("buffer:byte(%d, %s) does not match string:byte()"):format(x, y and ("%d"):format(y) or "nil")); | 188 assert.same( |
| 189 string_result, | |
| 190 buffer_result, | |
| 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") | |
| 192 ); | |
| 126 end | 193 end |
| 127 | 194 |
| 128 it("is equivalent to string:byte", function () | 195 it("is equivalent to string:byte", function () |
| 129 local b = dbuffer.new(11); | 196 local b = dbuffer.new(11); |
| 130 assert.truthy(b:write(s)); | 197 assert.truthy(b:write(s)); |
| 131 test_byte(b, 1); | 198 test_byte(b, 1); |
| 132 test_byte(b, 3); | 199 test_byte(b, 3); |
| 133 test_byte(b, -1); | 200 test_byte(b, -1); |
| 134 test_byte(b, -3); | 201 test_byte(b, -3); |
| 202 test_byte(b, nil, 5); | |
| 135 for i = -13, 13 do | 203 for i = -13, 13 do |
| 136 for j = -13, 13 do | 204 for j = -13, 13 do |
| 137 test_byte(b, i, j); | 205 test_byte(b, i, j); |
| 138 end | 206 end |
| 139 end | 207 end |