Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_iter.TestCase Class Reference

List of all members.

Public Member Functions

def check_iterator
def check_for_loop
def test_iter_basic
def test_iter_idempotency
def test_iter_for_loop
def test_iter_independence
def test_nested_comprehensions_iter
def test_nested_comprehensions_for
def test_iter_class_for
def test_iter_class_iter
def test_seq_class_for
def test_seq_class_iter
def test_new_style_iter_class
def test_iter_callable
def test_iter_function
def test_iter_function_stop
def test_exception_function
def test_exception_sequence
def test_stop_sequence
def test_iter_big_range
def test_iter_empty
def test_iter_tuple
def test_iter_range
def test_iter_string
def test_iter_dict
def test_iter_file
def test_builtin_list
def test_builtin_tuple
def test_builtin_filter
def test_builtin_max_min
def test_builtin_map
def test_builtin_zip
def test_unicode_join_endcase
def test_in_and_not_in
def test_countOf
def test_indexOf
def test_writelines
def test_unpack_iter
def test_ref_counting_behavior
def test_sinkstate_list
def test_sinkstate_tuple
def test_sinkstate_string
def test_sinkstate_sequence
def test_sinkstate_callable
def test_sinkstate_dict
def test_sinkstate_yield
def test_sinkstate_range
def test_sinkstate_enumerate
def test_3720

Public Attributes

 i
 truth
 vals
 it
 start
 finish

Detailed Description

Definition at line 49 of file test_iter.py.


Member Function Documentation

def test.test_iter.TestCase.check_for_loop (   self,
  expr,
  seq 
)

Definition at line 63 of file test_iter.py.

00063 
00064     def check_for_loop(self, expr, seq):
00065         res = []
00066         for val in expr:
00067             res.append(val)
00068         self.assertEqual(res, seq)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_iter.TestCase.check_iterator (   self,
  it,
  seq 
)

Definition at line 52 of file test_iter.py.

00052 
00053     def check_iterator(self, it, seq):
00054         res = []
00055         while 1:
00056             try:
00057                 val = next(it)
00058             except StopIteration:
00059                 break
00060             res.append(val)
00061         self.assertEqual(res, seq)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 864 of file test_iter.py.

00864 
00865     def test_3720(self):
00866         # Avoid a crash, when an iterator deletes its next() method.
00867         class BadIterator(object):
00868             def __iter__(self):
00869                 return self
00870             def __next__(self):
00871                 del BadIterator.__next__
00872                 return 1
00873 
00874         try:
00875             for i in BadIterator() :
00876                 pass
00877         except TypeError:
00878             pass
00879 

Definition at line 314 of file test_iter.py.

00314 
00315     def test_builtin_filter(self):
00316         self.assertEqual(list(filter(None, SequenceClass(5))),
00317                          list(range(1, 5)))
00318         self.assertEqual(list(filter(None, SequenceClass(0))), [])
00319         self.assertEqual(list(filter(None, ())), [])
00320         self.assertEqual(list(filter(None, "abc")), ["a", "b", "c"])
00321 
00322         d = {"one": 1, "two": 2, "three": 3}
00323         self.assertEqual(list(filter(None, d)), list(d.keys()))
00324 
00325         self.assertRaises(TypeError, filter, None, list)
00326         self.assertRaises(TypeError, filter, None, 42)
00327 
00328         class Boolean:
00329             def __init__(self, truth):
00330                 self.truth = truth
00331             def __bool__(self):
00332                 return self.truth
00333         bTrue = Boolean(True)
00334         bFalse = Boolean(False)
00335 
00336         class Seq:
00337             def __init__(self, *args):
00338                 self.vals = args
00339             def __iter__(self):
00340                 class SeqIter:
00341                     def __init__(self, vals):
00342                         self.vals = vals
00343                         self.i = 0
00344                     def __iter__(self):
00345                         return self
00346                     def __next__(self):
00347                         i = self.i
00348                         self.i = i + 1
00349                         if i < len(self.vals):
00350                             return self.vals[i]
00351                         else:
00352                             raise StopIteration
00353                 return SeqIter(self.vals)
00354 
00355         seq = Seq(*([bTrue, bFalse] * 25))
00356         self.assertEqual(list(filter(lambda x: not x, seq)), [bFalse]*25)
00357         self.assertEqual(list(filter(lambda x: not x, iter(seq))), [bFalse]*25)

