util.dbuffer: Add a bunch of missing test cases
authorMatthew Wild <mwild1@gmail.com>
Tue, 11 Oct 2022 11:38:32 +0100
changeset 12768 bf6d2f9fad4d
parent 12767 d26eefe98d09
child 12769 132a3c7b25fa
util.dbuffer: Add a bunch of missing test cases Found via mutation testing.
spec/util_dbuffer_spec.lua
--- a/spec/util_dbuffer_spec.lua	Tue Oct 11 11:37:55 2022 +0100
+++ b/spec/util_dbuffer_spec.lua	Tue Oct 11 11:38:32 2022 +0100
@@ -6,6 +6,8 @@
 		end);
 		it("can be created", function ()
 			assert.truthy(dbuffer.new());
+			assert.truthy(dbuffer.new(1));
+			assert.truthy(dbuffer.new(1024));
 		end);
 		it("won't create an empty buffer", function ()
 			assert.falsy(dbuffer.new(0));
@@ -15,10 +17,21 @@
 		end);
 	end);
 	describe(":write", function ()
-		local b = dbuffer.new();
+		local b = dbuffer.new(10, 3);
 		it("works", function ()
 			assert.truthy(b:write("hi"));
 		end);
+		it("fails when the buffer is full", function ()
+			local ret = b:write(" there world, this is a long piece of data");
+			assert.is_falsy(ret);
+		end);
+		it("works when max_chunks is reached", function ()
+			-- Chunks are an optimization, dbuffer should collapse chunks when needed
+			for _ = 1, 8 do
+				assert.truthy(b:write("!"));
+			end
+			assert.falsy(b:write("!")); -- Length reached
+		end);
 	end);
 
 	describe(":read", function ()
@@ -34,6 +47,14 @@
 			assert.equal(" ", b:read());
 			assert.equal("world", b:read());
 		end);
+		it("fails when there is not enough data in the buffer", function ()
+			local b = dbuffer.new(12);
+			b:write("hello");
+			b:write(" ");
+			b:write("world");
+			assert.is_falsy(b:read(12));
+			assert.is_falsy(b:read(13));
+		end);
 	end);
 
 	describe(":read_until", function ()
@@ -68,9 +89,46 @@
 			assert.equal(5, b:len());
 			assert.equal("world", b:read(5));
 		end);
+		it("works across chunks", function ()
+			assert.truthy(b:write("hello"));
+			assert.truthy(b:write(" "));
+			assert.truthy(b:write("world"));
+			assert.truthy(b:discard(3));
+			assert.equal(8, b:length());
+			assert.truthy(b:discard(3));
+			assert.equal(5, b:length());
+			assert.equal("world", b:read(5));
+		end);
+		it("can discard the entire buffer", function ()
+			assert.equal(b:len(), 0);
+			assert.truthy(b:write("hello world"));
+			assert.truthy(b:discard(11));
+			assert.equal(0, b:len());
+			assert.truthy(b:write("hello world"));
+			assert.truthy(b:discard(12));
+			assert.equal(0, b:len());
+			assert.truthy(b:write("hello world"));
+			assert.truthy(b:discard(128));
+			assert.equal(0, b:len());
+		end);
+		it("works on an empty buffer", function ()
+			assert.truthy(dbuffer.new():discard());
+			assert.truthy(dbuffer.new():discard(0));
+			assert.truthy(dbuffer.new():discard(1));
+		end);
 	end);
 
 	describe(":collapse()", function ()
+		it("works", function ()
+			local b = dbuffer.new();
+			b:write("hello");
+			b:write(" ");
+			b:write("world");
+			b:collapse(6);
+			local ret, bytes = b:read_chunk();
+			assert.equal("hello ", ret);
+			assert.equal(6, bytes);
+		end);
 		it("works on an empty buffer", function ()
 			local b = dbuffer.new();
 			b:collapse();
@@ -115,6 +173,11 @@
 				end
 			end
 		end);
+
+		it("works on an empty buffer", function ()
+			local b = dbuffer.new();
+			assert.equal("", b:sub(1, 12));
+		end);
 	end);
 
 	describe(":byte", function ()
@@ -122,7 +185,11 @@
 		local s = "hello world"
 		local function test_byte(b, x, y)
 			local string_result, buffer_result = {s:byte(x, y)}, {b:byte(x, y)};
-			assert.same(string_result, buffer_result, ("buffer:byte(%d, %s) does not match string:byte()"):format(x, y and ("%d"):format(y) or "nil"));
+			assert.same(
+				string_result,
+				buffer_result,
+				("buffer:byte(%s, %s) does not match string:byte()"):format(x and ("%d"):format(x) or "nil", y and ("%d"):format(y) or "nil")
+			);
 		end
 
 		it("is equivalent to string:byte", function ()
@@ -132,6 +199,7 @@
 			test_byte(b, 3);
 			test_byte(b, -1);
 			test_byte(b, -3);
+			test_byte(b, nil, 5);
 			for i = -13, 13 do
 				for j = -13, 13 do
 					test_byte(b, i, j);