| 1 |
# Test cases for misc primitives (compile and run) |
| 2 |
# |
| 3 |
# Please only add tests that don't have an obvious place in type-specific test |
| 4 |
# files such as run-strings.test, run-lists.test, etc. |
| 5 |
|
| 6 |
[case testGenericEquality] |
| 7 |
def eq(a: object, b: object) -> bool: |
| 8 |
if a == b: |
| 9 |
return True |
| 10 |
else: |
| 11 |
return False |
| 12 |
def ne(a: object, b: object) -> bool: |
| 13 |
if a != b: |
| 14 |
return True |
| 15 |
else: |
| 16 |
return False |
| 17 |
def f(o: object) -> bool: |
| 18 |
if [1, 2] == o: |
| 19 |
return True |
| 20 |
else: |
| 21 |
return False |
| 22 |
[file driver.py] |
| 23 |
from native import eq, ne, f |
| 24 |
assert eq('xz', 'x' + 'z') |
| 25 |
assert not eq('x', 'y') |
| 26 |
assert not ne('xz', 'x' + 'z') |
| 27 |
assert ne('x', 'y') |
| 28 |
assert f([1, 2]) |
| 29 |
assert not f([2, 2]) |
| 30 |
assert not f(1) |
| 31 |
|
| 32 |
[case testGenericBinaryOps] |
| 33 |
from typing import Any |
| 34 |
def add(x: Any, y: Any) -> Any: |
| 35 |
return x + y |
| 36 |
def subtract(x: Any, y: Any) -> Any: |
| 37 |
return x - y |
| 38 |
def multiply(x: Any, y: Any) -> Any: |
| 39 |
return x * y |
| 40 |
def floor_div(x: Any, y: Any) -> Any: |
| 41 |
return x // y |
| 42 |
def true_div(x: Any, y: Any) -> Any: |
| 43 |
return x / y |
| 44 |
def remainder(x: Any, y: Any) -> Any: |
| 45 |
return x % y |
| 46 |
def power(x: Any, y: Any) -> Any: |
| 47 |
return x ** y |
| 48 |
def lshift(x: Any, y: Any) -> Any: |
| 49 |
return x << y |
| 50 |
def rshift(x: Any, y: Any) -> Any: |
| 51 |
return x >> y |
| 52 |
def num_and(x: Any, y: Any) -> Any: |
| 53 |
return x & y |
| 54 |
def num_xor(x: Any, y: Any) -> Any: |
| 55 |
return x ^ y |
| 56 |
def num_or(x: Any, y: Any) -> Any: |
| 57 |
return x | y |
| 58 |
def lt(x: Any, y: Any) -> Any: |
| 59 |
if x < y: |
| 60 |
return True |
| 61 |
else: |
| 62 |
return False |
| 63 |
def le(x: Any, y: Any) -> Any: |
| 64 |
if x <= y: |
| 65 |
return True |
| 66 |
else: |
| 67 |
return False |
| 68 |
def gt(x: Any, y: Any) -> Any: |
| 69 |
if x > y: |
| 70 |
return True |
| 71 |
else: |
| 72 |
return False |
| 73 |
def ge(x: Any, y: Any) -> Any: |
| 74 |
if x >= y: |
| 75 |
return True |
| 76 |
else: |
| 77 |
return False |
| 78 |
def contains(x: Any, y: Any) -> Any: |
| 79 |
if x in y: |
| 80 |
return True |
| 81 |
else: |
| 82 |
return False |
| 83 |
def identity(x: Any, y: Any) -> Any: |
| 84 |
if x is y: |
| 85 |
return True |
| 86 |
else: |
| 87 |
return False |
| 88 |
def disidentity(x: Any, y: Any) -> Any: |
| 89 |
if x is not y: |
| 90 |
return True |
| 91 |
else: |
| 92 |
return False |
| 93 |
def not_eq_cond(a: Any, b: Any) -> bool: |
| 94 |
if not (a == b): |
| 95 |
return True |
| 96 |
else: |
| 97 |
return False |
| 98 |
def eq2(a: Any, b: Any) -> bool: |
| 99 |
return a == b |
| 100 |
def slice1(x: Any) -> Any: |
| 101 |
return x[:] |
| 102 |
def slice2(x: Any, y: Any) -> Any: |
| 103 |
return x[y:] |
| 104 |
def slice3(x: Any, y: Any) -> Any: |
| 105 |
return x[:y] |
| 106 |
def slice4(x: Any, y: Any, z: Any) -> Any: |
| 107 |
return x[y:z] |
| 108 |
def slice5(x: Any, y: Any, z: Any, zz: Any) -> Any: |
| 109 |
return x[y:z:zz] |
| 110 |
[file driver.py] |
| 111 |
from native import * |
| 112 |
assert add(5, 6) == 11 |
| 113 |
assert add('x', 'y') == 'xy' |
| 114 |
assert subtract(8, 3) == 5 |
| 115 |
assert multiply(8, 3) == 24 |
| 116 |
assert floor_div(8, 3) == 2 |
| 117 |
assert true_div(7, 2) == 3.5 |
| 118 |
assert remainder(11, 4) == 3 |
| 119 |
assert remainder('%.3d', 5) == '005' |
| 120 |
assert remainder('%d-%s', (5, 'xy')) == '5-xy' |
| 121 |
assert power(3, 4) == 81 |
| 122 |
assert lshift(5, 3) == 40 |
| 123 |
assert rshift(41, 3) == 5 |
| 124 |
assert num_and(99, 56) == 32 |
| 125 |
assert num_xor(99, 56) == 91 |
| 126 |
assert num_or(99, 56) == 123 |
| 127 |
assert lt('a', 'b') |
| 128 |
assert not lt('a', 'a') |
| 129 |
assert not lt('b', 'a') |
| 130 |
assert not gt('a', 'b') |
| 131 |
assert not gt('a', 'a') |
| 132 |
assert gt('b', 'a') |
| 133 |
assert le('a', 'b') |
| 134 |
assert le('a', 'a') |
| 135 |
assert not le('b', 'a') |
| 136 |
assert not ge('a', 'b') |
| 137 |
assert ge('a', 'a') |
| 138 |
assert ge('b', 'a') |
| 139 |
assert contains('x', 'axb') |
| 140 |
assert not contains('X', 'axb') |
| 141 |
assert contains('x', {'x', 'y'}) |
| 142 |
a = [1, 3, 5] |
| 143 |
assert slice1(a) == a |
| 144 |
assert slice1(a) is not a |
| 145 |
assert slice2(a, 1) == [3, 5] |
| 146 |
assert slice3(a, -1) == [1, 3] |
| 147 |
assert slice4(a, 1, -1) == [3] |
| 148 |
assert slice5(a, 2, 0, -1) == [5, 3] |
| 149 |
o1, o2 = object(), object() |
| 150 |
assert identity(o1, o1) |
| 151 |
assert not identity(o1, o2) |
| 152 |
assert not disidentity(o1, o1) |
| 153 |
assert disidentity(o1, o2) |
| 154 |
assert eq2('xz', 'x' + 'z') |
| 155 |
assert not eq2('x', 'y') |
| 156 |
assert not not_eq_cond('xz', 'x' + 'z') |
| 157 |
assert not_eq_cond('x', 'y') |
| 158 |
|
| 159 |
[case testGenericMiscOps] |
| 160 |
from typing import Any |
| 161 |
def neg(x: Any) -> Any: |
| 162 |
return -x |
| 163 |
def pos(x: Any) -> Any: |
| 164 |
return +x |
| 165 |
def invert(x: Any) -> Any: |
| 166 |
return ~x |
| 167 |
def get_item(o: Any, k: Any) -> Any: |
| 168 |
return o[k] |
| 169 |
def set_item(o: Any, k: Any, v: Any) -> Any: |
| 170 |
o[k] = v |
| 171 |
[file driver.py] |
| 172 |
from native import * |
| 173 |
assert neg(6) == -6 |
| 174 |
assert pos(6) == 6 |
| 175 |
assert invert(6) == -7 |
| 176 |
d = {'x': 5} |
| 177 |
assert get_item(d, 'x') == 5 |
| 178 |
set_item(d, 'y', 6) |
| 179 |
assert d['y'] == 6 |
| 180 |
|
| 181 |
[case testAnyAttributeAndMethodAccess] |
| 182 |
from typing import Any, List |
| 183 |
class C: |
| 184 |
a: int |
| 185 |
def m(self, x: int, a: List[int]) -> int: |
| 186 |
return self.a + x + a[0] |
| 187 |
def get_a(x: Any) -> Any: |
| 188 |
return x.a |
| 189 |
def set_a(x: Any, y: Any) -> None: |
| 190 |
x.a = y |
| 191 |
def call_m(x: Any) -> Any: |
| 192 |
return x.m(1, [3]) |
| 193 |
[file driver.py] |
| 194 |
from native import C, get_a, set_a, call_m |
| 195 |
class D: |
| 196 |
def m(self, x, a): |
| 197 |
return self.a + x + a[0] |
| 198 |
|
| 199 |
c = C() |
| 200 |
c.a = 6 |
| 201 |
d = D() |
| 202 |
d.a = 2 |
| 203 |
assert get_a(c) == 6 |
| 204 |
assert get_a(d) == 2 |
| 205 |
assert call_m(c) == 10 |
| 206 |
assert call_m(d) == 6 |
| 207 |
set_a(c, 5) |
| 208 |
assert c.a == 5 |
| 209 |
set_a(d, 4) |
| 210 |
assert d.a == 4 |
| 211 |
try: |
| 212 |
get_a(object()) |
| 213 |
except AttributeError: |
| 214 |
pass |
| 215 |
else: |
| 216 |
assert False |
| 217 |
try: |
| 218 |
call_m(object()) |
| 219 |
except AttributeError: |
| 220 |
pass |
| 221 |
else: |
| 222 |
assert False |
| 223 |
try: |
| 224 |
set_a(object(), 5) |
| 225 |
except AttributeError: |
| 226 |
pass |
| 227 |
else: |
| 228 |
assert False |
| 229 |
|
| 230 |
[case testFloat] |
| 231 |
def assign_and_return_float_sum() -> float: |
| 232 |
f1 = 1.0 |
| 233 |
f2 = 2.0 |
| 234 |
f3 = 3.0 |
| 235 |
return f1 * f2 + f3 |
| 236 |
|
| 237 |
def from_int(i: int) -> float: |
| 238 |
return float(i) |
| 239 |
|
| 240 |
def to_int(x: float) -> int: |
| 241 |
return int(x) |
| 242 |
|
| 243 |
def get_complex() -> complex: |
| 244 |
return 5.2j + 3.5 + 1j |
| 245 |
|
| 246 |
[file driver.py] |
| 247 |
from native import assign_and_return_float_sum, from_int, to_int, get_complex |
| 248 |
sum = 0.0 |
| 249 |
for i in range(10): |
| 250 |
sum += assign_and_return_float_sum() |
| 251 |
assert sum == 50.0 |
| 252 |
|
| 253 |
assert str(from_int(10)) == '10.0' |
| 254 |
assert str(to_int(3.14)) == '3' |
| 255 |
assert str(to_int(3)) == '3' |
| 256 |
assert get_complex() == 3.5 + 6.2j |
| 257 |
|
| 258 |
[case testDel] |
| 259 |
from typing import List |
| 260 |
from testutil import assertRaises |
| 261 |
|
| 262 |
def printDict(dict) -> None: |
| 263 |
l = list(dict.keys()) # type: List[str] |
| 264 |
l.sort() |
| 265 |
for key in l: |
| 266 |
print(key, dict[key]) |
| 267 |
print("#########") |
| 268 |
|
| 269 |
def delList() -> None: |
| 270 |
l = [1, 2, 3] |
| 271 |
print(tuple(l)) |
| 272 |
del l[1] |
| 273 |
print(tuple(l)) |
| 274 |
|
| 275 |
def delDict() -> None: |
| 276 |
d = {"one":1, "two":2} |
| 277 |
printDict(d) |
| 278 |
del d["one"] |
| 279 |
printDict(d) |
| 280 |
|
| 281 |
def delListMultiple() -> None: |
| 282 |
l = [1, 2, 3, 4, 5, 6, 7] |
| 283 |
print(tuple(l)) |
| 284 |
del l[1], l[2], l[3] |
| 285 |
print(tuple(l)) |
| 286 |
|
| 287 |
def delDictMultiple() -> None: |
| 288 |
d = {"one":1, "two":2, "three":3, "four":4} |
| 289 |
printDict(d) |
| 290 |
del d["two"], d["four"] |
| 291 |
printDict(d) |
| 292 |
|
| 293 |
class Dummy(): |
| 294 |
__deletable__ = ('x', 'y') |
| 295 |
|
| 296 |
def __init__(self, x: int, y: int) -> None: |
| 297 |
self.x = x |
| 298 |
self.y = y |
| 299 |
|
| 300 |
def delAttribute() -> None: |
| 301 |
dummy = Dummy(1, 2) |
| 302 |
del dummy.x |
| 303 |
with assertRaises(AttributeError): |
| 304 |
dummy.x |
| 305 |
|
| 306 |
def delAttributeMultiple() -> None: |
| 307 |
dummy = Dummy(1, 2) |
| 308 |
del dummy.x, dummy.y |
| 309 |
with assertRaises(AttributeError): |
| 310 |
dummy.x |
| 311 |
with assertRaises(AttributeError): |
| 312 |
dummy.y |
| 313 |
|
| 314 |
def delLocal(b: bool) -> int: |
| 315 |
dummy = 10 |
| 316 |
if b: |
| 317 |
del dummy |
| 318 |
return dummy |
| 319 |
|
| 320 |
def delLocalLoop() -> None: |
| 321 |
# Try deleting a local in a loop to make sure the control flow analysis works |
| 322 |
dummy = 1 |
| 323 |
for i in range(10): |
| 324 |
print(dummy) |
| 325 |
dummy *= 2 |
| 326 |
if i == 4: |
| 327 |
del dummy |
| 328 |
|
| 329 |
global_var = 10 |
| 330 |
del global_var |
| 331 |
|
| 332 |
[file driver.py] |
| 333 |
from native import ( |
| 334 |
delList, delDict, delListMultiple, delDictMultiple, delAttribute, |
| 335 |
delAttributeMultiple, delLocal, delLocalLoop, |
| 336 |
) |
| 337 |
import native |
| 338 |
from testutil import assertRaises |
| 339 |
|
| 340 |
delList() |
| 341 |
delDict() |
| 342 |
delListMultiple() |
| 343 |
delDictMultiple() |
| 344 |
delAttribute() |
| 345 |
delAttributeMultiple() |
| 346 |
with assertRaises(AttributeError): |
| 347 |
native.global_var |
| 348 |
with assertRaises(UnboundLocalError, 'local variable "dummy" referenced before assignment'): |
| 349 |
delLocal(True) |
| 350 |
assert delLocal(False) == 10 |
| 351 |
with assertRaises(UnboundLocalError, 'local variable "dummy" referenced before assignment'): |
| 352 |
delLocalLoop() |
| 353 |
[out] |
| 354 |
(1, 2, 3) |
| 355 |
(1, 3) |
| 356 |
one 1 |
| 357 |
two 2 |
| 358 |
######### |
| 359 |
two 2 |
| 360 |
######### |
| 361 |
(1, 2, 3, 4, 5, 6, 7) |
| 362 |
(1, 3, 5, 7) |
| 363 |
four 4 |
| 364 |
one 1 |
| 365 |
three 3 |
| 366 |
two 2 |
| 367 |
######### |
| 368 |
one 1 |
| 369 |
three 3 |
| 370 |
######### |
| 371 |
1 |
| 372 |
2 |
| 373 |
4 |
| 374 |
8 |
| 375 |
16 |