Here is the call graph for this function:

Definition at line 250 of file test_iter.py.

00250 
00251     def test_builtin_list(self):
00252         self.assertEqual(list(SequenceClass(5)), list(range(5)))
00253         self.assertEqual(list(SequenceClass(0)), [])
00254         self.assertEqual(list(()), [])
00255 
00256         d = {"one": 1, "two": 2, "three": 3}
00257         self.assertEqual(list(d), list(d.keys()))
00258 
00259         self.assertRaises(TypeError, list, list)
00260         self.assertRaises(TypeError, list, 42)
00261 
00262         f = open(TESTFN, "w")
00263         try:
00264             for i in range(5):
00265                 f.write("%d\n" % i)
00266         finally:
00267             f.close()
00268         f = open(TESTFN, "r")
00269         try:
00270             self.assertEqual(list(f), ["0\n", "1\n", "2\n", "3\n", "4\n"])
00271             f.seek(0, 0)
00272             self.assertEqual(list(f),
00273                              ["0\n", "1\n", "2\n", "3\n", "4\n"])
00274         finally:
00275             f.close()
00276             try:
00277                 unlink(TESTFN)
00278             except OSError:
00279                 pass

Here is the call graph for this function:

Definition at line 391 of file test_iter.py.

00391 
00392     def test_builtin_map(self):
00393         self.assertEqual(list(map(lambda x: x+1, SequenceClass(5))),
00394                          list(range(1, 6)))
00395 
00396         d = {"one": 1, "two": 2, "three": 3}
00397         self.assertEqual(list(map(lambda k, d=d: (k, d[k]), d)),
00398                          list(d.items()))
00399         dkeys = list(d.keys())
00400         expected = [(i < len(d) and dkeys[i] or None,
00401                      i,
00402                      i < len(d) and dkeys[i] or None)
00403                     for i in range(3)]
00404 
00405         f = open(TESTFN, "w")
00406         try:
00407             for i in range(10):
00408                 f.write("xy" * i + "\n") # line i has len 2*i+1
00409         finally:
00410             f.close()
00411         f = open(TESTFN, "r")
00412         try:
00413             self.assertEqual(list(map(len, f)), list(range(1, 21, 2)))
00414         finally:
00415             f.close()
00416             try:
00417                 unlink(TESTFN)
00418             except OSError:
00419                 pass

Here is the call graph for this function:

Definition at line 359 of file test_iter.py.

00359 
00360     def test_builtin_max_min(self):
00361         self.assertEqual(max(SequenceClass(5)), 4)
00362         self.assertEqual(min(SequenceClass(5)), 0)
00363         self.assertEqual(max(8, -1), 8)
00364         self.assertEqual(min(8, -1), -1)
00365 
00366         d = {"one": 1, "two": 2, "three": 3}
00367         self.assertEqual(max(d), "two")
00368         self.assertEqual(min(d), "one")
00369         self.assertEqual(max(d.values()), 3)
00370         self.assertEqual(min(iter(d.values())), 1)
00371 
00372         f = open(TESTFN, "w")
00373         try:
00374             f.write("medium line\n")
00375             f.write("xtra large line\n")
00376             f.write("itty-bitty line\n")
00377         finally:
00378             f.close()
00379         f = open(TESTFN, "r")
00380         try:
00381             self.assertEqual(min(f), "itty-bitty line\n")
00382             f.seek(0, 0)
00383             self.assertEqual(max(f), "xtra large line\n")
00384         finally:
00385             f.close()
00386             try:
00387                 unlink(TESTFN)
00388             except OSError:
00389                 pass

Here is the call graph for this function:

Definition at line 281 of file test_iter.py.

