Exemplo n.º 1
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])
Exemplo n.º 2
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])
Exemplo n.º 3
0
def test_hash():
    """Test __hash__ vs an empty bag."""
    bag_with_empty_tuple = frozenbag([()])
    assert not hash(frozenbag()) == hash(bag_with_empty_tuple)
    assert not hash(frozenbag()) == hash(frozenbag((0, )))
    assert not hash(frozenbag('a')) == hash(frozenbag(('aa')))
    assert not hash(frozenbag('a')) == hash(frozenbag(('aaa')))
    assert not hash(frozenbag('a')) == hash(frozenbag(('aaaa')))
    assert not hash(frozenbag('a')) == hash(frozenbag(('aaaaa')))
    assert hash(frozenbag('ba')) == hash(frozenbag(('ab')))
    assert hash(frozenbag('badce')) == hash(frozenbag(('dbeac')))
Exemplo n.º 4
0
def test_hash():
	"""Test __hash__ vs an empty bag."""
	bag_with_empty_tuple = frozenbag([()])
	assert not hash(frozenbag()) == hash(bag_with_empty_tuple)
	assert not hash(frozenbag()) == hash(frozenbag((0,)))
	assert not hash(frozenbag('a')) == hash(frozenbag(('aa')))
	assert not hash(frozenbag('a')) == hash(frozenbag(('aaa')))
	assert not hash(frozenbag('a')) == hash(frozenbag(('aaaa')))
	assert not hash(frozenbag('a')) == hash(frozenbag(('aaaaa')))
	assert hash(frozenbag('ba')) == hash(frozenbag(('ab')))
	assert hash(frozenbag('badce')) == hash(frozenbag(('dbeac')))