Exemple #1
0
def testAll():
    b = Bag()
    assert (b.size() == 0)
    assert (b.isEmpty())

    b.add(4)
    b.add(11)
    b.add(4)
    b.add(77)
    b.add(8)

    assert (b.size() == 5)
    assert (b.isEmpty() == False)

    assert (b.remove(44) == False)
    assert (b.remove(11))
    assert (b.size() == 4)
    assert (b.nrOccurrences(4) == 2)
    assert (b.search(77))
    assert (b.search(13) == False)

    itb = b.iterator()
    assert (itb.valid())
    cont = 0
    while (itb.valid()):
        cont += 1
        e = itb.getCurrent()
        itb.next()
        assert (b.search(e))
    assert (cont == b.size())
    itb.first()
    assert (itb.valid())
Exemple #2
0
def testCreate():
    b = Bag()
    assert(b.size() == 0)
    assert(b.isEmpty() == True)
    for i in range(-5,5):
        assert(b.search(i) == False)
    for i in range(-10,10):
        assert(b.remove(i) == False)
    for i in range(-10,10):
        assert(b.nrOccurrences(i) == 0)

    it = b.iterator()
    assert(it.valid() == False)
Exemple #3
0
def testQuantity():
    b = Bag()
    print('Adding')
    for i in range(10, 0, -1):
        print(i)
        for j in range(-30000, 30000, i):
            b.add(j)

    assert(b.size() == 175739)
    assert(b.nrOccurrences(-30000) == 10)
    it = b.iterator()
    print('Dimension: ', b.size())
    print('Iterator')
    assert(it.valid() == True)
    for i in range(0, b.size()):
        it.next()
	
    it.first()
    print('Iterator 2')
    count = 0
    while it.valid():
        if count % 1000 == 0:
            print('  ', count)
        e = it.getCurrent()
        assert(b.search(e) == True)
        assert(b.nrOccurrences(e) > 0)
        it.next()
        count += 1
	
    assert(it.valid() == False)
    print('Remove')
    for i in range(0, 10):
        print(i)
        for j in range(40000, -40000-1, -1):
            b.remove(j)
	
    assert(b.size() == 0)
Exemple #4
0
def testIterator():
    b = Bag()
    it = b.iterator()
    assert(it.valid() == False)
    try:
        it.next()
        assert(False)
    except ValueError:
        assert (True)
    try:
        it.getCurrent()
        assert(False)
    except ValueError:
        assert(True)
	
    for i in range(0,100):
        b.add(33)
	
    it2 = b.iterator()
    assert(it2.valid() == True)
    for i in range(0, 100):
        elem = it2.getCurrent()
        assert(elem == 33)
        it2.next()
	
    assert(it2.valid() == False)
    it2.first()
    assert(it2.valid() == True)
    for i in range(0,100):
        elem = it2.getCurrent()
        elem2 = it2.getCurrent()
        assert(elem == 33)
        assert(elem2 == 33)
        it2.next()
	
    assert(it2.valid() == False)
    try:
        it2.next()
        assert(False)
    except ValueError:
        assert(True)
	
    try:
        it2.getCurrent()
        assert(False)
    except ValueError:
        assert(True)
    b2 = Bag()
    for i in range(-100, 100):
        b2.add(i)
        b2.add(i)
        b2.add(i)
	
    it3 = b2.iterator()
    assert(it3.valid() == True)
    for i in range(0, 600):
        e1 = it3.getCurrent()
        it3.next()
	
    assert(it3.valid() == False)
    it3.first()
    assert(it3.valid() == True)
    b3 = Bag()
    for i in range(0,200, 4):
        b3.add(i)
	
    it4 = b3.iterator()
    assert(it4.valid() == True)
    count = 0
    while it4.valid(): 
        e = it4.getCurrent()
        assert(e % 4 == 0)
        it4.next()
        count += 1

    assert(count == 50)
    b4 = Bag()
    for i in range(0, 100):
        b4.add(i)
        b4.add(i * (-2))
        b4.add(i * 2)
        b4.add(i / 2)
        b4.add(i / (-2))
	
    elements = []
    it5 = b4.iterator()
    while it5.valid():
        e = it5.getCurrent()
        elements.append(e)
        it5.next()
	
    assert(len(elements) == b4.size())
    for i in range(0, len(elements)):
        lastElem = elements[len(elements) - i - 1]
        assert(b4.search(lastElem) == True)
        b4.remove(lastElem)
	
    b5 = Bag()
    for i  in range(0, 100):
        b5.add(i)
        b5.add(i * (-2))
        b5.add(i * 2)
        b5.add(i / 2)
        b5.add(i / (-2))
	
    elements2 = []
    it6 = b5.iterator()
    while it6.valid():
        e = it6.getCurrent()
        elements2.append(e)
        it6.next()
	
    assert(len(elements2) == b5.size())
    for i in range(0, len(elements2)):
        firstElem = elements2[i]
        assert(b5.search(firstElem) == True)
        b5.remove(firstElem)