Skip to content

Commit dfef897

Browse files
committed
Add error test cases
Signed-off-by: Dusan Borovcanin <[email protected]>
1 parent 2902ac6 commit dfef897

File tree

1 file changed

+88
-15
lines changed

1 file changed

+88
-15
lines changed

bit/test/test_api.lua

Lines changed: 88 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ local assert = require 'assert'
33

44
function TestAnd(t)
55
local tests = {
6+
{
7+
input1 = -3,
8+
input2 = 23,
9+
expected = nil,
10+
err = "cannot convert negative int -3 to uint32",
11+
},
12+
{
13+
input1 = 4294967296,
14+
input2 = 23,
15+
expected = nil,
16+
err = "int 4294967296 overflows uint32",
17+
},
618
{
719
input1 = 1,
820
input2 = 0,
@@ -15,16 +27,28 @@ function TestAnd(t)
1527
}
1628
}
1729
for _, tt in ipairs(tests) do
18-
t:Run(tostring(tt.input1).." and "..tostring(tt.input2) , function(t)
19-
local got = bit.band(tt.input1, tt.input2)
30+
t:Run(tostring(tt.input1) .. " and " .. tostring(tt.input2), function(t)
31+
local got, err = bit.band(tt.input1, tt.input2)
2032
assert:Equal(t, tt.expected, got)
33+
assert:Equal(t, tt.err, err)
2134
end)
2235
end
2336
end
2437

25-
2638
function TestOr(t)
2739
local tests = {
40+
{
41+
input1 = 5,
42+
input2 = -423,
43+
expected = nil,
44+
err = "cannot convert negative int -423 to uint32",
45+
},
46+
{
47+
input1 = 123,
48+
input2 = 4294967296,
49+
expected = nil,
50+
err = "int 4294967296 overflows uint32",
51+
},
2852
{
2953
input1 = 1,
3054
input2 = 0,
@@ -37,17 +61,28 @@ function TestOr(t)
3761
}
3862
}
3963
for _, tt in ipairs(tests) do
40-
t:Run(tostring(tt.input1).." or "..tostring(tt.input2), function(t)
41-
local got = bit.bor(tt.input1, tt.input2)
64+
t:Run(tostring(tt.input1) .. " or " .. tostring(tt.input2), function(t)
65+
local got, err = bit.bor(tt.input1, tt.input2)
4266
assert:Equal(t, tt.expected, got)
67+
assert:Equal(t, tt.err, err)
4368
end)
4469
end
4570
end
4671

47-
48-
4972
function TestXor(t)
5073
local tests = {
74+
{
75+
input1 = -1,
76+
input2 = -46,
77+
expected = nil,
78+
err = "cannot convert negative int -1 to uint32",
79+
},
80+
{
81+
input1 = 4294967300,
82+
input2 = 46,
83+
expected = nil,
84+
err = "int 4294967300 overflows uint32",
85+
},
5186
{
5287
input1 = 1,
5388
input2 = 0,
@@ -60,15 +95,28 @@ function TestXor(t)
6095
}
6196
}
6297
for _, tt in ipairs(tests) do
63-
t:Run(tostring(tt.input1).." xor "..tostring(tt.input2), function(t)
64-
local got = bit.bxor(tt.input1, tt.input2)
98+
t:Run(tostring(tt.input1) .. " xor " .. tostring(tt.input2), function(t)
99+
local got, err = bit.bxor(tt.input1, tt.input2)
65100
assert:Equal(t, tt.expected, got)
101+
assert:Equal(t, tt.err, err)
66102
end)
67103
end
68104
end
69105

70106
function TestLShift(t)
71107
local tests = {
108+
{
109+
input1 = 0,
110+
input2 = -10,
111+
expected = nil,
112+
err = "cannot convert negative int -10 to uint32",
113+
},
114+
{
115+
input1 = 4294967297,
116+
input2 = 4294967298,
117+
expected = nil,
118+
err = "int 4294967297 overflows uint32",
119+
},
72120
{
73121
input1 = 123456,
74122
input2 = 8,
@@ -81,15 +129,28 @@ function TestLShift(t)
81129
}
82130
}
83131
for _, tt in ipairs(tests) do
84-
t:Run(tostring(tt.input1).." << "..tostring(tt.input2), function(t)
85-
local got = bit.lshift(tt.input1, tt.input2)
132+
t:Run(tostring(tt.input1) .. " << " .. tostring(tt.input2), function(t)
133+
local got, err = bit.lshift(tt.input1, tt.input2)
86134
assert:Equal(t, tt.expected, got)
135+
assert:Equal(t, tt.err, err)
87136
end)
88137
end
89138
end
90139

91140
function TestRShift(t)
92141
local tests = {
142+
{
143+
input1 = -10,
144+
input2 = 0,
145+
expected = nil,
146+
err = "cannot convert negative int -10 to uint32",
147+
},
148+
{
149+
input1 = 4294967296,
150+
input2 = -3,
151+
expected = nil,
152+
err = "int 4294967296 overflows uint32",
153+
},
93154
{
94155
input1 = 123456,
95156
input2 = 8,
@@ -102,15 +163,26 @@ function TestRShift(t)
102163
}
103164
}
104165
for _, tt in ipairs(tests) do
105-
t:Run(tostring(tt.input1).." >> "..tostring(tt.input2), function(t)
106-
local got = bit.rshift(tt.input1, tt.input2)
166+
t:Run(tostring(tt.input1) .. " >> " .. tostring(tt.input2), function(t)
167+
local got, err = bit.rshift(tt.input1, tt.input2)
107168
assert:Equal(t, tt.expected, got)
169+
assert:Equal(t, tt.err, err)
108170
end)
109171
end
110172
end
111173

112174
function TestNot(t)
113175
local tests = {
176+
{
177+
input = -3,
178+
expected = nil,
179+
err = "cannot convert negative int -3 to uint32",
180+
},
181+
{
182+
input = 4294967297,
183+
expected = nil,
184+
err = "int 4294967297 overflows uint32",
185+
},
114186
{
115187
input = 65536,
116188
expected = 4294901759,
@@ -121,9 +193,10 @@ function TestNot(t)
121193
}
122194
}
123195
for _, tt in ipairs(tests) do
124-
t:Run("not "..tostring(tt.input), function(t)
125-
local got = bit.bnot(tt.input)
196+
t:Run("not " .. tostring(tt.input), function(t)
197+
local got, err = bit.bnot(tt.input)
126198
assert:Equal(t, tt.expected, got)
199+
assert:Equal(t, tt.err, err)
127200
end)
128201
end
129202
end

0 commit comments

Comments
 (0)