def test_use_cache_of_0(self): """ Test using cache """ dlist = DiskList(cache_size=0) l1 = [] l2 = [] l3 = [] l4 = [] for i in range(100): dlist.append(i) l1.append(i) for i in dlist: l2.append(i) for i in dlist: l3.append(i) l4 = dlist[0:50] for i in dlist[50:]: l4.append(i) self.assertTrue( all([ i == j and j == k and k == l for i, j, k, l in zip(l1, l2, l3, l4) ]))
def test_append(self): """ Test appending new items """ dlist = DiskList() dlist.append('0') dlist.append('1') dlist.append('2') dlist.append('3') self.assertTrue(len(dlist) == 4)
def test_clear(self): """ Test the clear method """ dlist = DiskList() dlist.append('0') dlist.append('1') dlist.clear() self.assertTrue(len(dlist) == 0)
def test_index_with_bounds(self): """ Test the index method with bounds """ dlist = DiskList() dlist.append('0') dlist.append('1') dlist.append('2') dlist.append('3') self.assertRaises(ValueError, lambda: dlist.index('2', 0, 1))
def test_index(self): """ Test the index method """ dlist = DiskList() dlist.append('0') dlist.append('1') dlist.append('2') dlist.append('3') self.assertTrue(dlist.index('2') == 2)
def test_getitem(self): """ Test the [] operator for getting items """ dlist = DiskList() dlist.append('0') dlist.append('1') dlist.append('2') dlist.append('3') self.assertTrue(dlist[0] == '0' and dlist[1] == '1' and dlist[2] == '2' and dlist[-1] == '3')
def test_clear_not_empty(self): """ Test clear method """ dlist = DiskList() dlist.append('0') dlist.append('1') dlist.append('2') dlist.append('3') dlist.clear() self.assertTrue(len(dlist) == 0)
def test_count(self): """ Test count method """ dlist = DiskList() dlist.append('0') dlist.append('1') dlist.append('3') dlist.append('3') self.assertTrue( dlist.count('1') == 1 and dlist.count('3') == 2 and dlist.count('1337') == 0)
def test_insert(self): """ Test inserting new items """ dlist = DiskList() dlist.append('0') dlist.append('1') dlist.append('2') dlist.append('3') dlist.insert(1, '10') self.assertTrue(len(dlist) == 5 and dlist[1] == '10')
def test_pop_without_index(self): """ Test the pop method without index """ dlist = DiskList() dlist.append('0') dlist.append('1') dlist.append('2') dlist.append('3') result = dlist.pop() self.assertTrue(dlist[0] == '0' and dlist[1] == '1' and dlist[2] == '2' and result == '3' and len(dlist) == 3)
def test_add(self): """ Test the + operator """ dlist1 = DiskList() dlist2 = DiskList() dlist1.append('1') dlist1.append('2') dlist2.append('3') dlist2.append('4') dlist = dlist1 + dlist2 self.assertTrue(len(dlist) == 4)
def test_iteration(self): """ Test iterating through the DiskList """ dlist = DiskList() dlist.append('0') dlist.append('1') dlist.append('2') dlist.append('3') result_list = [] for item in dlist: result_list.append(item) self.assertTrue(result_list[0] == '0' and result_list[1] == '1' and result_list[2] == '2' and result_list[3] == '3')
def test_remove(self): """ Test the remove method """ dlist = DiskList() dlist.append('0') dlist.append('1') dlist.append('2') dlist.append('3') dlist.append('0') dlist.remove('0') self.assertTrue(dlist[0] == '1' and dlist[1] == '2' and dlist[2] == '3' and dlist[3] == '0')
def main(): """ Compare disklists and lists """ # Instantiation print('|---------- Instanciation ----------|') # List start = timer() for _ in range(0, 1000): l = [] end = timer() print('List: {:.10f} sec'.format((end - start) / 1000)) # DiskList start = timer() for _ in range(0, 1000): dl = DiskList() end = timer() print('DiskList: {:.10f} sec'.format((end - start) / 1000)) # Instantiation with big object print('|---------- Instanciation with big object ----------|') # List start = timer() for _ in range(0, 1000): lbo = [] end = timer() print('List: {:.10f} sec'.format((end - start) / 1000)) # DiskList start = timer() for _ in range(0, 1000): dlbo = DiskList() end = timer() print('DiskList: {:.10f} sec'.format((end - start) / 1000)) # Append print('|---------- Appending ----------|') # List start = timer() for i in range(0, 1000): l.append(i) end = timer() print('List: {:.10f} sec'.format((end - start) / 1000)) # DiskList start = timer() for i in range(0, 1000): dl.append(i) end = timer() print('DiskList: {:.10f} sec'.format((end - start) / 1000)) # Append big object print('|---------- Appending big object ----------|') # List big_obj = ["test" for _ in range(0, 1000)] start = timer() for i in range(0, 1000): lbo.append(big_obj) end = timer() print('List: {:.10f} sec'.format((end - start) / 1000)) # DiskList start = timer() for i in range(0, 1000): dlbo.append(big_obj) end = timer() print('DiskList: {:.10f} sec'.format((end - start) / 1000)) # Inserting print('|---------- Inserting ----------|') # List start = timer() for i in range(0, 1000): l.insert(i, i) end = timer() print('List: {:.10f} sec'.format((end - start) / 1000)) # DiskList start = timer() for i in range(0, 1000): dl.insert(i, i) end = timer() print('DiskList: {:.10f} sec'.format((end - start) / 1000)) # Inserting big object print('|---------- Inserting big object ----------|') # List big_obj = ["test" for _ in range(0, 1000)] start = timer() for i in range(0, 1000): lbo.insert(i, big_obj) end = timer() print('List: {:.10f} sec'.format((end - start) / 1000)) # DiskList start = timer() for i in range(0, 1000): dlbo.insert(i, big_obj) end = timer() print('DiskList: {:.10f} sec'.format((end - start) / 1000)) # Getting with index print('|------ Getting with index ------|') # List start = timer() for i in range(0, 1000): l[i] end = timer() print('List: {:.10f} sec'.format((end - start) / 1000)) # DiskList start = timer() for _ in range(0, 1000): dl[i] end = timer() print('DiskList: {:.10f} sec'.format((end - start) / 1000)) # Setting with index print('|------ Setting with index ------|') # List start = timer() for i in range(0, 1000): l[i] = i end = timer() print('List: {:.10f} sec'.format((end - start) / 1000)) # DiskList start = timer() for _ in range(0, 1000): dl[i] = i end = timer() print('DiskList: {:.10f} sec'.format((end - start) / 1000)) # Iterating print('|---------- Iterating ----------|') # List start = timer() for i in range(0, 1000): for item in l: continue end = timer() print('List: {:.10f} sec'.format((end - start) / 1000)) # DiskList start = timer() for _ in range(0, 1000): for item in dl: continue end = timer() print('DiskList without cache: {:.10f} sec'.format((end - start) / 1000)) # DiskList dl = DiskList(cache_size=256) # 256 items for i in range(0, 1000): dl.append(i) start = timer() for _ in range(0, 1000): for item in dl: continue end = timer() print('DiskList with cache: {:.10f} sec'.format((end - start) / 1000)) # Iterating with big object print('|---------- Iterating with big object ----------|') # List start = timer() for i in range(0, 1000): for item in lbo: continue end = timer() print('List: {:.10f} sec'.format((end - start) / 1000)) # DiskList start = timer() for _ in range(0, 1000): for item in dlbo: continue end = timer() print('DiskList without cache: {:.10f} sec'.format((end - start) / 1000)) # DiskList dlbo = DiskList(cache_size=256) # 256 items big_obj = ["test" for _ in range(0, 1000)] for i in range(0, 1000): dlbo.append(big_obj) start = timer() for _ in range(0, 1000): for item in dlbo: continue end = timer() print('DiskList with cache: {:.10f} sec'.format((end - start) / 1000))