mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 03:04:38 +08:00
Update test_iter* from CPython 3.10.6
This commit is contained in:
1 parent
7d163b9745
commit
f3a8fdbb4a
2 files changed
+57
-73
No files matched your search
Vendored
+57
-31
@@ -2,9 +2,9 @@
|
||||
|
||||
import sys
|
||||
import unittest
|
||||
from test.support import run_unittest, cpython_only
|
||||
# from test.support import check_free_after_iterating
|
||||
from test.support import cpython_only
|
||||
from test.support.os_helper import TESTFN, unlink
|
||||
from test.support import check_free_after_iterating, ALWAYS_EQ, NEVER_EQ
|
||||
import pickle
|
||||
import collections.abc
|
||||
|
||||
@@ -42,6 +42,14 @@ class IteratingSequenceClass:
|
||||
def __iter__(self):
|
||||
return BasicIterClass(self.n)
|
||||
|
||||
class IteratorProxyClass:
|
||||
def __init__(self, i):
|
||||
self.i = i
|
||||
def __next__(self):
|
||||
return next(self.i)
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
class SequenceClass:
|
||||
def __init__(self, n):
|
||||
self.n = n
|
||||
@@ -51,6 +59,12 @@ class SequenceClass:
|
||||
else:
|
||||
raise IndexError
|
||||
|
||||
class SequenceProxyClass:
|
||||
def __init__(self, s):
|
||||
self.s = s
|
||||
def __getitem__(self, i):
|
||||
return self.s[i]
|
||||
|
||||
class UnlimitedSequenceClass:
|
||||
def __getitem__(self, i):
|
||||
return i
|
||||
@@ -63,6 +77,10 @@ class NoIterClass:
|
||||
return i
|
||||
__iter__ = None
|
||||
|
||||
class BadIterableClass:
|
||||
def __iter__(self):
|
||||
raise ZeroDivisionError
|
||||
|
||||
# Main test suite
|
||||
|
||||
class TestCase(unittest.TestCase):
|
||||
@@ -294,6 +312,7 @@ class TestCase(unittest.TestCase):
|
||||
def test_iter_big_range(self):
|
||||
self.check_for_loop(iter(range(10000)), list(range(10000)))
|
||||
|
||||
# Test an empty list
|
||||
def test_iter_empty(self):
|
||||
self.check_for_loop(iter([]), [])
|
||||
|
||||
@@ -318,13 +337,13 @@ class TestCase(unittest.TestCase):
|
||||
|
||||
# Test a file
|
||||
def test_iter_file(self):
|
||||
f = open(TESTFN, "w")
|
||||
f = open(TESTFN, "w", encoding="utf-8")
|
||||
try:
|
||||
for i in range(5):
|
||||
f.write("%d\n" % i)
|
||||
finally:
|
||||
f.close()
|
||||
f = open(TESTFN, "r")
|
||||
f = open(TESTFN, "r", encoding="utf-8")
|
||||
try:
|
||||
self.check_for_loop(f, ["0\n", "1\n", "2\n", "3\n", "4\n"], pickle=False)
|
||||
self.check_for_loop(f, [], pickle=False)
|
||||
@@ -347,13 +366,13 @@ class TestCase(unittest.TestCase):
|
||||
self.assertRaises(TypeError, list, list)
|
||||
self.assertRaises(TypeError, list, 42)
|
||||
|
||||
f = open(TESTFN, "w")
|
||||
f = open(TESTFN, "w", encoding="utf-8")
|
||||
try:
|
||||
for i in range(5):
|
||||
f.write("%d\n" % i)
|
||||
finally:
|
||||
f.close()
|
||||
f = open(TESTFN, "r")
|
||||
f = open(TESTFN, "r", encoding="utf-8")
|
||||
try:
|
||||
self.assertEqual(list(f), ["0\n", "1\n", "2\n", "3\n", "4\n"])
|
||||
f.seek(0, 0)
|
||||
@@ -380,13 +399,13 @@ class TestCase(unittest.TestCase):
|
||||
self.assertRaises(TypeError, tuple, list)
|
||||
self.assertRaises(TypeError, tuple, 42)
|
||||
|
||||
f = open(TESTFN, "w")
|
||||
f = open(TESTFN, "w", encoding="utf-8")
|
||||
try:
|
||||
for i in range(5):
|
||||
f.write("%d\n" % i)
|
||||
finally:
|
||||
f.close()
|
||||
f = open(TESTFN, "r")
|
||||
f = open(TESTFN, "r", encoding="utf-8")
|
||||
try:
|
||||
self.assertEqual(tuple(f), ("0\n", "1\n", "2\n", "3\n", "4\n"))
|
||||
f.seek(0, 0)
|
||||
@@ -457,14 +476,14 @@ class TestCase(unittest.TestCase):
|
||||
self.assertEqual(max(d.values()), 3)
|
||||
self.assertEqual(min(iter(d.values())), 1)
|
||||
|
||||
f = open(TESTFN, "w")
|
||||
f = open(TESTFN, "w", encoding="utf-8")
|
||||
try:
|
||||
f.write("medium line\n")
|
||||
f.write("xtra large line\n")
|
||||
f.write("itty-bitty line\n")
|
||||
finally:
|
||||
f.close()
|
||||
f = open(TESTFN, "r")
|
||||
f = open(TESTFN, "r", encoding="utf-8")
|
||||
try:
|
||||
self.assertEqual(min(f), "itty-bitty line\n")
|
||||
f.seek(0, 0)
|
||||
@@ -490,13 +509,13 @@ class TestCase(unittest.TestCase):
|
||||
i < len(d) and dkeys[i] or None)
|
||||
for i in range(3)]
|
||||
|
||||
f = open(TESTFN, "w")
|
||||
f = open(TESTFN, "w", encoding="utf-8")
|
||||
try:
|
||||
for i in range(10):
|
||||
f.write("xy" * i + "\n") # line i has len 2*i+1
|
||||
finally:
|
||||
f.close()
|
||||
f = open(TESTFN, "r")
|
||||
f = open(TESTFN, "r", encoding="utf-8")
|
||||
try:
|
||||
self.assertEqual(list(map(len, f)), list(range(1, 21, 2)))
|
||||
finally:
|
||||
@@ -537,12 +556,12 @@ class TestCase(unittest.TestCase):
|
||||
self.i = i+1
|
||||
return i
|
||||
|
||||
f = open(TESTFN, "w")
|
||||
f = open(TESTFN, "w", encoding="utf-8")
|
||||
try:
|
||||
f.write("a\n" "bbb\n" "cc\n")
|
||||
finally:
|
||||
f.close()
|
||||
f = open(TESTFN, "r")
|
||||
f = open(TESTFN, "r", encoding="utf-8")
|
||||
try:
|
||||
self.assertEqual(list(zip(IntsFrom(0), f, IntsFrom(-100))),
|
||||
[(0, "a\n", -100),
|
||||
@@ -605,13 +624,13 @@ class TestCase(unittest.TestCase):
|
||||
return "fooled you!"
|
||||
return next(self.it)
|
||||
|
||||
f = open(TESTFN, "w")
|
||||
f = open(TESTFN, "w", encoding="utf-8")
|
||||
try:
|
||||
f.write("a\n" + "b\n" + "c\n")
|
||||
finally:
|
||||
f.close()
|
||||
|
||||
f = open(TESTFN, "r")
|
||||
f = open(TESTFN, "r", encoding="utf-8")
|
||||
# Nasty: string.join(s) can't know whether unicode.join() is needed
|
||||
# until it's seen all of s's elements. But in this case, f's
|
||||
# iterator cannot be restarted. So what we're testing here is
|
||||
@@ -635,8 +654,16 @@ class TestCase(unittest.TestCase):
|
||||
for i in "abc", -1, 5, 42.42, (3, 4), [], {1: 1}, 3-12j, sc5:
|
||||
self.assertNotIn(i, sc5)
|
||||
|
||||
self.assertIn(ALWAYS_EQ, IteratorProxyClass(iter([1])))
|
||||
self.assertIn(ALWAYS_EQ, SequenceProxyClass([1]))
|
||||
self.assertNotIn(ALWAYS_EQ, IteratorProxyClass(iter([NEVER_EQ])))
|
||||
self.assertNotIn(ALWAYS_EQ, SequenceProxyClass([NEVER_EQ]))
|
||||
self.assertIn(NEVER_EQ, IteratorProxyClass(iter([ALWAYS_EQ])))
|
||||
self.assertIn(NEVER_EQ, SequenceProxyClass([ALWAYS_EQ]))
|
||||
|
||||
self.assertRaises(TypeError, lambda: 3 in 12)
|
||||
self.assertRaises(TypeError, lambda: 3 not in map)
|
||||
self.assertRaises(ZeroDivisionError, lambda: 3 in BadIterableClass())
|
||||
|
||||
d = {"one": 1, "two": 2, "three": 3, 1j: 2j}
|
||||
for k in d:
|
||||
@@ -649,12 +676,12 @@ class TestCase(unittest.TestCase):
|
||||
self.assertIn((k, v), d.items())
|
||||
self.assertNotIn((v, k), d.items())
|
||||
|
||||
f = open(TESTFN, "w")
|
||||
f = open(TESTFN, "w", encoding="utf-8")
|
||||
try:
|
||||
f.write("a\n" "b\n" "c\n")
|
||||
finally:
|
||||
f.close()
|
||||
f = open(TESTFN, "r")
|
||||
f = open(TESTFN, "r", encoding="utf-8")
|
||||
try:
|
||||
for chunk in "abc":
|
||||
f.seek(0, 0)
|
||||
@@ -686,12 +713,12 @@ class TestCase(unittest.TestCase):
|
||||
self.assertEqual(countOf(d.values(), 2j), 1)
|
||||
self.assertEqual(countOf(d.values(), 1j), 0)
|
||||
|
||||
f = open(TESTFN, "w")
|
||||
f = open(TESTFN, "w", encoding="utf-8")
|
||||
try:
|
||||
f.write("a\n" "b\n" "c\n" "b\n")
|
||||
finally:
|
||||
f.close()
|
||||
f = open(TESTFN, "r")
|
||||
f = open(TESTFN, "r", encoding="utf-8")
|
||||
try:
|
||||
for letter, count in ("a", 1), ("b", 2), ("c", 1), ("d", 0):
|
||||
f.seek(0, 0)
|
||||
@@ -719,13 +746,14 @@ class TestCase(unittest.TestCase):
|
||||
|
||||
self.assertRaises(TypeError, indexOf, 42, 1)
|
||||
self.assertRaises(TypeError, indexOf, indexOf, indexOf)
|
||||
self.assertRaises(ZeroDivisionError, indexOf, BadIterableClass(), 1)
|
||||
|
||||
f = open(TESTFN, "w")
|
||||
f = open(TESTFN, "w", encoding="utf-8")
|
||||
try:
|
||||
f.write("a\n" "b\n" "c\n" "d\n" "e\n")
|
||||
finally:
|
||||
f.close()
|
||||
f = open(TESTFN, "r")
|
||||
f = open(TESTFN, "r", encoding="utf-8")
|
||||
try:
|
||||
fiter = iter(f)
|
||||
self.assertEqual(indexOf(fiter, "b\n"), 1)
|
||||
@@ -744,8 +772,9 @@ class TestCase(unittest.TestCase):
|
||||
self.assertEqual(indexOf(iclass, i), i)
|
||||
self.assertRaises(ValueError, indexOf, iclass, -1)
|
||||
|
||||
# Test iterators with file.writelines().
|
||||
def test_writelines(self):
|
||||
f = open(TESTFN, "w")
|
||||
f = open(TESTFN, "w", encoding="utf-8")
|
||||
|
||||
try:
|
||||
self.assertRaises(TypeError, f.writelines, None)
|
||||
@@ -784,7 +813,7 @@ class TestCase(unittest.TestCase):
|
||||
f.writelines(Whatever(6, 6+2000))
|
||||
f.close()
|
||||
|
||||
f = open(TESTFN)
|
||||
f = open(TESTFN, encoding="utf-8")
|
||||
expected = [str(i) + "\n" for i in range(1, 2006)]
|
||||
self.assertEqual(list(f), expected)
|
||||
|
||||
@@ -828,14 +857,14 @@ class TestCase(unittest.TestCase):
|
||||
a, b, c = {1: 42, 2: 42, 3: 42}.values()
|
||||
self.assertEqual((a, b, c), (42, 42, 42))
|
||||
|
||||
f = open(TESTFN, "w")
|
||||
f = open(TESTFN, "w", encoding="utf-8")
|
||||
lines = ("a\n", "bb\n", "ccc\n")
|
||||
try:
|
||||
for line in lines:
|
||||
f.write(line)
|
||||
finally:
|
||||
f.close()
|
||||
f = open(TESTFN, "r")
|
||||
f = open(TESTFN, "r", encoding="utf-8")
|
||||
try:
|
||||
a, b, c = f
|
||||
self.assertEqual((a, b, c), lines)
|
||||
@@ -1007,11 +1036,8 @@ class TestCase(unittest.TestCase):
|
||||
def test_error_iter(self):
|
||||
for typ in (DefaultIterClass, NoIterClass):
|
||||
self.assertRaises(TypeError, iter, typ())
|
||||
|
||||
|
||||
def test_main():
|
||||
run_unittest(TestCase)
|
||||
self.assertRaises(ZeroDivisionError, iter, BadIterableClass())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_main()
|
||||
unittest.main()
|
||||
Vendored
-42
@@ -86,25 +86,16 @@ class TestXrange(TestInvariantWithoutMutations, unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.it = iter(range(n))
|
||||
|
||||
def test_invariant(self):
|
||||
super().test_invariant()
|
||||
|
||||
class TestXrangeCustomReversed(TestInvariantWithoutMutations, unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.it = reversed(range(n))
|
||||
|
||||
def test_invariant(self):
|
||||
super().test_invariant()
|
||||
|
||||
class TestTuple(TestInvariantWithoutMutations, unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.it = iter(tuple(range(n)))
|
||||
|
||||
def test_invariant(self):
|
||||
super().test_invariant()
|
||||
|
||||
## ------- Types that should not be mutated during iteration -------
|
||||
|
||||
class TestDeque(TestTemporarilyImmutable, unittest.TestCase):
|
||||
@@ -114,12 +105,6 @@ class TestDeque(TestTemporarilyImmutable, unittest.TestCase):
|
||||
self.it = iter(d)
|
||||
self.mutate = d.pop
|
||||
|
||||
def test_immutable_during_iteration(self):
|
||||
super().test_immutable_during_iteration()
|
||||
|
||||
def test_invariant(self):
|
||||
super().test_invariant()
|
||||
|
||||
class TestDequeReversed(TestTemporarilyImmutable, unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
@@ -127,12 +112,6 @@ class TestDequeReversed(TestTemporarilyImmutable, unittest.TestCase):
|
||||
self.it = reversed(d)
|
||||
self.mutate = d.pop
|
||||
|
||||
def test_immutable_during_iteration(self):
|
||||
super().test_immutable_during_iteration()
|
||||
|
||||
def test_invariant(self):
|
||||
super().test_invariant()
|
||||
|
||||
class TestDictKeys(TestTemporarilyImmutable, unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
@@ -140,9 +119,6 @@ class TestDictKeys(TestTemporarilyImmutable, unittest.TestCase):
|
||||
self.it = iter(d)
|
||||
self.mutate = d.popitem
|
||||
|
||||
def test_immutable_during_iteration(self):
|
||||
super().test_immutable_during_iteration()
|
||||
|
||||
class TestDictItems(TestTemporarilyImmutable, unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
@@ -150,9 +126,6 @@ class TestDictItems(TestTemporarilyImmutable, unittest.TestCase):
|
||||
self.it = iter(d.items())
|
||||
self.mutate = d.popitem
|
||||
|
||||
def test_immutable_during_iteration(self):
|
||||
super().test_immutable_during_iteration()
|
||||
|
||||
class TestDictValues(TestTemporarilyImmutable, unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
@@ -160,9 +133,6 @@ class TestDictValues(TestTemporarilyImmutable, unittest.TestCase):
|
||||
self.it = iter(d.values())
|
||||
self.mutate = d.popitem
|
||||
|
||||
def test_immutable_during_iteration(self):
|
||||
super().test_immutable_during_iteration()
|
||||
|
||||
class TestSet(TestTemporarilyImmutable, unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
@@ -170,12 +140,6 @@ class TestSet(TestTemporarilyImmutable, unittest.TestCase):
|
||||
self.it = iter(d)
|
||||
self.mutate = d.pop
|
||||
|
||||
def test_immutable_during_iteration(self):
|
||||
super().test_immutable_during_iteration()
|
||||
|
||||
def test_invariant(self):
|
||||
super().test_invariant()
|
||||
|
||||
## ------- Types that can mutate during iteration -------
|
||||
|
||||
class TestList(TestInvariantWithoutMutations, unittest.TestCase):
|
||||
@@ -183,9 +147,6 @@ class TestList(TestInvariantWithoutMutations, unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.it = iter(range(n))
|
||||
|
||||
def test_invariant(self):
|
||||
super().test_invariant()
|
||||
|
||||
def test_mutation(self):
|
||||
d = list(range(n))
|
||||
it = iter(d)
|
||||
@@ -206,9 +167,6 @@ class TestListReversed(TestInvariantWithoutMutations, unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.it = reversed(range(n))
|
||||
|
||||
def test_invariant(self):
|
||||
super().test_invariant()
|
||||
|
||||
def test_mutation(self):
|
||||
d = list(range(n))
|
||||
it = reversed(d)
|
||||
|
||||
Reference in new issue
Block a user