コード例 #1
0
 def test_pop(self):
   """ pop once """
   test = ArrayQueue()
   test.push(1)
   test.push(2)
   popped = test.pop()
   self.assertEqual(popped, 1)
コード例 #2
0
 def test_clear(self):
   test = ArrayQueue()
   test.push(1)
   test.push(2)
   test.push(3)
   self.assertEqual(test.show_array, [1, 2, 3, None, None, None, None, None, None, None])
   test.clear()
   self.assertEqual(test.show_array, [None, None, None, None, None, None, None, None, None, None])
コード例 #3
0
 def test_expandWhenFull(self):
   """when size of array is at capacity, double array"""
   test = ArrayQueue()
   for i in range(1, 11):
     test.push(i)
   self.assertEqual(test.show_array, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
   test.push(11)
   self.assertEqual(test.show_array, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, None, None, None, None, None, None, None, None, None])
コード例 #4
0
 def test_randomForFun(self):
   """random push/pops v.3, just making sure"""
   test = ArrayQueue()
   for i in range(1, 11):
     test.push(i)
   test.pop()
   test.pop()
   test.push('a')
   self.assertEqual(test.show_array, ['a', None, 3, 4, 5, 6, 7, 8, 9, 10])
コード例 #5
0
 def test_random(self):
   """random push/pops"""
   test = ArrayQueue()
   for i in range(1, 11):
     test.push(i)
   for i in range(1, 11):
     test.pop()
   test.push('a')
   test.push('b')
   self.assertEqual(test.show_array, ['a', 'b', None, None, None, None, None, None, None, None])
コード例 #6
0
 def test_popAll(self):
   """ pop all elements"""
   test = ArrayQueue()
   test.push(1)
   test.push(2)
   test.push(3)
   test.push(4)
   test.pop()
   test.pop()
   test.pop()
   test.pop()
   self.assertEqual(test.show_array, [None, None, None, None, None, None, None, None, None, None])
コード例 #7
0
class CatDogQueue(object):
    def __init__(self):
        self.dogqueue = ArrayQueue()
        self.catqueue = ArrayQueue()
        self.count = 0
    
    def add(self,pet):
        if pet.get_pettype() == "dog":
            self.count += 1
            petcount = PetCount(pet, self.count)
            self.dogqueue.push(petcount)
        elif pet.get_pettype() == "cat":
            self.count += 1
            petcount = PetCount(pet,self.count)
            self.catqueue.push(petcount)
        else:
            raise RuntimeError("no pet type")
    
    def pollall(self):
        if not self.isCatEmpty() and not self.isDogEmpty():
            if (self.dogqueue.peek().getCount()) < (self.catqueue.peek().getCount()):
                return self.dogqueue.poll()
            else:
                return self.catqueue.poll()
        elif self.isDogEmpty():
            return self.catqueue.poll()
        elif self.isCatEmpty():
            return self.dogqueue.poll()
        else:
            raise RuntimeError("err, queue is empty")
    
    def pollDog(self):
        if not self.isDogEmpty():
            # print(type(self.dogqueue.poll()))
            return  self.dogqueue.poll()
        else:
            raise RuntimeError("Dog queue is empty")

    def pollCat(self):
        if not self.isCatEmpty():
            return self.catqueue.poll()
        else:
            raise RuntimeError("Cat queue is empty")


    def isCatEmpty(self):
        return self.catqueue.isEmpyt()
    
    def isDogEmpty(self):
        return self.dogqueue.isEmpyt()
コード例 #8
0
 def test_random2(self):
   """random push/pops v.2"""
   test = ArrayQueue()
   test.push(1)
   test.push(2)
   test.push(3)
   test.push(4)
   test.pop()
   test.pop()
   test.pop()
   test.push('a')
   test.push('b')
   self.assertEqual(test.show_array, [None, None, None, 4, 'a', 'b', None, None, None, None])
コード例 #9
0
 def test_push(self):
   test = ArrayQueue()
   test.push(1)
   self.assertEqual(test.show_array, [1, None, None, None, None, None, None, None, None, None])
コード例 #10
0
 def test_pushToCapacity(self):
   """ Fill entire array """
   test = ArrayQueue()
   for i in range(1, 11):
     test.push(i)
   self.assertEqual(test.show_array, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
コード例 #11
0
 def test_peek(self):
   test = ArrayQueue()
   test.push(1)
   self.assertEqual(test.peek(), 1)