def test_list_subset_subslices(self): ''' This is a difficult test with many dimensions. The subindices and subsplices should all behave like normal multi-dimension lists. ''' test = [[1, 2, 3, 4, 5, 6, 7, 8], [5, 4, 3, 2, 1, 0]] wrap = listwrap.FixedListSubset(test) self.assertEqual(wrap[0, 0], 1) self.assertEqual(wrap[0, :1].compress_ranges_to_lists(), [1]) self.assertEqual(list(wrap[0, :1]), [1]) test = [[[0, 1, 2], ['a', 'b', 'c']], [[3, 4, 5]], [6, 7, 8]] wrap = listwrap.FixedListSubset(test) self.assertEqual(wrap[0, 0], [0, 1, 2]) self.assertEqual(wrap[0, 1:2, 1:3].compress_ranges_to_lists(), [['b', 'c']]) self.assertEqual(wrap[0, 1, 1:3].compress_ranges_to_lists(), ['b', 'c']) self.assertEqual(wrap[0, 0, 1:3].compress_ranges_to_lists(), [1, 2]) self.assertEqual(wrap[1, 0, 1:3].compress_ranges_to_lists(), [4, 5]) self.assertEqual(wrap[0:2, :, :2].compress_ranges_to_lists(), [[[0, 1], ['a', 'b']], [[3, 4]]]) # These access non-existent elements in the last sublist self.assertRaises(IndexError, wrap.__getitem__, [(3, 0, slice(1, 3))]) badwrap = wrap[:, :, 1:3] self.assertRaises(IndexError, badwrap.compress_ranges_to_lists)
def test_list_subset_basics(self): test = range(1, 9) wrap = listwrap.FixedListSubset(test) # Check that we can iterate over the list count = 0 for i, val in enumerate(wrap): self.assertEqual(i + 1, val) count += 1 # Ensure we saw all items in test self.assertEqual(len(test), count) # Ensure our wrapper has the same length self.assertEqual(len(test), len(wrap)) # Try restricting our range wrap = listwrap.FixedListSubset(test, slice(1, 4)) self.assertEqual(len(wrap), 3) count = 0 for i, val in enumerate(wrap): self.assertEqual(i + 2, val) count += 1 # Ensure our wrapper has the same length self.assertEqual(count, len(wrap)) wrap = listwrap.FixedListSubset(wrap) self.assertEqual(len(wrap), 3) wrap = listwrap.FixedListSubset(wrap, slice(1, 3)) self.assertEqual(len(wrap), 2)
def test_list_subset_getter(self): test = range(1, 9) wrap = listwrap.FixedListSubset(test, (2, 6)) # Check that we can iterate over the list count = 0 for i, val in enumerate(wrap): self.assertEqual(i + 3, val) count += 1 # Ensure we saw all items in test self.assertEqual(len(wrap), count) # Check a direct get requst self.assertEqual(wrap[0], test[2]) # Try a single range request wrap_sub = wrap[1:3] self.assertEqual(len(wrap_sub), 2) self.assertEqual(wrap_sub[0], test[3]) # Try operators on multi-dimension lists test = [[1, 2, 3], [4, 5], [6, 7, 8], 9] wrap = listwrap.FixedListSubset(test) wrap_sub = wrap[1:3] self.assertEqual(len(wrap_sub), 2) self.assertEqual(wrap_sub[0], test[1]) # Check a multi-dimension subselection definition wrap_sub = wrap[0:2, 1:] self.assertEqual(len(wrap_sub), 2) for dimension in range(len(wrap_sub)): self.assertEqual(len(wrap_sub[dimension]), len(test[dimension][1:])) for elemIndex in range(len(wrap_sub[dimension])): self.assertEqual(wrap_sub[dimension][elemIndex], test[dimension][1 + elemIndex]) # Make sure we can select an element at a dimensional depth self.assertEqual(wrap_sub[1][0], test[1][1])
def test_sublist_copy(self): test = list(range(1, 9)) wrap = listwrap.FixedListSubset(test, (2, 6)) test_sub = wrap.compress_ranges_to_lists() self.assertEqual(type(test_sub), type([])) self.assertEqual(len(test_sub), 4) self.assertEqual(test_sub, test[2:6]) test = [[1, 2], [3, 4, 5], [6, 7, 8]] wrap = listwrap.FixedListSubset(test) wrap_sub = wrap[:, 1:] #wrap_sub = listwrap.FixedListSubset(test, slice(None), (1,None)) test_sub = wrap_sub.compress_ranges_to_lists() self.assertEqual(test_sub, [[2], [4, 5], [7, 8]]) test = [[1, 2], [3, 4, 5], [6, 7, 8], 9] wrap = listwrap.FixedListSubset(test) wrap_sub = wrap[:, 1:] # The 9 should blow up when we try to index it self.assertRaises(IndexError, wrap_sub.compress_ranges_to_lists) # Try deep copy test = [[1, 2], [3, 4, 5], [6, 7, 8]] wrap = listwrap.FixedListSubset(test) wrap_sub = wrap[:, 1:] test_sub = wrap_sub.deep_copy_as_list() # Check that all values are present for i in range(len(test_sub)): for j in range(len(test_sub[i])): self.assertEqual(test_sub[i][j], test[i][j + 1]) # Increment all of test's values by 1 for i in range(len(test)): for j in range(len(test[i])): test[i][j] += 1 # Check that the copy didn't update for i in range(len(test_sub)): for j in range(len(test_sub[i])): self.assertEqual(test_sub[i][j], test[i][j + 1] - 1) # Check that shallow copy keeps references when it should import copy wrap_sub = listwrap.FixedListSubset(test)[1:] wrap_copy = copy.copy(wrap_sub) self.assertEqual(type(wrap_copy), type(wrap_sub)) wrap_copy[1][1] = 10 self.assertEqual(wrap_copy[1][1], wrap_sub[1][1]) # Check that deep copy keeps no references test = [[1, 2], [3, 4, 5], [6, 7, 8]] wrap_sub = listwrap.FixedListSubset(test)[1:] wrap_copy = copy.deepcopy(wrap_sub) self.assertEqual(type(wrap_copy), type(wrap_sub)) test[1][1] = 10 # wrap_sub should reference test, and wrap_copy should have # its own copy of the data self.assertNotEqual(wrap_copy[0][1], wrap_sub[0][1])
def test_list_subset_iters(self): test = range(1, 11, 3) wrap = listwrap.FixedListSubset(test, (0, 4, 2)) self.assertEqual(len(wrap), len(range(1, 11, 6))) for elem, match in zip(wrap, range(1, 11, 6)): self.assertEqual(match, elem)