Mercurial > prosody-hg
comparison spec/util_ringbuffer_spec.lua @ 14177:67b68147ed1d 13.0
util.ringbuffer: find(): Fix find logic bugs
If the buffer was full, find() would return earlier (wpos == rpos). Checking
blen is the better way to test if the buffer is empty.
Secondly, the loop scanned for blen - l, but if l (length of needle) was
larger than the buffer length, this calculation would underflow and cause a
lot of spinning.
New tests have been added to cover various edge cases of :find().
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Mon, 25 May 2026 14:44:20 +0100 |
| parents | 2bcabbeac9b0 |
| children |
comparison
equal
deleted
inserted
replaced
| 14176:2bcabbeac9b0 | 14177:67b68147ed1d |
|---|---|
| 46 | 46 |
| 47 it("works", function () | 47 it("works", function () |
| 48 local b = rb.new(); | 48 local b = rb.new(); |
| 49 assert.truthy(b:write("hello world")); | 49 assert.truthy(b:write("hello world")); |
| 50 test_find(b, "world", 7); | 50 test_find(b, "world", 7); |
| 51 end); | |
| 52 | |
| 53 it("works across wraps", function () | |
| 54 local b = rb.new(5); | |
| 55 assert.truthy(b:write("12345")); | |
| 56 assert.equal("123", b:read(3)); | |
| 57 assert.truthy(b:write("678")); | |
| 58 test_find(b, "45678", 1); | |
| 59 test_find(b, "567", 2); | |
| 60 test_find(b, "56", 2); | |
| 61 test_find(b, "5", 2); | |
| 62 test_find(b, "6", 3); | |
| 63 test_find(b, "7", 4); | |
| 64 test_find(b, "8", 5); | |
| 65 test_find(b, "9", nil); | |
| 66 end); | |
| 67 | |
| 68 it("no match when the needle is larger than the buffer", function () | |
| 69 for i = 5, 11 do | |
| 70 local b = rb.new(i); | |
| 71 assert.truthy(b:write("hello")); | |
| 72 test_find(b, "hello world", nil); | |
| 73 end | |
| 74 end); | |
| 75 | |
| 76 it("no match when the needle is an empty string", function () | |
| 77 local b = rb.new(5); | |
| 78 assert.truthy(b:write("hello")); | |
| 79 test_find(b, "", nil); | |
| 51 end); | 80 end); |
| 52 end); | 81 end); |
| 53 | 82 |
| 54 describe(":sub", function () | 83 describe(":sub", function () |
| 55 -- Helper function to compare buffer:sub() with string:sub() | 84 -- Helper function to compare buffer:sub() with string:sub() |
