Hanterar felaktiga indata samt testar detta
This commit is contained in:
+16
-12
@@ -1,6 +1,7 @@
|
|||||||
import io
|
|
||||||
|
|
||||||
def hanoi(n, source, auxiliary, target):
|
def hanoi(n, source, auxiliary, target):
|
||||||
|
# Example usage:
|
||||||
|
# hanoi(3, 'A', 'B', 'C')
|
||||||
if n < 1:
|
if n < 1:
|
||||||
print(f"No disks, nothing to do")
|
print(f"No disks, nothing to do")
|
||||||
return
|
return
|
||||||
@@ -14,16 +15,19 @@ def hanoi(n, source, auxiliary, target):
|
|||||||
# Move the n-1 disks that we left on auxiliary to target
|
# Move the n-1 disks that we left on auxiliary to target
|
||||||
hanoi(n - 1, auxiliary, source, target)
|
hanoi(n - 1, auxiliary, source, target)
|
||||||
|
|
||||||
# Example usage:
|
def get_hanoi_input():
|
||||||
# hanoi(3, 'A', 'B', 'C')
|
# Returns a positive integer, otherwise throws an exception
|
||||||
|
while True:
|
||||||
|
user_input = input("Please enter an integer: ")
|
||||||
|
try:
|
||||||
|
user_integer = int(user_input)
|
||||||
|
return user_integer
|
||||||
|
except ValueError:
|
||||||
|
print("That's not a valid integer. Please try again.")
|
||||||
|
|
||||||
if __name__== "__main__":
|
if __name__== "__main__":
|
||||||
user_input = input("Please enter an integer: ")
|
# Call the new function to get the input
|
||||||
try:
|
user_integer = get_hanoi_input()
|
||||||
user_integer = int(user_input)
|
# Now call hanoi with the obtained integer
|
||||||
print(f"You entered the integer: {user_integer}")
|
hanoi(user_integer, 'A', 'B', 'C')
|
||||||
except ValueError:
|
|
||||||
print("That's not a valid integer. Please try again.")
|
|
||||||
|
|
||||||
hanoi(user_integer,'A','B','C')
|
|
||||||
|
|
||||||
+41
-40
@@ -1,46 +1,47 @@
|
|||||||
import unittest
|
import unittest
|
||||||
from unittest import mock
|
|
||||||
from hanoi import hanoi # Assuming the code is in a file named hanoi.py
|
|
||||||
import io
|
import io
|
||||||
import sys
|
from unittest.mock import patch
|
||||||
|
from hanoi import hanoi, get_hanoi_input
|
||||||
|
|
||||||
class TestHanoi(unittest.TestCase):
|
class TestHanoi(unittest.TestCase):
|
||||||
|
|
||||||
def test_hanoi_with_one_disk(self):
|
def test_hanoi_zero_disks(self):
|
||||||
with self.subTest(n=1, source='A', auxiliary='B', target='C'):
|
expected_output = "No disks, nothing to do\n"
|
||||||
expected_output = "Move disk 1 from A to C\n"
|
# with self.assertRaises(SystemExit):
|
||||||
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
||||||
hanoi(1, 'A', 'B', 'C')
|
hanoi(0, 'A', 'B', 'C')
|
||||||
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
|
||||||
|
def test_hanoi_one_disk(self):
|
||||||
def test_hanoi_with_two_disks(self):
|
expected_output = "Move disk 1 from A to C\n"
|
||||||
with self.subTest(n=2, source='A', auxiliary='B', target='C'):
|
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
||||||
expected_output = "Move disk 1 from A to B\nMove disk 2 from A to C\nMove disk 1 from B to C\n"
|
hanoi(1, 'A', 'B', 'C')
|
||||||
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
||||||
hanoi(2, 'A', 'B', 'C')
|
|
||||||
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
def test_hanoi_two_disks(self):
|
||||||
|
expected_output = "Move disk 1 from A to B\nMove disk 2 from A to C\nMove disk 1 from B to C\n"
|
||||||
def test_hanoi_with_three_disks(self):
|
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
||||||
with self.subTest(n=3, source='A', auxiliary='B', target='C'):
|
hanoi(2, 'A', 'B', 'C')
|
||||||
expected_output = "Move disk 1 from A to C\nMove disk 2 from A to B\nMove disk 1 from C to B\nMove disk 3 from A to C\nMove disk 1 from B to A\nMove disk 2 from B to C\nMove disk 1 from A to C\n"
|
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
||||||
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
|
||||||
hanoi(3, 'A', 'B', 'C')
|
def test_hanoi_three_disks(self):
|
||||||
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
expected_output = "Move disk 1 from A to C\nMove disk 2 from A to B\nMove disk 1 from C to B\nMove disk 3 from A to C\nMove disk 1 from B to A\nMove disk 2 from B to C\nMove disk 1 from A to C\n"
|
||||||
|
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
||||||
def test_hanoi_with_zero_disks(self):
|
hanoi(3, 'A', 'B', 'C')
|
||||||
with self.subTest(n=0, source='A', auxiliary='B', target='C'):
|
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
||||||
expected_output = "No disks, nothing to do\n"
|
|
||||||
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
def test_hanoi_negative_input(self):
|
||||||
hanoi(0, 'A', 'B', 'C')
|
expected_output = "No disks, nothing to do\n"
|
||||||
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
# with self.assertRaises(SystemExit):
|
||||||
|
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
||||||
def test_hanoi_with_negative_disks(self):
|
hanoi(-1, 'A', 'B', 'C')
|
||||||
with self.subTest(n=-1, source='A', auxiliary='B', target='C'):
|
|
||||||
expected_output = "No disks, nothing to do\n"
|
def test_get_hanoi_input_valid_integer(self):
|
||||||
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
with unittest.mock.patch('builtins.input', return_value='4'):
|
||||||
hanoi(-1, 'A', 'B', 'C')
|
self.assertEqual(get_hanoi_input(), 4)
|
||||||
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
|
||||||
|
def test_get_hanoi_input_invalid_input(self):
|
||||||
|
with unittest.mock.patch('builtins.input', side_effect=['abc', '5']):
|
||||||
|
self.assertEqual(get_hanoi_input(), 5)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user