def test_list_with_random_numbers(helper): # test add_end() and remove_end() lst = helper.get_list(length=100) llist = LinkedList() for i in lst: llist.add_end(i) assert len(llist) == len(lst) assert llist._head.get_data() == lst[0] assert not llist.is_empty() for _ in range(len(lst)): assert llist[0] == lst[0] lst.pop() llist.remove_end() assert len(llist) == 0 assert llist.is_empty() # test add_front() and remove_front() lst = helper.get_list(length=100) for i in lst: llist.add_front(i) assert len(llist) == len(lst) assert llist._head.get_data() == lst[-1] assert not llist.is_empty() for _ in range(len(lst)): assert llist[0] == lst[-1] lst.pop() llist.remove_front() assert len(llist) == 0 assert llist.is_empty()
def test_list_with_same_value(helper): length = helper.get_pos_int() val = helper.get_value() ll = LinkedList() # test add_end for _ in range(length): ll.add_end(val) # test add_front for _ in range(length): ll.add_front(val) # test __setitem__() ll[1] = val assert ll == ll.reverse() assert ll == ll.copy() assert not ll.is_empty() assert len(ll) == 2 * length assert ll.count(val) == 2 * length assert ll.to_list() == [val] * (2 * length) # test split left_list, right_list = ll.split(length) assert len(left_list) == len(right_list) == length # test clear left_list.clear() right_list.clear() assert len(left_list) == len(right_list) == 0 # test remove for i in range(length): if i > length // 2: ll.remove_end() else: ll.remove_front() assert len(ll) == ll.count(val) == length ll.remove(val, all=True) assert ll.is_empty() assert len(ll) == 0
def test_split_method(helper): lst = helper.get_list(length=100) ll = LinkedList(lst) for i in range(len(lst)): # test left list left_list, right_list = ll.split(i) assert isinstance(left_list, LinkedList) assert left_list.to_list() == lst[:i] assert left_list.copy().to_list() == lst[:i] assert left_list.reverse().to_list() == lst[:i][::-1] # test right list assert isinstance(right_list, LinkedList) assert right_list.to_list() == lst[i:] assert right_list.copy().to_list() == lst[i:] assert right_list.reverse().to_list() == lst[i:][::-1] ll.add_front(0) ll.add_end("apple") assert ll._length == len(ll) == len(lst) + 2 assert ll.to_list() == [0] + lst + ["apple"]
def test_creating_linked_list_from_constructor(helper): # Using constructor val = helper.get_value() ll = LinkedList() ll.add_front(val) assert isinstance(ll._head, Node) assert ll._head.get_data() == val assert ll._head.get_next() == ll._head._next is None assert len(ll) == ll._length == 1 assert ll.to_list() == [item for item in ll] == [val] # Using Node val = helper.get_value() ll = LinkedList() ll.add_end(val) assert isinstance(ll._head, Node) ll._head.get_data() == val ll._head.get_next() == ll._head._next is None assert len(ll) == ll._length == 1 assert ll.to_list() == [item for item in ll] == [val]
def test_list_with_one_element(helper): val = helper.get_value() ll = LinkedList() ll.insert(0, val) assert isinstance(ll._head, Node) assert ll._head.get_data() == val assert ll._head.get_next() is None assert len(ll) == 1 assert not ll.is_empty() assert val in ll assert [item for item in ll] == [val] assert ll.to_list() == [val] assert ll == ll.copy() assert ll == ll.reverse() # ==================== test rotate ==================== assert ll == ll.rotate_left(helper.get_pos_int(), inplace=False) assert ll == ll.rotate_right(helper.get_pos_int(), inplace=False) # ==================== test operators ==================== assert ll != LinkedList() assert ll > LinkedList() assert ll >= LinkedList() assert LinkedList() < ll assert LinkedList() <= ll # ==================== test add/remove ==================== new_value = helper.get_value() ll.add_front(new_value) ll.remove_front() ll.add_end(new_value) ll.remove_end() assert ll == LinkedList([val]) # ==================== test insert/split ==================== with pytest.raises(IndexError): ll.insert(2, helper.get_value()) with pytest.raises(IndexError): ll.insert(-1, helper.get_value()) with pytest.raises(IndexError): ll.split(helper.get_pos_int(a=2))
def test_list_with_known_values(): ll = LinkedList() ll.add_front(10) ll.add_front(5) assert ll.to_list() == [5, 10] ll.remove(20) ll.remove_front() assert ll == LinkedList([10]) ll.remove_end() assert ll == LinkedList() ll.insert(0, 100) ll.insert(1, 200) ll.insert(1, 100) assert 100 in ll and 200 in ll assert ll == LinkedList([100, 100, 200]) assert ll.copy().to_list() == [100, 100, 200] assert ll.reverse() == LinkedList([200, 100, 100]) ll.remove(100) rev = ll.reverse() assert ll == rev == LinkedList([200]) ll.clear() assert not rev.is_empty() assert ll.is_empty() # ======================================== ll = LinkedList() ll.add_front(6) ll.add_end(20) ll.insert(1, 10) ll.insert(2, 77) ll.insert(4, 43) ll.insert(0, 2) assert 43 in ll assert ll[1:4].to_list() == [6, 10, 77] assert ll.copy().to_list() == [2, 6, 10, 77, 20, 43] del ll[len(ll) - 1] assert ll.reverse().to_list() == [20, 77, 10, 6, 2] assert ll._length == len(ll) == 5 ll.clear() assert ll.is_empty()