Skip to content

Commit ddc7da4

Browse files
Add test for in-place ops
1 parent afabde2 commit ddc7da4

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

tests/snippets/inplace_ops.py

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
class InPlace:
2+
def __init__(self, val):
3+
self.val = val
4+
5+
def __ipow__(self, other):
6+
self.val **= other
7+
return self
8+
9+
def __imul__(self, other):
10+
self.val *= other
11+
return self
12+
13+
def __imatmul__(self, other):
14+
# I guess you could think of an int as a 1x1 matrix
15+
self.val *= other
16+
return self
17+
18+
def __itruediv__(self, other):
19+
self.val /= other
20+
return self
21+
22+
def __ifloordiv__(self, other):
23+
self.val //= other
24+
return self
25+
26+
def __imod__(self, other):
27+
self.val %= other
28+
return self
29+
30+
def __iadd__(self, other):
31+
self.val += other
32+
return self
33+
34+
def __isub__(self, other):
35+
self.val -= other
36+
return self
37+
38+
def __ilshift__(self, other):
39+
self.val <<= other
40+
return self
41+
42+
def __irshift__(self, other):
43+
self.val >>= other
44+
return self
45+
46+
def __iand__(self, other):
47+
self.val &= other
48+
return self
49+
50+
def __ixor__(self, other):
51+
self.val ^= other
52+
return self
53+
54+
def __ior__(self, other):
55+
self.val |= other
56+
return self
57+
58+
59+
i = InPlace(2)
60+
i **= 3
61+
assert i.val == 8
62+
63+
i = InPlace(2)
64+
i *= 2
65+
assert i.val == 4
66+
67+
i = InPlace(2)
68+
i @= 2
69+
assert i.val == 4
70+
71+
i = InPlace(1)
72+
i /= 2
73+
assert i.val == 0.5
74+
75+
i = InPlace(1)
76+
i //= 2
77+
assert i.val == 0
78+
79+
i = InPlace(10)
80+
i %= 3
81+
assert i.val == 1
82+
83+
i = InPlace(1)
84+
i += 1
85+
assert i.val == 2
86+
87+
i = InPlace(2)
88+
i -= 1
89+
assert i.val == 1
90+
91+
i = InPlace(2)
92+
i <<= 3
93+
assert i.val == 16
94+
95+
i = InPlace(16)
96+
i >>= 3
97+
assert i.val == 2
98+
99+
i = InPlace(0b010101)
100+
i &= 0b111000
101+
assert i.val == 0b010000
102+
103+
i = InPlace(0b010101)
104+
i ^= 0b111000
105+
assert i.val == 0b101101
106+
107+
i = InPlace(0b010101)
108+
i |= 0b111000
109+
assert i.val == 0b111101

0 commit comments

Comments
 (0)