-
Notifications
You must be signed in to change notification settings - Fork 752
Operator overloads support #1324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5130aaa
0d0ca87
253f9c7
8cdb61c
c26e589
3222a54
550ff31
eab8edc
c2be3f1
581a047
e11327f
35be4bc
f19c281
d7f52d2
5855a1b
e7da0bc
a376838
6923a78
4c992d8
8cce61d
09a2047
41bd07f
10ccf1e
5682e0c
5f45c70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,332 @@ | ||
using NUnit.Framework; | ||
|
||
using Python.Runtime; | ||
|
||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace Python.EmbeddingTest | ||
{ | ||
public class TestOperator | ||
{ | ||
[OneTimeSetUp] | ||
public void SetUp() | ||
{ | ||
PythonEngine.Initialize(); | ||
} | ||
|
||
[OneTimeTearDown] | ||
public void Dispose() | ||
{ | ||
PythonEngine.Shutdown(); | ||
} | ||
|
||
public class OperableObject | ||
{ | ||
public int Num { get; set; } | ||
|
||
public OperableObject(int num) | ||
{ | ||
Num = num; | ||
} | ||
|
||
public static OperableObject operator ~(OperableObject a) | ||
{ | ||
return new OperableObject(~a.Num); | ||
} | ||
|
||
public static OperableObject operator +(OperableObject a) | ||
{ | ||
return new OperableObject(+a.Num); | ||
} | ||
|
||
public static OperableObject operator -(OperableObject a) | ||
{ | ||
return new OperableObject(-a.Num); | ||
} | ||
|
||
public static OperableObject operator +(int a, OperableObject b) | ||
{ | ||
return new OperableObject(a + b.Num); | ||
} | ||
public static OperableObject operator +(OperableObject a, OperableObject b) | ||
{ | ||
return new OperableObject(a.Num + b.Num); | ||
} | ||
public static OperableObject operator +(OperableObject a, int b) | ||
{ | ||
return new OperableObject(a.Num + b); | ||
} | ||
|
||
public static OperableObject operator -(int a, OperableObject b) | ||
{ | ||
return new OperableObject(a - b.Num); | ||
} | ||
public static OperableObject operator -(OperableObject a, OperableObject b) | ||
{ | ||
return new OperableObject(a.Num - b.Num); | ||
} | ||
public static OperableObject operator -(OperableObject a, int b) | ||
{ | ||
return new OperableObject(a.Num - b); | ||
} | ||
|
||
public static OperableObject operator *(int a, OperableObject b) | ||
{ | ||
return new OperableObject(a * b.Num); | ||
} | ||
public static OperableObject operator *(OperableObject a, OperableObject b) | ||
{ | ||
return new OperableObject(a.Num * b.Num); | ||
} | ||
public static OperableObject operator *(OperableObject a, int b) | ||
{ | ||
return new OperableObject(a.Num * b); | ||
} | ||
|
||
public static OperableObject operator /(int a, OperableObject b) | ||
{ | ||
return new OperableObject(a / b.Num); | ||
} | ||
public static OperableObject operator /(OperableObject a, OperableObject b) | ||
{ | ||
return new OperableObject(a.Num / b.Num); | ||
} | ||
public static OperableObject operator /(OperableObject a, int b) | ||
{ | ||
return new OperableObject(a.Num / b); | ||
} | ||
|
||
public static OperableObject operator %(int a, OperableObject b) | ||
{ | ||
return new OperableObject(a % b.Num); | ||
} | ||
public static OperableObject operator %(OperableObject a, OperableObject b) | ||
{ | ||
return new OperableObject(a.Num % b.Num); | ||
} | ||
public static OperableObject operator %(OperableObject a, int b) | ||
{ | ||
return new OperableObject(a.Num % b); | ||
} | ||
|
||
public static OperableObject operator &(int a, OperableObject b) | ||
{ | ||
return new OperableObject(a & b.Num); | ||
} | ||
public static OperableObject operator &(OperableObject a, OperableObject b) | ||
{ | ||
return new OperableObject(a.Num & b.Num); | ||
} | ||
public static OperableObject operator &(OperableObject a, int b) | ||
{ | ||
return new OperableObject(a.Num & b); | ||
} | ||
|
||
public static OperableObject operator |(int a, OperableObject b) | ||
{ | ||
return new OperableObject(a | b.Num); | ||
} | ||
public static OperableObject operator |(OperableObject a, OperableObject b) | ||
{ | ||
return new OperableObject(a.Num | b.Num); | ||
} | ||
public static OperableObject operator |(OperableObject a, int b) | ||
{ | ||
return new OperableObject(a.Num | b); | ||
} | ||
|
||
public static OperableObject operator ^(int a, OperableObject b) | ||
{ | ||
return new OperableObject(a ^ b.Num); | ||
} | ||
public static OperableObject operator ^(OperableObject a, OperableObject b) | ||
{ | ||
return new OperableObject(a.Num ^ b.Num); | ||
} | ||
public static OperableObject operator ^(OperableObject a, int b) | ||
{ | ||
return new OperableObject(a.Num ^ b); | ||
} | ||
|
||
public static OperableObject operator <<(OperableObject a, int offset) | ||
{ | ||
return new OperableObject(a.Num << offset); | ||
} | ||
|
||
public static OperableObject operator >>(OperableObject a, int offset) | ||
{ | ||
return new OperableObject(a.Num >> offset); | ||
} | ||
} | ||
|
||
[Test] | ||
public void OperatorOverloads() | ||
{ | ||
string name = string.Format("{0}.{1}", | ||
typeof(OperableObject).DeclaringType.Name, | ||
typeof(OperableObject).Name); | ||
string module = MethodBase.GetCurrentMethod().DeclaringType.Namespace; | ||
|
||
PythonEngine.Exec($@" | ||
from {module} import * | ||
cls = {name} | ||
a = cls(-2) | ||
b = cls(10) | ||
c = ~a | ||
assert c.Num == ~a.Num | ||
|
||
c = +a | ||
assert c.Num == +a.Num | ||
|
||
a = cls(2) | ||
c = -a | ||
assert c.Num == -a.Num | ||
|
||
c = a + b | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At this point, both of these calls will succeed (and do the same thing):
Is that desirable behavior? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the first statement would have previously failed and now succeeds. I guess this is a question for @lostmsu if we want to let something that's wrongly written, not fail There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code I wrote is Python code. If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Breaking should not be a big deal, since we are making major version bump with (hopefully) a good overhaul. |
||
assert c.Num == a.Num + b.Num | ||
|
||
c = a - b | ||
assert c.Num == a.Num - b.Num | ||
|
||
c = a * b | ||
assert c.Num == a.Num * b.Num | ||
|
||
c = a / b | ||
assert c.Num == a.Num // b.Num | ||
|
||
c = a % b | ||
assert c.Num == a.Num % b.Num | ||
|
||
c = a & b | ||
assert c.Num == a.Num & b.Num | ||
|
||
c = a | b | ||
assert c.Num == a.Num | b.Num | ||
|
||
c = a ^ b | ||
assert c.Num == a.Num ^ b.Num | ||
"); | ||
} | ||
|
||
[Test] | ||
public void OperatorOverloadMissingArgument() | ||
{ | ||
string name = string.Format("{0}.{1}", | ||
typeof(OperableObject).DeclaringType.Name, | ||
typeof(OperableObject).Name); | ||
string module = MethodBase.GetCurrentMethod().DeclaringType.Namespace; | ||
|
||
Assert.Throws<PythonException>(() => | ||
PythonEngine.Exec($@" | ||
from {module} import * | ||
cls = {name} | ||
a = cls(2) | ||
b = cls(10) | ||
a.op_Addition() | ||
")); | ||
} | ||
|
||
[Test] | ||
christabella marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public void ForwardOperatorOverloads() | ||
{ | ||
string name = string.Format("{0}.{1}", | ||
typeof(OperableObject).DeclaringType.Name, | ||
typeof(OperableObject).Name); | ||
string module = MethodBase.GetCurrentMethod().DeclaringType.Namespace; | ||
|
||
PythonEngine.Exec($@" | ||
from {module} import * | ||
cls = {name} | ||
a = cls(2) | ||
b = 10 | ||
c = a + b | ||
assert c.Num == a.Num + b | ||
|
||
c = a - b | ||
assert c.Num == a.Num - b | ||
|
||
c = a * b | ||
assert c.Num == a.Num * b | ||
|
||
c = a / b | ||
assert c.Num == a.Num // b | ||
|
||
c = a % b | ||
assert c.Num == a.Num % b | ||
|
||
c = a & b | ||
assert c.Num == a.Num & b | ||
|
||
c = a | b | ||
assert c.Num == a.Num | b | ||
|
||
c = a ^ b | ||
assert c.Num == a.Num ^ b | ||
"); | ||
} | ||
|
||
|
||
[Test] | ||
public void ReverseOperatorOverloads() | ||
{ | ||
string name = string.Format("{0}.{1}", | ||
typeof(OperableObject).DeclaringType.Name, | ||
typeof(OperableObject).Name); | ||
string module = MethodBase.GetCurrentMethod().DeclaringType.Namespace; | ||
|
||
PythonEngine.Exec($@" | ||
from {module} import * | ||
cls = {name} | ||
a = 2 | ||
b = cls(10) | ||
|
||
c = a + b | ||
assert c.Num == a + b.Num | ||
|
||
c = a - b | ||
assert c.Num == a - b.Num | ||
|
||
c = a * b | ||
assert c.Num == a * b.Num | ||
|
||
christabella marked this conversation as resolved.
Show resolved
Hide resolved
|
||
c = a / b | ||
assert c.Num == a // b.Num | ||
|
||
c = a % b | ||
assert c.Num == a % b.Num | ||
|
||
c = a & b | ||
assert c.Num == a & b.Num | ||
|
||
c = a | b | ||
assert c.Num == a | b.Num | ||
|
||
c = a ^ b | ||
assert c.Num == a ^ b.Num | ||
"); | ||
|
||
} | ||
[Test] | ||
public void ShiftOperatorOverloads() | ||
{ | ||
string name = string.Format("{0}.{1}", | ||
typeof(OperableObject).DeclaringType.Name, | ||
typeof(OperableObject).Name); | ||
string module = MethodBase.GetCurrentMethod().DeclaringType.Namespace; | ||
|
||
PythonEngine.Exec($@" | ||
from {module} import * | ||
cls = {name} | ||
a = cls(2) | ||
b = cls(10) | ||
|
||
c = a << b.Num | ||
assert c.Num == a.Num << b.Num | ||
|
||
c = a >> b.Num | ||
assert c.Num == a.Num >> b.Num | ||
"); | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.