def testPushPop(self): cir = CircularArray(10) # try pop with empty array self.assertEqual(cir.pop(), None) # since this is a circular array # it should support multiple fill out and clear for j in range(10): # push ten elements for i in range(10): self.assertEqual(cir.push(i), True) # it is full, it should return false # means fail to push again self.assertEqual(cir.push(1), False) for i in range(10): self.assertEqual(i, cir.pop()) # it should support push and pop right after each other for i in range(100): ran = random.randint(-10000, 10000) self.assertEqual(cir.push(ran), True) self.assertEqual(cir.pop(), ran)
def testState(self): cir = CircularArray(5) self.assertEqual(cir.isEmpty(), True) self.assertEqual(cir.isFull(), False) cir.push('A') self.assertEqual(cir.isEmpty(), False) self.assertEqual(cir.isFull(), False) cir.push('B') self.assertEqual(cir.isEmpty(), False) self.assertEqual(cir.isFull(), False) cir.push('C') self.assertEqual(cir.isEmpty(), False) self.assertEqual(cir.isFull(), False) cir.push('D') self.assertEqual(cir.isEmpty(), False) self.assertEqual(cir.isFull(), False) cir.push('E') self.assertEqual(cir.isEmpty(), False) self.assertEqual(cir.isFull(), True) # pop self.assertEqual(cir.pop(), 'A') self.assertEqual(cir.isEmpty(), False) self.assertEqual(cir.isFull(), False) self.assertEqual(cir.pop(), 'B') self.assertEqual(cir.isEmpty(), False) self.assertEqual(cir.isFull(), False) self.assertEqual(cir.pop(), 'C') self.assertEqual(cir.isEmpty(), False) self.assertEqual(cir.isFull(), False) self.assertEqual(cir.pop(), 'D') self.assertEqual(cir.isEmpty(), False) self.assertEqual(cir.isFull(), False) self.assertEqual(cir.pop(), 'E') self.assertEqual(cir.isEmpty(), True) self.assertEqual(cir.isFull(), False) self.assertEqual(cir.pop(), None) self.assertEqual(cir.isEmpty(), True) self.assertEqual(cir.isFull(), False)
class TestCircularArray(unittest.TestCase): def setUp(self): """ [1, 2, 3, 4, 5, 6] 0 1 2 3 4 5 """ self.data = [1, 2, 3, 4, 5, 6] self.array = CircularArray(self.data) def _compare_contents(self, array=None, data=None): array, data = array or self.array, data or self.data self.assertEqual([i for i in self.array], data) def _call_method(self, method, *args): getattr(self.array, method)(*args) getattr(self.data, method)(*args) def test_setitem(self): with self.subTest("positive"): self._call_method("__setitem__", 1, 5) self._compare_contents() with self.subTest("negative"): self._call_method("__setitem__", -1, 5) self._compare_contents() def test_insert(self): with self.subTest("front"): self._call_method("insert", 1, 1) self._compare_contents() with self.subTest("back"): self._call_method("insert", -2, 1) self._compare_contents() def test_delitem(self): with self.subTest("front"): self._call_method("__delitem__", 1) self._compare_contents() with self.subTest("back"): self._call_method("__delitem__", -2) self._compare_contents() def test_append_right(self): for i in range(self.array._initialSize + 1): self._call_method("append", i) self._compare_contents() def test_pop_right(self): self.test_append_right() for _ in range(self.array._initialSize): self.assertEqual(self.array.pop(), self.data.pop()) self._compare_contents() def test_pop_left(self): self.test_append_right() for _ in range(self.array._initialSize): self.assertEqual(self.array.popLeft(), self.data[0]) del self.data[0] self._compare_contents() def test_append_left(self): for i in range(self.array._initialSize): self.array.appendLeft(i) self.data.insert(0, i) self._compare_contents() def test_shrink(self): self.data = [] self.array = CircularArray(self.data) for i in range(self.array._initialSize + 1): self.array.append(i) self.data.append(i) self.assertEqual(len(self.array._array), self.array._initialSize * self.array._resizeFactor) for i in range(self.array._initialSize + 1): self.array.pop() self.data.pop() self._compare_contents() self.assertEqual(len(self.array._array), self.array._initialSize) def test_repr(self): self.assertEqual(str(self.array), f"<Deque {self.data}>") def test_speed(self): k = 1000 array, list = CircularArray(), [] start_array = time.time() for i in range(k): array.insert(0, i) end_array = time.time() start_list = time.time() for i in range(k): list.insert(0, k) end_list = time.time() print("\n") print(f"array time: {round(end_array-start_array, 4)}") print(f"list time: {round(end_list-start_list, 4)}")