Merge pull request #176 from v-for-vandal/lua_eq
Add default __eq implementation for Lua
This commit is contained in:
commit
c17f77750a
6 changed files with 157 additions and 18 deletions
|
|
@ -86,3 +86,14 @@ f4_name = tostring(f4)
|
|||
assert( f2_name == "Foo" )
|
||||
assert( f3_name == "Foo" )
|
||||
assert( f4_name == "FooSubSub" )
|
||||
|
||||
-- Test __eq implementation supplied by default
|
||||
|
||||
-- eq_f1 and eq_f2 must be different userdata with same Foo* pointer. If eq_f1 and eq_f2 are the same userdata (e.g.)
|
||||
-- > eq_f1 = smth
|
||||
-- > eq_f2 = eq_f1
|
||||
-- then default Lua equality comparison kicks in and considers them equal. Access to global_fptr is actually a
|
||||
-- function call (internally) and it returns new userdata each time.
|
||||
eq_f1 = cb.Bar.global_fptr
|
||||
eq_f2 = cb.Bar.global_fptr
|
||||
assert( eq_f1 == eq_f2 )
|
||||
|
|
|
|||
47
Examples/test-suite/lua/equality_runme.lua
Normal file
47
Examples/test-suite/lua/equality_runme.lua
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
require("import") -- the import fn
|
||||
import("equality") -- import code
|
||||
eq=equality -- renaming import
|
||||
|
||||
-- catch "undefined" global variables
|
||||
local env = _ENV -- Lua 5.2
|
||||
if not env then env = getfenv () end -- Lua 5.1
|
||||
setmetatable(env, {__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
|
||||
|
||||
-- === No equality operator ===
|
||||
|
||||
-- logically same data without equality operator are not equal
|
||||
p1 = eq.MakePoint(10,9);
|
||||
p2 = eq.MakePoint(10,9);
|
||||
|
||||
assert( p1 ~= p2 );
|
||||
|
||||
-- different wrappers for same Point* are equal
|
||||
p3 = eq.GetZeroPointPtr()
|
||||
p4 = eq.GetZeroPointPtr()
|
||||
|
||||
assert( p3 == p4 )
|
||||
|
||||
|
||||
-- === Logically correct equality operator ===
|
||||
|
||||
ed1 = eq.EqualOpDefined(10)
|
||||
ed2 = eq.EqualOpDefined(10)
|
||||
ed3 = eq.EqualOpDefined(15)
|
||||
|
||||
assert( ed1 == ed2 )
|
||||
assert( ed1 ~= ed3 )
|
||||
|
||||
|
||||
-- === Logically incorrect equality operator ===
|
||||
|
||||
ew1 = eq.EqualOpWrong()
|
||||
ew2 = eq.EqualOpWrong()
|
||||
|
||||
assert( ew1 ~= ew2 );
|
||||
|
||||
ew3 = eq.EqualOpWrong.GetStaticObject()
|
||||
ew4 = eq.EqualOpWrong.GetStaticObject()
|
||||
|
||||
-- Even though these are pointers to same object, operator== overload should
|
||||
-- state that they are not equal
|
||||
assert( ew3 ~= ew4 )
|
||||
Loading…
Add table
Add a link
Reference in a new issue