|
| 1 | +local bit = require 'bit' |
| 2 | +local assert = require 'assert' |
| 3 | + |
| 4 | +function TestAnd(t) |
| 5 | + local tests = { |
| 6 | + { |
| 7 | + input1 = -3, |
| 8 | + input2 = 23, |
| 9 | + expected = nil, |
| 10 | + err = "cannot convert negative int -3 to uint32", |
| 11 | + }, |
| 12 | + { |
| 13 | + input1 = 4294967296, |
| 14 | + input2 = 23, |
| 15 | + expected = nil, |
| 16 | + err = "int 4294967296 overflows uint32", |
| 17 | + }, |
| 18 | + { |
| 19 | + input1 = 1, |
| 20 | + input2 = 0, |
| 21 | + expected = 0, |
| 22 | + }, |
| 23 | + { |
| 24 | + input1 = 111, |
| 25 | + input2 = 222, |
| 26 | + expected = 78, |
| 27 | + } |
| 28 | + } |
| 29 | + for _, tt in ipairs(tests) do |
| 30 | + t:Run(tostring(tt.input1) .. " and " .. tostring(tt.input2), function(t) |
| 31 | + local got, err = bit.band(tt.input1, tt.input2) |
| 32 | + assert:Equal(t, tt.expected, got) |
| 33 | + assert:Equal(t, tt.err, err) |
| 34 | + end) |
| 35 | + end |
| 36 | +end |
| 37 | + |
| 38 | +function TestOr(t) |
| 39 | + local tests = { |
| 40 | + { |
| 41 | + input1 = 5, |
| 42 | + input2 = -423, |
| 43 | + expected = nil, |
| 44 | + err = "cannot convert negative int -423 to uint32", |
| 45 | + }, |
| 46 | + { |
| 47 | + input1 = 123, |
| 48 | + input2 = 4294967296, |
| 49 | + expected = nil, |
| 50 | + err = "int 4294967296 overflows uint32", |
| 51 | + }, |
| 52 | + { |
| 53 | + input1 = 1, |
| 54 | + input2 = 0, |
| 55 | + expected = 1, |
| 56 | + }, |
| 57 | + { |
| 58 | + input1 = 111, |
| 59 | + input2 = 222, |
| 60 | + expected = 255, |
| 61 | + } |
| 62 | + } |
| 63 | + for _, tt in ipairs(tests) do |
| 64 | + t:Run(tostring(tt.input1) .. " or " .. tostring(tt.input2), function(t) |
| 65 | + local got, err = bit.bor(tt.input1, tt.input2) |
| 66 | + assert:Equal(t, tt.expected, got) |
| 67 | + assert:Equal(t, tt.err, err) |
| 68 | + end) |
| 69 | + end |
| 70 | +end |
| 71 | + |
| 72 | +function TestXor(t) |
| 73 | + local tests = { |
| 74 | + { |
| 75 | + input1 = -1, |
| 76 | + input2 = -46, |
| 77 | + expected = nil, |
| 78 | + err = "cannot convert negative int -1 to uint32", |
| 79 | + }, |
| 80 | + { |
| 81 | + input1 = 4294967300, |
| 82 | + input2 = 46, |
| 83 | + expected = nil, |
| 84 | + err = "int 4294967300 overflows uint32", |
| 85 | + }, |
| 86 | + { |
| 87 | + input1 = 1, |
| 88 | + input2 = 0, |
| 89 | + expected = 1, |
| 90 | + }, |
| 91 | + { |
| 92 | + input1 = 111, |
| 93 | + input2 = 222, |
| 94 | + expected = 177, |
| 95 | + } |
| 96 | + } |
| 97 | + for _, tt in ipairs(tests) do |
| 98 | + t:Run(tostring(tt.input1) .. " xor " .. tostring(tt.input2), function(t) |
| 99 | + local got, err = bit.bxor(tt.input1, tt.input2) |
| 100 | + assert:Equal(t, tt.expected, got) |
| 101 | + assert:Equal(t, tt.err, err) |
| 102 | + end) |
| 103 | + end |
| 104 | +end |
| 105 | + |
| 106 | +function TestLShift(t) |
| 107 | + local tests = { |
| 108 | + { |
| 109 | + input1 = 0, |
| 110 | + input2 = -10, |
| 111 | + expected = nil, |
| 112 | + err = "cannot convert negative int -10 to uint32", |
| 113 | + }, |
| 114 | + { |
| 115 | + input1 = 4294967297, |
| 116 | + input2 = 4294967298, |
| 117 | + expected = nil, |
| 118 | + err = "int 4294967297 overflows uint32", |
| 119 | + }, |
| 120 | + { |
| 121 | + input1 = 123456, |
| 122 | + input2 = 8, |
| 123 | + expected = 31604736, |
| 124 | + }, |
| 125 | + { |
| 126 | + input1 = 0XFF, |
| 127 | + input2 = 8, |
| 128 | + expected = 65280, |
| 129 | + } |
| 130 | + } |
| 131 | + for _, tt in ipairs(tests) do |
| 132 | + t:Run(tostring(tt.input1) .. " << " .. tostring(tt.input2), function(t) |
| 133 | + local got, err = bit.lshift(tt.input1, tt.input2) |
| 134 | + assert:Equal(t, tt.expected, got) |
| 135 | + assert:Equal(t, tt.err, err) |
| 136 | + end) |
| 137 | + end |
| 138 | +end |
| 139 | + |
| 140 | +function TestRShift(t) |
| 141 | + local tests = { |
| 142 | + { |
| 143 | + input1 = -10, |
| 144 | + input2 = 0, |
| 145 | + expected = nil, |
| 146 | + err = "cannot convert negative int -10 to uint32", |
| 147 | + }, |
| 148 | + { |
| 149 | + input1 = 4294967296, |
| 150 | + input2 = -3, |
| 151 | + expected = nil, |
| 152 | + err = "int 4294967296 overflows uint32", |
| 153 | + }, |
| 154 | + { |
| 155 | + input1 = 123456, |
| 156 | + input2 = 8, |
| 157 | + expected = 482, |
| 158 | + }, |
| 159 | + { |
| 160 | + input1 = 0XFF, |
| 161 | + input2 = 1, |
| 162 | + expected = 0x7F, |
| 163 | + } |
| 164 | + } |
| 165 | + for _, tt in ipairs(tests) do |
| 166 | + t:Run(tostring(tt.input1) .. " >> " .. tostring(tt.input2), function(t) |
| 167 | + local got, err = bit.rshift(tt.input1, tt.input2) |
| 168 | + assert:Equal(t, tt.expected, got) |
| 169 | + assert:Equal(t, tt.err, err) |
| 170 | + end) |
| 171 | + end |
| 172 | +end |
| 173 | + |
| 174 | +function TestNot(t) |
| 175 | + local tests = { |
| 176 | + { |
| 177 | + input = -3, |
| 178 | + expected = nil, |
| 179 | + err = "cannot convert negative int -3 to uint32", |
| 180 | + }, |
| 181 | + { |
| 182 | + input = 4294967297, |
| 183 | + expected = nil, |
| 184 | + err = "int 4294967297 overflows uint32", |
| 185 | + }, |
| 186 | + { |
| 187 | + input = 65536, |
| 188 | + expected = 4294901759, |
| 189 | + }, |
| 190 | + { |
| 191 | + input = 4294901759, |
| 192 | + expected = 65536, |
| 193 | + } |
| 194 | + } |
| 195 | + for _, tt in ipairs(tests) do |
| 196 | + t:Run("not " .. tostring(tt.input), function(t) |
| 197 | + local got, err = bit.bnot(tt.input) |
| 198 | + assert:Equal(t, tt.expected, got) |
| 199 | + assert:Equal(t, tt.err, err) |
| 200 | + end) |
| 201 | + end |
| 202 | +end |
0 commit comments