test_random_sequence.py 925 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import pytest
  2. from networkx.utils import (
  3. powerlaw_sequence,
  4. random_weighted_sample,
  5. weighted_choice,
  6. zipf_rv,
  7. )
  8. def test_degree_sequences():
  9. seq = powerlaw_sequence(10, seed=1)
  10. seq = powerlaw_sequence(10)
  11. assert len(seq) == 10
  12. def test_zipf_rv():
  13. r = zipf_rv(2.3, xmin=2, seed=1)
  14. r = zipf_rv(2.3, 2, 1)
  15. r = zipf_rv(2.3)
  16. assert type(r), int
  17. pytest.raises(ValueError, zipf_rv, 0.5)
  18. pytest.raises(ValueError, zipf_rv, 2, xmin=0)
  19. def test_random_weighted_sample():
  20. mapping = {"a": 10, "b": 20}
  21. s = random_weighted_sample(mapping, 2, seed=1)
  22. s = random_weighted_sample(mapping, 2)
  23. assert sorted(s) == sorted(mapping.keys())
  24. pytest.raises(ValueError, random_weighted_sample, mapping, 3)
  25. def test_random_weighted_choice():
  26. mapping = {"a": 10, "b": 0}
  27. c = weighted_choice(mapping, seed=1)
  28. c = weighted_choice(mapping)
  29. assert c == "a"