|
| 1 | +local bit = require("bit") |
| 2 | + |
| 3 | +function Test_and(t) |
| 4 | + local result, err = bit.band(1, 0) |
| 5 | + assert(not err, err) |
| 6 | + assert(result == 0, "and get: " .. tostring(result)) |
| 7 | + result, err = bit.band(5, 6) |
| 8 | + assert(not err, err) |
| 9 | + assert(result == 4, "and get: " .. tostring(result)) |
| 10 | +end |
| 11 | + |
| 12 | +function Test_or(t) |
| 13 | + local result, err = bit.bor(1, 0) |
| 14 | + assert(not err, err) |
| 15 | + assert(result == 1, "or get: " .. tostring(result)) |
| 16 | + result, err = bit.bor(5, 6) |
| 17 | + assert(not err, err) |
| 18 | + assert(result == 7, "or get: " .. tostring(result)) |
| 19 | +end |
| 20 | + |
| 21 | +function Test_xor(t) |
| 22 | + local result, err = bit.bxor(1, 0) |
| 23 | + assert(not err, err) |
| 24 | + assert(result == 1, "xor get: " .. tostring(result)) |
| 25 | + result, err = bit.bxor(5, 6) |
| 26 | + assert(not err, err) |
| 27 | + assert(result == 3, "xor get: " .. tostring(result)) |
| 28 | +end |
| 29 | + |
| 30 | +function Test_left_shift(t) |
| 31 | + local result, err = bit.lshift(1, 0) |
| 32 | + assert(not err, err) |
| 33 | + assert(result == 1, "left_shift get: " .. tostring(result)) |
| 34 | + result, err = bit.lshift(0xFF, 8) |
| 35 | + assert(not err, err) |
| 36 | + assert(result == 65280, "left_shift get: " .. tostring(result)) |
| 37 | +end |
| 38 | + |
| 39 | +function Test_right_shift(t) |
| 40 | + local result, err = bit.rshift(42, 2) |
| 41 | + assert(not err, err) |
| 42 | + assert(result == 10, "right_shift get: " .. tostring(result)) |
| 43 | + result, err = bit.rshift(0xFF, 4) |
| 44 | + assert(not err, err) |
| 45 | + assert(result == 15, "right_shift get: " .. tostring(result)) |
| 46 | +end |
| 47 | + |
| 48 | +function Test_not(t) |
| 49 | + local result, err = bit.bnot(65536) |
| 50 | + assert(not err, err) |
| 51 | + assert(result == 4294901759, "not get: " .. tostring(result)) |
| 52 | + result, err = bit.bnot(4294901759) |
| 53 | + assert(not err, err) |
| 54 | + assert(result == 65536, "not get: " .. tostring(result)) |
| 55 | +end |
0 commit comments