Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

More tests for equality operator overloading

  • Loading branch information...
commit 46d7645c9ae1ecafd5447cb1f287c5f8d1dc5b4f 1 parent 4457d96
@v-for-vandal v-for-vandal authored
View
1  Examples/test-suite/common.mk
@@ -210,6 +210,7 @@ CPP_TEST_CASES += \
enum_template \
enum_thorough \
enum_var \
+ equality \
evil_diamond \
evil_diamond_ns \
evil_diamond_prop \
View
67 Examples/test-suite/equality.i
@@ -0,0 +1,67 @@
+/* File : equality.i */
+/*
+ Specific test for operator== overload. Partially overlaps with
+ operator_overload.i
+*/
+
+%module equality
+
+%inline %{
+
+/* Point has no equality operator */
+typedef struct Point
+{
+ double x;
+ double y;
+} Point;
+
+static const Point s_zeroPoint = { 0.0, 0.0 };
+/* stack version */
+Point MakePoint(double x, double y)
+ { Point new_point = {x, y}; return new_point; }
+
+const Point* GetZeroPointPtr() { return &s_zeroPoint; }
+Point GetZeroPointCopy() { return s_zeroPoint; }
+
+/* EqualOpDefined has correct equality operator */
+class EqualOpDefined {
+public:
+ EqualOpDefined():
+ x(5) {}
+ EqualOpDefined(int val):
+ x(val) {}
+
+ int x;
+};
+
+/* EqualOpWrong has logically incorrect equality operator */
+class EqualOpWrong {
+public:
+ inline static const EqualOpWrong* GetStaticObject();
+};
+
+static const EqualOpWrong s_wrongEqOp;
+
+const EqualOpWrong* EqualOpWrong::GetStaticObject()
+ { return &s_wrongEqOp; }
+
+
+inline bool operator==( const EqualOpDefined& first, const EqualOpDefined& second )
+ { return first.x == second.x; }
+
+inline bool operator==( const EqualOpWrong& first, const EqualOpWrong& second )
+ { return false; }
+
+%}
+
+/*
+ in order to wrapper this correctly
+ we need to extend the class
+ to make the friends & non members part of the class
+*/
+%extend EqualOpDefined {
+ bool operator==(const EqualOpDefined& b){return (*$self) == b;}
+}
+%extend EqualOpWrong {
+ bool operator==(const EqualOpWrong& b){return (*$self) == b;}
+}
View
47 Examples/test-suite/lua/equality_runme.lua
@@ -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 )
Please sign in to comment.
Something went wrong with that request. Please try again.