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 /src | |
parent | bd994461ef7c2553da9a6945c685152bad50eb8f (diff) | |
download | luasystem-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.c | 35 |
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 |
78 | local flags4 = sys.bitflag(7) -- b0111 | 78 | local flags4 = sys.bitflag(7) -- b0111 |
79 | local flags5 = sys.bitflag(255) -- b11111111 | 79 | local flags5 = sys.bitflag(255) -- b11111111 |
80 | print(flags5 >= flags4) -- true, all bits in flags4 are set in flags5 | 80 | print(flags5 ~= flags4) -- true, not the same flags |
81 | 81 | local flags6 = sys.bitflag(7) -- b0111 | |
82 | -- comparing with 0 flags: comparison and `has` behave differently | 82 | print(flags6 == flags4) -- true, same flags |
83 | local flags6 = sys.bitflag(0) -- b0000 | 83 | |
84 | local flags7 = sys.bitflag(1) -- b0001 | 84 | -- comparison of subsets |
85 | print(flags6 < flags7) -- true, flags6 is a subset of flags7 | 85 | local flags7 = sys.bitflag(0) -- b0000 |
86 | print(flags7:has(flags6)) -- false, flags6 is not set in flags7 | 86 | local flags8 = sys.bitflag(3) -- b0011 |
87 | local flags9 = sys.bitflag(7) -- b0111 | ||
88 | print(flags9:has(flags8)) -- true, flags8 bits are all set in flags9 | ||
89 | print(flags8:has(flags7)) -- false, flags7 (== 0) is not set in flags8 | ||
87 | */ | 90 | */ |
88 | static int lsbf_new(lua_State *L) { | 91 | static 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 | ||
133 | static 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 | /*** |
142 | Checks if the given flags are set. | 137 | Checks if the given flags are set. |
143 | This is different from the `>=` and `<=` operators because if the flag to check | 138 | This 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 | ||
165 | static 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 | |||
173 | static int lsbf_index(lua_State *L) { | 160 | static 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} |