class TestUnorderedList(unittest.TestCase): """ Submodule for unittests, derives from unittest.TestCase """ def setUp(self): """ Create object for all tests """ self.unordered_list = UnorderedList() self.unordered_list.add("Patrik") self.unordered_list.add("Karlsson") def tearDown(self): """ Remove dependencies after test """ self.unordered_list = None def test_insert(self): """ Test method insert """ # List looks like this 0: Patrik 1: Karlsson # Trying to insert new value at index 3 and -1 should raise exception self.assertRaises(IndexErrorException, self.unordered_list.insert, int(3), "test") self.assertRaises(IndexErrorException, self.unordered_list.insert, int(-1), "test") # Trying to insert new value at index 0 should work and not raise # exception self.unordered_list.insert(int(0), "test") # 0: test 1: Patrik 2: Karlsson # Trying to insert new value at index 3 should work and not raise # exception self.unordered_list.insert(int(3), "test2") # 0: test 1: Patrik 2: Karlsson 3: test2 # Check if index 0 == test and index 3 = test2 self.assertEqual(self.unordered_list.get(int(0)), "test") self.assertEqual(self.unordered_list.get(int(3)), "test2") def test_set(self): """ Test method set """ # Trying to replace new value at index 2 or -1 should raise exception self.assertRaises(IndexErrorException, self.unordered_list.set, int(2), "test") self.assertRaises(IndexErrorException, self.unordered_list.set, int(-1), "test") # Replace a valid index value and check so that the value = new value self.unordered_list.set(int(0), "test") self.assertEqual(self.unordered_list.get(0), "test") def test_get(self): """ Test method get """ # Trying to get value at index 2 or -1 should raise exception self.assertRaises(IndexErrorException, self.unordered_list.get, int(2)) self.assertRaises(IndexErrorException, self.unordered_list.get, int(-1)) # Test get value at indec 0 and 1 should return Patrik and Karlsson self.assertEqual(self.unordered_list.get(int(0)), "Patrik") self.assertEqual(self.unordered_list.get(int(1)), "Karlsson") def test_index_of(self): """ Test index_of method """ # Trying to get value at index 2 or -1 should raise exception self.assertRaises(ValueErrorException, self.unordered_list.index_of, "Patrik_test") self.assertRaises(ValueErrorException, self.unordered_list.index_of, "Karlsson_test") # Test get index for Patrik and Karlsson should return 0 and 1 self.assertEqual(self.unordered_list.index_of("Patrik"), 0) self.assertEqual(self.unordered_list.index_of("Karlsson"), 1) def test_remove(self): """ Test remove method """ # Trying to remove a value that doesnt exist should raise exception self.assertRaises(ValueErrorException, self.unordered_list.remove, "test") # Before removing items in list should be 2 self.assertEqual(self.unordered_list.size(), 2) self.unordered_list.remove("Patrik") # After removing items in list should be 1 self.assertEqual(self.unordered_list.size(), 1) # After removing last item in list, the list shall be emtpy self.unordered_list.remove("Karlsson") self.assertEqual(self.unordered_list.is_empty(), True)
class TestUnorderedList(unittest.TestCase): """ Test class for UnorderedList. """ def setUp(self): """ Setup the objects. """ self.ulist = UnorderedList() self.ulist.add("test node") def tearDown(self): """ Remove the objects. """ self.ulist = None def test_insert(self): """ Test insert method. """ # Raise ValueExistentError. self.ulist = UnorderedList() with self.assertRaises(ValueExistentError): self.ulist.insert(0, "test") # Raise IndexOutOfRangeError. self.ulist.add("test node") with self.assertRaises(IndexOutOfRangeError): self.ulist.insert(-1, "test") with self.assertRaises(IndexOutOfRangeError): self.ulist.insert(1, "test") def test_set(self): """ Test set method. """ with self.assertRaises(IndexOutOfRangeError): self.ulist.set(-1, "test") with self.assertRaises(IndexOutOfRangeError): self.ulist.set(1, "test") def test_get(self): """ Test get method. """ with self.assertRaises(IndexOutOfRangeError): self.ulist.get(-1) with self.assertRaises(IndexOutOfRangeError): self.ulist.get(1) def test_index_of(self): """ Test index_of method. """ with self.assertRaises(ValueExistentError): self.ulist.index_of("test node ") def test_remove(self): """ Test remove method. """ with self.assertRaises(ValueExistentError): self.ulist.index_of(" test node") def test_populated_list_bubble_sort(self): """ Test lists are same after sorting. """ print() lst = [1, 3, 4, 8, 9] self.ulist.remove("test node") self.ulist.add(9) self.ulist.add(1) self.ulist.add(4) self.ulist.add(3) self.ulist.add(8) sort.bubble_sort(self.ulist) current_node = self.ulist.head for item in lst: if current_node.next != None: self.assertEqual(item, current_node.data) current_node = current_node.next def test_empty_list_bubble_sort(self): """ Test if list is empty after sorting. """ self.ulist.remove("test node") sort.bubble_sort(self.ulist) self.assertTrue(self.ulist.is_empty())
class Handler(): """handle the linked list""" def __init__(self): """initate object""" self.uol = UnorderedList() # self.uol.add(3) # self.uol.add(1) # self.uol.add(2) # self.uol.add("anna") # self.uol.add("cindy") # self.uol.add("bertil") def handle_is_empty(self): """1 is_empty: Returnera True/False för om listan är tom eller inte.""" print('Is list empty? {}'.format(self.uol.is_empty())) return True def handle_add(self): """ 2 add: Lägg till nytt element/nod sist i listan.""" value = (input('Add: ')) self.uol.add(value) return True def handle_insert(self): """3 insert: Lägg till nytt element/nod på specifikt index. Om index inte finns lyft exception.""" value = (input("New value: ")) try: i = int(input("At index: ")) self.uol.insert(value, i) except (ValueError, WrongType): print("Not an integer, try again!") except AttError: print("No value at that index.") return True def handle_set(self): """4 set: Skriv över element med ny data som finns på index. Om index inte finns lyft exception.""" value = (input("New value: ")) try: i = int(input("At index: ")) self.uol.set(value, i) except (ValueError, WrongType): print("Not an integer, try again!") except AttError: print("No element at that index.") def handle_size(self): """5 size: Returnera antaler element i listan.""" print('Number of elements in list: {}'.\ format(self.uol.size())) def handle_get(self): """6 get: Returnera värde på index. Om index inte finns lyft exception.""" try: i = int(input('Get element at index: ')) element = self.uol.get(i) if element: print('Data at index: {}, is: {}'.\ format(i, element)) except (TypeError, ValueError): print("Not an integer, try again!") except OutOfIndex: print("Out of index.") def handle_index_of(self): """7 index_of: Om data finns i listan returnera dess index. Om nod med data inte finns lyft exception.""" value = str(input("Get index of: ")) value.strip() try: index = self.uol.index_of(value) if index: print('Your word is at index: {}'.\ format(index)) except NoValue: print('Could not find your word {}'.\ format(value)) def handle_print_list(self): """8 print_list: Skriv ut listans innehåll.""" print(self.uol.print_list()) def handle_remove(self): """9 remove: Ta bort nod med samma data. Om nod med data inte finns lyft exception.""" value = input("Remove: ") value.strip() #remove whitespace try: self.uol.remove(value) print('Removed {}'.format(value)) except NoValue: print('Could not remove {}'.\ format(value)) def handle_print_el(self): """print elements""" self.uol.print_el() def handle_insertion_sort(self): """sort with insertion_sort() function""" print(insertion_sort(self.uol)) def handle_bubble_sort(self): """sort with bubble_sort() function""" try: print(bubble_sort(self.uol)) except WrongType: return print("Can't sort string.") except NoValue: return print("There are no elements to sort.") def handle_input(self, choice): """handle user input""" if choice == "1": self.handle_is_empty() elif choice == "2": self.handle_add() elif choice == "3": self.handle_insert() elif choice == "4": self.handle_set() elif choice == "5": self.handle_size() elif choice == "6": self.handle_get() elif choice == "7": self.handle_index_of() elif choice == "8": self.handle_print_list() elif choice == "9": self.handle_remove() elif choice == "10": self.handle_insertion_sort() elif choice == "11": self.handle_bubble_sort() else: print("invalid choice.") def start(self): """eternal loop for menu until user shoose q or keyinterrupt""" while True: print("--------------------------\n" + "1) check if list is empty. Return True/False.\n" + "2) add a new value to the end of the list.\n" + "3) insert new value at a specific index.\n" + "4) replace a value at a specific index.\n" + "5) get number of values in list.\n" + "6) get value at a specific index.\n" + "7) get index of specific value.\n" + "8) print the entire list.\n" + "9) remove node with your value.\n" + "10) sort list using insert_sort.\n" + "11) sort list using bubble.\n" + "q) quit.\n") choice = (input("--> ")) if choice == "q": break elif choice is None: print("please enter your choice") continue elif choice == "p": self.handle_print_el() else: self.handle_input(choice)
class Handler: """ Handler class """ def __init__(self): """ Initialize Unordered List class and start""" self.my_list = UnorderedList() self.start() def start(self): """ Start """ while True: print(""" [1] Check if the list is empty [2] Add value last in list [3] Insert value at a specific index [4] Replace value at a specific index [5] Show size of list [6] Show value at a specific index [7] Show index of a specific value [8] Show all values in the list [9] Remove node that match value [exit] Exit """) val = input('Choice: \n>>> ') if val == "1": print("\nCheck if the list is empty:") print(self.my_list.is_empty()) elif val == "2": print("\nAdd value last in list:") data = input("Value: ") self.my_list.add(data) elif val == "3": print("\nInsert value at a specific index:") index = input("Index: ") data = input("Value: ") try: self.my_list.insert(int(index), data) except IndexErrorException as e: print(e) elif val == "4": print("\nReplace value at a specific index:") index = input("Index: ") data = input("Value: ") try: self.my_list.set(int(index), data) except IndexErrorException as e: print(e) elif val == "5": print("\nShow size of list:") res = self.my_list.size() print(res) elif val == "6": print("\nShow value at a specific index:") data = input("Index: ") try: res = self.my_list.get(int(data)) print(res) except IndexErrorException as e: print(e) elif val == "7": print("\nShow index by value:") data = input("Value: ") try: res = self.my_list.index_of(data) print(res) except ValueErrorException as e: print(e) elif val == "8": print("\nShow all values in the list:") self.my_list.print_list() print(" ") elif val == "9": print("\nRemove node by value:") data = input("Value: ") try: self.my_list.remove(data) except ValueErrorException as e: print(e) elif val == "exit": sys.exit() else: print("No choice that match! try again")