testIterator.py :  » Web-Frameworks » Zope » Zope-2.6.0 » lib » python » ZTUtils » tests » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Zope 
Zope » Zope 2.6.0 » lib » python » ZTUtils » tests » testIterator.py
import os, sys, unittest

from ZTUtils import Iterator

try:
    iter
    do_piter_test = 1
except NameError:
    do_piter_test = 0

class IteratorTests(unittest.TestCase):

    def testIterator0(self):
        it = Iterator(())
        assert not it.next(), "Empty iterator"

    def testIterator1(self):
        it = Iterator((1,))
        assert it.next() and not it.next(), "Single-element iterator"

    def testIteratorMany(self):
        it = Iterator('text')
        for c in 'text':
            assert it.next(), "Multi-element iterator"
        assert not it.next(), "Multi-element iterator"

    def testStart(self):
        for size in range(4):
            it = Iterator(range(size+1))
            it.next()
            assert it.start, "Start true on element 1 of %s" % (size + 1)
            el = 1
            while it.next():
                el = el + 1
                assert not it.start, (
                    "Start false on element %s of %s" % (el, size+1))

    def testEnd(self):
        for size in range(4):
            size = size + 1
            it = Iterator(range(size))
            el = 0
            while it.next():
                el = el + 1
                if el == size:
                    assert it.end, "End true on element %s" % size
                else:
                    assert not it.end, (
                        "End false on element %s of %s" % (el, size))

    def testIndex(self):
        it = Iterator(range(5))
        for el in range(5):
            assert it.next(), "Iterator stopped too soon"
            assert it.index == el, "Incorrect index"
            assert it.number() == el + 1, "Incorrect number"
            assert it.item == el, "Incorrect item"

    def testFirstLast(self):
        it = Iterator([1])
        it.next()
        assert it.first() == it.last() == 1, "Bad first/last on singleton"
        four = range(4)
        for a in 2,3:
            for b in four:
                for c in four:
                    s = 'a' * a + 'b' * b + 'c' * c
                    it = Iterator(s)
                    it.next()
                    assert it.first(), "First element not first()"
                    last = s[0]
                    lastlast = it.last()
                    while it.next():
                        assert ((it.item != last) == it.first()), (
                            "first() error")
                        assert ((it.item != last) == lastlast), (
                            "last() error" % (it.item,
                            last, lastlast))
                        last = it.item
                        lastlast = it.last()
                    assert lastlast, "Last element not last()"

    if do_piter_test:
        def testIterOfIter(self):
            for i in range(4):
                r = range(i)
                it1 = Iterator(r)
                it2 = Iterator(iter(r))
                while it1.next() and it2.next():
                    assert it1.item == it2.item, "Item mismatch with iter()"
                    assert it1.index == it2.index, (
                        "Index mismatch with iter()")
                assert not (it1.next() or it2.next()), (
                    "Length mismatch with iter()")

        def testIterIter(self):
            wo_iter = map(lambda x:(x, x), range(4))
            for i in range(4):
                r = range(i)
                w_iter = []
                it = Iterator(r)
                for x in it:
                    w_iter.append((x, it.index))
                assert w_iter == wo_iter[:i], (
                    "for-loop failure on full iterator")
            it = Iterator(range(4))
            it.next(); it.next(); it.next()
            w_iter = []
            for x in it:
                w_iter.append((x, it.index))
            assert w_iter == wo_iter[2:], "for-loop failure on half iteration"

def test_suite():
    return unittest.makeSuite(IteratorTests)

if __name__=='__main__':
    main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.