Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions lua/tests/gmod/unit/libraries/bit/arshift.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--- @type GLuaTest_TestGroup
return {
groupName = "bit.arshift",
cases = {
{
name = "Should be a function",
func = function()
expect( bit.arshift ).to.beA( "function" )
end
},

{
name = "Should correctly shift positive numbers",
func = function()
expect( bit.arshift( 8, 1 ) ).to.equal( 4 )
expect( bit.arshift( 16, 2 ) ).to.equal( 4 )
expect( bit.arshift( 0, 1 ) ).to.equal( 0 )
expect( bit.arshift( 312, 0 ) ).to.equal( 312 )
expect( bit.arshift( 123, 32 ) ).to.equal( 123 )
end
},

{
name = "Returns correct values for negative arhithmetic shifting",
func = function()
expect( bit.arshift( -8, 1 ) ).to.equal( -4 )
expect( bit.arshift( -16, 2 ) ).to.equal( -4 )
expect( bit.arshift( -12345, 5 ) ).to.equal( -386 )
expect( bit.arshift( -1, 10 ) ).to.equal( -1 )
end
},

{
name = "Fails on invalid input",
func = function()
expect( bit.arshift, nil, nil ).to.err()
expect( bit.arshift, "abc", "def" ).to.err()
expect( bit.arshift, {}, {} ).to.err()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Might be better to catch the full error message here

end
},
}
}
54 changes: 54 additions & 0 deletions lua/tests/gmod/unit/libraries/bit/band.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
--- @type GLuaTest_TestGroup
return {
groupName = "bit.band",
cases = {
{
name = "Should be a function",
func = function()
expect( bit.band ).to.beA( "function" )
end
},

{
name = "Should handle positive numbers correctly",
func = function()
expect( bit.band( 8, 8 ) ).to.equal( 8 )
expect( bit.band( 42, 17 ) ).to.equal( 0 )
expect( bit.band( 13, 7 ) ).to.equal( 5 )
end
},

{
name = "Should handle negative numbers correctly",
func = function()
expect( bit.band( -8, 8 ) ).to.equal( 8 )
expect( bit.band( -10, -12 ) ).to.equal( -12 )
end
},

{
name = "Functions properly with more than 2 args",
func = function()
expect( bit.band( 15, 67, 13 ) ).to.equal( 1 )
expect( bit.band( 300, 200, 300 ) ).to.equal( 8 )
expect( bit.band( 1, 2, 4, 8, 16, 32, 64 ) ).to.equal( 0 )
end
},

{
name = "Returns same result when args are flipped",
func = function()
expect( bit.band( 123, 456 ) ).to.equal( bit.band( 456, 123 ) )
end
},

{
name = "Fails on invalid input",
func = function()
expect( bit.band, nil, nil ).to.err()
expect( bit.band, "abc", "def" ).to.err()
expect( bit.band, {}, {} ).to.err()
end
},
}
}
42 changes: 42 additions & 0 deletions lua/tests/gmod/unit/libraries/bit/bnot.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--- @type GLuaTest_TestGroup
return {
groupName = "bit.bnot",
cases = {
{
name = "Should be a function",
func = function()
expect( bit.bnot ).to.beA( "function" )
end
},

{
name = "Should correctly invert positive and negative numbers",
func = function()
expect( bit.bnot( 8 ) ).to.equal( -9 )
expect( bit.bnot( 32 ) ).to.equal( -33 )
expect( bit.bnot( -27534 ) ).to.equal( 27533 )
expect( bit.bnot( -69 ) ).to.equal( 68 )
end
},

{
name = "Should handle edge cases correctly",
func = function()
expect( bit.bnot( 0 ) ).to.equal( -1 )
expect( bit.bnot( -1 ) ).to.equal( 0 )

expect( bit.bnot( 2147483647 ) ).to.equal( -2147483648 )
expect( bit.bnot( -2147483648 ) ).to.equal( 2147483647 )
end
},

{
name = "Fails on invalid input",
func = function()
expect( bit.bnot, nil ).to.err()
expect( bit.bnot, "abc" ).to.err()
expect( bit.bnot, {} ).to.err()
end
},
}
}
62 changes: 62 additions & 0 deletions lua/tests/gmod/unit/libraries/bit/bor.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
--- @type GLuaTest_TestGroup
return {
groupName = "bit.bor",
cases = {
{
name = "Should be a function",
func = function()
expect( bit.bor ).to.beA( "function" )
end
},

{
name = "Should handle positive numbers correctly",
func = function()
expect( bit.bor( 8, 8 ) ).to.equal( 8 )
expect( bit.bor( 8, 1 ) ).to.equal( 9 )
expect( bit.bor( 27533, 17 ) ).to.equal( 27549 )
end
},

{
name = "Should handle negative numbers correctly",
func = function()
expect( bit.bor( -1, 0 ) ).to.equal( -1 )
expect( bit.bor( -8, 1 ) ).to.equal( -7 )
expect( bit.bor( -100, 50 ) ).to.equal( -66 )
end
},

{
name = "Should handle zero values correctly",
func = function()
expect( bit.bor( 0, 69 ) ).to.equal( 69 )
expect( bit.bor( -42, 0 ) ).to.equal( -42 )
end
},

{
name = "Returns correct value with multiple args",
func = function()
expect( bit.bor( 1, 2, 4, 8 ) ).to.equal( 15 )
end
},

{
name = "Handles Min and max values",
func = function()
expect( bit.bor( 2147483647, 2147483648 ) ).to.equal( -1 )
expect( bit.bor( -2147483648, 0 ) ).to.equal( -2147483648 )
end
},

{
name = "Fails on invalid input",
func = function()
expect( bit.bor, nil, nil ).to.err()
expect( bit.bor, "abc", "def" ).to.err()
expect( bit.bor, {}, {} ).to.err()
end
},
}
}
45 changes: 45 additions & 0 deletions lua/tests/gmod/unit/libraries/bit/bswap.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--- @type GLuaTest_TestGroup
return {
groupName = "bit.bswap",
cases = {
{
name = "Should be a function",
func = function()
expect( bit.bswap ).to.beA( "function" )
end
},

{
name = "positive number byte swap",
func = function()
expect( bit.bswap( 327 ) ).to.equal( 1191247872 )
expect( bit.bswap( 8 ) ).to.equal( 134217728 )
end
},

{
name = "Negative number byte swap",
func = function()
expect( bit.bswap( -8 ) ).to.equal( -117440513 )
expect( bit.bswap( -100 ) ).to.equal( -1660944385 )
end
},

{
name = "Swapping 0 and -1 returns the correct value",
func = function()
expect( bit.bswap( 0 ) ).to.equal( 0 )
expect( bit.bswap( -1 ) ).to.equal( -1 )
end
},

{
name = "Fails on invalid input",
func = function()
expect( bit.bswap, nil ).to.err()
expect( bit.bswap, "abc" ).to.err()
expect( bit.bswap, {} ).to.err()
end
},
}
}
56 changes: 56 additions & 0 deletions lua/tests/gmod/unit/libraries/bit/bxor.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
--- @type GLuaTest_TestGroup
return {
groupName = "bit.bxor",
cases = {
{
name = "Should be a function",
func = function()
expect( bit.bxor ).to.beA( "function" )
end
},

{
name = "Positive XOR operation",
func = function()
expect( bit.bxor( 19, 82 ) ).to.equal( 65 )
expect( bit.bxor( 327, 63 ) ).to.equal( 376 )
expect( bit.bxor( 234, 0 ) ).to.equal( 234 )
expect( bit.bxor( 32, 32 ) ).to.equal( 0 )
end
},

{
name = "Negative XOR operation",
func = function()
expect( bit.bxor( -99, -55 ) ).to.equal( 84 )
expect( bit.bxor( -420, -67 ) ).to.equal( 481 )
expect( bit.bxor( -32, 0 ) ).to.equal( -32 )
expect( bit.bxor( 42, -1 ) ).to.equal( -43 )
end
},

{
name = "Handles min and max 32-bit integers correctly",
func = function()
expect( bit.bxor( 2147483647, -2147483648 ) ).to.equal( -1 )
expect( bit.bxor( 2147483647, 1 ) ).to.equal( 2147483646 )
end
},

{
name = "Handles multiple arguments correctly",
func = function()
expect( bit.bxor( 1, 2, 3, 4, 5 ) ).to.equal( 1 )
end
},

{
name = "Fails on invalid input",
func = function()
expect( bit.bxor, nil, nil ).to.err()
expect( bit.bxor, "abc", "def" ).to.err()
expect( bit.bxor, {}, {} ).to.err()
end
},
}
}
53 changes: 53 additions & 0 deletions lua/tests/gmod/unit/libraries/bit/lshift.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
--- @type GLuaTest_TestGroup
return {
groupName = "bit.lshift",
cases = {
{
name = "Should be a function",
func = function()
expect( bit.lshift ).to.beA( "function" )
end
},

{
name = "Should correctly shift positive numbers",
func = function()
expect( bit.lshift( 15, 4 ) ).to.equal( 240 )
expect( bit.lshift( 96, 4 ) ).to.equal( 1536 )
expect( bit.lshift( 63, 2 ) ).to.equal( 252 )
end
},

{
name = "Should correctly shift negative numbers",
func = function()
expect( bit.lshift( -8, 1 ) ).to.equal( -16 )
end
},

{
name = "Values are getting clamped over the signed 32-bit int limit",
func = function()
expect( bit.lshift( 4294967295, 1 ) ).to.equal( -2 )
expect( bit.lshift( 1, 31 ) ).to.equal( -2147483648 )
end
},

{
name = "Shifting 0 times should return the same value",
func = function()
expect( bit.lshift( 1, 0 ) ).to.equal( 1 )
expect( bit.lshift( 12345678, 0 ) ).to.equal( 12345678 )
end
},

{
name = "Fails on invalid input",
func = function()
expect( bit.lshift, nil, nil ).to.err()
expect( bit.lshift, "abc", "def" ).to.err()
expect( bit.lshift, {}, {} ).to.err()
end
},
}
}
Loading