aboutsummaryrefslogtreecommitdiff
path: root/testes
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-08-24 10:17:54 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-08-24 10:17:54 -0300
commit8c8a91f2ef7acccb99e3737913faad8d48b39571 (patch)
tree0807151944b7f7fd00eedfcfe94b4ee26fe25b21 /testes
parentf99509581ee73c1c2dbddb3398e87c098771d31f (diff)
downloadlua-8c8a91f2ef7acccb99e3737913faad8d48b39571.tar.gz
lua-8c8a91f2ef7acccb99e3737913faad8d48b39571.tar.bz2
lua-8c8a91f2ef7acccb99e3737913faad8d48b39571.zip
Deprecated the emulation of '__le' using '__lt'
As hinted in the manual for Lua 5.3, the emulation of the metamethod for '__le' using '__le' has been deprecated. It is slow, complicates the logic, and it is easy to avoid this emulation by defining a proper '__le' function. Moreover, often this emulation was used wrongly, with a programmer assuming that an order is total when it is not (e.g., NaN in floating-point numbers).
Diffstat (limited to 'testes')
-rw-r--r--testes/coroutine.lua8
-rw-r--r--testes/events.lua34
2 files changed, 18 insertions, 24 deletions
diff --git a/testes/coroutine.lua b/testes/coroutine.lua
index 182c1e18..36eae44a 100644
--- a/testes/coroutine.lua
+++ b/testes/coroutine.lua
@@ -1,4 +1,4 @@
1-- $Id: testes/coroutine.lua $ 1-- $Id: testes/coroutine.lua 2018-07-25 15:31:04 -0300 $
2-- See Copyright Notice in file all.lua 2-- See Copyright Notice in file all.lua
3 3
4print "testing coroutines" 4print "testing coroutines"
@@ -619,10 +619,8 @@ end
619 619
620assert(run(function () if (a>=b) then return '>=' else return '<' end end, 620assert(run(function () if (a>=b) then return '>=' else return '<' end end,
621 {"le", "sub"}) == "<") 621 {"le", "sub"}) == "<")
622-- '<=' using '<'
623mt.__le = nil
624assert(run(function () if (a<=b) then return '<=' else return '>' end end, 622assert(run(function () if (a<=b) then return '<=' else return '>' end end,
625 {"lt"}) == "<=") 623 {"le", "sub"}) == "<=")
626assert(run(function () if (a==b) then return '==' else return '~=' end end, 624assert(run(function () if (a==b) then return '==' else return '~=' end end,
627 {"eq"}) == "~=") 625 {"eq"}) == "~=")
628 626
@@ -677,7 +675,7 @@ do -- a few more tests for comparsion operators
677 return val(a) < val(b) 675 return val(a) < val(b)
678 end, 676 end,
679 } 677 }
680 local mt2 = { __lt = mt1.__lt } -- no __le 678 local mt2 = { __lt = mt1.__lt, __le = mt1.__le }
681 679
682 local function run (f) 680 local function run (f)
683 local co = coroutine.wrap(f) 681 local co = coroutine.wrap(f)
diff --git a/testes/events.lua b/testes/events.lua
index 21a822a7..c4d43d51 100644
--- a/testes/events.lua
+++ b/testes/events.lua
@@ -1,4 +1,4 @@
1-- $Id: testes/events.lua $ 1-- $Id: testes/events.lua 2018-07-25 15:31:04 -0300 $
2-- See Copyright Notice in file all.lua 2-- See Copyright Notice in file all.lua
3 3
4print('testing metatables') 4print('testing metatables')
@@ -217,6 +217,13 @@ t.__lt = function (a,b,c)
217 return a<b, "dummy" 217 return a<b, "dummy"
218end 218end
219 219
220t.__le = function (a,b,c)
221 assert(c == nil)
222 if type(a) == 'table' then a = a.x end
223 if type(b) == 'table' then b = b.x end
224 return a<=b, "dummy"
225end
226
220function Op(x) return setmetatable({x=x}, t) end 227function Op(x) return setmetatable({x=x}, t) end
221 228
222local function test () 229local function test ()
@@ -236,15 +243,6 @@ end
236 243
237test() 244test()
238 245
239t.__le = function (a,b,c)
240 assert(c == nil)
241 if type(a) == 'table' then a = a.x end
242 if type(b) == 'table' then b = b.x end
243 return a<=b, "dummy"
244end
245
246test() -- retest comparisons, now using both `lt' and `le'
247
248 246
249-- test `partial order' 247-- test `partial order'
250 248
@@ -266,14 +264,6 @@ t.__lt = function (a,b)
266 return next(b) ~= nil 264 return next(b) ~= nil
267end 265end
268 266
269t.__le = nil
270
271assert(Set{1,2,3} < Set{1,2,3,4})
272assert(not(Set{1,2,3,4} < Set{1,2,3,4}))
273assert((Set{1,2,3,4} <= Set{1,2,3,4}))
274assert((Set{1,2,3,4} >= Set{1,2,3,4}))
275assert((Set{1,3} <= Set{3,5})) -- wrong!! model needs a `le' method ;-)
276
277t.__le = function (a,b) 267t.__le = function (a,b)
278 for k in pairs(a) do 268 for k in pairs(a) do
279 if not b[k] then return false end 269 if not b[k] then return false end
@@ -281,10 +271,15 @@ t.__le = function (a,b)
281 return true 271 return true
282end 272end
283 273
284assert(not (Set{1,3} <= Set{3,5})) -- now its OK! 274assert(Set{1,2,3} < Set{1,2,3,4})
275assert(not(Set{1,2,3,4} < Set{1,2,3,4}))
276assert((Set{1,2,3,4} <= Set{1,2,3,4}))
277assert((Set{1,2,3,4} >= Set{1,2,3,4}))
278assert(not (Set{1,3} <= Set{3,5}))
285assert(not(Set{1,3} <= Set{3,5})) 279assert(not(Set{1,3} <= Set{3,5}))
286assert(not(Set{1,3} >= Set{3,5})) 280assert(not(Set{1,3} >= Set{3,5}))
287 281
282
288t.__eq = function (a,b) 283t.__eq = function (a,b)
289 for k in pairs(a) do 284 for k in pairs(a) do
290 if not b[k] then return false end 285 if not b[k] then return false end
@@ -376,6 +371,7 @@ t1 = {}; c = {}; setmetatable(c, t1)
376d = {} 371d = {}
377t1.__eq = function () return true end 372t1.__eq = function () return true end
378t1.__lt = function () return true end 373t1.__lt = function () return true end
374t1.__le = function () return false end
379setmetatable(d, t1) 375setmetatable(d, t1)
380assert(c == d and c < d and not(d <= c)) 376assert(c == d and c < d and not(d <= c))
381t2 = {} 377t2 = {}