Beispiel #1
0
    def test_filter(self):
        lst = UnrolledLinkedList(5).add(1).add(2).add(3).add(4).add(5)

        def is_even(n):
            return n % 2 == 0

        self.assertEqual(lst.filter(is_even).to_list(), [2, 4])
        self.assertEqual(lst.filter(lambda x: x % 2 == 0).to_list(), [2, 4])
Beispiel #2
0
 def test_remove(self):
     # self.assertEqual(UnrolledLinkedList(5).remove(1).to_list(), [])
     self.assertEqual(UnrolledLinkedList(5).add(1).remove(1).to_list(), [])
     self.assertEqual(
         UnrolledLinkedList(5).add(1).add(2).add(3).add(4).add(5).add(
             6).add(7).remove(6).to_list(), [1, 2, 3, 4, 5, 7])
     self.assertRaises(
         ValueError, lambda: UnrolledLinkedList(5).add(1).add(2).add(3).add(
             4).remove(5))
Beispiel #3
0
 def test_to_list(self):
     self.assertEqual(UnrolledLinkedList(Node()).to_list(), [])
     lst = UnrolledLinkedList(Node())
     lst.insert(0, 'a')
     self.assertEqual(lst.to_list(), ['a'])
     lst.insert(1, 2)
     self.assertEqual(lst.to_list(), ['a', 2])
Beispiel #4
0
 def test_add_to_head(self):
     lst = UnrolledLinkedList(Node())
     self.assertEqual(lst.to_list(), [])
     lst.add_to_head(1)
     self.assertEqual(lst.to_list(), [1])
     lst.add_to_head('a')
     self.assertEqual(lst.to_list(), ['a', 1])
Beispiel #5
0
 def test_size(self):
     lst = UnrolledLinkedList(Node())
     self.assertEqual(lst.size(), 0)
     lst.insert(0, 1)
     self.assertEqual(lst.size(), 1)
     lst.insert(lst.total_size, 2)
     self.assertEqual(lst.size(), 2)
Beispiel #6
0
 def test_find(self):
     x = [
         1,
         2,
         3,
     ]
     lst = UnrolledLinkedList(Node())
     lst.insert(0, 1)
     lst.insert(1, 2)
     index = lst.find(1)
     self.assertEqual(0, index)
Beispiel #7
0
    def test_reduce(self):
        lst = UnrolledLinkedList(5)
        self.assertEqual(lst.reduce(lambda state, e: state + e, 0), 0)
        self.assertEqual(lst.reduce(lambda state, e: state + e, 1), 1)

        lst = UnrolledLinkedList(5)
        lst.from_list([1, 2, 3, 4])
        self.assertEqual(lst.reduce(lambda state, e: state + e, 0), 10)
Beispiel #8
0
    def test_immutability(self, a, b, c):
        lst1 = UnrolledLinkedList(5).from_list(a)
        lst2 = UnrolledLinkedList(5).from_list(b)
        lst3 = UnrolledLinkedList(5).from_list(c)

        concat_lst1_lst2_none = lst1.mconcat(lst2.mconcat(None)).to_list()
        concat_lst1_none_lst2 = lst1.mconcat(None).mconcat(lst2).to_list()
        self.assertEqual(concat_lst1_lst2_none, concat_lst1_none_lst2)

        concat1 = lst1.mconcat(lst2.mconcat(lst3)).to_list()
        concat2 = lst1.mconcat(lst2).mconcat(lst3).to_list()
        self.assertEqual(concat1, concat2)
Beispiel #9
0
 def test_mconcat(self):
     lst1 = UnrolledLinkedList(5).add(1).add(2).add(3)
     lst2 = UnrolledLinkedList(5).add(4).add(5).add(6).add(7)
     concat = lst1.mconcat(lst2)
     lst3 = UnrolledLinkedList(5).add(1).add(2).add(3).add(4).add(5).add(
         6).add(7)
     self.assertEqual(concat.to_list(), lst3.to_list())
