def test_free_tests(self, n_nodes): print("\n ** FREE TESTS **") """Actions: add, search and remove N (define below) items Expected result: Find all items and have empty list "()" """ N = n_nodes obj = SingleLinkedList() obj_dataset = set() while len(obj_dataset) < N: obj_dataset.add(randint(0,N)) # this approach is not good, but works for simple case. obj_data = [e for e in obj_dataset] print("TEST DATA : Input {}".format(obj_data)) shuffle(obj_data) for d in obj_data: obj.insert(d) #print("After inserting {}, we got {}".format(d, obj)) print("Resulted {}: {}".format(type(obj), obj)) shuffle(obj_data) for d in obj_data: temp_obj = obj.search(d) if temp_obj is None: print("FAILED to find existing item {}".format(d)) obj_data = sorted(obj_data) for i in range(0, len(obj_data)-1): temp_obj = obj.successor(obj_data[i]) if temp_obj != obj_data[i+1]: print("FAILED to find sucessor item {}".format(d)) obj_data = sorted(obj_data, reverse=True) for i in range(0, len(obj_data)-1): temp_obj = obj.predecessor(obj_data[i]) if temp_obj != obj_data[i+1]: print("FAILED to find predecessor item {}".format(d)) shuffle(obj_data) for d in obj_data: result = obj.delete(d) #print("After deleting {}, we got {}".format(d, obj)) if result is None: print("FAILED : could not delete {}.".format(d)) if str(obj) != "()": print("FAILED : obj is not empty after deleting all nodes. Got {}".format(obj)) elif len(obj) != 0: print("FAILED : obj is not empty after deleting all nodes. Tree is has {} nodes".format(len(obj))) else: print("INSERTED, SEARCHED, SUCCESSOR, PREDECESSOR, DELETED {} items.".format(N))
def _create_obj(self,all_nodes): sl = SingleLinkedList() for node in all_nodes: sl.insert(node) return sl