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
This commit is contained in:
parent
8307e12982
commit
01b07443ff
1 changed files with 45 additions and 7 deletions
|
|
@ -50,16 +50,24 @@ see bottom for a set of possible tests
|
||||||
%csmethodmodifiers operator--(int) "protected";
|
%csmethodmodifiers operator--(int) "protected";
|
||||||
%typemap(cscode) Op %{
|
%typemap(cscode) Op %{
|
||||||
public static Op operator++(Op 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) {
|
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
|
#endif
|
||||||
|
|
||||||
%inline %{
|
%inline %{
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
class Op{
|
class Op{
|
||||||
public:
|
public:
|
||||||
int i;
|
int i;
|
||||||
|
|
@ -114,6 +122,9 @@ public:
|
||||||
Op operator--(int) {Op o = *this; --(*this); return o;} // postfix --
|
Op operator--(int) {Op o = *this; --(*this); return o;} // postfix --
|
||||||
|
|
||||||
// TODO: <<,<<=
|
// TODO: <<,<<=
|
||||||
|
|
||||||
|
// This method just checks that the operators are implemented correctly
|
||||||
|
static void sanity_check();
|
||||||
};
|
};
|
||||||
|
|
||||||
// just to complicate matters
|
// just to complicate matters
|
||||||
|
|
@ -185,12 +196,11 @@ __concat__ for contatenation (if language supports)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/* Sample test code in C++
|
%{
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
int main(int argc,char** argv)
|
void Op::sanity_check()
|
||||||
{
|
{
|
||||||
// test routine:
|
// test routine:
|
||||||
Op a;
|
Op a;
|
||||||
|
|
@ -261,6 +271,34 @@ int main(int argc,char** argv)
|
||||||
//assert(str(Op(1))=="Op(1)");
|
//assert(str(Op(1))=="Op(1)");
|
||||||
//assert(str(Op(-3))=="Op(-3)");
|
//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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
%}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue