def test_a_little_bigger(): a = ArrayList() base = ['3' for _ in range(15)] for _ in range(15): a.append(3) assert str(a) == ', '.join(base)
def test_long_insertion(): a = ArrayList() base = ['3' for _ in range(1000)] for _ in range(1000): a.append(3) assert str(a) == ', '.join(base)
def test_delete_1(self): L = ArrayList() L.append( 1 ) L.append( (1, 2, 3) ) L.append( (4, 5) ) del L[0] assert np.allclose(L[0], [1,2,3]) assert np.allclose(L[1], [4,5])
def test_append(self): example_int = ArrayList(1, 2, 3) example_float = ArrayList(1., 2, 3) example_str = ArrayList("1", "2", "3") example_int.append(4) example_float.append(4.) example_str.append("4") self.assertEqual(example_int[3], 4) self.assertEqual(example_float[3], 4) self.assertEqual(example_str[3], "4")
l = [] t = time.clock() for i in range(n): l.append(i) t0 = time.clock() -t L = np.ones(0) t = time.clock() for i in range(n): L = np.append(L,i) t1 = time.clock() -t a = ArrayList(dtype=int) t = time.clock() for i in range(n): a.append(i) t2 = time.clock() -t a = ArrayList(dtype=int) t = time.clock() for i in range(n//1000): a.append(i+np.arange(1000),1) t3 = time.clock() -t a = ArrayList(dtype=int) t = time.clock() a.append(i+np.arange(n),1) t4 = time.clock() -t print("Python list: %.3fs" % (t0)) print("Numpy array: %.3fs" % (t1))
class TestArrayList(TestCase): def setUp(self): self.al = ArrayList() def test_append__when_list_is_empty__should_add_to_then_end(self): self.al.append(1) values = list(self.al) self.assertEqual([1], values) def test_append__to_return(self): result = self.al.append(1) self.assertEqual(self.al, result) def test_append__when_list_is_not_empty__should_add_to_then_end(self): self.al.append(1) self.al.append(2) self.al.append(3) values = list(self.al) self.assertEqual([1, 2, 3], values) def test_append__1024_values_expect_append__to_the_end(self): values = [x for x in range(1024)] [self.al.append(x) for x in values] list_values = list(self.al) self.assertEqual(values, list_values) def test_remove__when_index_is_valid__expect_remove_values_and_return_it( self): self.al.append(1) self.al.append(2) self.al.append(33) self.al.append(4) result = self.al.remove(2) self.assertEqual([1, 2, 4], list(self.al)) self.assertEqual(33, result) def test_remove__when_index_is_invalid__expect_remove_values_and_raise( self): self.al.append(1) self.al.append(2) self.al.append(3) self.al.append(4) with self.assertRaises(IndexError): self.al.remove(self.al.size()) def test_get__when_index_is_valid__expect_remove_values_and_return_it( self): self.al.append(1) self.al.append(2) self.al.append(33) self.al.append(4) result = self.al.get(2) self.assertEqual(33, result) def test_get__when_index_is_invalid__expect_remove_values_and_raise(self): self.al.append(1) self.al.append(2) self.al.append(3) self.al.append(4) with self.assertRaises(IndexError): self.al.get(self.al.size()) def test_extend_with_empty_iterable_expect_to_be_same(self): self.al.append(1) self.al.extend([]) self.assertEqual([1], list(self.al)) def test_extend__when_empty_with_not_empty_iterable__expect_to_be_same( self): self.al.extend([1]) self.assertEqual([1], list(self.al)) def test_extend_with_ist_expect_to_append_the_list(self): self.al.append(1) self.al.extend([2]) self.assertEqual([1, 2], list(self.al)) def test_extend_with_generator_expect_to_append_the_list(self): self.al.append(1) self.al.extend([x for x in range(1)]) self.assertEqual([1, 0], list(self.al)) def test_extend__with_not_iterable_expect_to_raise_valueError(self): self.al.append(1) with self.assertRaises(ValueError): self.al.extend(2) def test_insert__when_index_is_valid__expect_insert_values_at_index(self): self.al.append(1) self.al.append(2) self.al.append(4) self.al.append(5) self.al.append(6) self.al.append(7) self.al.append(8) self.al.append(9) self.al.insert(2, 33) self.assertEqual([1, 2, 33, 4, 5, 6, 7, 8, 9], list(self.al)) def test_insert__when_index_is_invalid__expect_insert_values_and_raise( self): self.al.append(1) self.al.append(2) self.al.append(4) with self.assertRaises(IndexError): self.al.insert(self.al.size() + 1, 2) def test_pop__expect_to_remove_last_element_and_remove_it(self): self.al.append(1) self.al.append(2) self.al.append(3) self.al.append(4) result = self.al.pop() self.assertEqual(4, result) self.assertListEqual([1, 2, 3], list(self.al)) def test_pop__when_empty_expect_raise(self): with self.assertRaises(IndexError): self.al.pop() def test_clear_expect_to_be_empy(self): [self.al.append(x) for x in range(15)] self.al.clear() self.assertEqual([], list(self.al)) def test_index__when_item_is_present__expect_to_return_correct_index(self): [self.al.append(x) for x in range(15)] index = self.al.index(5) self.assertEqual(5, index) def test_index__when_item_is_not_present__expect_to_raise(self): [self.al.append(x) for x in range(15)] with self.assertRaises(ValueError): self.al.index(17) def test_count__when_item_is_present__one_time_expect_to_return_1(self): [self.al.append(x) for x in range(15)] expected_count = 1 actual_count = self.al.count(5) self.assertEqual(expected_count, actual_count) def test_count__when_item_is_not_present__one_time_expect_to_return_0( self): [self.al.append(x) for x in range(15)] expected_count = 0 actual_count = self.al.count(55) self.assertEqual(expected_count, actual_count) def test_count__when_item_is_present__multiple_times_expect_to_return_correct_count( self): [self.al.append(x) for x in range(15)] self.al.append(5) self.al.insert(3, 5) self.al.insert(7, 5) self.al.insert(1, 5) self.al.insert(9, 5) expected_count = 6 actual_count = self.al.count(5) self.assertEqual(expected_count, actual_count) def test_count__when_item_is_present__multiple_times_and_once_poped_expect_to_return_correct_count( self): [self.al.append(x) for x in range(15)] self.al.insert(3, 5) self.al.insert(7, 5) self.al.insert(1, 5) self.al.insert(9, 5) self.al.append(5) self.al.pop() expected_count = 5 actual_count = self.al.count(5) self.assertEqual(expected_count, actual_count) def test_reverse__expect_in_reversed_order(self): [self.al.append(x) for x in range(5)] expected = [x for x in range(4, -1, -1)] actual = self.al.revers() self.assertEqual(expected, actual) def test_copy__expect_in_return_an_other_list_with_same_values(self): [self.al.append(x) for x in range(5)] copied_list = self.al.copy() expected_result = [x for x in range(5)] actual_result = list(copied_list) self.assertNotEqual(copied_list, self.al) self.assertEqual(expected_result, actual_result) def test_add_first__when_empty__expect_to_add(self): self.al.add_first(1) self.assertEqual([1], list(self.al)) def test_add_first__when_not_empty__expect_to_add(self): [self.al.append(x) for x in range(5)] self.al.add_first(1) self.assertEqual([1, 0, 1, 2, 3, 4], list(self.al)) def test_dictionize__when_empty__expect_empty_dict(self): actual = self.al.dictionize() expected = {} self.assertDictEqual(expected, actual) def test_dictionize__when_even_element_count__expect_empty_dict(self): self.al.append(1) self.al.append(2) self.al.append(3) self.al.append(4) actual = self.al.dictionize() expected = {1: 2, 3: 4} self.assertDictEqual(expected, actual) def test_dictionize__when_odd_element_count__expect_empty_dict(self): self.al.append(1) self.al.append(2) self.al.append(3) self.al.append(4) self.al.append(5) actual = self.al.dictionize() expected = {1: 2, 3: 4, 5: " "} self.assertDictEqual(expected, actual)
def test_setitem(self): L = ArrayList() L.append(np.arange(10), 1) L[0] = 0 assert L[0] == 0
def test_delitem_all_items_2(self): L = ArrayList() L.append(np.arange(10), 1) del L[...] assert len(L) == 0
def test_append_3(self): L = ArrayList() L.append(np.arange(10), 1 + np.arange(4)) assert len(L) == 4 assert np.allclose(L[3], [6, 7, 8, 9])
def test_insert_3(self): L = ArrayList() L.append( 1 ) L.insert( 0, np.arange(10), 1+np.arange(4) ) assert len(L) == 5 assert np.allclose(L[3], [6,7,8,9])
def test_append_1(self): L = ArrayList() L.append(1) assert L[0] == 1
def test_insert_1(self): L = ArrayList() L.append( 1 ) L.insert( 0, 2 ) assert len(L) == 2 assert L[0] == 2
def test_insert_2(self): L = ArrayList() L.append( 1 ) L.insert( 0, np.arange(10), 2) assert len(L) == 6 assert np.allclose(L[4],[8,9])
def test_append_3(self): L = ArrayList() L.append( np.arange(10), 1+np.arange(4) ) assert len(L) == 4 assert np.allclose(L[3], [6,7,8,9])
def test_append_2(self): L = ArrayList() L.append( np.arange(10), 2) assert len(L) == 5 assert np.allclose(L[4],[8,9])
def test_append_1(self): L = ArrayList() L.append( 1 ) assert L[0] == 1
def test_delitem_many_items(self): L = ArrayList() L.append(np.arange(10), 1) del L[1:] assert len(L) == 1 assert np.allclose(L[0], 0)
def test_insert_2(self): L = ArrayList() L.append(1) L.insert(0, np.arange(10), 2) assert len(L) == 6 assert np.allclose(L[4], [8, 9])
def test_insert_1(self): L = ArrayList() L.append(0) L.append([[1,2],[3,4,5]]) assert len(L) == 3 assert np.allclose(L[1], [1,2])
def test_append_2(self): L = ArrayList() L.append(np.arange(10), 2) assert len(L) == 5 assert np.allclose(L[4], [8, 9])
def test_insert_1(self): L = ArrayList() L.append(0) L.append([[1, 2], [3, 4, 5]]) assert len(L) == 3 assert np.allclose(L[1], [1, 2])
def test_insert_1(self): L = ArrayList() L.append(1) L.insert(0, 2) assert len(L) == 2 assert L[0] == 2
def test_repeated_insertion(): a = ArrayList() for _ in range(10): a.append(0) assert str(a) == '0, 0, 0, 0, 0, 0, 0, 0, 0, 0'
def test_insert_4(self): L = ArrayList() L.append(1) L.insert(-1, np.arange(10), 1 + np.arange(4)) assert len(L) == 5 assert np.allclose(L[3], [6, 7, 8, 9])
def test_delete_2(self): L = ArrayList() L.append(np.arange(10), 1) del L[:-1] assert np.allclose(L[0], [9])
def test_delete_3(self): L = ArrayList() L.append(np.arange(10), 1) del L[1:] assert len(L) == 1 assert np.allclose(L[0], 0)
def test_delete_4(self): L = ArrayList() L.append(np.arange(10), 1) del L[:] assert len(L) == 0
import time from array_list import ArrayList n = 100000 L = [] start = time.clock() for i in range(n): L.append(i) stop = time.clock() print("Python list, append %d items: %.5f" % (n, (stop - start))) L = ArrayList(dtype=int) start = time.clock() for i in range(n): L.append(i) stop = time.clock() print("Array list, append %d items: %.5f" % (n, (stop - start))) L = ArrayList(dtype=int) start = time.clock() L.append(range(n), 1) stop = time.clock() print("Array list, append %d items at once: %.5f" % (n, (stop - start))) L = [] start = time.clock() for i in range(n): L.insert(0, i) stop = time.clock() print("Python list, prepend %d items: %.5f" % (n, (stop - start)))