mirror of
https://github.com/fluencelabs/redis
synced 2025-06-14 01:31:21 +00:00
Lua: Add bitop
A few people have written custom C commands because bit manipulation isn't exposed through Lua. Let's give them Mike Pall's bitop. This adds bitop 1.0.2 (2012-05-08) from http://bitop.luajit.org/ bitop is imported as "bit" into the global namespace. New Lua commands: bit.tobit, bit.tohex, bit.bnot, bit.band, bit.bor, bit.bxor, bit.lshift, bit.rshift, bit.arshift, bit.rol, bit.ror, bit.bswap Verification of working (the asserts would abort on error, so (nil) is correct): 127.0.0.1:6379> eval "assert(bit.tobit(1) == 1); assert(bit.band(1) == 1); assert(bit.bxor(1,2) == 3); assert(bit.bor(1,2,4,8,16,32,64,128) == 255)" 0 (nil) 127.0.0.1:6379> eval 'assert(0x7fffffff == 2147483647, "broken hex literals"); assert(0xffffffff == -1 or 0xffffffff == 2^32-1, "broken hex literals"); assert(tostring(-1) == "-1", "broken tostring()"); assert(tostring(0xffffffff) == "-1" or tostring(0xffffffff) == "4294967295", "broken tostring()")' 0 (nil) Tests also integrated into the scripting tests and can be run with: ./runtest --single unit/scripting Tests are excerpted from `bittest.lua` included in the bitop distribution.
This commit is contained in:
@ -184,6 +184,25 @@ start_server {tags {"scripting"}} {
|
||||
set e
|
||||
} {*against a key*}
|
||||
|
||||
test {EVAL - Numerical sanity check from bitop} {
|
||||
r eval {assert(0x7fffffff == 2147483647, "broken hex literals");
|
||||
assert(0xffffffff == -1 or 0xffffffff == 2^32-1,
|
||||
"broken hex literals");
|
||||
assert(tostring(-1) == "-1", "broken tostring()");
|
||||
assert(tostring(0xffffffff) == "-1" or
|
||||
tostring(0xffffffff) == "4294967295",
|
||||
"broken tostring()")
|
||||
} 0
|
||||
} {}
|
||||
|
||||
test {EVAL - Verify minimal bitop functionality} {
|
||||
r eval {assert(bit.tobit(1) == 1);
|
||||
assert(bit.band(1) == 1);
|
||||
assert(bit.bxor(1,2) == 3);
|
||||
assert(bit.bor(1,2,4,8,16,32,64,128) == 255)
|
||||
} 0
|
||||
} {}
|
||||
|
||||
test {SCRIPTING FLUSH - is able to clear the scripts cache?} {
|
||||
r set mykey myval
|
||||
set v [r evalsha fd758d1589d044dd850a6f05d52f2eefd27f033f 1 mykey]
|
||||
|
Reference in New Issue
Block a user