00281 
00282     def test_builtin_tuple(self):
00283         self.assertEqual(tuple(SequenceClass(5)), (0, 1, 2, 3, 4))
00284         self.assertEqual(tuple(SequenceClass(0)), ())
00285         self.assertEqual(tuple([]), ())
00286         self.assertEqual(tuple(()), ())
00287         self.assertEqual(tuple("abc"), ("a", "b", "c"))
00288 
00289         d = {"one": 1, "two": 2, "three": 3}
00290         self.assertEqual(tuple(d), tuple(d.keys()))
00291 
00292         self.assertRaises(TypeError, tuple, list)
00293         self.assertRaises(TypeError, tuple, 42)
00294 
00295         f = open(TESTFN, "w")
00296         try:
00297             for i in range(5):
00298                 f.write("%d\n" % i)
00299         finally:
00300             f.close()
00301         f = open(TESTFN, "r")
00302         try:
00303             self.assertEqual(tuple(f), ("0\n", "1\n", "2\n", "3\n", "4\n"))
00304             f.seek(0, 0)
00305             self.assertEqual(tuple(f),
00306                              ("0\n", "1\n", "2\n", "3\n", "4\n"))
00307         finally:
00308             f.close()
00309             try:
00310                 unlink(TESTFN)
00311             except OSError:
00312                 pass

Here is the call graph for this function:

Definition at line 421 of file test_iter.py.

00421 
00422     def test_builtin_zip(self):
00423         self.assertEqual(list(zip()), [])
00424         self.assertEqual(list(zip(*[])), [])
00425         self.assertEqual(list(zip(*[(1, 2), 'ab'])), [(1, 'a'), (2, 'b')])
00426 
00427         self.assertRaises(TypeError, zip, None)
00428         self.assertRaises(TypeError, zip, range(10), 42)
00429         self.assertRaises(TypeError, zip, range(10), zip)
00430 
00431         self.assertEqual(list(zip(IteratingSequenceClass(3))),
00432                          [(0,), (1,), (2,)])
00433         self.assertEqual(list(zip(SequenceClass(3))),
00434                          [(0,), (1,), (2,)])
00435 
00436         d = {"one": 1, "two": 2, "three": 3}
00437         self.assertEqual(list(d.items()), list(zip(d, d.values())))
00438 
00439         # Generate all ints starting at constructor arg.
00440         class IntsFrom:
00441             def __init__(self, start):
00442                 self.i = start
00443 
00444             def __iter__(self):
00445                 return self
00446 
00447             def __next__(self):
00448                 i = self.i
00449                 self.i = i+1
00450                 return i
00451 
00452         f = open(TESTFN, "w")
00453         try:
00454             f.write("a\n" "bbb\n" "cc\n")
00455         finally:
00456             f.close()
00457         f = open(TESTFN, "r")
00458         try:
00459             self.assertEqual(list(zip(IntsFrom(0), f, IntsFrom(-100))),
00460                              [(0, "a\n", -100),
00461                               (1, "bbb\n", -99),
00462                               (2, "cc\n", -98)])
00463         finally:
00464             f.close()
00465             try:
00466                 unlink(TESTFN)
00467             except OSError:
00468                 pass
00469 
00470         self.assertEqual(list(zip(range(5))), [(i,) for i in range(5)])
00471 
00472         # Classes that lie about their lengths.
00473         class NoGuessLen5:
00474             def __getitem__(self, i):
00475                 if i >= 5:
00476                     raise IndexError
00477                 return i
00478 
00479         class Guess3Len5(NoGuessLen5):
00480             def __len__(self):
00481                 return 3
00482 
00483         class Guess30Len5(NoGuessLen5):
00484             def __len__(self):
00485                 return 30
00486 
00487         def lzip(*args):
00488             return list(zip(*args))
00489 
00490         self.assertEqual(len(Guess3Len5()), 3)
00491         self.assertEqual(len(Guess30Len5()), 30)
00492         self.assertEqual(lzip(NoGuessLen5()), lzip(range(5)))
00493         self.assertEqual(lzip(Guess3Len5()), lzip(range(5)))
00494         self.assertEqual(lzip(Guess30Len5()), lzip(range(5)))
00495 
00496         expected = [(i, i) for i in range(5)]
00497         for x in NoGuessLen5(), Guess3Len5(), Guess30Len5():
00498             for y in NoGuessLen5(), Guess3Len5(), Guess30Len5():
00499                 self.assertEqual(lzip(x, y), expected)

Here is the call graph for this function:

Definition at line 583 of file test_iter.py.

