aboutsummaryrefslogtreecommitdiff
path: root/src
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 /src
parentbd994461ef7c2553da9a6945c685152bad50eb8f (diff)
downloadluasystem-fdf62fe6c4125d29b3097e49566b2bbe1f650382.tar.gz
luasystem-fdf62fe6c4125d29b3097e49566b2bbe1f650382.tar.bz2
luasystem-fdf62fe6c4125d29b3097e49566b2bbe1f650382.zip
review: bitflags no comparison, extra tests
Diffstat (limited to 'src')
-rw-r--r--src/bitflags.c35
1 files changed, 10 insertions, 25 deletions
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}