def test_iterations(): """ Test SortedDict iterations (for key in dict, keys(), values(), items()). """ expected_sorted_keys = ['cat', 'lion', 'panthera', 'serval', 'tiger'] expected_sorted_values = [2223., 1.5, -.88, 39e12, -2.7] sorted_dict = SortedDict( str, float, { 'lion': 1.5, 'tiger': -2.7, 'panthera': -.88, 'cat': 2223., 'serval': 39e12 }) computed_sorted_keys = [key for key in sorted_dict] computed_sorted_keys_from_keys = list(sorted_dict.keys()) computed_sorted_values = list(sorted_dict.values()) keys_from_items = [] values_from_items = [] for key, value in sorted_dict.items(): keys_from_items.append(key) values_from_items.append(value) assert_equals(expected_sorted_keys, computed_sorted_keys) assert_equals(expected_sorted_keys, computed_sorted_keys_from_keys) assert_equals(expected_sorted_keys, keys_from_items) assert_equals(expected_sorted_values, values_from_items) assert_equals(expected_sorted_values, computed_sorted_values)
def test_sub_and_remove_sub(): """Test SortedDict methods sub() and remove_sub().""" sorted_dict = SortedDict( int, str, { k: 'value of %s' % k for k in (2, 5, 1, 9, 4, 5, 20, 0, 6, 17, 8, 3, 7, 0, 4) }) assert sorted_dict.sub() == list(sorted_dict.values()) assert sorted_dict.sub(-10, 4) == [ 'value of 0', 'value of 1', 'value of 2', 'value of 3', 'value of 4' ] assert sorted_dict.sub(15) == ['value of 17', 'value of 20'] sorted_dict.remove_sub(-10, 4) assert all(k not in sorted_dict for k in (0, 1, 2, 3, 4)) sorted_dict.remove_sub(15) assert all(k not in sorted_dict for k in (17, 20))