Software /
code /
prosody
Annotate
spec/util_ringbuffer_spec.lua @ 10936:d770435f0f84
util.argparse: Move exiting and error to util.startup
It's not so nice to have a library that exits the entire application
from under you, so this and the error reporting belongs in util.startup.
The argparse code was originally in util.startup but moved out in
1196f1e8d178 but the error handling should have stayed.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 17 Jun 2020 19:32:12 +0200 (2020-06-17) |
parent | 10901:5e33926f4b43 |
child | 10949:8b5b35baf370 |
rev | line source |
---|---|
10897
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 local rb = require "util.ringbuffer"; |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 describe("util.ringbuffer", function () |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 describe("#new", function () |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 it("has a constructor", function () |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 assert.Function(rb.new); |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 end); |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 it("can be created", function () |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 assert.truthy(rb.new()); |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 end); |
10898
c6465fb3c839
util.ringbuffer: Prevent creation of zero-size buffer
Kim Alvefur <zash@zash.se>
parents:
10897
diff
changeset
|
10 it("won't create an empty buffer", function () |
c6465fb3c839
util.ringbuffer: Prevent creation of zero-size buffer
Kim Alvefur <zash@zash.se>
parents:
10897
diff
changeset
|
11 assert.has_error(function () |
c6465fb3c839
util.ringbuffer: Prevent creation of zero-size buffer
Kim Alvefur <zash@zash.se>
parents:
10897
diff
changeset
|
12 rb.new(0); |
c6465fb3c839
util.ringbuffer: Prevent creation of zero-size buffer
Kim Alvefur <zash@zash.se>
parents:
10897
diff
changeset
|
13 end); |
c6465fb3c839
util.ringbuffer: Prevent creation of zero-size buffer
Kim Alvefur <zash@zash.se>
parents:
10897
diff
changeset
|
14 end); |
10899
8048255ae61e
util.ringbuffer: Prevent creation of buffer with negative size
Kim Alvefur <zash@zash.se>
parents:
10898
diff
changeset
|
15 it("won't create a negatively sized buffer", function () |
8048255ae61e
util.ringbuffer: Prevent creation of buffer with negative size
Kim Alvefur <zash@zash.se>
parents:
10898
diff
changeset
|
16 assert.has_error(function () |
8048255ae61e
util.ringbuffer: Prevent creation of buffer with negative size
Kim Alvefur <zash@zash.se>
parents:
10898
diff
changeset
|
17 rb.new(-1); |
8048255ae61e
util.ringbuffer: Prevent creation of buffer with negative size
Kim Alvefur <zash@zash.se>
parents:
10898
diff
changeset
|
18 end); |
8048255ae61e
util.ringbuffer: Prevent creation of buffer with negative size
Kim Alvefur <zash@zash.se>
parents:
10898
diff
changeset
|
19 end); |
10897
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 end); |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 describe(":write", function () |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 local b = rb.new(); |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 it("works", function () |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 assert.truthy(b:write("hi")); |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 end); |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 end); |
10901
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
27 describe(":sub", function () |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
28 -- Helper function to compare buffer:sub() with string:sub() |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
29 local function test_sub(b, x, y) |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
30 local s = b:read(#b, true); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
31 local string_result, buffer_result = s:sub(x, y), b:sub(x, y); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
32 assert.equals(string_result, buffer_result, ("buffer:sub(%d, %s) does not match string:sub()"):format(x, y and ("%d"):format(y) or "nil")); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
33 end |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
34 |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
35 it("works", function () |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
36 local b = rb.new(); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
37 b:write("hello world"); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
38 assert.equals("hello", b:sub(1, 5)); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
39 end); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
40 |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
41 it("supports optional end parameter", function () |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
42 local b = rb.new(); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
43 b:write("hello world"); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
44 assert.equals("hello world", b:sub(1)); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
45 assert.equals("world", b:sub(-5)); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
46 end); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
47 |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
48 it("is equivalent to string:sub", function () |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
49 local b = rb.new(6); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
50 b:write("foobar"); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
51 b:read(3); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
52 b:write("foo"); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
53 for i = -13, 13 do |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
54 for j = -13, 13 do |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
55 test_sub(b, i, j); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
56 end |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
57 end |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
58 end); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
59 end); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
60 |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
61 describe(":byte", function () |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
62 -- Helper function to compare buffer:byte() with string:byte() |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
63 local function test_byte(b, x, y) |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
64 local s = b:read(#b, true); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
65 local string_result, buffer_result = {s:byte(x, y)}, {b:byte(x, y)}; |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
66 assert.same(string_result, buffer_result, ("buffer:byte(%d, %s) does not match string:byte()"):format(x, y and ("%d"):format(y) or "nil")); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
67 end |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
68 |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
69 it("is equivalent to string:byte", function () |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
70 local b = rb.new(6); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
71 b:write("foobar"); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
72 b:read(3); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
73 b:write("foo"); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
74 test_byte(b, 1); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
75 test_byte(b, 3); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
76 test_byte(b, -1); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
77 test_byte(b, -3); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
78 for i = -13, 13 do |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
79 for j = -13, 13 do |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
80 test_byte(b, i, j); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
81 end |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
82 end |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
83 end); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
84 end); |
10897
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 end); |