aboutsummaryrefslogtreecommitdiff
path: root/.venv/lib/python3.12/site-packages/networkx/utils/tests/test_random_sequence.py
blob: 1d1b95799f75aa668801a5af482bee36fdb8d837 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import pytest

from networkx.utils import (
    powerlaw_sequence,
    random_weighted_sample,
    weighted_choice,
    zipf_rv,
)


def test_degree_sequences():
    seq = powerlaw_sequence(10, seed=1)
    seq = powerlaw_sequence(10)
    assert len(seq) == 10


def test_zipf_rv():
    r = zipf_rv(2.3, xmin=2, seed=1)
    r = zipf_rv(2.3, 2, 1)
    r = zipf_rv(2.3)
    assert type(r), int
    pytest.raises(ValueError, zipf_rv, 0.5)
    pytest.raises(ValueError, zipf_rv, 2, xmin=0)


def test_random_weighted_sample():
    mapping = {"a": 10, "b": 20}
    s = random_weighted_sample(mapping, 2, seed=1)
    s = random_weighted_sample(mapping, 2)
    assert sorted(s) == sorted(mapping.keys())
    pytest.raises(ValueError, random_weighted_sample, mapping, 3)


def test_random_weighted_choice():
    mapping = {"a": 10, "b": 0}
    c = weighted_choice(mapping, seed=1)
    c = weighted_choice(mapping)
    assert c == "a"