From 838f8ebd5bb26c2c96644af9e4f80ea7e150d976 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 29 May 2020 17:53:00 +0200 Subject: util.ringbuffer: Add some initial tests --- spec/util_ringbuffer_spec.lua | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 spec/util_ringbuffer_spec.lua (limited to 'spec/util_ringbuffer_spec.lua') diff --git a/spec/util_ringbuffer_spec.lua b/spec/util_ringbuffer_spec.lua new file mode 100644 index 00000000..5d63b24b --- /dev/null +++ b/spec/util_ringbuffer_spec.lua @@ -0,0 +1,17 @@ +local rb = require "util.ringbuffer"; +describe("util.ringbuffer", function () + describe("#new", function () + it("has a constructor", function () + assert.Function(rb.new); + end); + it("can be created", function () + assert.truthy(rb.new()); + end); + end); + describe(":write", function () + local b = rb.new(); + it("works", function () + assert.truthy(b:write("hi")); + end); + end); +end); -- cgit v1.2.3 From 0bb1474ce668f50c568e7751ac553cbf0acdba77 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 29 May 2020 18:11:42 +0200 Subject: util.ringbuffer: Prevent creation of zero-size buffer --- spec/util_ringbuffer_spec.lua | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'spec/util_ringbuffer_spec.lua') diff --git a/spec/util_ringbuffer_spec.lua b/spec/util_ringbuffer_spec.lua index 5d63b24b..ccb5493a 100644 --- a/spec/util_ringbuffer_spec.lua +++ b/spec/util_ringbuffer_spec.lua @@ -7,6 +7,11 @@ describe("util.ringbuffer", function () it("can be created", function () assert.truthy(rb.new()); end); + it("won't create an empty buffer", function () + assert.has_error(function () + rb.new(0); + end); + end); end); describe(":write", function () local b = rb.new(); -- cgit v1.2.3 From 4bab7af07d4707eda9246156e20610d95967679c Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Thu, 4 Jun 2020 16:11:08 +0200 Subject: util.ringbuffer: Prevent creation of buffer with negative size Previously this would have been (unsigned)-1 which is a large positive integer. --- spec/util_ringbuffer_spec.lua | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'spec/util_ringbuffer_spec.lua') diff --git a/spec/util_ringbuffer_spec.lua b/spec/util_ringbuffer_spec.lua index ccb5493a..9512bfd4 100644 --- a/spec/util_ringbuffer_spec.lua +++ b/spec/util_ringbuffer_spec.lua @@ -12,6 +12,11 @@ describe("util.ringbuffer", function () rb.new(0); end); end); + it("won't create a negatively sized buffer", function () + assert.has_error(function () + rb.new(-1); + end); + end); end); describe(":write", function () local b = rb.new(); -- cgit v1.2.3 From 14a436817d5faaa1942abb6263a43ca3847f5c5c Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 4 Jun 2020 15:19:20 +0100 Subject: util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods --- spec/util_ringbuffer_spec.lua | 58 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'spec/util_ringbuffer_spec.lua') diff --git a/spec/util_ringbuffer_spec.lua b/spec/util_ringbuffer_spec.lua index 9512bfd4..72656899 100644 --- a/spec/util_ringbuffer_spec.lua +++ b/spec/util_ringbuffer_spec.lua @@ -24,4 +24,62 @@ describe("util.ringbuffer", function () assert.truthy(b:write("hi")); end); end); + describe(":sub", function () + -- Helper function to compare buffer:sub() with string:sub() + local function test_sub(b, x, y) + local s = b:read(#b, true); + local string_result, buffer_result = s:sub(x, y), b:sub(x, y); + assert.equals(string_result, buffer_result, ("buffer:sub(%d, %s) does not match string:sub()"):format(x, y and ("%d"):format(y) or "nil")); + end + + it("works", function () + local b = rb.new(); + b:write("hello world"); + assert.equals("hello", b:sub(1, 5)); + end); + + it("supports optional end parameter", function () + local b = rb.new(); + b:write("hello world"); + assert.equals("hello world", b:sub(1)); + assert.equals("world", b:sub(-5)); + end); + + it("is equivalent to string:sub", function () + local b = rb.new(6); + b:write("foobar"); + b:read(3); + b:write("foo"); + for i = -13, 13 do + for j = -13, 13 do + test_sub(b, i, j); + end + end + end); + end); + + describe(":byte", function () + -- Helper function to compare buffer:byte() with string:byte() + local function test_byte(b, x, y) + local s = b:read(#b, true); + 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")); + end + + it("is equivalent to string:byte", function () + local b = rb.new(6); + b:write("foobar"); + b:read(3); + b:write("foo"); + test_byte(b, 1); + test_byte(b, 3); + test_byte(b, -1); + test_byte(b, -3); + for i = -13, 13 do + for j = -13, 13 do + test_byte(b, i, j); + end + end + end); + end); end); -- cgit v1.2.3 From dd0274b8c7499ceac91696cfcfb8e0b799bf0f52 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Tue, 23 Jun 2020 16:50:26 +0100 Subject: util.ringbuffer: Add test for :discard() --- spec/util_ringbuffer_spec.lua | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'spec/util_ringbuffer_spec.lua') diff --git a/spec/util_ringbuffer_spec.lua b/spec/util_ringbuffer_spec.lua index 72656899..9a50fd20 100644 --- a/spec/util_ringbuffer_spec.lua +++ b/spec/util_ringbuffer_spec.lua @@ -24,6 +24,17 @@ describe("util.ringbuffer", function () assert.truthy(b:write("hi")); end); end); + + describe(":discard", function () + local b = rb.new(); + it("works", function () + assert.truthy(b:write("hello world")); + assert.truthy(b:discard(6)); + assert.equal(5, #b); + assert.equal("world", b:read(5)); + end); + end); + describe(":sub", function () -- Helper function to compare buffer:sub() with string:sub() local function test_sub(b, x, y) -- cgit v1.2.3 From b9a670dace9252eaaf3de5871e5a1c6d355989fb Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 24 Jun 2020 12:34:20 +0100 Subject: util.ringbuffer: Ensure unsigned chars are always returned from :byte() --- spec/util_ringbuffer_spec.lua | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'spec/util_ringbuffer_spec.lua') diff --git a/spec/util_ringbuffer_spec.lua b/spec/util_ringbuffer_spec.lua index 9a50fd20..5657b3f6 100644 --- a/spec/util_ringbuffer_spec.lua +++ b/spec/util_ringbuffer_spec.lua @@ -92,5 +92,12 @@ describe("util.ringbuffer", function () end end end); + + it("works with characters > 127", function () + local b = rb.new(); + b:write(string.char(0, 140)); + local r = { b:byte(1, 2) }; + assert.same({ 0, 140 }, r); + end); end); end); -- cgit v1.2.3 From f3add85e3c05049f24a498fd28783cff54f25b3b Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 24 Jun 2020 13:00:11 +0100 Subject: util.ringbuffer: Add some additional asserts to tests --- spec/util_ringbuffer_spec.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'spec/util_ringbuffer_spec.lua') diff --git a/spec/util_ringbuffer_spec.lua b/spec/util_ringbuffer_spec.lua index 5657b3f6..3c052937 100644 --- a/spec/util_ringbuffer_spec.lua +++ b/spec/util_ringbuffer_spec.lua @@ -45,20 +45,20 @@ describe("util.ringbuffer", function () it("works", function () local b = rb.new(); - b:write("hello world"); + assert.truthy(b:write("hello world")); assert.equals("hello", b:sub(1, 5)); end); it("supports optional end parameter", function () local b = rb.new(); - b:write("hello world"); + assert.truthy(b:write("hello world")); assert.equals("hello world", b:sub(1)); assert.equals("world", b:sub(-5)); end); it("is equivalent to string:sub", function () local b = rb.new(6); - b:write("foobar"); + assert.truthy(b:write("foobar")); b:read(3); b:write("foo"); for i = -13, 13 do @@ -79,7 +79,7 @@ describe("util.ringbuffer", function () it("is equivalent to string:byte", function () local b = rb.new(6); - b:write("foobar"); + assert.truthy(b:write("foo"..string.char(0, 140).."obar")); b:read(3); b:write("foo"); test_byte(b, 1); -- cgit v1.2.3 From 558a365575c4ac2d601c19fcbd2bd6fd27078ac6 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 25 Jun 2020 15:45:13 +0100 Subject: util.ringbuffer: Fix accidentally committed test change (thanks buildbot) --- spec/util_ringbuffer_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/util_ringbuffer_spec.lua') diff --git a/spec/util_ringbuffer_spec.lua b/spec/util_ringbuffer_spec.lua index 3c052937..633885a8 100644 --- a/spec/util_ringbuffer_spec.lua +++ b/spec/util_ringbuffer_spec.lua @@ -79,7 +79,7 @@ describe("util.ringbuffer", function () it("is equivalent to string:byte", function () local b = rb.new(6); - assert.truthy(b:write("foo"..string.char(0, 140).."obar")); + assert.truthy(b:write("foobar")); b:read(3); b:write("foo"); test_byte(b, 1); -- cgit v1.2.3