def test_rich_comp_subset(): """Test rich comparisons for bags that are subsets of other bags.""" b1 = Bag('abc') b2 = Bag('aabc') assert not b1 > b2 assert not b1 >= b2 assert b1 < b2 assert b1 <= b2
def test_nlargest(): """Test nlargest.""" abra = Bag('abracadabra') sort_key = lambda e: (-e[1], e[0]) abra_counts = [('a', 5), ('b', 2), ('r', 2), ('c', 1), ('d', 1)] assert sorted(abra.nlargest(), key=sort_key) == abra_counts assert sorted(abra.nlargest(3), key=sort_key) == abra_counts[:3] assert Bag('abcaba').nlargest(3) == [('a', 3), ('b', 2), ('c', 1)]
def test_init(): """Test __init__.""" b = Bag('abracadabra') assert b.count('a') == 5 assert b.count('b') == 2 assert b.count('r') == 2 assert b.count('c') == 1 assert b.count('d') == 1 b2 = bag(b) assert b2 == b
def test_rich_comp_unorderable_eq_len(): """Test rich comparisons for bags of equal length but unorderable.""" b1 = Bag('abb') b2 = Bag('abc') assert not b1 < b2 assert not b1 <= b2 assert not b1 > b2 assert not b1 >= b2 assert not b1 == b2 assert b1 != b2
def test_rich_comp_equal(): """Test rich comparisons for equal bags.""" assert Bag() <= Bag() assert not Bag() < Bag() assert Bag() >= Bag() assert not Bag() > Bag() b1 = Bag('aabc') b2 = Bag('aabc') assert not b2 > b1 assert b2 >= b1 assert not b2 < b1 assert b2 <= b1
def test_str(): """Test __str__.""" def compare_bag_string(b): s = str(b) assert s.startswith('{') assert s.endswith('}') return set(s[1:-1].split(', ')) assert str(Bag()) == 'Bag()' assert "'a'^5" in str(Bag('abracadabra')) assert "'b'^2" in str(Bag('abracadabra')) assert "'c'" in str(Bag('abracadabra')) abra_elems = set(("'a'^5", "'b'^2", "'r'^2", "'c'", "'d'")) assert compare_bag_string(bag('abracadabra')) == abra_elems
def test_rich_comp_unorderable_diff_len(): """Test rich comparisons for bags of unequal length and unorderable.""" b1 = Bag('abd') b2 = Bag('aabc') assert not b1 > b2 assert not b1 >= b2 assert not b1 < b2 assert not b1 <= b2 assert not b2 > b1 assert not b2 >= b1 assert not b2 < b1 assert not b2 <= b1 assert not b1 == b2 assert b1 != b2
def test_repr(): """Test __repr__.""" ms = Bag() assert ms == eval(ms.__repr__()) ms = Bag('abracadabra') assert ms == eval(ms.__repr__()) assert repr(bag('a')) == "bag(('a',))"
def test_copy(): """Test copy.""" b = Bag() assert b.copy() == b assert b.copy() is not b b = Bag('abc') assert b.copy() == b assert b.copy() is not b
def test_contains(): """Test __contains__.""" assert 'a' in Bag('bbac') assert 'a' not in Bag() assert 'a' not in Bag('missing letter')
def test_len(): """Test __len__.""" assert len(Bag()) == 0 assert len(Bag('abc')) == 3 assert len(Bag('aaba')) == 4
def test_from_map(): """Test from_mapping.""" assert Bag.from_mapping({'a': 1, 'b': 2}) == Bag('abb') assert Bag.from_mapping({'a': 1, 'b': 2, 'c': 0}) == Bag('abb')
def test_count(): """Test count.""" ms = Bag('abracadabra') assert ms.count('a') == 5 assert ms.count('x') == 0
def test_compare_unorderable(): assert not Bag('ac') <= Bag('ab') assert not Bag('ac') >= Bag('ab')
def test_compare_ne_set(bag_data, set_data): assert not Bag(bag_data) == set(set_data)
def test_compare_eq_set(bag_data, set_data): """Test comparisons to Sets that should be not equal.""" assert Bag(bag_data) != set(set_data)