本文整理汇总了Python中stack.Stack.clear方法的典型用法代码示例。如果您正苦于以下问题:Python Stack.clear方法的具体用法?Python Stack.clear怎么用?Python Stack.clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stack.Stack
的用法示例。
在下文中一共展示了Stack.clear方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestStack
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import clear [as 别名]
class TestStack(unittest.TestCase):
def setUp(self):
self.stack = Stack()
def test_init(self):
self.assertEqual(self.stack.get_size(), 0)
new_stack = Stack("Bob")
self.assertEqual(new_stack.get_size(), 1)
def test_push(self):
initial_size = self.stack.get_size()
self.stack.push("Bob")
self.assertEqual(self.stack.get_size(), initial_size + 1)
def test_peek(self):
self.stack.push("Adrian")
self.stack.push("Bob")
self.stack.push("Claire")
self.assertEqual(self.stack.peek(), "Claire")
def test_pop(self):
self.stack.push("Adrian")
self.stack.push("Bob")
self.stack.push("Claire")
initial_size = self.stack.get_size()
popped_item = self.stack.pop()
self.assertEqual(popped_item, "Claire")
self.assertEqual(self.stack.get_size(), initial_size - 1)
def test_clear(self):
self.stack.push("Adrian")
self.stack.push("Bob")
self.stack.push("Claire")
self.assertEqual(self.stack.get_size(), 3)
self.stack.clear()
self.assertEqual(self.stack.get_size(), 0)
def test_get_size(self):
self.stack.push("Bob")
self.assertEqual(self.stack.linked_list.size, self.stack.get_size())
示例2: test_clear
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import clear [as 别名]
def test_clear():
s = Stack()
s.push(1)
s.push(2)
s.clear()
assert_equals(s.is_empty(), True)
示例3: StackTestCase
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import clear [as 别名]
class StackTestCase(unittest.TestCase):
def setUp(self):
self.stack = Stack()
def test_isEmpty(self):
self.assertTrue(self.stack.isEmpty())
self.stack.push(1)
self.assertFalse(self.stack.isEmpty())
def test_push_and_pop(self):
val = 1
self.stack.push(val)
self.assertFalse(self.stack.isEmpty())
self.assertEqual(val, self.stack.pop())
self.assertTrue(self.stack.isEmpty())
def test_peek(self):
self.assertIsNone(self.stack.peek())
val = 1
self.stack.push(val)
self.assertFalse(self.stack.isEmpty())
self.assertEqual(val, self.stack.peek())
self.assertFalse(self.stack.isEmpty())
def test_size(self):
self.assertEqual(0, self.stack.size())
self.stack.push(1)
self.assertEqual(1, self.stack.size())
def test_clear(self):
self.stack.push(1)
self.stack.clear()
self.assertTrue(self.stack.isEmpty())
def test_str(self):
self.assertEqual('{}', str(self.stack))
self.stack.push(1)
self.assertEqual('{1}', str(self.stack))
self.stack.push(2)
self.assertEqual('{1, 2}', str(self.stack))
def test_advanced(self):
self.assertTrue(self.stack.isEmpty())
for i in range(5):
self.assertEqual(i, self.stack.size())
self.stack.push(i)
self.assertEqual(i, self.stack.peek())
for i in range(4,-1, -1):
self.assertEqual(i + 1, self.stack.size())
self.assertEqual(i, self.stack.pop())
self.assertTrue(self.stack.isEmpty())
self.assertIsNone(self.stack.peek())
self.assertIsNone(self.stack.pop())