00583 
00584     def test_countOf(self):
00585         from operator import countOf
00586         self.assertEqual(countOf([1,2,2,3,2,5], 2), 3)
00587         self.assertEqual(countOf((1,2,2,3,2,5), 2), 3)
00588         self.assertEqual(countOf("122325", "2"), 3)
00589         self.assertEqual(countOf("122325", "6"), 0)
00590 
00591         self.assertRaises(TypeError, countOf, 42, 1)
00592         self.assertRaises(TypeError, countOf, countOf, countOf)
00593 
00594         d = {"one": 3, "two": 3, "three": 3, 1j: 2j}
00595         for k in d:
00596             self.assertEqual(countOf(d, k), 1)
00597         self.assertEqual(countOf(d.values(), 3), 3)
00598         self.assertEqual(countOf(d.values(), 2j), 1)
00599         self.assertEqual(countOf(d.values(), 1j), 0)
00600 
00601         f = open(TESTFN, "w")
00602         try:
00603             f.write("a\n" "b\n" "c\n" "b\n")
00604         finally:
00605             f.close()
00606         f = open(TESTFN, "r")
00607         try:
00608             for letter, count in ("a", 1), ("b", 2), ("c", 1), ("d", 0):
00609                 f.seek(0, 0)
00610                 self.assertEqual(countOf(f, letter + "\n"), count)
00611         finally:
00612             f.close()
00613             try:
00614                 unlink(TESTFN)
00615             except OSError:
00616                 pass

Here is the call graph for this function:

Definition at line 162 of file test_iter.py.

00162 
00163     def test_exception_function(self):
00164         def spam(state=[0]):
00165             i = state[0]
00166             state[0] = i+1
00167             if i == 10:
00168                 raise RuntimeError
00169             return i
00170         res = []
00171         try:
00172             for x in iter(spam, 20):
00173                 res.append(x)
00174         except RuntimeError:
00175             self.assertEqual(res, list(range(10)))
00176         else:
00177             self.fail("should have raised RuntimeError")

Here is the call graph for this function:

Definition at line 179 of file test_iter.py.

00179 
00180     def test_exception_sequence(self):
00181         class MySequenceClass(SequenceClass):
00182             def __getitem__(self, i):
00183                 if i == 10:
00184                     raise RuntimeError
00185                 return SequenceClass.__getitem__(self, i)
00186         res = []
00187         try:
00188             for x in MySequenceClass(20):
00189                 res.append(x)
00190         except RuntimeError:
00191             self.assertEqual(res, list(range(10)))
00192         else:
00193             self.fail("should have raised RuntimeError")

Here is the call graph for this function:

Definition at line 542 of file test_iter.py.

00542 
00543     def test_in_and_not_in(self):
00544         for sc5 in IteratingSequenceClass(5), SequenceClass(5):
00545             for i in range(5):
00546                 self.assertIn(i, sc5)
00547             for i in "abc", -1, 5, 42.42, (3, 4), [], {1: 1}, 3-12j, sc5:
00548                 self.assertNotIn(i, sc5)
00549 
00550         self.assertRaises(TypeError, lambda: 3 in 12)
00551         self.assertRaises(TypeError, lambda: 3 not in map)
00552 
00553         d = {"one": 1, "two": 2, "three": 3, 1j: 2j}
00554         for k in d:
00555             self.assertIn(k, d)
00556             self.assertNotIn(k, d.values())
00557         for v in d.values():
00558             self.assertIn(v, d.values())
00559             self.assertNotIn(v, d)
00560         for k, v in d.items():
00561             self.assertIn((k, v), d.items())
00562             self.assertNotIn((v, k), d.items())
00563 
00564         f = open(TESTFN, "w")
00565         try:
00566             f.write("a\n" "b\n" "c\n")
00567         finally:
00568             f.close()
00569         f = open(TESTFN, "r")
00570         try:
00571             for chunk in "abc":
00572                 f.seek(0, 0)
00573                 self.assertNotIn(chunk, f)
00574                 f.seek(0, 0)
00575                 self.assertIn((chunk + "\n"), f)
00576         finally:
00577             f.close()
00578             try:
00579                 unlink(TESTFN)
00580             except OSError:
00581                 pass

Here is the call graph for this function:

Definition at line 618 of file test_iter.py.

