test_utils.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from __future__ import unicode_literals
  2. from prompt_toolkit.utils import take_using_weights
  3. import itertools
  4. def test_using_weights():
  5. def take(generator, count):
  6. return list(itertools.islice(generator, 0, count))
  7. # Check distribution.
  8. data = take(take_using_weights(['A', 'B', 'C'], [5, 10, 20]), 35)
  9. assert data.count('A') == 5
  10. assert data.count('B') == 10
  11. assert data.count('C') == 20
  12. assert data == [
  13. 'A', 'B', 'C', 'C', 'B', 'C', 'C', 'A', 'B', 'C', 'C', 'B', 'C',
  14. 'C', 'A', 'B', 'C', 'C', 'B', 'C', 'C', 'A', 'B', 'C', 'C',
  15. 'B', 'C', 'C', 'A', 'B', 'C', 'C', 'B', 'C', 'C']
  16. # Another order.
  17. data = take(take_using_weights(['A', 'B', 'C'], [20, 10, 5]), 35)
  18. assert data.count('A') == 20
  19. assert data.count('B') == 10
  20. assert data.count('C') == 5
  21. # Bigger numbers.
  22. data = take(take_using_weights(['A', 'B', 'C'], [20, 10, 5]), 70)
  23. assert data.count('A') == 40
  24. assert data.count('B') == 20
  25. assert data.count('C') == 10
  26. # Negative numbers.
  27. data = take(take_using_weights(['A', 'B', 'C'], [-20, 10, 0]), 70)
  28. assert data.count('A') == 0
  29. assert data.count('B') == 70
  30. assert data.count('C') == 0