Example #1
0
def test_samples_only_non_zero_while_updating(weights, rnd):
    sampler = Sampler()

    model = {}

    for k, v in weights:
        model[k] = v
        sampler[k] = v
        if v > 0:
            assert sampler[k] == model[k]
        else:
            assert k not in sampler
        if sum(model.values()) > 0:
            s = sampler.sample(rnd)
            assert model[s] > 0
Example #2
0
def test_zero_weights_are_absent():
    sampler = Sampler()
    sampler[1] = 2
    assert sampler[1] == 2
    sampler[1] = 0
    with pytest.raises(KeyError):
        sampler[1]
Example #3
0
def test_non_empty_sampler_is_truthy():
    sampler = Sampler()
    assert not sampler
    sampler[0] = 1
    assert sampler
    del sampler[0]
    assert not sampler
Example #4
0
def test_empty_sample_is_error():
    rnd = Random(0)
    sampler = Sampler()
    with pytest.raises(IndexError):
        sampler.sample(rnd)
    sampler[1] = 1
    sampler[1] = 0
    with pytest.raises(IndexError):
        sampler.sample(rnd)
Example #5
0
def test_skips_zero_weights():
    sampler = Sampler({1: 1, 2: 0, 3: 1})
    assert sorted(sampler) == [1, 3]
Example #6
0
def test_iterates_as_dict(weights):
    sampler = Sampler(weights)
    assert sorted(sampler) == sorted(weights)
    assert sorted(sampler.items()) == sorted(weights.items())
Example #7
0
def test_can_delete_an_item(rnd, data, weights):
    keys = sorted(weights)
    sampler = Sampler(weights)
    key = data.draw(st.sampled_from(keys))
    del sampler[key]
    assert sampler.sample(rnd) != key
Example #8
0
def test_sample_from_single(rnd):
    sampler = Sampler()
    sampler[0] = 1
    assert sampler.sample(rnd) == 0
Example #9
0
def test_samples_only_non_zero(weights, rnd):
    assume(sum(weights.values()) > 0)
    sampler = Sampler(weights)

    assert weights[sampler.sample(rnd)] > 0
Example #10
0
def test_items_not_set_are_not_in_sampler():
    sampler = Sampler()
    assert 3 not in sampler