|
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)) |
| 1 | +local bit = require 'bit' |
| 2 | +local assert = require 'assert' |
| 3 | + |
| 4 | +function TestAnd(t) |
| 5 | + local tests = { |
| 6 | + { |
| 7 | + input1 = 1, |
| 8 | + input2 = 0, |
| 9 | + expected = 0, |
| 10 | + }, |
| 11 | + { |
| 12 | + input1 = 111, |
| 13 | + input2 = 222, |
| 14 | + expected = 78, |
| 15 | + } |
| 16 | + } |
| 17 | + 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) |
| 20 | + assert:Equal(t, tt.expected, got) |
| 21 | + end) |
| 22 | + end |
10 | 23 | end |
11 | 24 |
|
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)) |
| 25 | + |
| 26 | +function TestOr(t) |
| 27 | + local tests = { |
| 28 | + { |
| 29 | + input1 = 1, |
| 30 | + input2 = 0, |
| 31 | + expected = 1, |
| 32 | + }, |
| 33 | + { |
| 34 | + input1 = 111, |
| 35 | + input2 = 222, |
| 36 | + expected = 255, |
| 37 | + } |
| 38 | + } |
| 39 | + 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) |
| 42 | + assert:Equal(t, tt.expected, got) |
| 43 | + end) |
| 44 | + end |
19 | 45 | end |
20 | 46 |
|
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)) |
| 47 | + |
| 48 | + |
| 49 | +function TestXor(t) |
| 50 | + local tests = { |
| 51 | + { |
| 52 | + input1 = 1, |
| 53 | + input2 = 0, |
| 54 | + expected = 1, |
| 55 | + }, |
| 56 | + { |
| 57 | + input1 = 111, |
| 58 | + input2 = 222, |
| 59 | + expected = 177, |
| 60 | + } |
| 61 | + } |
| 62 | + 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) |
| 65 | + assert:Equal(t, tt.expected, got) |
| 66 | + end) |
| 67 | + end |
28 | 68 | end |
29 | 69 |
|
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)) |
| 70 | +function TestLShift(t) |
| 71 | + local tests = { |
| 72 | + { |
| 73 | + input1 = 123456, |
| 74 | + input2 = 8, |
| 75 | + expected = 31604736, |
| 76 | + }, |
| 77 | + { |
| 78 | + input1 = 0XFF, |
| 79 | + input2 = 8, |
| 80 | + expected = 65280, |
| 81 | + } |
| 82 | + } |
| 83 | + 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) |
| 86 | + assert:Equal(t, tt.expected, got) |
| 87 | + end) |
| 88 | + end |
37 | 89 | end |
38 | 90 |
|
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)) |
| 91 | +function TestRShift(t) |
| 92 | + local tests = { |
| 93 | + { |
| 94 | + input1 = 123456, |
| 95 | + input2 = 8, |
| 96 | + expected = 482, |
| 97 | + }, |
| 98 | + { |
| 99 | + input1 = 0XFF, |
| 100 | + input2 = 1, |
| 101 | + expected = 0x7F, |
| 102 | + } |
| 103 | + } |
| 104 | + 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) |
| 107 | + assert:Equal(t, tt.expected, got) |
| 108 | + end) |
| 109 | + end |
46 | 110 | end |
47 | 111 |
|
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)) |
| 112 | +function TestNot(t) |
| 113 | + local tests = { |
| 114 | + { |
| 115 | + input = 65536, |
| 116 | + expected = 4294901759, |
| 117 | + }, |
| 118 | + { |
| 119 | + input = 4294901759, |
| 120 | + expected = 65536, |
| 121 | + } |
| 122 | + } |
| 123 | + for _, tt in ipairs(tests) do |
| 124 | + t:Run("not "..tostring(tt.input), function(t) |
| 125 | + local got = bit.bnot(tt.input) |
| 126 | + assert:Equal(t, tt.expected, got) |
| 127 | + end) |
| 128 | + end |
55 | 129 | end |
0 commit comments