4 from test_support
import run_unittest
, TESTFN
, unlink
6 # Test result of triple loop (too big to inline)
7 TRIPLETS
= [(0, 0, 0), (0, 0, 1), (0, 0, 2),
8 (0, 1, 0), (0, 1, 1), (0, 1, 2),
9 (0, 2, 0), (0, 2, 1), (0, 2, 2),
11 (1, 0, 0), (1, 0, 1), (1, 0, 2),
12 (1, 1, 0), (1, 1, 1), (1, 1, 2),
13 (1, 2, 0), (1, 2, 1), (1, 2, 2),
15 (2, 0, 0), (2, 0, 1), (2, 0, 2),
16 (2, 1, 0), (2, 1, 1), (2, 1, 2),
17 (2, 2, 0), (2, 2, 1), (2, 2, 2)]
22 def __init__(self
, n
):
32 class IteratingSequenceClass
:
33 def __init__(self
, n
):
36 return BasicIterClass(self
.n
)
39 def __init__(self
, n
):
41 def __getitem__(self
, i
):
49 class TestCase(unittest
.TestCase
):
51 # Helper to check that an iterator returns a given sequence
52 def check_iterator(self
, it
, seq
):
60 self
.assertEqual(res
, seq
)
62 # Helper to check that a for loop generates a given sequence
63 def check_for_loop(self
, expr
, seq
):
67 self
.assertEqual(res
, seq
)
69 # Test basic use of iter() function
70 def test_iter_basic(self
):
71 self
.check_iterator(iter(range(10)), range(10))
73 # Test that iter(iter(x)) is the same as iter(x)
74 def test_iter_idempotency(self
):
78 self
.assert_(it
is it2
)
80 # Test that for loops over iterators work
81 def test_iter_for_loop(self
):
82 self
.check_for_loop(iter(range(10)), range(10))
84 # Test several independent iterators over the same list
85 def test_iter_independence(self
):
92 self
.assertEqual(res
, TRIPLETS
)
94 # Test triple list comprehension using iterators
95 def test_nested_comprehensions_iter(self
):
98 for i
in iter(seq
) for j
in iter(seq
) for k
in iter(seq
)]
99 self
.assertEqual(res
, TRIPLETS
)
101 # Test triple list comprehension without iterators
102 def test_nested_comprehensions_for(self
):
104 res
= [(i
, j
, k
) for i
in seq
for j
in seq
for k
in seq
]
105 self
.assertEqual(res
, TRIPLETS
)
107 # Test a class with __iter__ in a for loop
108 def test_iter_class_for(self
):
109 self
.check_for_loop(IteratingSequenceClass(10), range(10))
111 # Test a class with __iter__ with explicit iter()
112 def test_iter_class_iter(self
):
113 self
.check_iterator(iter(IteratingSequenceClass(10)), range(10))
115 # Test for loop on a sequence class without __iter__
116 def test_seq_class_for(self
):
117 self
.check_for_loop(SequenceClass(10), range(10))
119 # Test iter() on a sequence class without __iter__
120 def test_seq_class_iter(self
):
121 self
.check_iterator(iter(SequenceClass(10)), range(10))
123 # Test two-argument iter() with callable instance
124 def test_iter_callable(self
):
132 raise IndexError # Emergency stop
134 self
.check_iterator(iter(C(), 10), range(10))
136 # Test two-argument iter() with function
137 def test_iter_function(self
):
142 self
.check_iterator(iter(spam
, 10), range(10))
144 # Test two-argument iter() with function that raises StopIteration
145 def test_iter_function_stop(self
):
152 self
.check_iterator(iter(spam
, 20), range(10))
154 # Test exception propagation through function iterator
155 def test_exception_function(self
):
164 for x
in iter(spam
, 20):
167 self
.assertEqual(res
, range(10))
169 self
.fail("should have raised RuntimeError")
171 # Test exception propagation through sequence iterator
172 def test_exception_sequence(self
):
173 class MySequenceClass(SequenceClass
):
174 def __getitem__(self
, i
):
177 return SequenceClass
.__getitem
__(self
, i
)
180 for x
in MySequenceClass(20):
183 self
.assertEqual(res
, range(10))
185 self
.fail("should have raised RuntimeError")
187 # Test for StopIteration from __getitem__
188 def test_stop_sequence(self
):
189 class MySequenceClass(SequenceClass
):
190 def __getitem__(self
, i
):
193 return SequenceClass
.__getitem
__(self
, i
)
194 self
.check_for_loop(MySequenceClass(20), range(10))
197 def test_iter_big_range(self
):
198 self
.check_for_loop(iter(range(10000)), range(10000))
201 def test_iter_empty(self
):
202 self
.check_for_loop(iter([]), [])
205 def test_iter_tuple(self
):
206 self
.check_for_loop(iter((0,1,2,3,4,5,6,7,8,9)), range(10))
209 def test_iter_xrange(self
):
210 self
.check_for_loop(iter(xrange(10)), range(10))
213 def test_iter_string(self
):
214 self
.check_for_loop(iter("abcde"), ["a", "b", "c", "d", "e"])
216 # Test a Unicode string
217 def test_iter_unicode(self
):
218 self
.check_for_loop(iter(u
"abcde"), [u
"a", u
"b", u
"c", u
"d", u
"e"])
221 def test_iter_dict(self
):
225 self
.check_for_loop(dict, dict.keys())
228 def test_iter_file(self
):
229 f
= open(TESTFN
, "w")
235 f
= open(TESTFN
, "r")
237 self
.check_for_loop(f
, ["0\n", "1\n", "2\n", "3\n", "4\n"])
238 self
.check_for_loop(f
, [])
246 run_unittest(TestCase
)