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())
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)
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)
def testAdd(): b = Bag() for i in range(10): b.add(i) assert(b.isEmpty() == False) assert(b.size() == 10) for i in range(-10, 20): b.add(i) assert(b.isEmpty() == False) assert(b.size() == 40) for i in range(-100, 100): b.add(i) assert(b.isEmpty() == False) assert(b.size() == 240) for i in range(-200,200): count = b.nrOccurrences(i) if i < -100: assert(count == 0) assert(b.search(i) == False) elif i < -10: assert(count == 1) assert(b.search(i) == True) elif i < 0: assert(count == 2) assert(b.search(i) == True) elif i < 10: assert(count == 3) assert(b.search(i) == True) elif i < 20: assert(count == 2) assert(b.search(i) == True) elif i < 100: assert(count == 1) assert(b.search(i) == True) else: assert(count == 0) assert(b.search(i) == False) for i in range(10000, -10000, -1): b.add(i) assert(b.size() == 20240)
def testRemove(): b = Bag() for i in range(-100,100): assert(b.remove(i) == False) assert(b.size() == 0) for i in range(-100,100, 2): b.add(i) for i in range (-100,100): if i % 2 == 0: assert(b.remove(i) == True) else : assert(b.remove(i) == False) assert(b.size() == 0) for i in range(-100,101, 2): b.add(i) for i in range(100,-100, -1): if i % 2 == 0: assert(b.remove(i) == True) else: assert(b.remove(i) == False) assert(b.size() == 1) b.remove(-100) for i in range(-100, 100): b.add(i) b.add(i) b.add(i) b.add(i) b.add(i) assert(b.size() == 1000) for i in range(-100,100): assert(b.nrOccurrences(i) == 5) for i in range(-100, 100): assert(b.remove(i) == True) assert(b.size() == 800) for i in range(-100, 100): assert(b.nrOccurrences(i) == 4) for i in range(-200, 200): if (i < -100) or (i >= 100): assert(b.remove(i) == False) assert(b.remove(i) == False) assert(b.remove(i) == False) assert(b.remove(i) == False) assert(b.remove(i) == False) else: assert(b.remove(i) == True) assert(b.remove(i) == True) assert(b.remove(i) == True) assert(b.remove(i) == True) assert(b.remove(i) == False) assert(b.size() == 0) for i in range(-1000,1000): assert(b.nrOccurrences(i) == 0) min = -200 max = 200 while min < max: b.add(min) b.add(max) min += 1 max -= 1 b.add(0) b.add(0) assert(b.size() == 402) for i in range(-30, 30): assert(b.search(i) == True) assert(b.remove(i) == True) if i != 0: assert(b.search(i) == False) else: assert(b.search(i) == True) assert(b.size() == 342)