def test_isub(): b = bag('aabbc') b -= bag('bd') assert b == bag('aabc') b = bag('aabbc') b -= set('bd') assert b == bag('aabc')
def test_add_op(): """Test __iadd__.""" b1 = bag('abc') result = b1 + bag('ab') assert result == bag('aabbc') assert b1 == bag('abc') assert result is not b1
def test_ixor(): b = bag('abbc') b ^= bag('bg') assert b == bag('abcg') b = bag('abbc') b ^= set('bg') assert b == bag('abcg')
def test_hashability(): """Test __hash__ for bags. Since bag is mutable and frozenbag is hashable, the second should be usable for dictionary keys and the second should raise a key or value error when used as a key or placed in a set. """ a = bag([1, 2, 3]) # Mutable multiset. b = frozenbag([1, 1, 2, 3]) # prototypical frozen multiset. c = frozenbag([4, 4, 5, 5, b, b]) # make sure we can nest them d = frozenbag([4, frozenbag([1, 3, 2, 1]), 4, 5, b, 5]) # c and d are the same; make sure nothing weird happens to hashes. assert c == d # Make sure both constructions work. dic = { b: 3, d: 5, d: 7, } assert len(dic) == 2 # Make sure no duplicates in dictionary. # Make sure TypeErrors are raised when using mutable bags for keys. with pytest.raises(TypeError): dic[a] = 4 with pytest.raises(TypeError): frozenbag([a, 1]) with pytest.raises(TypeError): bag([a, 1]) # test commutativity of bag instantiation. assert bag([4, 4, 5, 5, c]) == bag([4, 5, d, 4, 5])
def test_remove_all(): b = bag('abc') with pytest.raises(ValueError): b.remove_all('cd') assert b == bag('abc') b.remove_all('bc') assert b == bag('a')
def test_discard(): """Test discard.""" b = bag('abc') b.discard('a') assert b == bag('bc') b.discard('a') assert b == bag('bc')
def test_iadd(): b = bag('abc') b += bag('cde') assert b == bag('abccde') b = bag('abc') b += 'cde' assert b == bag('abccde')
def test_iadd(): """Test __iadd__.""" b = bag('abc') b += bag('cde') assert b == bag('abccde') b = bag('abc') b += 'cde' assert b == bag('abccde')
def test_isub(): """Test __isub__ and discard_all.""" b = bag('aabbc') b -= bag('bd') assert b == bag('aabc') b = bag('aabc') b -= set('bd') assert b == bag('aac')
def test_isub(): """Test __isub__.""" b = bag('aabbc') b -= bag('bd') assert b == bag('aabc') b = bag('aabbc') b -= set('bd') assert b == bag('aabc')
def test_ixor(): """Test __ixor__.""" b = bag('abbc') b ^= bag('bg') assert b == bag('abcg') b = bag('abbc') b ^= set('bg') assert b == bag('abcg')
def test_le(): assert _basebag() <= _basebag() assert _basebag() <= _basebag('a') assert _basebag('abc') <= _basebag('aabbbc') assert not _basebag('abbc') <= _basebag('abc') with pytest.raises(TypeError): bag('abc') < set('abc') assert not bag('aabc') < bag('abc')
def test_discard(): """Test discard.""" b = bag('aabc') b.discard('a') assert b == bag('abc') b.discard('a') assert b == bag('bc') b.discard('a') assert b == bag('bc')
def test_str(): assert str(_basebag()) == '_basebag()' assert "'a'^5" in str(_basebag('abracadabra')) assert "'b'^2" in str(_basebag('abracadabra')) assert "'c'" in str(_basebag('abracadabra')) abra_elems = set(("'a'^5", "'b'^2", "'r'^2", "'c'", "'d'")) assert compare_bag_string(bag('abracadabra')) == abra_elems if not _compat.is_py2: assert compare_bag_string(bag('abc')) == compare_bag_string(set('abc'))
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(_basebag()) == '_basebag()' assert "'a'^5" in str(_basebag('abracadabra')) assert "'b'^2" in str(_basebag('abracadabra')) assert "'c'" in str(_basebag('abracadabra')) abra_elems = set(("'a'^5", "'b'^2", "'r'^2", "'c'", "'d'")) assert compare_bag_string(bag('abracadabra')) == abra_elems if not is_py2: assert compare_bag_string(bag('abc')) == compare_bag_string(set('abc'))
def test_nlargest_deprecated(): """Test that nlargest raises a DeprecationWarning.""" b = bag() with warnings.catch_warnings(): warnings.simplefilter('error') with pytest.raises(DeprecationWarning): b.nlargest()
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(_basebag()) == '_basebag()' assert "'a'^5" in str(_basebag('abracadabra')) assert "'b'^2" in str(_basebag('abracadabra')) assert "'c'" in str(_basebag('abracadabra')) abra_elems = set(("'a'^5", "'b'^2", "'r'^2", "'c'", "'d'")) assert compare_bag_string(bag('abracadabra')) == abra_elems if not _compat.is_py2: assert compare_bag_string(bag('abc')) == compare_bag_string(set('abc'))
def test_repr(): """Test __repr__.""" ms = _basebag() assert ms == eval(ms.__repr__()) ms = _basebag('abracadabra') assert ms == eval(ms.__repr__()) assert repr(bag('a')) == "bag(('a',))"
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_init(): b = _basebag('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_ixor(): """Test __ixor__.""" b = bag('abbbccd') b ^= bag('bbcdg') assert b == bag('abcg') b = bag('bbcdg') b ^= bag('abbbccd') assert b == bag('acbg') b = bag('abbc') b ^= set('bg') assert b == bag('abcg')
def test_init(): """Test __init__.""" b = _basebag('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_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_nlargest_deprecated(): """Test that nlargest raises a DeprecationWarning.""" b = bag() with pytest.deprecated_call(): b.nlargest()
def test_pop(): """Test bag.pop.""" b = bag('a') assert b.pop() == 'a' with pytest.raises(KeyError): b.pop()
def test_comparison_chaining(): assert bag('a') < bag('aa') <= bag('aa')
def test_ior(): """Test __ior__.""" b = bag() b |= bag() assert b == bag() b = bag('aab') b |= bag() assert b == bag('aab') b = bag('aab') b |= bag('ac') assert b == bag('aabc') b = bag('aab') b |= set('ac') assert b == bag('aabc')
def test_isdisjoint(): """Test isdisjoint.""" assert bag().isdisjoint(bag()) assert bag().isdisjoint(bag('abc')) assert not bag('ab').isdisjoint(bag('ac')) assert bag('ab').isdisjoint(bag('cd'))
def test_mul(): """Test __mul__.""" assert bag('aab') * set('a') == bag((('a', 'a'), ('a', 'a'), ('b', 'a')))
def test_product_commutative(): """Test product for a commutative operator.""" assert bag((1, 2)).product([2, 1], operator=mul) == bag((2, 1, 4, 2))
def test_product(): """Test product""" assert bag('aab').product(set('a'), operator=concat) == bag(('aa', 'aa', 'ba'))
def test_mul_empty_set(): """Test __mul__ on an empty set.""" assert bag('aab') * set() == bag()
def test_xor(): """Test __xor__.""" assert bag('abc') ^ bag() == bag('abc') assert bag('aabc') ^ bag('ab') == bag('ac') assert bag('aabcc') ^ bag('abcde') == bag('acde')
def test_sub(): """Test __sub__.""" assert bag('abc') - bag() == bag('abc') assert bag('abbc') - bag('bd') == bag('abc')
def test_product(): """Test product""" assert bag('aab').product(set('a'), operator=concat) == bag( ('aa', 'aa', 'ba'))
def test_clear(): """Test clear.""" b = bag('abc') b.clear() assert b == bag()
def test_add(): """Test __add__.""" b = bag('abc') b.add('a') assert b == bag('aabc')
def test_iand(): """Test __iand__.""" b = bag() b &= bag() assert b == bag() b = bag('aab') b &= bag() assert b == bag() b = bag('aab') b &= bag('ac') assert b == bag('a') b = bag('aab') b &= set('ac') assert b == bag('a')
def test_or(): """Test __or__.""" assert bag('abcc') | bag() == bag('abcc') assert bag('abcc') | bag('aabd') == bag('aabccd') assert bag('aabc') | set('abdd') == bag('aabcd')
def test_and(): """Test __and__.""" assert bag('aabc') & bag('aacd') == bag('aac') assert bag() & bag('safgsd') == bag() assert bag('abcc') & bag() == bag() assert bag('abcc') & bag('aabd') == bag('ab') assert bag('aabc') & set('abdd') == bag('ab')
def test_num_unique_elems(): """Test Bag.num_unique_elements.""" assert bag('abracadabra').num_unique_elements() == 5
def test_rich_comp_type_mismatch(): """Test rich comparisons for bags with type mismatches.""" with pytest.raises(TypeError): bag('abc') < 'abc' with pytest.raises(TypeError): bag('abc') <= 'abc' with pytest.raises(TypeError): bag('abc') > 'abc' with pytest.raises(TypeError): bag('abc') >= 'abc' with pytest.raises(TypeError): 'abc' < bag('abc') with pytest.raises(TypeError): 'abc' <= bag('abc') with pytest.raises(TypeError): 'abc' > bag('abc') with pytest.raises(TypeError): 'abc' >= bag('abc') assert not bag('abc') == 'abc' assert not 'abc' == bag('abc')
def test_num_unique_elems(): """Test _basebag.num_unique_elements.""" assert bag('abracadabra').num_unique_elements() == 5