From 01b07443ff12c24bcddf42ba5039c031c663bcbc Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Sat, 7 Jan 2006 01:05:46 +0000 Subject: [PATCH] Fix C# increment/decrement operator wrappers Add in runtime test to check increment/decrement operators are working correctly (pure C++ test) Expose the function that checks all C++ operators so it can be called from target language git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@8269 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Examples/test-suite/operator_overload.i | 52 +++++++++++++++++--- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/SWIG/Examples/test-suite/operator_overload.i b/SWIG/Examples/test-suite/operator_overload.i index 935b0fd8b..6361459c4 100644 --- a/SWIG/Examples/test-suite/operator_overload.i +++ b/SWIG/Examples/test-suite/operator_overload.i @@ -50,16 +50,24 @@ see bottom for a set of possible tests %csmethodmodifiers operator--(int) "protected"; %typemap(cscode) Op %{ public static Op operator++(Op op) { - return op.PlusPlusPostfix(0); + // Unlike C++, operator++ must not modify the parameter and both prefix and postfix operations call this method + Op newOp = new Op(op.i); + newOp.PlusPlusPostfix(0); + return newOp; } public static Op operator--(Op op) { - return op.MinusMinusPrefix(); + // Unlike C++, operator-- must not modify the parameter and both prefix and postfix operations call this method + Op newOp = new Op(op.i); + newOp.MinusMinusPrefix(); + return newOp; } %} #endif %inline %{ +#include + class Op{ public: int i; @@ -114,6 +122,9 @@ public: Op operator--(int) {Op o = *this; --(*this); return o;} // postfix -- // TODO: <<,<<= + + // This method just checks that the operators are implemented correctly + static void sanity_check(); }; // just to complicate matters @@ -185,12 +196,11 @@ __concat__ for contatenation (if language supports) */ -/* Sample test code in C++ +%{ #include -#include -int main(int argc,char** argv) +void Op::sanity_check() { // test routine: Op a; @@ -261,6 +271,34 @@ int main(int argc,char** argv) //assert(str(Op(1))=="Op(1)"); //assert(str(Op(-3))=="Op(-3)"); - printf("ok\n"); + // test ++ and -- + Op j(100); + int original = j.i; + { + Op newOp = j++; + int newInt = original++; + assert(j.i == original); + assert(newOp.i == newInt); + } + { + Op newOp = j--; + int newInt = original--; + assert(j.i == original); + assert(newOp.i == newInt); + } + { + Op newOp = ++j; + int newInt = ++original; + assert(j.i == original); + assert(newOp.i == newInt); + } + { + Op newOp = --j; + int newInt = --original; + assert(j.i == original); + assert(newOp.i == newInt); + } } -*/ + +%} +