00618 
00619     def test_indexOf(self):
00620         from operator import indexOf
00621         self.assertEqual(indexOf([1,2,2,3,2,5], 1), 0)
00622         self.assertEqual(indexOf((1,2,2,3,2,5), 2), 1)
00623         self.assertEqual(indexOf((1,2,2,3,2,5), 3), 3)
00624         self.assertEqual(indexOf((1,2,2,3,2,5), 5), 5)
00625         self.assertRaises(ValueError, indexOf, (1,2,2,3,2,5), 0)
00626         self.assertRaises(ValueError, indexOf, (1,2,2,3,2,5), 6)
00627 
00628         self.assertEqual(indexOf("122325", "2"), 1)
00629         self.assertEqual(indexOf("122325", "5"), 5)
00630         self.assertRaises(ValueError, indexOf, "122325", "6")
00631 
00632         self.assertRaises(TypeError, indexOf, 42, 1)
00633         self.assertRaises(TypeError, indexOf, indexOf, indexOf)
00634 
00635         f = open(TESTFN, "w")
00636         try:
00637             f.write("a\n" "b\n" "c\n" "d\n" "e\n")
00638         finally:
00639             f.close()
00640         f = open(TESTFN, "r")
00641         try:
00642             fiter = iter(f)
00643             self.assertEqual(indexOf(fiter, "b\n"), 1)
00644             self.assertEqual(indexOf(fiter, "d\n"), 1)
00645             self.assertEqual(indexOf(fiter, "e\n"), 0)
00646             self.assertRaises(ValueError, indexOf, fiter, "a\n")
00647         finally:
00648             f.close()
00649             try:
00650                 unlink(TESTFN)
00651             except OSError:
00652                 pass
00653 
00654         iclass = IteratingSequenceClass(3)
00655         for i in range(3):
00656             self.assertEqual(indexOf(iclass, i), i)
00657         self.assertRaises(ValueError, indexOf, iclass, -1)

Here is the call graph for this function:

Definition at line 70 of file test_iter.py.

00070 
00071     def test_iter_basic(self):
00072         self.check_iterator(iter(range(10)), list(range(10)))

Here is the call graph for this function:

Definition at line 204 of file test_iter.py.

00204 
00205     def test_iter_big_range(self):
00206         self.check_for_loop(iter(range(10000)), list(range(10000)))

Here is the call graph for this function:

Definition at line 131 of file test_iter.py.

00131 
00132     def test_iter_callable(self):
00133         class C:
00134             def __init__(self):
00135                 self.i = 0
00136             def __call__(self):
00137                 i = self.i
00138                 self.i = i + 1
00139                 if i > 100:
00140                     raise IndexError # Emergency stop
00141                 return i
00142         self.check_iterator(iter(C(), 10), list(range(10)))

Here is the call graph for this function:

Definition at line 108 of file test_iter.py.

00108 
00109     def test_iter_class_for(self):
00110         self.check_for_loop(IteratingSequenceClass(10), list(range(10)))

Here is the call graph for this function:

Definition at line 112 of file test_iter.py.

00112 
00113     def test_iter_class_iter(self):
00114         self.check_iterator(iter(IteratingSequenceClass(10)), list(range(10)))

Here is the call graph for this function:

Definition at line 224 of file test_iter.py.

00224 
00225     def test_iter_dict(self):
00226         dict = {}
00227         for i in range(10):
00228             dict[i] = None
00229         self.check_for_loop(dict, list(dict.keys()))

Here is the call graph for this function:

Definition at line 208 of file test_iter.py.

00208 
00209     def test_iter_empty(self):
00210         self.check_for_loop(iter([]), [])

Here is the call graph for this function:

Definition at line 231 of file test_iter.py.

00231 
00232     def test_iter_file(self):
00233         f = open(TESTFN, "w")
00234         try:
00235             for i in range(5):
00236                 f.write("%d\n" % i)
00237         finally:
00238             f.close()
00239         f = open(TESTFN, "r")
00240         try:
00241             self.check_for_loop(f, ["0\n", "1\n", "2\n", "3\n", "4\n"])
00242             self.check_for_loop(f, [])
00243         finally:
00244             f.close()
00245             try:
00246                 unlink(TESTFN)
00247             except OSError:
00248                 pass

Here is the call graph for this function:

Definition at line 81 of file test_iter.py.

00081 
00082     def test_iter_for_loop(self):
00083         self.check_for_loop(iter(range(10)), list(range(10)))

Here is the call graph for this function:

