2020-07-09 07:57:55 +08:00
|
|
|
import unittest
|
2015-07-03 11:04:15 +08:00
|
|
|
|
|
|
|
|
2020-07-09 07:57:55 +08:00
|
|
|
class TestStacks(unittest.TestCase):
|
2015-07-06 18:15:53 +08:00
|
|
|
|
2015-07-03 11:04:15 +08:00
|
|
|
def test_pop_on_empty(self, num_stacks, stack_size):
|
|
|
|
print('Test: Pop on empty stack')
|
|
|
|
stacks = Stacks(num_stacks, stack_size)
|
|
|
|
stacks.pop(0)
|
|
|
|
|
|
|
|
def test_push_on_full(self, num_stacks, stack_size):
|
|
|
|
print('Test: Push to full stack')
|
|
|
|
stacks = Stacks(num_stacks, stack_size)
|
2015-07-06 18:15:53 +08:00
|
|
|
for i in range(0, stack_size):
|
2015-07-03 11:04:15 +08:00
|
|
|
stacks.push(2, i)
|
|
|
|
stacks.push(2, stack_size)
|
|
|
|
|
|
|
|
def test_stacks(self, num_stacks, stack_size):
|
|
|
|
print('Test: Push to non-full stack')
|
|
|
|
stacks = Stacks(num_stacks, stack_size)
|
|
|
|
stacks.push(0, 1)
|
|
|
|
stacks.push(0, 2)
|
|
|
|
stacks.push(1, 3)
|
|
|
|
stacks.push(2, 4)
|
|
|
|
|
|
|
|
print('Test: Pop on non-empty stack')
|
2020-07-09 07:57:55 +08:00
|
|
|
self.assertEqual(stacks.pop(0), 2)
|
|
|
|
self.assertEqual(stacks.pop(0), 1)
|
|
|
|
self.assertEqual(stacks.pop(1), 3)
|
|
|
|
self.assertEqual(stacks.pop(2), 4)
|
2015-07-03 11:04:15 +08:00
|
|
|
|
|
|
|
print('Success: test_stacks\n')
|
|
|
|
|
2015-07-12 03:39:59 +08:00
|
|
|
|
2015-07-03 11:04:15 +08:00
|
|
|
def main():
|
|
|
|
num_stacks = 3
|
2015-07-06 18:15:53 +08:00
|
|
|
stack_size = 100
|
2015-07-03 11:04:15 +08:00
|
|
|
test = TestStacks()
|
2020-07-09 07:57:55 +08:00
|
|
|
test.assertRaises(Exception, test.test_pop_on_empty, num_stacks,
|
|
|
|
stack_size)
|
|
|
|
test.assertRaises(Exception, test.test_push_on_full, num_stacks,
|
|
|
|
stack_size)
|
2015-07-03 11:04:15 +08:00
|
|
|
test.test_stacks(num_stacks, stack_size)
|
2015-07-06 18:15:53 +08:00
|
|
|
|
2015-07-12 03:39:59 +08:00
|
|
|
|
2015-07-03 11:04:15 +08:00
|
|
|
if __name__ == '__main__':
|
2020-07-09 07:57:55 +08:00
|
|
|
main()
|