Ejemplo n.º 1
0
    def test_pop(self):
        ul = UnorderedList()

        # It's not possible to pop from empty lists
        self.assertRaises(EmptyList, ul.pop)
        ul.add(1)
        self.assertRaises(IndexError, ul.pop, -1)  # Index out of range
        self.assertRaises(IndexError, ul.pop, 200)  # Index out of range
        self.assertListEqual(ul._vals(), [1])

        ul.add(2)
        ul.add(3)
        ul.add(4)
        ul.add(5)

        self.assertEqual(ul.index(5), 0)
        self.assertListEqual(ul._vals(), [5, 4, 3, 2, 1])
        self.assertEqual(ul.pop(4), 1)  # Pop last item using index
        self.assertListEqual(ul._vals(), [5, 4, 3, 2])
        self.assertEqual(ul.pop(0), 5)  # Pop first
        self.assertListEqual(ul._vals(), [4, 3, 2])
        self.assertEqual(ul.pop(), 2)
        self.assertEqual(ul.pop(0), 4)
        self.assertListEqual(ul._vals(), [3])

        self.assertEqual(ul.pop(), 3)   # Pop last item without index
        self.assertEqual(ul.size(), 0)
        ul.add(1)
        self.assertEqual(ul.pop(0), 1)  # Pop last item using index

        ul.add(1)
        ul.add(2)
        ul.add(3)

        self.assertEqual(ul.pop(1), 2)
Ejemplo n.º 2
0
 def test_index(self):
     ul = UnorderedList()
     self.assertRaises(EmptyList, ul.index, 2)
     ul.add(5)
     ul.add(4)
     ul.add(5)
     ul.add(6)
     self.assertRaises(IndexError, ul.index, 8)
     self.assertEqual(ul.index(5), 1)
Ejemplo n.º 3
0
	def test_index_not_existing(self):
		l = UnorderedList()
		l.add(3)
		self.assertFalse(l.index(4))
Ejemplo n.º 4
0
	def test_index(self):
		l = UnorderedList()
		l.add(3)
		l.add(4)
		self.assertEqual(l.index(3), 0)
