def test_04_partiellement_plein_size_isEmpty_should_be_false_isFull_shouldBeFalse( self): stack = Stack(1) stack.push(1) self.assertEqual(stack.size(), 1) self.assertFalse(stack.isEmpty()) self.assertTrue(stack.isFull())
def test_c_UnionStack(self): stack1 = Stack(3) stack2 = Stack(3) numbers = [1, 2, 3, 4, 5, 6] numbersstack1 = [1, 2, 3] numbersstack2 = [4, 5, 6] for i in numbersstack1: stack1.push(i) for i in numbersstack2: stack2.push(i) unitedStack = Calculator.union(stack1, stack2) numbersContained = 0 for i in numbers: number = unitedStack.pop().value if number in numbers: numbersContained += 1 numbers.remove(number) self.assertEqual(numbersContained, len(numbers)) self.assertEqual(unitedStack.max_size, stack1.max_size + stack2.max_size)
def testUnionTwoStacks(self): for i in range(5): for j in range(5): stack1 = Stack(20) for k in range(10): stack1.push(k+i*100) stack2 = Stack(20) for k in range(10): stack2.push(k+j*1000) resultat = Calculator.union(stack1, stack2) self.assertIsInstance(resultat, Stack) self.assertEqual(resultat.max_size, stack1.max_size + stack2.max_size) self.assertEqual(resultat.size(), stack1.size() + stack2.size()) for k in range(9, -1, -1): self.assertEqual(resultat.pop(), k+j*1000) for k in range(9, -1, -1): self.assertEqual(resultat.pop(), k+i*100)
class testCalculator(unittest.TestCase): def setUp(self): self.MAX_SIZE1 = 10 self.MAX_SIZE2 = 5 self.stack = Stack(self.MAX_SIZE1) self.stack.push(15) self.stack.push(1) self.stack.push(5) self.queue = Queue(self.MAX_SIZE2) self.queue.enqueue(16) self.queue.enqueue(13) self.queue.enqueue(46) # CLass 1: Testing Node def test_04_empty_node_points_to_None(self): self.assertRaises(ValueError, Calculator.union, Stack(self.MAX_SIZE1), Queue(self.MAX_SIZE1)) def test_04_should_raise_two_enpty_queues(self): self.assertRaises(ValueError, Calculator.union, Stack(self.MAX_SIZE1), Queue(self.MAX_SIZE1)) def test_04_should_return_new_size_of_merged_queue(self): unionList = Calculator.union(self.stack, self.stack) self.assertEqual(unionList.n, 6) def test_04_should_return_new_size_of_merged_queue(self): unionList = Calculator.union(self.stack, self.stack) self.assertEqual(unionList.n, 6) def test_04_should_return_first_element_of_merged_queue(self): unionList = Calculator.union(self.stack, self.stack) self.assertEqual(unionList.first.value, 5) def test_04_should_raise_error_queue_even_with_first_null(self): self.assertRaises(ValueError, Calculator.union, Queue(self.MAX_SIZE1), self.queue) def test_04_should_raise_when_one_stack_is_empty(self): self.assertRaises(ValueError, Calculator.union, self.stack, Stack(self.MAX_SIZE1)) def test_04_should_raise_two_different_types(self): self.assertRaises(ValueError, Calculator.union, Stack(self.MAX_SIZE1), Queue(self.MAX_SIZE1)) def test_04_should_raise_two_empty_linkedList(self): self.assertRaises(ValueError, Calculator.union, LinkedList(), LinkedList()) def test_04_should_return_new_size_of_merged_queue(self): unionList = Calculator.union(self.stack, self.stack) self.assertEqual(unionList.n, 6) def test_04_should_return_first_element_of_merged_queue(self): unionList = Calculator.union(self.queue, self.queue) self.assertEqual(unionList.first.value, 16) def test_04_should_return_first_element_of_merged_stack(self): unionList = Calculator.union(self.stack, self.stack) self.assertEqual(unionList.first.value, 5) def test_04_should_raise_when_two_nonEmpty_with_different_type(self): self.assertRaises(ValueError, Calculator.union, self.stack, self.queue)
def test_04_plein_puch__size_isEmpty_should_be_true_isFull_shouldBeFalse(self): stack = Stack(1) stack.push(1) self.assertEqual(stack.size(), 1) self.assertFalse(stack.isEmpty()) self.assertTrue(stack.isFull()) stack.pop() self.assertTrue(stack.isEmpty()) self.assertFalse(stack.isFull())
def test_05_test_Visit_Log_Stack(self): stack = Stack(6) for i in range(5): stack.push(i) stack.accept(ScreenPrinter("test")) display_message = "\n-------\n" expected = 'test\n' + display_message + ' 4 ' + display_message + ' 3 ' + display_message + ' 2 ' + display_message + ' 1 ' + display_message + ' 0 ' + display_message + '\n' self.assertEqual(self.capturedOutput.getvalue(), expected)
def test_05_test_Visit_Log_Stack(self): stack = Stack(6) for i in range(5): stack.push(i) stack.accept(FilePrinter("./ThisIsATestFile.txt","")) with open('./ThisIsATestFile.txt', 'r') as myfile: result=myfile.read(); separator = "\n-------\n"; expected = separator+' 4 '+separator+' 3 '+separator+' 2 '+separator+' 1 '+separator+' 0 '+separator; self.assertEqual(result, expected);
def convert_int_to_bin(dec_num): s = Stack() while dec_num > 0: rem = dec_num % 2 s.push(rem) dec_num = dec_num // 2 binary = "" while len(s.get_stack()) > 0: binary = binary + str(s.pop()) return binary
def testVisitLogStack(self): stack = Stack(10) for i in range(5): stack.push(i) stack.accept(ScreenPrinter("")) s = "\n-------\n" expected = '\n' + s + ' 4 ' + s + ' 3 ' + s + ' 2 ' + s + ' 1 ' + s + ' 0 ' + s + '\n' self.assertEqual(self.capturedOutput.getvalue(), expected)
class StackTest(unittest.TestCase): def setUp(self): self.stack = Stack(3) # Test __init__() def test_a_Init(self): self.assertEqual(self.stack.max_size, 3) # Test __init__() -> push() -> pop() def test_b_PushPop(self): self.stack.push("test") self.assertFalse(self.stack.isEmpty()) self.assertTrue(self.stack.size() == 1) self.assertEqual(self.stack.check().value, "test") self.assertEqual(self.stack.peek().value, "test") self.assertTrue(self.stack.isEmpty()) self.stack.push("test1") self.stack.push("test2") self.stack.push("test3") self.assertTrue(self.stack.isFull()) self.assertEqual(self.stack.size(), self.stack.max_size) try: self.stack.push("test4") except Exception as ex: self.assertEqual(str(ex), "Stack overflow") self.assertEqual(self.stack.pop().value, "test3") self.assertEqual(self.stack.pop().value, "test2") self.assertEqual(self.stack.pop().value, "test1") try: self.stack.pop() except Exception as ex: self.assertEqual(str(ex), "Stack underflow")
def testVisitLogStack(self): stack = Stack(10) for i in range(5): stack.push(i) stack.accept(FilePrinter("./test.txt", "")) with open('./test.txt', 'r') as myfile: result = myfile.read() s = "\n-------\n" expected = s + ' 4 ' + s + ' 3 ' + s + ' 2 ' + s + ' 1 ' + s + ' 0 ' + s self.assertEqual(result, expected)
class FilePrinterTest(unittest.TestCase): def setUp(self): self.printer = FilePrinter("testFile.txt", "testName") self.linkedList = LinkedList() self.stack = Stack(3) self.queue = Queue(3) def tearDown(self): try: os.remove(self.printer.file_path) except IOError: pass # Test __init__() def test_a_Init(self): self.assertEqual(self.printer.file_path, "testFile.txt") self.assertEqual(self.printer.name, "testName") def test_b_VisitLinkedList(self): self.linkedList.append(1) self.linkedList.append(2) self.linkedList.append(3) self.linkedList.accept(self.printer) with open(self.printer.file_path, 'r') as testFile: self.assertEqual(testFile.read(), self.printer.name + "\n(1,2,3)\n") def test_c_VisitStack(self): self.stack.push(1) self.stack.push(2) self.stack.push(3) self.stack.accept(self.printer) filler="\n-------\n" with open(self.printer.file_path, 'r') as testFile: self.assertEqual(testFile.read(), self.printer.name +filler+' 3 '+filler+' 2 '+filler+' 1 '+filler) def test_d_VisitLogQueue(self): self.queue.enqueue(1) self.queue.enqueue(2) self.queue.enqueue(3) self.queue.accept(self.printer) with open(self.printer.file_path, 'r') as testFile: self.assertEqual(testFile.read(), self.printer.name + "\n|1|2|3|\n")
def isBalancedSet(bracket_set): s = Stack() for bracket in bracket_set: cb = opening_brackets.get(bracket) ob = closing_brackets.get(bracket) if cb is not None: s.push(bracket) elif ob is not None: pb = s.pop() if ob == pb: continue else: return -1 else: return None return -1 if len(s.get_stack()) > 0 else 1
def testUnionStackAndQueue(self): stacks = [] for n in range(5): stack = Stack(20) for i in range(10): stack.push(i+n*100) stacks.append(stack) queues = [] for n in range(5): queue = Queue(20) for i in range(10): queue.enqueue(i+n*1000) queues.append(queue) for stack in stacks: for queue in queues: resultat = Calculator.union(stack, queue) self.assertIsInstance(resultat, LinkedList) self.assertNotIsInstance(resultat, Stack) self.assertNotIsInstance(resultat, Queue) self.assertEqual(resultat.size(), stack.size() + queue.size()) noeud = stack.first while noeud.next: self.assertTrue(contient(resultat, noeud.value)) noeud = noeud.next noeud = queue.first while noeud.next: self.assertTrue(contient(resultat, noeud.value)) noeud = noeud.next
class ScreenPrinterTest(unittest.TestCase): def setUp(self): self.screenPrinter = ScreenPrinter("test") self.linkedList = LinkedList() self.stack = Stack(3) self.queue = Queue(3) # Test __init__() def test_a_Init(self): self.assertEqual(self.screenPrinter.name, "test") def test_b_VisitLinkedList(self): self.linkedList.append(1) self.linkedList.append(2) self.linkedList.append(3) sys.stdout = io.StringIO() self.linkedList.accept(ScreenPrinter("")) self.assertEqual(sys.stdout.getvalue(), "\n\n(1,2,3)\n\n") sys.stdout = sys.__stdout__ def test_c_VisitStack(self): self.stack.push(1) self.stack.push(2) self.stack.push(3) sys.stdout = io.StringIO() self.stack.accept(ScreenPrinter("")) filler = "\n-------\n" self.assertEqual( sys.stdout.getvalue(), '\n' + filler + ' 3 ' + filler + ' 2 ' + filler + ' 1 ' + filler + '\n') sys.stdout = sys.__stdout__ def test_d_VisitLogQueue(self): self.queue.enqueue(1) self.queue.enqueue(2) self.queue.enqueue(3) sys.stdout = io.StringIO() self.queue.accept(ScreenPrinter("")) self.assertEqual(sys.stdout.getvalue(), "\n\n|1|2|3|\n\n") sys.stdout = sys.__stdout__
def testPushPop(self): for size in range(5, 10): stack = Stack(size) for i in range(1, size): stack.push(i) self.assertEqual(stack.size(), i) self.assertEqual(stack.check(), i) self.assertFalse(stack.isEmpty()) self.assertFalse(stack.isFull()) stack.push("last") self.assertEqual(stack.size(), size) self.assertEqual(stack.check(), "last") self.assertFalse(stack.isEmpty()) self.assertTrue(stack.isFull()) for i in range(10): self.assertRaises(ValueError, stack.push, "fail") self.assertEqual(stack.size(), size) self.assertEqual(stack.check(), "last") self.assertFalse(stack.isEmpty()) self.assertTrue(stack.isFull()) self.assertEqual(stack.pop(), "last") for i in range(size - 1, 1, -1): self.assertEqual(stack.pop(), i) self.assertFalse(stack.isEmpty()) self.assertFalse(stack.isFull()) self.assertEqual(stack.pop(), 1) self.assertTrue(stack.isEmpty()) self.assertFalse(stack.isFull()) self.assertRaises(ValueError, stack.pop)
class testStack(unittest.TestCase): def setUp(self): self.MAX_SIZE = 10 self.stack = Stack(self.MAX_SIZE) # Cas de test pour le cas vide def test_04_Vide_isEmpty_should_be_true(self): self.assertTrue(self.stack.isEmpty()) def test_04_vide_isFull_should_BeFalse_isEmpty_should_be_true(self): self.assertFalse(self.stack.isFull()) self.assertTrue(self.stack.isEmpty()) def test_04_vide_Size_should_BeZero_isEmpty_should_be_true(self): self.assertEqual(self.stack.size(), 0) self.assertTrue(self.stack.isEmpty()) def test_04_vide_peek_should_raiseError_isEmpty_should_be_true(self): self.assertRaises(ValueError, self.stack.peek) self.assertTrue(self.stack.isEmpty()) def test_04_vide_pop_should_raiseError_isEmpty_should_be_true(self): self.stack.pop() self.assertEqual(0, self.stack.n) self.assertTrue(self.stack.isEmpty()) def test_04_vide___str____isEmpty_should_be_true(self): self.stack.__str__() self.assertTrue(self.stack.isEmpty()) # transition de l'etat vide a l'etat partiellement_plein def test_04_partiellement_plein_push__isEmpty_should_be_false_isFull_shouldBeFalse( self): self.stack.push(1) self.assertFalse(self.stack.isEmpty()) self.assertFalse(self.stack.isFull()) def test_04_partiellement_plein_pop_size_greatanThan_1__isEmpty_should_be_false_isFull_shouldBeFalse( self): self.stack.push(1) self.stack.push(2) self.stack.pop() self.assertFalse(self.stack.isEmpty()) self.assertFalse(self.stack.isFull()) def test_04_partiellement_plein_size_isEmpty_should_be_false_isFull_shouldBeFalse( self): self.stack.push(1) self.assertEqual(self.stack.size(), 1) self.assertFalse(self.stack.isEmpty()) self.assertFalse(self.stack.isFull()) def test_04_partiellement_plein_pop_isEmpty_should_be_false_isFull_shouldBeFalse( self): self.stack.push(1) self.stack.push(15) self.assertEqual(self.stack.peek(), 15) self.assertFalse(self.stack.isEmpty()) self.assertFalse(self.stack.isFull()) def test_04_partiellement_plein___str____isEmpty_should_be_false_isFull_shouldBeFalse( self): self.stack.push(1) self.stack.__str__() self.assertFalse(self.stack.isEmpty()) self.assertFalse(self.stack.isFull()) # push[n < max_size] stay in the same state def test_04_partiellement_plein_push_size_greatanThan_1__isEmpty_should_be_false_isFull_shouldBeFalse( self): self.stack.push(1) self.stack.push(2) self.assertFalse(self.stack.isEmpty()) self.assertFalse(self.stack.isFull()) # transition de l'etat partiellement_plein a l'etat plein def test_04_plein_push_size_greatanThan_1__isEmpty_should_be_false_isFull_shouldBeFalse( self): for i in range(1, self.MAX_SIZE + 1): self.stack.push(i) self.assertFalse(self.stack.isEmpty()) self.assertTrue(self.stack.isFull()) def test_04_plein_push__isEmpty_should_be_false_isFull_shouldBeFalse(self): for i in range(1, self.MAX_SIZE + 1): self.stack.push(i) self.assertEqual(self.stack.size(), self.MAX_SIZE) self.assertTrue(self.stack.isFull()) def test_04_plein_push_should_be_false_isFull_shouldBeFalse(self): for i in range(1, self.MAX_SIZE + 1): self.stack.push(i) self.assertEqual(self.stack.size(), self.MAX_SIZE) self.assertRaises(ValueError, self.stack.push, self.MAX_SIZE + 1) self.assertEqual(self.stack.peek(), 10) self.stack.push(4) self.assertEqual(self.stack.peek(), 4) self.assertEqual(self.stack.size(), 9) self.assertFalse(self.stack.isFull()) def test_04_plein_pop_should_be_false_isFull_shouldBeFalse(self): for i in range(1, self.MAX_SIZE + 1): self.stack.push(i) self.stack.pop() self.assertEqual(self.stack.size(), self.MAX_SIZE - 1) self.assertFalse(self.stack.isEmpty()) self.assertFalse(self.stack.isFull()) def test_04_plein___str____isEmpty_should_be_false_isFull_should_be_true( self): for i in range(self.MAX_SIZE): self.stack.push(i) self.stack.__str__() self.assertFalse(self.stack.isEmpty()) self.assertTrue(self.stack.isFull()) def test_04_plein_push_should_not_add_when_trials_max_trials(self): for i in range(self.MAX_SIZE): self.stack.push(i) self.assertEqual(self.stack.size(), self.MAX_SIZE) self.assertRaises(ValueError, self.stack.push, self.MAX_SIZE + 1) # tests for stack with max_size == 1 def test_04_partiellement_plein_size_isEmpty_should_be_false_isFull_shouldBeFalse( self): stack = Stack(1) stack.push(1) self.assertEqual(stack.size(), 1) self.assertFalse(stack.isEmpty()) self.assertTrue(stack.isFull()) def test_04_plein_puch__size_isEmpty_should_be_true_isFull_shouldBeFalse( self): stack = Stack(1) stack.push(1) self.assertEqual(stack.size(), 1) self.assertFalse(stack.isEmpty()) self.assertTrue(stack.isFull()) stack.pop() self.assertTrue(stack.isEmpty()) self.assertFalse(stack.isFull()) def test_04_stack_should_return_correct_value_to_string(self): self.stack.push(4) self.assertEqual(str(self.stack.peek()), '4')