コード例 #1
0
def test_isub():
    b = bag('aabbc')
    b -= bag('bd')
    assert b == bag('aabc')
    b = bag('aabbc')
    b -= set('bd')
    assert b == bag('aabc')
コード例 #2
0
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
コード例 #3
0
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
コード例 #4
0
def test_ixor():
    b = bag('abbc')
    b ^= bag('bg')
    assert b == bag('abcg')
    b = bag('abbc')
    b ^= set('bg')
    assert b == bag('abcg')
コード例 #5
0
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])
コード例 #6
0
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')
コード例 #7
0
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])
コード例 #8
0
def test_discard():
    """Test discard."""
    b = bag('abc')
    b.discard('a')
    assert b == bag('bc')
    b.discard('a')
    assert b == bag('bc')
コード例 #9
0
def test_iadd():
    b = bag('abc')
    b += bag('cde')
    assert b == bag('abccde')
    b = bag('abc')
    b += 'cde'
    assert b == bag('abccde')
コード例 #10
0
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')
コード例 #11
0
def test_iadd():
	"""Test __iadd__."""
	b = bag('abc')
	b += bag('cde')
	assert b == bag('abccde')
	b = bag('abc')
	b += 'cde'
	assert b == bag('abccde')
コード例 #12
0
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')
コード例 #13
0
def test_iadd():
    """Test __iadd__."""
    b = bag('abc')
    b += bag('cde')
    assert b == bag('abccde')
    b = bag('abc')
    b += 'cde'
    assert b == bag('abccde')
コード例 #14
0
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')
コード例 #15
0
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')
コード例 #16
0
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')
コード例 #17
0
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')
コード例 #18
0
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')
コード例 #19
0
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'))
コード例 #20
0
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'))
コード例 #21
0
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()
コード例 #22
0
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'))
コード例 #23
0
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()
コード例 #24
0
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',))"
コード例 #25
0
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',))"
コード例 #26
0
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
コード例 #27
0
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')
コード例 #28
0
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
コード例 #29
0
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
コード例 #30
0
def test_nlargest_deprecated():
    """Test that nlargest raises a DeprecationWarning."""
    b = bag()
    with pytest.deprecated_call():
        b.nlargest()
コード例 #31
0
def test_pop():
	"""Test bag.pop."""
	b = bag('a')
	assert b.pop() == 'a'
	with pytest.raises(KeyError):
		b.pop()
コード例 #32
0
def test_comparison_chaining():
	assert bag('a') < bag('aa') <= bag('aa')
コード例 #33
0
def test_pop():
    """Test bag.pop."""
    b = bag('a')
    assert b.pop() == 'a'
    with pytest.raises(KeyError):
        b.pop()
コード例 #34
0
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')
コード例 #35
0
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'))
コード例 #36
0
def test_mul():
	"""Test __mul__."""
	assert bag('aab') * set('a') == bag((('a', 'a'), ('a', 'a'), ('b', 'a')))
コード例 #37
0
def test_product_commutative():
    """Test product for a commutative operator."""
    assert bag((1, 2)).product([2, 1], operator=mul) == bag((2, 1, 4, 2))
コード例 #38
0
def test_product():
	"""Test product"""
	assert bag('aab').product(set('a'), operator=concat) == bag(('aa', 'aa', 'ba'))
コード例 #39
0
def test_mul_empty_set():
	"""Test __mul__ on an empty set."""
	assert bag('aab') * set() == bag()
コード例 #40
0
def test_product_commutative():
	"""Test product for a commutative operator."""
	assert bag((1, 2)).product([2, 1], operator=mul) == bag((2, 1, 4, 2))
コード例 #41
0
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')
コード例 #42
0
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')
コード例 #43
0
def test_mul_empty_set():
    """Test __mul__ on an empty set."""
    assert bag('aab') * set() == bag()
コード例 #44
0
def test_sub():
	"""Test __sub__."""
	assert bag('abc') - bag() == bag('abc')
	assert bag('abbc') - bag('bd') == bag('abc')
コード例 #45
0
def test_product():
    """Test product"""
    assert bag('aab').product(set('a'), operator=concat) == bag(
        ('aa', 'aa', 'ba'))
コード例 #46
0
def test_clear():
	"""Test clear."""
	b = bag('abc')
	b.clear()
	assert b == bag()
コード例 #47
0
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')
コード例 #48
0
def test_add():
	"""Test __add__."""
	b = bag('abc')
	b.add('a')
	assert b == bag('aabc')
コード例 #49
0
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')
コード例 #50
0
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')
コード例 #51
0
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')
コード例 #52
0
def test_clear():
    """Test clear."""
    b = bag('abc')
    b.clear()
    assert b == bag()
コード例 #53
0
def test_num_unique_elems():
    """Test Bag.num_unique_elements."""
    assert bag('abracadabra').num_unique_elements() == 5
コード例 #54
0
def test_sub():
    """Test __sub__."""
    assert bag('abc') - bag() == bag('abc')
    assert bag('abbc') - bag('bd') == bag('abc')
コード例 #55
0
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')
コード例 #56
0
def test_mul():
    """Test __mul__."""
    assert bag('aab') * set('a') == bag((('a', 'a'), ('a', 'a'), ('b', 'a')))
コード例 #57
0
def test_num_unique_elems():
	"""Test _basebag.num_unique_elements."""
	assert bag('abracadabra').num_unique_elements() == 5
コード例 #58
0
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')