Beispiel #10
0
 def test_iter(self):
     lst = UnrolledLinkedList(5).add(1).add(2).add(3).add(4).add(5).add(
         6).add(7)
     lst2 = []
     for n in lst:
         try:
             lst2.append(n)
         except StopIteration:
             pass
     self.assertEqual(lst2, [1, 2, 3, 4, 5, 6, 7])
Beispiel #11
0
    def test_iter(self):
        x = [1, 2, 3]
        lst = UnrolledLinkedList(Node())
        lst.from_list(x)
        tmp = []
        i = 0
        for e in lst.head.elements:
            i += 1
            if i > len(x):
                break
            tmp.append(e)
        self.assertEqual(x, tmp)
        self.assertEqual(lst.to_list(), tmp)

        i = iter(UnrolledLinkedList())
        self.assertRaises(StopIteration, lambda: next(i))
Beispiel #12
0
 def test_from_list_to_list_equality(self, a):
     lst = UnrolledLinkedList(Node())
     lst.from_list(a)
     b = lst.to_list()
     self.assertEqual(a, b)
Beispiel #13
0
 def test_reduce(self):
     lst = UnrolledLinkedList(Node())
     self.assertEqual(lst.reduce(lambda st, e: st + e, 0), 0)
     lst = UnrolledLinkedList(Node())
     lst.from_list([1, 2, 3])
     self.assertEqual(lst.reduce(lambda a, b: a + b, 0), 6)
     test_data = [[], ['a'], ['a', 'b']]
     for e in test_data:
         lst = UnrolledLinkedList(Node())
         lst.from_list(e)
         self.assertEqual(lst.reduce(lambda st, _: st + 1, 0), lst.size())
Beispiel #14
0
    def test_map(self):
        lst = UnrolledLinkedList(Node())
        lst.map(str)
        self.assertEqual(lst.to_list(), [])

        lst1 = UnrolledLinkedList(Node())
        lst1.from_list([1, 2, 3])
        lst1.map(str)
        self.assertEqual(lst1.to_list(), ["1", "2", "3"])
Beispiel #15
0
 def test_add_to_tail(self):
     lst = UnrolledLinkedList(Node())
     self.assertEqual(lst.to_list(), [])
     lst.add_to_tail(1)
     self.assertEqual(lst.to_list(), [1])
Beispiel #16
0
 def test_set(self):
     lst = UnrolledLinkedList(5)
     self.assertRaises(IndexError, lambda: lst.set(0, 1))
     lst.add(1).add(2).add(3).add(4)
     self.assertEqual(lst.set(1, 5).to_list(), [1, 5, 3, 4])
Beispiel #17
0
 def test_from_list(self):
     test_data = [[], [1, 2], [1, 2, 3, 4, 5, 6, 7]]
     for e in test_data:
         lst = UnrolledLinkedList(5).from_list(e)
         self.assertEqual(lst.to_list(), e)
Beispiel #18
0
 def test_find(self):
     lst = UnrolledLinkedList(5)
     self.assertEqual(lst.find(2), False)
     lst = UnrolledLinkedList(5).add(1).add(2).add(3)
     self.assertEqual(lst.find(2), True)
     self.assertEqual(lst.find(4), False)
Beispiel #19
0
    def test_monoid_associativity(self, a, b, c):
        ULlsta = UnrolledLinkedList(Node())
        ULlstb = UnrolledLinkedList(Node())
        ULlstc = UnrolledLinkedList(Node())
        ULlsta.from_list(a)
        ULlstb.from_list(b)
        ULlstc.from_list(c)

        ULlst_test1 = UnrolledLinkedList(Node())
        ULlst_testb = UnrolledLinkedList(Node())
        ULlst_testb.mconcat(ULlsta, ULlstb)
        ULlst_test1.mconcat(ULlst_testb, ULlstc)

        ULlsta = UnrolledLinkedList(Node())
        ULlstb = UnrolledLinkedList(Node())
        ULlstc = UnrolledLinkedList(Node())
        ULlsta.from_list(a)
        ULlstb.from_list(b)
        ULlstc.from_list(c)

        ULlst_test2 = UnrolledLinkedList(Node())
        ULlst_testb = UnrolledLinkedList(Node())
        ULlst_testb.mconcat(ULlstb, ULlstc)
        ULlst_test2.mconcat(ULlsta, ULlst_testb)

        ULlst1 = ULlst_test1.to_list()
        ULlst2 = ULlst_test2.to_list()
        self.assertEqual(ULlst1, ULlst2)
