コード例 #1
0
	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))
コード例 #2
0
	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())
コード例 #3
0
 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))
コード例 #4
0
    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)
コード例 #5
0
	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)
コード例 #6
0
	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)))
コード例 #7
0
    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)))
コード例 #8
0
	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)
コード例 #9
0
    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())
コード例 #10
0
    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)
コード例 #11
0
	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)
コード例 #12
0
    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)
コード例 #13
0
	def test_aliassing(self):
		a = loglinear.revorder([3, 2, 1])
		r = loglinear.jointrevorder(a, a)
		self.assertEqual([3, 2, 1], list(r))
コード例 #14
0
 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))
コード例 #15
0
 def test_aliassing(self):
     a = loglinear.revorder([3, 2, 1])
     r = loglinear.jointrevorder(a, a)
     self.assertEqual([3, 2, 1], list(r))
コード例 #16
0
 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))
コード例 #17
0
	def test_both_empty(self):
		r = loglinear.jointrevorder(loglinear.revorder([]), 
			loglinear.revorder([]))
		self.assertEqual([], list(r))
コード例 #18
0
 def test_both_empty(self):
     r = loglinear.jointrevorder(loglinear.revorder([]),
                                 loglinear.revorder([]))
     self.assertEqual([], list(r))
コード例 #19
0
	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))
コード例 #20
0
	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))