Definition at line 144 of file test_iter.py.

00144 
00145     def test_iter_function(self):
00146         def spam(state=[0]):
00147             i = state[0]
00148             state[0] = i+1
00149             return i
00150         self.check_iterator(iter(spam, 10), list(range(10)))

Here is the call graph for this function:

Definition at line 152 of file test_iter.py.

00152 
00153     def test_iter_function_stop(self):
00154         def spam(state=[0]):
00155             i = state[0]
00156             if i == 10:
00157                 raise StopIteration
00158             state[0] = i+1
00159             return i
00160         self.check_iterator(iter(spam, 20), list(range(10)))

Here is the call graph for this function:

Definition at line 74 of file test_iter.py.

00074 
00075     def test_iter_idempotency(self):
00076         seq = list(range(10))
00077         it = iter(seq)
00078         it2 = iter(it)
00079         self.assertTrue(it is it2)

Here is the call graph for this function:

Definition at line 85 of file test_iter.py.

00085 
00086     def test_iter_independence(self):
00087         seq = range(3)
00088         res = []
00089         for i in iter(seq):
00090             for j in iter(seq):
00091                 for k in iter(seq):
00092                     res.append((i, j, k))
00093         self.assertEqual(res, TRIPLETS)

Here is the call graph for this function:

Definition at line 216 of file test_iter.py.

00216 
00217     def test_iter_range(self):
00218         self.check_for_loop(iter(range(10)), list(range(10)))

Here is the call graph for this function:

Definition at line 220 of file test_iter.py.

00220 
00221     def test_iter_string(self):
00222         self.check_for_loop(iter("abcde"), ["a", "b", "c", "d", "e"])

Here is the call graph for this function:

Definition at line 212 of file test_iter.py.

00212 
00213     def test_iter_tuple(self):
00214         self.check_for_loop(iter((0,1,2,3,4,5,6,7,8,9)), list(range(10)))

Here is the call graph for this function:

Definition at line 102 of file test_iter.py.

00102 
00103     def test_nested_comprehensions_for(self):
00104         seq = range(3)
00105         res = [(i, j, k) for i in seq for j in seq for k in seq]
00106         self.assertEqual(res, TRIPLETS)

Here is the call graph for this function:

Definition at line 95 of file test_iter.py.

00095 
00096     def test_nested_comprehensions_iter(self):
00097         seq = range(3)
00098         res = [(i, j, k)
00099                for i in iter(seq) for j in iter(seq) for k in iter(seq)]
00100         self.assertEqual(res, TRIPLETS)

Here is the call graph for this function:

Definition at line 124 of file test_iter.py.

00124 
00125     def test_new_style_iter_class(self):
00126         class IterClass(object):
00127             def __iter__(self):
00128                 return self
00129         self.assertRaises(TypeError, iter, IterClass())

Here is the call graph for this function:

Definition at line 766 of file test_iter.py.

00766 
00767     def test_ref_counting_behavior(self):
00768         class C(object):
00769             count = 0
00770             def __new__(cls):
00771                 cls.count += 1
00772                 return object.__new__(cls)
00773             def __del__(self):
00774                 cls = self.__class__
00775                 assert cls.count > 0
00776                 cls.count -= 1
00777         x = C()
00778         self.assertEqual(C.count, 1)
00779         del x
00780         self.assertEqual(C.count, 0)
00781         l = [C(), C(), C()]
00782         self.assertEqual(C.count, 3)
00783         try:
00784             a, b = iter(l)
00785         except ValueError:
00786             pass
00787         del l
00788         self.assertEqual(C.count, 0)
00789 

Here is the call graph for this function:

Definition at line 116 of file test_iter.py.

00116 
00117     def test_seq_class_for(self):
00118         self.check_for_loop(SequenceClass(10), list(range(10)))

Here is the call graph for this function:

Definition at line 120 of file test_iter.py.

00120 
00121     def test_seq_class_iter(self):
00122         self.check_iterator(iter(SequenceClass(10)), list(range(10)))

Here is the call graph for this function:

Definition at line 822 of file test_iter.py.

00822 
00823     def test_sinkstate_callable(self):
00824         # This used to fail
00825         def spam(state=[0]):
00826             i = state[0]
00827             state[0] = i+1
00828             if i == 10:
00829                 raise AssertionError("shouldn't have gotten this far")
00830             return i
00831         b = iter(spam, 5)
00832         self.assertEqual(list(b), list(range(5)))
00833         self.assertEqual(list(b), [])

