run-lists.test 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. # Test cases for lists (compile and run)
  2. [case testListPlusEquals]
  3. from typing import Any
  4. def append(x: Any) -> None:
  5. x += [1]
  6. [file driver.py]
  7. from native import append
  8. x = []
  9. append(x)
  10. assert x == [1]
  11. [case testListSum]
  12. from typing import List
  13. def sum(a: List[int], l: int) -> int:
  14. sum = 0
  15. i = 0
  16. while i < l:
  17. sum = sum + a[i]
  18. i = i + 1
  19. return sum
  20. [file driver.py]
  21. from native import sum
  22. print(sum([], 0))
  23. print(sum([3], 1))
  24. print(sum([5, 6, -4], 3))
  25. print(sum([2**128 + 5, -2**127 - 8], 2))
  26. [out]
  27. 0
  28. 3
  29. 7
  30. 170141183460469231731687303715884105725
  31. [case testListSet]
  32. from typing import List
  33. def copy(a: List[int], b: List[int], l: int) -> int:
  34. i = 0
  35. while i < l:
  36. a[i] = b[i]
  37. i = i + 1
  38. return 0
  39. [file driver.py]
  40. from native import copy
  41. a = [0, '']
  42. copy(a, [-1, 5], 2)
  43. print(1, a)
  44. copy(a, [2**128 + 5, -2**127 - 8], 2)
  45. print(2, a)
  46. [out]
  47. 1 [-1, 5]
  48. 2 [340282366920938463463374607431768211461, -170141183460469231731687303715884105736]
  49. [case testSieve]
  50. from typing import List
  51. def primes(n: int) -> List[int]:
  52. a = [1] * (n + 1)
  53. a[0] = 0
  54. a[1] = 0
  55. i = 0
  56. while i < n:
  57. if a[i] == 1:
  58. j = i * i
  59. while j < n:
  60. a[j] = 0
  61. j = j + i
  62. i = i + 1
  63. return a
  64. [file driver.py]
  65. from native import primes
  66. print(primes(3))
  67. print(primes(13))
  68. [out]
  69. \[0, 0, 1, 1]
  70. \[0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1]
  71. [case testListBuild]
  72. def test_list_build() -> None:
  73. # Currently LIST_BUILDING_EXPANSION_THRESHOLD equals to 10
  74. # long list built by list_build_op
  75. l1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
  76. l1.pop()
  77. l1.append(100)
  78. assert l1 == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100]
  79. # short list built by Setmem
  80. l2 = [1, 2]
  81. l2.append(3)
  82. l2.pop()
  83. l2.pop()
  84. assert l2 == [1]
  85. # empty list
  86. l3 = []
  87. l3.append('a')
  88. assert l3 == ['a']
  89. [case testListPrims]
  90. from typing import List
  91. def test_append() -> None:
  92. l = [1, 2]
  93. l.append(10)
  94. assert l == [1, 2, 10]
  95. l.append(3)
  96. l.append(4)
  97. l.append(5)
  98. assert l == [1, 2, 10, 3, 4, 5]
  99. def test_pop_last() -> None:
  100. l = [1, 2, 10, 3, 4, 5]
  101. l.pop()
  102. l.pop()
  103. assert l == [1, 2, 10, 3]
  104. def test_pop_index() -> None:
  105. l = [1, 2, 10, 3]
  106. l.pop(2)
  107. assert l == [1, 2, 3]
  108. l.pop(-2)
  109. assert l == [1, 3]
  110. def test_count() -> None:
  111. l = [1, 3]
  112. assert l.count(1) == 1
  113. assert l.count(2) == 0
  114. def test_insert() -> None:
  115. l = [1, 3]
  116. l.insert(0, 0)
  117. assert l == [0, 1, 3]
  118. l.insert(2, 2)
  119. assert l == [0, 1, 2, 3]
  120. l.insert(4, 4)
  121. assert l == [0, 1, 2, 3, 4]
  122. l.insert(-1, 5)
  123. assert l == [0, 1, 2, 3, 5, 4]
  124. l = [1, 3]
  125. l.insert(100, 5)
  126. assert l == [1, 3, 5]
  127. l.insert(-100, 6)
  128. assert l == [6, 1, 3, 5]
  129. for long_int in 1 << 100, -(1 << 100):
  130. try:
  131. l.insert(long_int, 5)
  132. except Exception as e:
  133. # The error message is used by CPython
  134. assert type(e).__name__ == 'OverflowError'
  135. assert str(e) == 'Python int too large to convert to C ssize_t'
  136. else:
  137. assert False
  138. def test_sort() -> None:
  139. l = [1, 4, 3, 6, -1]
  140. l.sort()
  141. assert l == [-1, 1, 3, 4, 6]
  142. l.sort()
  143. assert l == [-1, 1, 3, 4, 6]
  144. l = []
  145. l.sort()
  146. assert l == []
  147. def test_reverse() -> None:
  148. l = [1, 4, 3, 6, -1]
  149. l.reverse()
  150. assert l == [-1, 6, 3, 4, 1]
  151. l.reverse()
  152. assert l == [1, 4, 3, 6, -1]
  153. l = []
  154. l.reverse()
  155. assert l == []
  156. def test_remove() -> None:
  157. l = [1, 3, 4, 3]
  158. l.remove(3)
  159. assert l == [1, 4, 3]
  160. l.remove(3)
  161. assert l == [1, 4]
  162. try:
  163. l.remove(3)
  164. except ValueError:
  165. pass
  166. else:
  167. assert False
  168. def test_index() -> None:
  169. l = [1, 3, 4, 3]
  170. assert l.index(1) == 0
  171. assert l.index(3) == 1
  172. assert l.index(4) == 2
  173. try:
  174. l.index(0)
  175. except ValueError:
  176. pass
  177. else:
  178. assert False
  179. [case testListOfUserDefinedClass]
  180. class C:
  181. x: int
  182. def f() -> int:
  183. c = C()
  184. c.x = 5
  185. a = [c]
  186. d = a[0]
  187. return d.x + 1
  188. def g() -> int:
  189. a = [C()]
  190. a[0].x = 3
  191. return a[0].x + 4
  192. [file driver.py]
  193. from native import f, g
  194. print(f())
  195. print(g())
  196. [out]
  197. 6
  198. 7
  199. [case testListOps]
  200. def test_slicing() -> None:
  201. # Use dummy adds to avoid constant folding
  202. zero = int()
  203. two = zero + 2
  204. s = ["f", "o", "o", "b", "a", "r"]
  205. assert s[two:] == ["o", "b", "a", "r"]
  206. assert s[:two] == ["f", "o"]
  207. assert s[two:-two] == ["o", "b"]
  208. assert s[two:two] == []
  209. assert s[two:two + 1] == ["o"]
  210. assert s[-two:] == ["a", "r"]
  211. assert s[:-two] == ["f", "o", "o", "b"]
  212. assert s[:] == ["f", "o", "o", "b", "a", "r"]
  213. assert s[two:333] == ["o", "b", "a", "r"]
  214. assert s[333:two] == []
  215. assert s[two:-333] == []
  216. assert s[-333:two] == ["f", "o"]
  217. long_int: int = 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000
  218. assert s[1:long_int] == ["o", "o", "b", "a", "r"]
  219. assert s[long_int:] == []
  220. assert s[-long_int:-1] == ["f", "o", "o", "b", "a"]
  221. [case testOperatorInExpression]
  222. def tuple_in_int0(i: int) -> bool:
  223. return i in []
  224. def tuple_in_int1(i: int) -> bool:
  225. return i in (1,)
  226. def tuple_in_int3(i: int) -> bool:
  227. return i in (1, 2, 3)
  228. def tuple_not_in_int0(i: int) -> bool:
  229. return i not in []
  230. def tuple_not_in_int1(i: int) -> bool:
  231. return i not in (1,)
  232. def tuple_not_in_int3(i: int) -> bool:
  233. return i not in (1, 2, 3)
  234. def tuple_in_str(s: "str") -> bool:
  235. return s in ("foo", "bar", "baz")
  236. def tuple_not_in_str(s: "str") -> bool:
  237. return s not in ("foo", "bar", "baz")
  238. def list_in_int0(i: int) -> bool:
  239. return i in []
  240. def list_in_int1(i: int) -> bool:
  241. return i in (1,)
  242. def list_in_int3(i: int) -> bool:
  243. return i in (1, 2, 3)
  244. def list_not_in_int0(i: int) -> bool:
  245. return i not in []
  246. def list_not_in_int1(i: int) -> bool:
  247. return i not in (1,)
  248. def list_not_in_int3(i: int) -> bool:
  249. return i not in (1, 2, 3)
  250. def list_in_str(s: "str") -> bool:
  251. return s in ("foo", "bar", "baz")
  252. def list_not_in_str(s: "str") -> bool:
  253. return s not in ("foo", "bar", "baz")
  254. def list_in_mixed(i: object):
  255. return i in [[], (), "", 0, 0.0, False, 0j, {}, set(), type]
  256. [file driver.py]
  257. from native import *
  258. assert not tuple_in_int0(0)
  259. assert not tuple_in_int1(0)
  260. assert tuple_in_int1(1)
  261. assert not tuple_in_int3(0)
  262. assert tuple_in_int3(1)
  263. assert tuple_in_int3(2)
  264. assert tuple_in_int3(3)
  265. assert not tuple_in_int3(4)
  266. assert tuple_not_in_int0(0)
  267. assert tuple_not_in_int1(0)
  268. assert not tuple_not_in_int1(1)
  269. assert tuple_not_in_int3(0)
  270. assert not tuple_not_in_int3(1)
  271. assert not tuple_not_in_int3(2)
  272. assert not tuple_not_in_int3(3)
  273. assert tuple_not_in_int3(4)
  274. assert tuple_in_str("foo")
  275. assert tuple_in_str("bar")
  276. assert tuple_in_str("baz")
  277. assert not tuple_in_str("apple")
  278. assert not tuple_in_str("pie")
  279. assert not tuple_in_str("\0")
  280. assert not tuple_in_str("")
  281. assert not list_in_int0(0)
  282. assert not list_in_int1(0)
  283. assert list_in_int1(1)
  284. assert not list_in_int3(0)
  285. assert list_in_int3(1)
  286. assert list_in_int3(2)
  287. assert list_in_int3(3)
  288. assert not list_in_int3(4)
  289. assert list_not_in_int0(0)
  290. assert list_not_in_int1(0)
  291. assert not list_not_in_int1(1)
  292. assert list_not_in_int3(0)
  293. assert not list_not_in_int3(1)
  294. assert not list_not_in_int3(2)
  295. assert not list_not_in_int3(3)
  296. assert list_not_in_int3(4)
  297. assert list_in_str("foo")
  298. assert list_in_str("bar")
  299. assert list_in_str("baz")
  300. assert not list_in_str("apple")
  301. assert not list_in_str("pie")
  302. assert not list_in_str("\0")
  303. assert not list_in_str("")
  304. assert list_in_mixed(0)
  305. assert list_in_mixed([])
  306. assert list_in_mixed({})
  307. assert list_in_mixed(())
  308. assert list_in_mixed(False)
  309. assert list_in_mixed(0.0)
  310. assert not list_in_mixed([1])
  311. assert not list_in_mixed(object)
  312. assert list_in_mixed(type)
  313. [case testListBuiltFromGenerator]
  314. def test() -> None:
  315. source_a = ["a", "b", "c"]
  316. a = list(x + "f2" for x in source_a)
  317. assert a == ["af2", "bf2", "cf2"]
  318. source_b = [1, 2, 3, 4, 5]
  319. b = [x * 2 for x in source_b]
  320. assert b == [2, 4, 6, 8, 10]
  321. source_c = [10, 20, 30]
  322. c = [x + "f4" for x in (str(y) + "yy" for y in source_c)]
  323. assert c == ["10yyf4", "20yyf4", "30yyf4"]
  324. source_d = [True, False]
  325. d = [not x for x in source_d]
  326. assert d == [False, True]
  327. source_e = [0, 1, 2]
  328. e = list((x ** 2) for x in (y + 2 for y in source_e))
  329. assert e == [4, 9, 16]
  330. source_str = "abcd"
  331. f = list("str:" + x for x in source_str)
  332. assert f == ["str:a", "str:b", "str:c", "str:d"]
  333. [case testNextBug]
  334. from typing import List, Optional
  335. def test(x: List[int]) -> None:
  336. res = next((i for i in x), None)
  337. [case testListGetItemWithBorrow]
  338. from typing import List
  339. class D:
  340. def __init__(self, n: int) -> None:
  341. self.n = n
  342. class C:
  343. def __init__(self, d: D) -> None:
  344. self.d = d
  345. def test_index_with_literal() -> None:
  346. d1 = D(1)
  347. d2 = D(2)
  348. a = [C(d1), C(d2)]
  349. d = a[0].d
  350. assert d is d1
  351. d = a[1].d
  352. assert d is d2
  353. d = a[-1].d
  354. assert d is d2
  355. d = a[-2].d
  356. assert d is d1