Ejemplo n.º 5
0
class UnorderedListTests(unittest.TestCase):
    def setUp(self):
        self.ul = UnorderedList()
    
    def test_init(self):
        self.assertEqual(self.ul.head, None)

    def test_is_empty(self):
        self.assertTrue(self.ul.is_empty())
        self.ul.add("hej")
        self.assertFalse(self.ul.is_empty())

    def test_add(self):
        self.ul.add("hello")
        self.assertTrue(self.ul.head.data == "hello")

    def test_size(self):
        self.ul.add("hello")
        self.ul.add("hej")
        self.assertEqual(self.ul.size(), 2)

    def test_search(self):
        self.ul.add("hello")
        self.ul.add("hej")
        self.assertTrue(self.ul.search("hello"))
        self.assertFalse(self.ul.search("FinnsEj"))

    def test_remove(self):
        self.ul.add("hej")
        self.ul.remove("hej")
        self.assertEqual(self.ul.head, None)
        self.assertRaises(ValueError, self.ul.remove, "NotAdded")
        self.assertRaises(ValueError, self.ul.remove, 1)
        self.ul.add("LA")
        self.ul.add("NY")
        self.ul.remove("LA")
        self.assertEqual(self.ul.head.next, None)
        self.assertEqual(self.ul.head.data, "NY")
        self.ul.add("LA")
        self.ul.add("Seattle")
        self.ul.add("Orlando")
        self.ul.append("Soledad")
        self.assertRaises(ValueError, self.ul.remove, "NotAdded")
        self.ul.remove("Seattle")
        self.assertEqual(self.ul.head.next.next.data, "NY")
        self.ul.remove("Orlando")
        self.assertRaises(ValueError, self.ul.remove, "NotAdded")


    def test_append(self):
        self.ul.append("first")
        self.ul.append("second")
        self.ul.append("last")
        self.assertTrue(self.ul.head.next.next.data, "last")

    def test_insert(self):
        self.ul.insert("first")
        self.assertEqual(self.ul.head.next, None)
        self.assertRaises(ValueError, self.ul.insert, 0, 100)
        self.ul.add("firstadd")
        self.ul.add("secondadd")
        self.ul.insert("in between?",1)
        self.ul.insert("last", 3)
        self.assertEqual(self.ul.head.next.data, "in between?")

    def test_index(self):
        self.ul.add("firstadd")
        self.ul.add("secondadd")
        self.ul.insert("in between?",1)
        self.assertTrue(self.ul.index("in between?"), 1)
        self.assertRaises(ValueError, self.ul.index, "NoneExisting")

    def test_pop(self):
        self.assertRaises(ValueError, self.ul.pop, None)  # Nothing to pop
        self.ul.add(1)
        var = self.ul.pop(0)
        self.ul.add(1)
        var2 = self.ul.pop(0)
        self.assertEqual(1, var)
        self.assertEqual(1, var2)
        self.assertTrue(self.ul.is_empty())
        self.ul.add("hej")
        self.ul.add("hejda")
        self.ul.add("tjena")
        var3 = self.ul.pop(1)
        self.assertEqual("hejda", var3)
        self.assertTrue(self.ul.head.next.data == "hej")
        self.assertRaises(ValueError, self.ul.pop, 100)
        self.ul.pop()
        self.assertEqual(self.ul.head.data, "tjena")
        ins = UnorderedList()
        ins.append("first")
        ins.append("second")
        ins.append("last")
        ins.append("*last")
        ins.pop(3)
        self.assertEqual(ins.head.next.next.data, "last")
        ins.pop()
        self.assertEqual(ins.head.next.data, "second")
        ins.pop(1)
        self.assertEqual(ins.head.data, "first")
        ins.append("yes")
        ins.pop(0)
        self.assertEqual(ins.head.data, "yes")
        self.assertRaises(ValueError, ins.pop, 100)
Ejemplo n.º 6
0
class UnorderedListTests(unittest.TestCase):

    def setUp(self):
        self.ul = UnorderedList()

    def test_is_empty(self):
        self.assertTrue(self.ul.is_empty())
        self.ul.add('test')
        self.assertFalse(self.ul.is_empty())

    def test_add(self):
        self.ul.add('a')
        self.ul.add('b')
        self.assertEqual(self.ul.index('b'), 0)
        self.assertEqual(self.ul.size(), 2)

    def test_size(self):
        self.assertEqual(self.ul.size(), 0)
        self.ul.add('a')
        self.assertEqual(self.ul.size(), 1)
        self.ul.add('b')
        self.assertEqual(self.ul.size(), 2)

    def test_search(self):
        self.ul.add(1)
        self.assertTrue(self.ul.search(1))
        self.assertFalse(self.ul.search('1'))

    def test_remove(self):
        self.ul.remove('test')
        self.ul.add('a')
        self.ul.add('b')
        self.ul.add('c')
        self.ul.remove('test')
        self.ul.remove('b')
        self.assertEqual(self.ul.size(), 2)
        self.ul.add('b')
        self.ul.remove('a')
        self.assertEqual(self.ul.size(), 2)
        self.ul.remove('test')
        self.ul.remove('b')
        self.ul.remove('c')
        self.ul.remove('test')


    def test_append(self):
        self.ul.append('a')
        self.ul.add('b')
        self.ul.add('c')
        self.ul.append('d')
        self.assertEqual(self.ul.index('d'), 3)
        self.ul.append('e')
        self.assertEqual(self.ul.index('e'), 4)

    def test_insert(self):
        self.ul.insert(5, 'test')
        self.ul.insert(0, 'd')
        self.ul.add('b')
        self.ul.add('a')
        self.ul.insert(2, 'c')
        order = [self.ul.pop(), self.ul.pop(), self.ul.pop()]
        self.assertEqual(order, ['a', 'b', 'c'])

    def test_index(self):
        self.assertRaises(EmptyList, self.ul.index, 'test')
        self.ul.add('a')
        self.ul.add('b')
        self.ul.add('c')
        self.assertEqual(self.ul.index('c'), 0)
        self.assertEqual(self.ul.index('b'), 1)
        self.assertEqual(self.ul.index('a'), 2)

    def test_pop(self):
        self.assertRaises(EmptyList, self.ul.pop)
        self.assertRaises(EmptyList, self.ul.pop, 2)
        self.ul.add('test')
        self.assertRaises(IndexError, self.ul.pop, 2)
        self.ul.remove('test')
        self.ul.add('a')
        self.ul.add('b')
        self.ul.add('c')
        self.ul.add('d')
        self.assertEqual(self.ul.pop(), 'd')
        self.assertEqual(self.ul.pop(1), 'b')
Ejemplo n.º 7
0
class UnorderedListTests(unittest.TestCase):
    def setUp(self):
        self.ul = UnorderedList()

    def test_is_empty(self):
        self.assertTrue(self.ul.is_empty())
        self.ul.add('test')
        self.assertFalse(self.ul.is_empty())

    def test_add(self):
        self.ul.add('a')
        self.ul.add('b')
        self.assertEqual(self.ul.index('b'), 0)
        self.assertEqual(self.ul.size(), 2)

    def test_size(self):
        self.assertEqual(self.ul.size(), 0)
        self.ul.add('a')
        self.assertEqual(self.ul.size(), 1)
        self.ul.add('b')
        self.assertEqual(self.ul.size(), 2)

    def test_search(self):
        self.ul.add(1)
        self.assertTrue(self.ul.search(1))
        self.assertFalse(self.ul.search('1'))

    def test_remove(self):
        self.ul.remove('test')
        self.ul.add('a')
        self.ul.add('b')
        self.ul.add('c')
        self.ul.remove('test')
        self.ul.remove('b')
        self.assertEqual(self.ul.size(), 2)
        self.ul.add('b')
        self.ul.remove('a')
        self.assertEqual(self.ul.size(), 2)
        self.ul.remove('test')
        self.ul.remove('b')
        self.ul.remove('c')
        self.ul.remove('test')

    def test_append(self):
        self.ul.append('a')
        self.ul.add('b')
        self.ul.add('c')
        self.ul.append('d')
        self.assertEqual(self.ul.index('d'), 3)
        self.ul.append('e')
        self.assertEqual(self.ul.index('e'), 4)

    def test_insert(self):
        self.ul.insert(5, 'test')
        self.ul.insert(0, 'd')
        self.ul.add('b')
        self.ul.add('a')
        self.ul.insert(2, 'c')
        order = [self.ul.pop(), self.ul.pop(), self.ul.pop()]
        self.assertEqual(order, ['a', 'b', 'c'])

    def test_index(self):
        self.assertRaises(EmptyList, self.ul.index, 'test')
        self.ul.add('a')
        self.ul.add('b')
        self.ul.add('c')
        self.assertEqual(self.ul.index('c'), 0)
        self.assertEqual(self.ul.index('b'), 1)
        self.assertEqual(self.ul.index('a'), 2)

    def test_pop(self):
        self.assertRaises(EmptyList, self.ul.pop)
        self.assertRaises(EmptyList, self.ul.pop, 2)
        self.ul.add('test')
        self.assertRaises(IndexError, self.ul.pop, 2)
        self.ul.remove('test')
        self.ul.add('a')
        self.ul.add('b')
        self.ul.add('c')
        self.ul.add('d')
        self.assertEqual(self.ul.pop(), 'd')
        self.assertEqual(self.ul.pop(1), 'b')
Ejemplo n.º 8
0
class UnorderedListTests(unittest.TestCase):
    def setUp(self):
        self.ul = UnorderedList()

    def test_init(self):
        self.assertEqual(self.ul.head, None)

    def test_is_empty(self):
        self.assertTrue(self.ul.is_empty())
        self.ul.add("hej")
        self.assertFalse(self.ul.is_empty())

    def test_add(self):
        self.ul.add("hello")
        self.assertTrue(self.ul.head.data == "hello")

    def test_size(self):
        self.ul.add("hello")
        self.ul.add("hej")
        self.assertEqual(self.ul.size(), 2)

    def test_search(self):
        self.ul.add("hello")
        self.ul.add("hej")
        self.assertTrue(self.ul.search("hello"))
        self.assertFalse(self.ul.search("FinnsEj"))

    def test_remove(self):
        self.ul.add("hej")
        self.ul.remove("hej")
        self.assertEqual(self.ul.head, None)
        self.assertRaises(ValueError, self.ul.remove, "NotAdded")
        self.assertRaises(ValueError, self.ul.remove, 1)
        self.ul.add("LA")
        self.ul.add("NY")
        self.ul.remove("LA")
        self.assertEqual(self.ul.head.next, None)
        self.assertEqual(self.ul.head.data, "NY")
        self.ul.add("LA")
        self.ul.add("Seattle")
        self.ul.add("Orlando")
        self.ul.append("Soledad")
        self.assertRaises(ValueError, self.ul.remove, "NotAdded")
        self.ul.remove("Seattle")
        self.assertEqual(self.ul.head.next.next.data, "NY")
        self.ul.remove("Orlando")
        self.assertRaises(ValueError, self.ul.remove, "NotAdded")

    def test_append(self):
        self.ul.append("first")
        self.ul.append("second")
        self.ul.append("last")
        self.assertTrue(self.ul.head.next.next.data, "last")

    def test_insert(self):
        self.ul.insert("first")
        self.assertEqual(self.ul.head.next, None)
        self.assertRaises(ValueError, self.ul.insert, 0, 100)
        self.ul.add("firstadd")
        self.ul.add("secondadd")
        self.ul.insert("in between?", 1)
        self.ul.insert("last", 3)
        self.assertEqual(self.ul.head.next.data, "in between?")

    def test_index(self):
        self.ul.add("firstadd")
        self.ul.add("secondadd")
        self.ul.insert("in between?", 1)
        self.assertTrue(self.ul.index("in between?"), 1)
        self.assertRaises(ValueError, self.ul.index, "NoneExisting")

    def test_pop(self):
        self.assertRaises(ValueError, self.ul.pop, None)  # Nothing to pop
        self.ul.add(1)
        var = self.ul.pop(0)
        self.ul.add(1)
        var2 = self.ul.pop(0)
        self.assertEqual(1, var)
        self.assertEqual(1, var2)
        self.assertTrue(self.ul.is_empty())
        self.ul.add("hej")
        self.ul.add("hejda")
        self.ul.add("tjena")
        var3 = self.ul.pop(1)
        self.assertEqual("hejda", var3)
        self.assertTrue(self.ul.head.next.data == "hej")
        self.assertRaises(ValueError, self.ul.pop, 100)
        self.ul.pop()
        self.assertEqual(self.ul.head.data, "tjena")
        ins = UnorderedList()
        ins.append("first")
        ins.append("second")
        ins.append("last")
        ins.append("*last")
        ins.pop(3)
        self.assertEqual(ins.head.next.next.data, "last")
        ins.pop()
        self.assertEqual(ins.head.next.data, "second")
        ins.pop(1)
        self.assertEqual(ins.head.data, "first")
        ins.append("yes")
        ins.pop(0)
        self.assertEqual(ins.head.data, "yes")
        self.assertRaises(ValueError, ins.pop, 100)