diff 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
line wrap: on
line diff
--- a/spec/util_ringbuffer_spec.lua	Mon May 25 14:41:06 2026 +0100
+++ b/spec/util_ringbuffer_spec.lua	Mon May 25 14:44:20 2026 +0100
@@ -49,6 +49,35 @@
 			assert.truthy(b:write("hello world"));
 			test_find(b, "world", 7);
 		end);
+
+		it("works across wraps", function ()
+			local b = rb.new(5);
+			assert.truthy(b:write("12345"));
+			assert.equal("123", b:read(3));
+			assert.truthy(b:write("678"));
+			test_find(b, "45678", 1);
+			test_find(b, "567", 2);
+			test_find(b, "56", 2);
+			test_find(b, "5", 2);
+			test_find(b, "6", 3);
+			test_find(b, "7", 4);
+			test_find(b, "8", 5);
+			test_find(b, "9", nil);
+		end);
+
+		it("no match when the needle is larger than the buffer", function ()
+			for i = 5, 11 do
+				local b = rb.new(i);
+				assert.truthy(b:write("hello"));
+				test_find(b, "hello world", nil);
+			end
+		end);
+
+		it("no match when the needle is an empty string", function ()
+			local b = rb.new(5);
+			assert.truthy(b:write("hello"));
+			test_find(b, "", nil);
+		end);
 	end);
 
 	describe(":sub", function ()