Mercurial > prosody-hg
annotate spec/util_ringbuffer_spec.lua @ 14195:8d726f5d044a 13.0
net.websocket: Fix traceback in client if server doesn't respond with Connection header
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Tue, 26 May 2026 12:26:59 +0100 |
| parents | 67b68147ed1d |
| children |
| 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); |
|
10949
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
27 |
|
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
28 describe(":discard", function () |
|
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
29 local b = rb.new(); |
|
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
30 it("works", function () |
|
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
31 assert.truthy(b:write("hello world")); |
|
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
32 assert.truthy(b:discard(6)); |
|
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
33 assert.equal(5, #b); |
|
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
34 assert.equal("world", b:read(5)); |
|
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
35 end); |
|
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
36 end); |
|
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
37 |
|
14176
2bcabbeac9b0
util.ringbuffer: Fix incorrect returned position from :find() for #needle~=1
Matthew Wild <mwild1@gmail.com>
parents:
10960
diff
changeset
|
38 describe(":find", function () |
|
2bcabbeac9b0
util.ringbuffer: Fix incorrect returned position from :find() for #needle~=1
Matthew Wild <mwild1@gmail.com>
parents:
10960
diff
changeset
|
39 local function test_find(b, str, expected) |
|
2bcabbeac9b0
util.ringbuffer: Fix incorrect returned position from :find() for #needle~=1
Matthew Wild <mwild1@gmail.com>
parents:
10960
diff
changeset
|
40 if expected ~= nil then |
|
2bcabbeac9b0
util.ringbuffer: Fix incorrect returned position from :find() for #needle~=1
Matthew Wild <mwild1@gmail.com>
parents:
10960
diff
changeset
|
41 assert.equal(expected, b:find(str)); |
|
2bcabbeac9b0
util.ringbuffer: Fix incorrect returned position from :find() for #needle~=1
Matthew Wild <mwild1@gmail.com>
parents:
10960
diff
changeset
|
42 else |
|
2bcabbeac9b0
util.ringbuffer: Fix incorrect returned position from :find() for #needle~=1
Matthew Wild <mwild1@gmail.com>
parents:
10960
diff
changeset
|
43 assert.is_nil(b:find(str)); |
|
2bcabbeac9b0
util.ringbuffer: Fix incorrect returned position from :find() for #needle~=1
Matthew Wild <mwild1@gmail.com>
parents:
10960
diff
changeset
|
44 end |
|
2bcabbeac9b0
util.ringbuffer: Fix incorrect returned position from :find() for #needle~=1
Matthew Wild <mwild1@gmail.com>
parents:
10960
diff
changeset
|
45 end |
|
2bcabbeac9b0
util.ringbuffer: Fix incorrect returned position from :find() for #needle~=1
Matthew Wild <mwild1@gmail.com>
parents:
10960
diff
changeset
|
46 |
|
2bcabbeac9b0
util.ringbuffer: Fix incorrect returned position from :find() for #needle~=1
Matthew Wild <mwild1@gmail.com>
parents:
10960
diff
changeset
|
47 it("works", function () |
|
2bcabbeac9b0
util.ringbuffer: Fix incorrect returned position from :find() for #needle~=1
Matthew Wild <mwild1@gmail.com>
parents:
10960
diff
changeset
|
48 local b = rb.new(); |
|
2bcabbeac9b0
util.ringbuffer: Fix incorrect returned position from :find() for #needle~=1
Matthew Wild <mwild1@gmail.com>
parents:
10960
diff
changeset
|
49 assert.truthy(b:write("hello world")); |
|
2bcabbeac9b0
util.ringbuffer: Fix incorrect returned position from :find() for #needle~=1
Matthew Wild <mwild1@gmail.com>
parents:
10960
diff
changeset
|
50 test_find(b, "world", 7); |
|
2bcabbeac9b0
util.ringbuffer: Fix incorrect returned position from :find() for #needle~=1
Matthew Wild <mwild1@gmail.com>
parents:
10960
diff
changeset
|
51 end); |
|
14177
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
52 |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
53 it("works across wraps", function () |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
54 local b = rb.new(5); |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
55 assert.truthy(b:write("12345")); |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
56 assert.equal("123", b:read(3)); |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
57 assert.truthy(b:write("678")); |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
58 test_find(b, "45678", 1); |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
59 test_find(b, "567", 2); |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
60 test_find(b, "56", 2); |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
61 test_find(b, "5", 2); |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
62 test_find(b, "6", 3); |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
63 test_find(b, "7", 4); |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
64 test_find(b, "8", 5); |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
65 test_find(b, "9", nil); |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
66 end); |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
67 |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
68 it("no match when the needle is larger than the buffer", function () |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
69 for i = 5, 11 do |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
70 local b = rb.new(i); |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
71 assert.truthy(b:write("hello")); |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
72 test_find(b, "hello world", nil); |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
73 end |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
74 end); |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
75 |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
76 it("no match when the needle is an empty string", function () |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
77 local b = rb.new(5); |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
78 assert.truthy(b:write("hello")); |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
79 test_find(b, "", nil); |
|
67b68147ed1d
util.ringbuffer: find(): Fix find logic bugs
Matthew Wild <mwild1@gmail.com>
parents:
14176
diff
changeset
|
80 end); |
|
14176
2bcabbeac9b0
util.ringbuffer: Fix incorrect returned position from :find() for #needle~=1
Matthew Wild <mwild1@gmail.com>
parents:
10960
diff
changeset
|
81 end); |
|
2bcabbeac9b0
util.ringbuffer: Fix incorrect returned position from :find() for #needle~=1
Matthew Wild <mwild1@gmail.com>
parents:
10960
diff
changeset
|
82 |
|
10901
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
83 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
|
84 -- 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
|
85 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
|
86 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
|
87 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
|
88 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
|
89 end |
|
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
90 |
|
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
91 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
|
92 local b = rb.new(); |
|
10954
fc310727adfb
util.ringbuffer: Add some additional asserts to tests
Matthew Wild <mwild1@gmail.com>
parents:
10953
diff
changeset
|
93 assert.truthy(b:write("hello world")); |
|
10901
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
94 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
|
95 end); |
|
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
96 |
|
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
97 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
|
98 local b = rb.new(); |
|
10954
fc310727adfb
util.ringbuffer: Add some additional asserts to tests
Matthew Wild <mwild1@gmail.com>
parents:
10953
diff
changeset
|
99 assert.truthy(b:write("hello world")); |
|
10901
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
100 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
|
101 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
|
102 end); |
|
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
103 |
|
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
104 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
|
105 local b = rb.new(6); |
|
10954
fc310727adfb
util.ringbuffer: Add some additional asserts to tests
Matthew Wild <mwild1@gmail.com>
parents:
10953
diff
changeset
|
106 assert.truthy(b:write("foobar")); |
|
10901
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
107 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
|
108 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
|
109 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
|
110 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
|
111 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
|
112 end |
|
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
113 end |
|
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
114 end); |
|
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
115 end); |
|
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
116 |
|
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
117 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
|
118 -- 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
|
119 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
|
120 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
|
121 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
|
122 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
|
123 end |
|
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
124 |
|
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
125 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
|
126 local b = rb.new(6); |
|
10960
f84e0e2faae2
util.ringbuffer: Fix accidentally committed test change (thanks buildbot)
Matthew Wild <mwild1@gmail.com>
parents:
10954
diff
changeset
|
127 assert.truthy(b:write("foobar")); |
|
10901
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
128 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
|
129 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
|
130 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
|
131 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
|
132 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
|
133 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
|
134 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
|
135 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
|
136 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
|
137 end |
|
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
138 end |
|
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
139 end); |
|
10953
c3b3ac63f4c3
util.ringbuffer: Ensure unsigned chars are always returned from :byte()
Matthew Wild <mwild1@gmail.com>
parents:
10949
diff
changeset
|
140 |
|
c3b3ac63f4c3
util.ringbuffer: Ensure unsigned chars are always returned from :byte()
Matthew Wild <mwild1@gmail.com>
parents:
10949
diff
changeset
|
141 it("works with characters > 127", function () |
|
c3b3ac63f4c3
util.ringbuffer: Ensure unsigned chars are always returned from :byte()
Matthew Wild <mwild1@gmail.com>
parents:
10949
diff
changeset
|
142 local b = rb.new(); |
|
c3b3ac63f4c3
util.ringbuffer: Ensure unsigned chars are always returned from :byte()
Matthew Wild <mwild1@gmail.com>
parents:
10949
diff
changeset
|
143 b:write(string.char(0, 140)); |
|
c3b3ac63f4c3
util.ringbuffer: Ensure unsigned chars are always returned from :byte()
Matthew Wild <mwild1@gmail.com>
parents:
10949
diff
changeset
|
144 local r = { b:byte(1, 2) }; |
|
c3b3ac63f4c3
util.ringbuffer: Ensure unsigned chars are always returned from :byte()
Matthew Wild <mwild1@gmail.com>
parents:
10949
diff
changeset
|
145 assert.same({ 0, 140 }, r); |
|
c3b3ac63f4c3
util.ringbuffer: Ensure unsigned chars are always returned from :byte()
Matthew Wild <mwild1@gmail.com>
parents:
10949
diff
changeset
|
146 end); |
|
10901
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
147 end); |
|
10897
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 end); |
