summaryrefslogtreecommitdiff
path: root/Examples/test-suite/lua/equality_runme.lua
blob: cadbede0a431fa3df70147213909b0324aa34a42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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 )