def test_one_empty(self): e = loglinear.revorder([]) f = loglinear.revorder([3, 2, 1]) r = loglinear.jointrevorder(e, f) self.assertEqual([3, 2, 1], list(r)) r = loglinear.jointrevorder(f, e) self.assertEqual([3, 2, 1], list(r))
def test_backing_iterators(self): a_yielded = [] b_yielded = [] def record(x, yielded): yielded.append(x) return x def a_gen(): yield record(4, a_yielded) yield record(3, a_yielded) yield record(1, a_yielded) def b_gen(): yield record(2, b_yielded) yield record(0, b_yielded) a = loglinear.revorder(a_gen()) b = loglinear.revorder(b_gen()) r = loglinear.jointrevorder(a, b) i = iter(r) self.assertEqual([], a_yielded) self.assertEqual([], b_yielded) self.assertEqual(4, i.next()) self.assertEqual([4], a_yielded) self.assertEqual([2], b_yielded) self.assertEqual(3, i.next()) self.assertEqual([4, 3], a_yielded) self.assertEqual([2], b_yielded) self.assertEqual(2, i.next()) self.assertEqual([4, 3, 1], a_yielded) self.assertEqual([2], b_yielded) self.assertEqual(1, i.next()) self.assertEqual([4, 3, 1], a_yielded) self.assertEqual([2, 0], b_yielded) self.assertEqual(0, i.next())
def test_source_advance(self): """Verify that a revorder correctly advances the backing iterator as for as it has to to satisfy has and iterator requests""" yielded = [] def record(x): yielded.append(x) return x def gen(): yield record(4) yield record(3) yield record(1) yield record(0) r = loglinear.revorder(gen()) it = iter(r) self.assertEqual([], yielded) self.assertTrue(3 in r) self.assertEqual([4, 3], yielded) self.assertEqual(4, it.next()) self.assertEqual(3, it.next()) self.assertEqual([4, 3], yielded) self.assertEqual(1, it.next()) self.assertEqual([4, 3, 1], yielded) self.assertFalse(2 in r) self.assertTrue(4 in r) self.assertEqual([4, 3, 1], yielded)
def test_source_advance(self): """Verify that a revorder correctly advances the backing iterator as for as it has to to satisfy has and iterator requests""" yielded = [] def record(x): yielded.append(x) return x def gen(): yield record(4) yield record(3) yield record(1) yield record(0) r = loglinear.revorder(gen()) it = iter(r) self.assertEqual([], yielded) self.assertTrue(3 in r) self.assertEqual([4,3], yielded) self.assertEqual(4, it.next()) self.assertEqual(3, it.next()) self.assertEqual([4,3], yielded) self.assertEqual(1, it.next()) self.assertEqual([4,3,1], yielded) self.assertFalse(2 in r) self.assertTrue(4 in r) self.assertEqual([4,3,1], yielded)
def test_iterate(self): """Verify that a revorder is iterable in the same order as its backing iterator, and can be iterated more than once.""" def gen(): yield 3 yield 2 yield 1 r = loglinear.revorder(gen()) self.assertEqual([3,2,1], list(iter(r))) self.assertEqual([3,2,1], list(iter(r)))
def test_iterate(self): """Verify that a revorder is iterable in the same order as its backing iterator, and can be iterated more than once.""" def gen(): yield 3 yield 2 yield 1 r = loglinear.revorder(gen()) self.assertEqual([3, 2, 1], list(iter(r))) self.assertEqual([3, 2, 1], list(iter(r)))
def test_empty(self): """Verify that a revorder backed by an empty iterator is empty.""" class empty_iter: def __init__(self): self.called = 0 def __iter__(self): return self def next(self): self.called += 1 raise StopIteration it = empty_iter() r = loglinear.revorder(it) self.assertEqual(0, it.called) self.assertEqual([], list(r)) self.assertEqual(1, it.called)
def test_has(self): """Verify that a revorder correctly reports which elements it contains and only advances the backing iterator as for as it has to to do so.""" yielded = [] def record(x): yielded.append(x) return x def gen(): yield record(4) yield record(3) yield record(1) yield record(0) r = loglinear.revorder(gen()) self.assertEqual([], yielded) self.assertTrue(4 in r) self.assertEqual([4], yielded) self.assertFalse(2 in r) self.assertEqual([4,3,1], yielded) self.assertTrue(3 in r) self.assertEqual([4,3,1], yielded)
def test_has(self): """Verify that a revorder correctly reports which elements it contains and only advances the backing iterator as for as it has to to do so.""" yielded = [] def record(x): yielded.append(x) return x def gen(): yield record(4) yield record(3) yield record(1) yield record(0) r = loglinear.revorder(gen()) self.assertEqual([], yielded) self.assertTrue(4 in r) self.assertEqual([4], yielded) self.assertFalse(2 in r) self.assertEqual([4, 3, 1], yielded) self.assertTrue(3 in r) self.assertEqual([4, 3, 1], yielded)
def test_aliassing(self): a = loglinear.revorder([3, 2, 1]) r = loglinear.jointrevorder(a, a) self.assertEqual([3, 2, 1], list(r))
def test_interleave(self): a = loglinear.revorder([7, 5, 3, 2]) b = loglinear.revorder([9, 8, 6, 4, 1]) r = loglinear.jointrevorder(a, b) self.assertEqual([9, 8, 7, 6, 5, 4, 3, 2, 1], list(r))
def test_no_doubles(self): a = loglinear.revorder([3, 2]) b = loglinear.revorder([2, 1]) r = loglinear.jointrevorder(a, b) self.assertEqual([3, 2, 1], list(r))
def test_both_empty(self): r = loglinear.jointrevorder(loglinear.revorder([]), loglinear.revorder([])) self.assertEqual([], list(r))