Here is the call graph for this function:

Definition at line 834 of file test_iter.py.

00834 
00835     def test_sinkstate_dict(self):
00836         # XXX For a more thorough test, see towards the end of:
00837         # http://mail.python.org/pipermail/python-dev/2002-July/026512.html
00838         a = {1:1, 2:2, 0:0, 4:4, 3:3}
00839         for b in iter(a), a.keys(), a.items(), a.values():
00840             b = iter(a)
00841             self.assertEqual(len(list(b)), 5)
00842             self.assertEqual(list(b), [])

Here is the call graph for this function:

Definition at line 857 of file test_iter.py.

00857 
00858     def test_sinkstate_enumerate(self):
00859         a = range(5)
00860         e = enumerate(a)
00861         b = iter(e)
00862         self.assertEqual(list(b), list(zip(range(5), range(5))))
00863         self.assertEqual(list(b), [])

Here is the call graph for this function:

Definition at line 794 of file test_iter.py.

00794 
00795     def test_sinkstate_list(self):
00796         # This used to fail
00797         a = list(range(5))
00798         b = iter(a)
00799         self.assertEqual(list(b), list(range(5)))
00800         a.extend(range(5, 10))
00801         self.assertEqual(list(b), [])

Here is the call graph for this function:

Definition at line 851 of file test_iter.py.

00851 
00852     def test_sinkstate_range(self):
00853         a = range(5)
00854         b = iter(a)
00855         self.assertEqual(list(b), list(range(5)))
00856         self.assertEqual(list(b), [])

Here is the call graph for this function:

Definition at line 814 of file test_iter.py.

00814 
00815     def test_sinkstate_sequence(self):
00816         # This used to fail
00817         a = SequenceClass(5)
00818         b = iter(a)
00819         self.assertEqual(list(b), list(range(5)))
00820         a.n = 10
00821         self.assertEqual(list(b), [])

Here is the call graph for this function:

Definition at line 808 of file test_iter.py.

00808 
00809     def test_sinkstate_string(self):
00810         a = "abcde"
00811         b = iter(a)
00812         self.assertEqual(list(b), ['a', 'b', 'c', 'd', 'e'])
00813         self.assertEqual(list(b), [])

Here is the call graph for this function:

Definition at line 802 of file test_iter.py.

00802 
00803     def test_sinkstate_tuple(self):
00804         a = (0, 1, 2, 3, 4)
00805         b = iter(a)
00806         self.assertEqual(list(b), list(range(5)))
00807         self.assertEqual(list(b), [])

Here is the call graph for this function:

Definition at line 843 of file test_iter.py.

00843 
00844     def test_sinkstate_yield(self):
00845         def gen():
00846             for i in range(5):
00847                 yield i
00848         b = gen()
00849         self.assertEqual(list(b), list(range(5)))
00850         self.assertEqual(list(b), [])

Here is the call graph for this function:

Definition at line 195 of file test_iter.py.

00195 
00196     def test_stop_sequence(self):
00197         class MySequenceClass(SequenceClass):
00198             def __getitem__(self, i):
00199                 if i == 10:
00200                     raise StopIteration
00201                 return SequenceClass.__getitem__(self, i)
00202         self.check_for_loop(MySequenceClass(20), list(range(10)))

Here is the call graph for this function:

Definition at line 500 of file test_iter.py.

00500 
00501     def test_unicode_join_endcase(self):
00502 
00503         # This class inserts a Unicode object into its argument's natural
00504         # iteration, in the 3rd position.
00505         class OhPhooey:
00506             def __init__(self, seq):
00507                 self.it = iter(seq)
00508                 self.i = 0
00509 
00510             def __iter__(self):
00511                 return self
00512 
00513             def __next__(self):
00514                 i = self.i
00515                 self.i = i+1
00516                 if i == 2:
00517                     return "fooled you!"
00518                 return next(self.it)
00519 
00520         f = open(TESTFN, "w")
00521         try:
00522             f.write("a\n" + "b\n" + "c\n")
00523         finally:
00524             f.close()
00525 
00526         f = open(TESTFN, "r")
00527         # Nasty:  string.join(s) can't know whether unicode.join() is needed
00528         # until it's seen all of s's elements.  But in this case, f's
00529         # iterator cannot be restarted.  So what we're testing here is
00530         # whether string.join() can manage to remember everything it's seen
00531         # and pass that on to unicode.join().
00532         try:
00533             got = " - ".join(OhPhooey(f))
00534             self.assertEqual(got, "a\n - b\n - fooled you! - c\n")
00535         finally:
00536             f.close()
00537             try:
00538                 unlink(TESTFN)
00539             except OSError:
00540                 pass

