forked from ManuelBlanc/lua-bit64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.luau
69 lines (63 loc) · 2.37 KB
/
tests.luau
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
--!strict
--!nolint IntegerParsing
local bit64 = require("bit64")
local function assertEqual(actual, expected, message: string?)
assert(actual == expected, message or string.format("Expected %s but got %s", tostring(expected), tostring(actual)))
end
local tests = {
{
name = "join and split",
test = function()
local n = 0xFFFFFFFFFFFFF
local high, low = bit64.split(n)
assertEqual(high, 1048575, "split high part failed")
assertEqual(low, 4294967295, "split low part failed")
local joined = bit64.join(high, low)
assertEqual(joined, n, "join failed")
end
},
{
name = "low and high",
test = function()
local n = 0xFFFFFFFFFFFFF
assertEqual(bit64.low(n), 4294967295, "low function failed")
assertEqual(bit64.high(n), 1048575, "high function failed")
end
},
{
name = "bitwise operations",
test = function()
local n1, n2 = 0x123456789ABCD, 0xFEDCBA9876543
assertEqual(bit64.band(n1, n2), 318038595412289, "bitwise AND failed")
assertEqual(bit64.bor(n1, n2), 4485801007116239, "bitwise OR failed")
assertEqual(bit64.bxor(n1, n2), 4167762411703950, "bitwise XOR failed")
assertEqual(
bit64.bnot(0b1111111111110000000000000000000000000000000000000000000000000000),
0b0000000000001111111111111111111111111111111111111111111111111111,
"bitwise NOT failed"
)
end
},
{
name = "shifts",
test = function()
local n = 0x123456789ABCDEF0
assertEqual(bit64.lshift(n, 4), 2541551405711093504, "left shift failed")
assertEqual(bit64.rshift(n, 4), 81985529216486895, "right shift failed")
assertEqual(bit64.arshift(0xF000000000000000, 4), 18374686479671623680, "arithmetic right shift failed")
end
},
{
name = "rotations",
test = function()
local n = 0x123456789ABCDEF0
assertEqual(bit64.lrotate(n, 4), 2541551405711093505, "left rotate failed")
assertEqual(bit64.rrotate(n, 4), 81985529216486895, "right rotate failed")
end
},
}
for _, entry in tests do
print(string.format("Running test '%s'", entry.name))
entry.test()
end
print("All tests passed!")