diff options
author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2024-04-30 21:56:51 +0200 |
---|---|---|
committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2024-04-30 21:56:51 +0200 |
commit | fdf62fe6c4125d29b3097e49566b2bbe1f650382 (patch) | |
tree | 81dcf1a0818af6754c44583db3464ddfcab26af4 | |
parent | bd994461ef7c2553da9a6945c685152bad50eb8f (diff) | |
download | luasystem-fdf62fe6c4125d29b3097e49566b2bbe1f650382.zip |
review: bitflags no comparison, extra tests
-rw-r--r-- | spec/05-bitflags_spec.lua | 17 | ||||
-rw-r--r-- | src/bitflags.c | 35 |
2 files changed, 15 insertions, 37 deletions
diff --git a/spec/05-bitflags_spec.lua b/spec/05-bitflags_spec.lua index 01bf958..8024245 100644 --- a/spec/05-bitflags_spec.lua +++ b/spec/05-bitflags_spec.lua @@ -71,11 +71,13 @@ describe("BitFlags library", function() end) it("sets and clears bits correctly", function() - local bf = sys.bitflag(0) + local bf = sys.bitflag(8) -- b1000 bf[1] = true - assert.is_true(bf[1]) + assert.is_true(bf[1]) -- b1010 + assert.equals(10, bf:value()) bf[1] = false - assert.is_false(bf[1]) + assert.is_false(bf[1]) -- b1000 + assert.equals(8, bf:value()) end) it("errors on setting invalid bit indexes", function() @@ -85,15 +87,6 @@ describe("BitFlags library", function() assert.has_error(function() bf.not_a_number = true end, "index must be a number") end) - it("handles <= and >= operations", function() - local bf1 = sys.bitflag(3) -- b0011 - local bf2 = sys.bitflag(15) -- b1111 - assert.is_true(bf2 >= bf1) -- all bits in bf1 are set in bf2 - assert.is_true(bf2 > bf1) -- all bits in bf1 are set in bf2 and some more - assert.is_false(bf2 <= bf1) -- not all bits in bf2 are set in bf1 - assert.is_false(bf2 < bf1) -- not all bits in bf2 are set in bf1 - end) - it("checks for a subset using 'has'", function() local bf1 = sys.bitflag(3) -- b0011 local bf2 = sys.bitflag(3) -- b0011 diff --git a/src/bitflags.c b/src/bitflags.c index 89a88b7..d90ad1d 100644 --- a/src/bitflags.c +++ b/src/bitflags.c @@ -77,13 +77,16 @@ print(flags3:value()) -- 0 -- comparing flags local flags4 = sys.bitflag(7) -- b0111 local flags5 = sys.bitflag(255) -- b11111111 -print(flags5 >= flags4) -- true, all bits in flags4 are set in flags5 - --- comparing with 0 flags: comparison and `has` behave differently -local flags6 = sys.bitflag(0) -- b0000 -local flags7 = sys.bitflag(1) -- b0001 -print(flags6 < flags7) -- true, flags6 is a subset of flags7 -print(flags7:has(flags6)) -- false, flags6 is not set in flags7 +print(flags5 ~= flags4) -- true, not the same flags +local flags6 = sys.bitflag(7) -- b0111 +print(flags6 == flags4) -- true, same flags + +-- comparison of subsets +local flags7 = sys.bitflag(0) -- b0000 +local flags8 = sys.bitflag(3) -- b0011 +local flags9 = sys.bitflag(7) -- b0111 +print(flags9:has(flags8)) -- true, flags8 bits are all set in flags9 +print(flags8:has(flags7)) -- false, flags7 (== 0) is not set in flags8 */ static int lsbf_new(lua_State *L) { LSBF_BITFLAG flags = 0; @@ -130,14 +133,6 @@ static int lsbf_eq(lua_State *L) { return 1; } -static int lsbf_le(lua_State *L) { - LSBF_BITFLAG a = lsbf_checkbitflags(L, 1); - LSBF_BITFLAG b = lsbf_checkbitflags(L, 2); - // Check if all bits in b are also set in a - lua_pushboolean(L, (a & b) == a); - return 1; -} - /*** Checks if the given flags are set. This is different from the `>=` and `<=` operators because if the flag to check @@ -162,14 +157,6 @@ static int lsbf_has(lua_State *L) { return 1; } -static int lsbf_lt(lua_State *L) { - LSBF_BITFLAG a = lsbf_checkbitflags(L, 1); - LSBF_BITFLAG b = lsbf_checkbitflags(L, 2); - // Check if a is strictly less than b, meaning a != b and a is a subset of b - lua_pushboolean(L, (a != b) && ((a & b) == a)); - return 1; -} - static int lsbf_index(lua_State *L) { if (!lua_isnumber(L, 2)) { // the parameter isn't a number, just lookup the key in the metatable @@ -219,8 +206,6 @@ static const struct luaL_Reg lsbf_methods[] = { {"__add", lsbf_add}, {"__sub", lsbf_sub}, {"__eq", lsbf_eq}, - {"__le", lsbf_le}, - {"__lt", lsbf_lt}, {"__index", lsbf_index}, {"__newindex", lsbf_newindex}, {NULL, NULL} |