|
| 1 | +--[[ |
| 2 | +SPDX-License-Identifier: ISC |
| 3 | +Copyright (c) 2023-2025, Sergey Bronnikov. |
| 4 | +
|
| 5 | +Synopsis: bit32.bxor (...) |
| 6 | +]] |
| 7 | + |
| 8 | +local luzer = require("luzer") |
| 9 | +local test_lib = require("lib") |
| 10 | + |
| 11 | +local bxor |
| 12 | +local band |
| 13 | +local bor |
| 14 | +local bnot |
| 15 | +if test_lib.version() == "LuaJIT" then |
| 16 | + local bitop = require("bit") |
| 17 | + bxor = bitop.bxor |
| 18 | + band = bitop.band |
| 19 | + bor = bitop.bor |
| 20 | + bnot = bitop.bnot |
| 21 | +else |
| 22 | + bxor = test_lib.bitwise_op("~") |
| 23 | + band = test_lib.bitwise_op("&") |
| 24 | + bor = test_lib.bitwise_op("|") |
| 25 | + bnot = test_lib.bitwise_op("~") |
| 26 | +end |
| 27 | + |
| 28 | +local function TestOneInput(buf) |
| 29 | + local fdp = luzer.FuzzedDataProvider(buf) |
| 30 | + local x = test_lib.random_number(fdp) |
| 31 | + local y = test_lib.random_number(fdp) |
| 32 | + local z = test_lib.random_number(fdp) |
| 33 | + |
| 34 | + assert(bxor(x, y) == bxor(y, x), "x ^ y ~= y ^ x") |
| 35 | + assert(bxor(x, bxor(y, z)) == bxor(bxor(x, y), z), |
| 36 | + "x ^ (y ^ z) ~= (x ^ y) ^ z") |
| 37 | + assert(bxor(x, x) == 0, "x ^ x ~= 0") |
| 38 | + assert(bxor(x, y) == bor(band(x, bnot(y)), band(bnot(x), y)), |
| 39 | + "a ^ b = (a & ~b) | (~a & b)") |
| 40 | + |
| 41 | + -- FIXME |
| 42 | + -- assert(bxor(x, 0xffff) == bnot(x), "x ^ 0xFFFF ~= ~x") |
| 43 | + -- assert(bxor(x, 0) == 0, "x ^ 0 ~= x") |
| 44 | + -- assert(bxor(bxor(x, y), y) == x, "x ^ y ^ y ~= x") |
| 45 | + -- assert(bxor(x, y) == band(bor(x, y), bor(bnot(x), bnot(y)), |
| 46 | + -- "a ^ b = (a | b) & (~a | ~b)")) |
| 47 | +end |
| 48 | + |
| 49 | +local args = { |
| 50 | + artifact_prefix = "bitop_bxor_", |
| 51 | +} |
| 52 | +luzer.Fuzz(TestOneInput, nil, args) |
0 commit comments