Beispiel #20
0
    def test_monoid_identity(self, a):
        lst = UnrolledLinkedList(Node())
        lst.from_list(a)
        res = UnrolledLinkedList(Node())
        lst_mconcat = UnrolledLinkedList(Node())
        lst_mconcat.mconcat(lst, res.empty())
        self.assertEqual(lst_mconcat.to_list(), a)

        lst_mconcat = UnrolledLinkedList(Node())
        lst_mconcat.mconcat(res.empty(), lst)
        self.assertEqual(lst_mconcat.to_list(), a)
Beispiel #21
0
 def test_to_list(self):
     self.assertEqual(UnrolledLinkedList(5).to_list(), [])
     self.assertEqual(
         UnrolledLinkedList(5).add(1).add(2).add(3).to_list(), [1, 2, 3])
Beispiel #22
0
 def test_to_list2(self, a):
     lst = UnrolledLinkedList(5)
     for item in a:
         lst.add(item)
     self.assertEqual(lst.to_list(), a)
Beispiel #23
0
    def test_map(self):
        lst = UnrolledLinkedList(5)
        lst.map(str)
        self.assertEqual(lst.to_list(), [])

        lst = UnrolledLinkedList(5)
        lst.from_list([1, 2, 3])
        lst.map(str)
        self.assertEqual(lst.to_list(), ['1', '2', '3'])

        lst = UnrolledLinkedList(5)
        lst.from_list([1, 2, 3])
        lst.map(lambda x: x + 1)
        self.assertEqual(lst.to_list(), [2, 3, 4])
Beispiel #24
0
 def test_reverse2(self, a):
     x = UnrolledLinkedList(5).from_list(a).reverse()
     self.assertEqual(x.to_list(), a[::-1])
Beispiel #25
0
 def test_reverse(self):
     self.assertEqual(UnrolledLinkedList(5).reverse().to_list(), [])
     self.assertEqual(
         UnrolledLinkedList(5).add(1).add(2).add(3).reverse().to_list(),
         [3, 2, 1])
Beispiel #26
0
 def test_python_len_and_list_size_equality(self, a):
     lst = UnrolledLinkedList(Node())
     lst.from_list(a)
     self.assertEqual(lst.size(), len(a))
Beispiel #27
0
 def test_add(self):
     self.assertEqual(UnrolledLinkedList(5).add(None).to_list(), [None])
     self.assertEqual(
         UnrolledLinkedList(5).add(1).add(2).add(3).add(4).add(None).add(
             "123").to_list(), [1, 2, 3, 4, None, "123"])
Beispiel #28
0
 def test_from_list(self):
     test_data = [[], ['a'], ['a', 'b']]
     for i in test_data:
         lst = UnrolledLinkedList(Node())
         lst.from_list(i)
         self.assertEqual(lst.to_list(), i)
Beispiel #29
0
 def test_size(self):
     self.assertEqual(UnrolledLinkedList(5).size(), 0)
     self.assertEqual(UnrolledLinkedList(5).add(1).size(), 1)
     self.assertEqual(
         UnrolledLinkedList(5).add(1).add(2).add(3).add(4).add(5).add(
             6).add(7).size(), 7)
Beispiel #30
0
 def test_from_list2(self, a):
     lst1 = UnrolledLinkedList(5)
     lst2 = UnrolledLinkedList(5)
     for item in a:
         lst2.add(item)
     self.assertEqual(lst1.from_list(a).to_list(), lst2.to_list())