aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThijs Schreijer <thijs@thijsschreijer.nl>2024-04-30 21:56:51 +0200
committerThijs Schreijer <thijs@thijsschreijer.nl>2024-04-30 21:56:51 +0200
commitfdf62fe6c4125d29b3097e49566b2bbe1f650382 (patch)
tree81dcf1a0818af6754c44583db3464ddfcab26af4
parentbd994461ef7c2553da9a6945c685152bad50eb8f (diff)
downloadluasystem-fdf62fe6c4125d29b3097e49566b2bbe1f650382.tar.gz
luasystem-fdf62fe6c4125d29b3097e49566b2bbe1f650382.tar.bz2
luasystem-fdf62fe6c4125d29b3097e49566b2bbe1f650382.zip
review: bitflags no comparison, extra tests
-rw-r--r--spec/05-bitflags_spec.lua17
-rw-r--r--src/bitflags.c35
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()
71 end) 71 end)
72 72
73 it("sets and clears bits correctly", function() 73 it("sets and clears bits correctly", function()
74 local bf = sys.bitflag(0) 74 local bf = sys.bitflag(8) -- b1000
75 bf[1] = true 75 bf[1] = true
76 assert.is_true(bf[1]) 76 assert.is_true(bf[1]) -- b1010
77 assert.equals(10, bf:value())
77 bf[1] = false 78 bf[1] = false
78 assert.is_false(bf[1]) 79 assert.is_false(bf[1]) -- b1000
80 assert.equals(8, bf:value())
79 end) 81 end)
80 82
81 it("errors on setting invalid bit indexes", function() 83 it("errors on setting invalid bit indexes", function()
@@ -85,15 +87,6 @@ describe("BitFlags library", function()
85 assert.has_error(function() bf.not_a_number = true end, "index must be a number") 87 assert.has_error(function() bf.not_a_number = true end, "index must be a number")
86 end) 88 end)
87 89
88 it("handles <= and >= operations", function()
89 local bf1 = sys.bitflag(3) -- b0011
90 local bf2 = sys.bitflag(15) -- b1111
91 assert.is_true(bf2 >= bf1) -- all bits in bf1 are set in bf2
92 assert.is_true(bf2 > bf1) -- all bits in bf1 are set in bf2 and some more
93 assert.is_false(bf2 <= bf1) -- not all bits in bf2 are set in bf1
94 assert.is_false(bf2 < bf1) -- not all bits in bf2 are set in bf1
95 end)
96
97 it("checks for a subset using 'has'", function() 90 it("checks for a subset using 'has'", function()
98 local bf1 = sys.bitflag(3) -- b0011 91 local bf1 = sys.bitflag(3) -- b0011
99 local bf2 = sys.bitflag(3) -- b0011 92 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
77-- comparing flags 77-- comparing flags
78local flags4 = sys.bitflag(7) -- b0111 78local flags4 = sys.bitflag(7) -- b0111
79local flags5 = sys.bitflag(255) -- b11111111 79local flags5 = sys.bitflag(255) -- b11111111
80print(flags5 >= flags4) -- true, all bits in flags4 are set in flags5 80print(flags5 ~= flags4) -- true, not the same flags
81 81local flags6 = sys.bitflag(7) -- b0111
82-- comparing with 0 flags: comparison and `has` behave differently 82print(flags6 == flags4) -- true, same flags
83local flags6 = sys.bitflag(0) -- b0000 83
84local flags7 = sys.bitflag(1) -- b0001 84-- comparison of subsets
85print(flags6 < flags7) -- true, flags6 is a subset of flags7 85local flags7 = sys.bitflag(0) -- b0000
86print(flags7:has(flags6)) -- false, flags6 is not set in flags7 86local flags8 = sys.bitflag(3) -- b0011
87local flags9 = sys.bitflag(7) -- b0111
88print(flags9:has(flags8)) -- true, flags8 bits are all set in flags9
89print(flags8:has(flags7)) -- false, flags7 (== 0) is not set in flags8
87*/ 90*/
88static int lsbf_new(lua_State *L) { 91static int lsbf_new(lua_State *L) {
89 LSBF_BITFLAG flags = 0; 92 LSBF_BITFLAG flags = 0;
@@ -130,14 +133,6 @@ static int lsbf_eq(lua_State *L) {
130 return 1; 133 return 1;
131} 134}
132 135
133static int lsbf_le(lua_State *L) {
134 LSBF_BITFLAG a = lsbf_checkbitflags(L, 1);
135 LSBF_BITFLAG b = lsbf_checkbitflags(L, 2);
136 // Check if all bits in b are also set in a
137 lua_pushboolean(L, (a & b) == a);
138 return 1;
139}
140
141/*** 136/***
142Checks if the given flags are set. 137Checks if the given flags are set.
143This is different from the `>=` and `<=` operators because if the flag to check 138This is different from the `>=` and `<=` operators because if the flag to check
@@ -162,14 +157,6 @@ static int lsbf_has(lua_State *L) {
162 return 1; 157 return 1;
163} 158}
164 159
165static int lsbf_lt(lua_State *L) {
166 LSBF_BITFLAG a = lsbf_checkbitflags(L, 1);
167 LSBF_BITFLAG b = lsbf_checkbitflags(L, 2);
168 // Check if a is strictly less than b, meaning a != b and a is a subset of b
169 lua_pushboolean(L, (a != b) && ((a & b) == a));
170 return 1;
171}
172
173static int lsbf_index(lua_State *L) { 160static int lsbf_index(lua_State *L) {
174 if (!lua_isnumber(L, 2)) { 161 if (!lua_isnumber(L, 2)) {
175 // the parameter isn't a number, just lookup the key in the metatable 162 // the parameter isn't a number, just lookup the key in the metatable
@@ -219,8 +206,6 @@ static const struct luaL_Reg lsbf_methods[] = {
219 {"__add", lsbf_add}, 206 {"__add", lsbf_add},
220 {"__sub", lsbf_sub}, 207 {"__sub", lsbf_sub},
221 {"__eq", lsbf_eq}, 208 {"__eq", lsbf_eq},
222 {"__le", lsbf_le},
223 {"__lt", lsbf_lt},
224 {"__index", lsbf_index}, 209 {"__index", lsbf_index},
225 {"__newindex", lsbf_newindex}, 210 {"__newindex", lsbf_newindex},
226 {NULL, NULL} 211 {NULL, NULL}