Here is the call graph for this function:

Definition at line 712 of file test_iter.py.

00712 
00713     def test_unpack_iter(self):
00714         a, b = 1, 2
00715         self.assertEqual((a, b), (1, 2))
00716 
00717         a, b, c = IteratingSequenceClass(3)
00718         self.assertEqual((a, b, c), (0, 1, 2))
00719 
00720         try:    # too many values
00721             a, b = IteratingSequenceClass(3)
00722         except ValueError:
00723             pass
00724         else:
00725             self.fail("should have raised ValueError")
00726 
00727         try:    # not enough values
00728             a, b, c = IteratingSequenceClass(2)
00729         except ValueError:
00730             pass
00731         else:
00732             self.fail("should have raised ValueError")
00733 
00734         try:    # not iterable
00735             a, b, c = len
00736         except TypeError:
00737             pass
00738         else:
00739             self.fail("should have raised TypeError")
00740 
00741         a, b, c = {1: 42, 2: 42, 3: 42}.values()
00742         self.assertEqual((a, b, c), (42, 42, 42))
00743 
00744         f = open(TESTFN, "w")
00745         lines = ("a\n", "bb\n", "ccc\n")
00746         try:
00747             for line in lines:
00748                 f.write(line)
00749         finally:
00750             f.close()
00751         f = open(TESTFN, "r")
00752         try:
00753             a, b, c = f
00754             self.assertEqual((a, b, c), lines)
00755         finally:
00756             f.close()
00757             try:
00758                 unlink(TESTFN)
00759             except OSError:
00760                 pass
00761 
00762         (a, b), (c,) = IteratingSequenceClass(2), {42: 24}
00763         self.assertEqual((a, b, c), (0, 1, 42))
00764 

Here is the call graph for this function:

Definition at line 659 of file test_iter.py.

00659 
00660     def test_writelines(self):
00661         f = open(TESTFN, "w")
00662 
00663         try:
00664             self.assertRaises(TypeError, f.writelines, None)
00665             self.assertRaises(TypeError, f.writelines, 42)
00666 
00667             f.writelines(["1\n", "2\n"])
00668             f.writelines(("3\n", "4\n"))
00669             f.writelines({'5\n': None})
00670             f.writelines({})
00671 
00672             # Try a big chunk too.
00673             class Iterator:
00674                 def __init__(self, start, finish):
00675                     self.start = start
00676                     self.finish = finish
00677                     self.i = self.start
00678 
00679                 def __next__(self):
00680                     if self.i >= self.finish:
00681                         raise StopIteration
00682                     result = str(self.i) + '\n'
00683                     self.i += 1
00684                     return result
00685 
00686                 def __iter__(self):
00687                     return self
00688 
00689             class Whatever:
00690                 def __init__(self, start, finish):
00691                     self.start = start
00692                     self.finish = finish
00693 
00694                 def __iter__(self):
00695                     return Iterator(self.start, self.finish)
00696 
00697             f.writelines(Whatever(6, 6+2000))
00698             f.close()
00699 
00700             f = open(TESTFN)
00701             expected = [str(i) + "\n" for i in range(1, 2006)]
00702             self.assertEqual(list(f), expected)
00703 
00704         finally:
00705             f.close()
00706             try:
00707                 unlink(TESTFN)
00708             except OSError:
00709                 pass
00710 

Here is the call graph for this function:


Member Data Documentation

Definition at line 675 of file test_iter.py.

Definition at line 134 of file test_iter.py.

Definition at line 506 of file test_iter.py.

Definition at line 674 of file test_iter.py.

Definition at line 329 of file test_iter.py.

Definition at line 337 of file test_iter.py.


The documentation for this class was generated from the following file: