def test_size(self): l = UnorderedList() self.assertEqual(l.size(), 0) l.add(7) self.assertEqual(l.size(), 1) l.add(13) self.assertEqual(l.size(), 2)
def test_is_empty(self): l = UnorderedList() self.assertTrue(l.is_empty()) l.add(7) self.assertFalse(l.is_empty()) l.add(13) self.assertFalse(l.is_empty())
def test_search(self): ul = UnorderedList() ul.add(5) ul.add(4) ul.add(6) self.assertFalse(ul.search(7)) ul.add(7) ul.add(8) self.assertTrue(ul.search(7))
def test_pop_no_position(self): l = UnorderedList() l.add(4) l.add(5) l.add(6) l.add(7) self.assertEqual(l.pop(), 4) self.assertEqual(l.pop(), 5) self.assertEqual(l.pop(), 6) self.assertEqual(l.pop(), 7)
def test_pop_one_item(self): l = UnorderedList() l.add(2) l.add(5) l.add(4) self.assertEqual(l.pop(2), 2) self.assertEqual(l.pop(0), 4)
def test_pop(self): l = UnorderedList() l.add(1) l.add(2) l.add(3) l.add(4) self.assertEqual(l.pop(1), 3) self.assertEqual(l.pop(1), 2)
def test_insert(self): l = UnorderedList() self.assertTrue(l.insert(0, 4)) l.add(5) l.add(3) self.assertTrue(l.insert(2, 5)) self.assertTrue(l.insert(0, 8)) self.assertTrue(l.search(8))
def test_append(self): ul = UnorderedList() self.assertTrue(ul.append(5)) self.assertEqual(ul.size(), 1) self.assertTrue(ul.append("a")) self.assertEqual(ul.size(), 2) self.assertTrue(ul.append('b')) self.assertTrue(ul.append('c')) self.assertTrue(ul.append('d'))
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)
def test_insert(self): ul = UnorderedList() self.assertTrue(ul.insert(0, 4)) ul.add(5) ul.add(3) self.assertTrue(ul.insert(2, 5)) self.assertTrue(ul.insert(0, 8)) self.assertTrue(ul.search(5)) self.assertRaises(IndexError, ul.insert, 8, 100)
def test_append(self): l = UnorderedList() l.add(1) l.add(2) l.append(3) self.assertEqual(l.head.next.next.data, 3)
def test_remove(self): l = UnorderedList() l.add(1) l.add(2) l.add(3) l.add(4) l.add(5) self.assertTrue(l.remove(3)) l.remove(4) l.remove(1) l.remove(5) l.remove(2) self.assertTrue(l.is_empty())
def test_search(self): l = UnorderedList() self.assertFalse(l.search(3)) l.add(2) l.add(4) self.assertTrue(l.search(2))
def test_size(self): ul = UnorderedList() ul.add(5) self.assertLess(ul.size(), 2) ul.add(8) self.assertEqual(ul.size(), 2)
def test_index_not_existing(self): l = UnorderedList() l.add(3) self.assertFalse(l.index(4))
def test_is_empty(self): ul = UnorderedList() self.assertTrue(ul.is_empty()) ul.add(4) self.assertFalse(ul.is_empty())
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)
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')
def setUp(self): self.ul = UnorderedList()
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)
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)
def test_index(self): l = UnorderedList() l.add(3) l.add(4) self.assertEqual(l.index(3), 0)
def test_pop_empty_list(self): l = UnorderedList() self.assertRaises(EmptyList, l.pop)
def test_index_of_empty_list(self): l = UnorderedList() self.assertRaises(EmptyList, l.index, 1)
def test_pop_non_position(self): l = UnorderedList() l.add(2) self.assertRaises(IndexError, l.pop, 3)
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)
def test_remove(self): ul = UnorderedList() self.assertRaises(EmptyList, ul.remove, 5) ul.add(5) ul.add(6) self.assertFalse(ul.remove(4)) self.assertTrue(ul.remove(5)) self.assertEqual(ul.size(), 1) self.assertTrue(ul.remove(6)) self.assertEqual(ul.size(), 0) ul.add(1) ul.add(2) ul.add(3) self.assertTrue(ul.remove(2))