aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/prompt-toolkit/py3/tests/test_utils.py
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /contrib/python/prompt-toolkit/py3/tests/test_utils.py
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'contrib/python/prompt-toolkit/py3/tests/test_utils.py')
-rw-r--r--contrib/python/prompt-toolkit/py3/tests/test_utils.py76
1 files changed, 76 insertions, 0 deletions
diff --git a/contrib/python/prompt-toolkit/py3/tests/test_utils.py b/contrib/python/prompt-toolkit/py3/tests/test_utils.py
new file mode 100644
index 0000000000..6a1c17955c
--- /dev/null
+++ b/contrib/python/prompt-toolkit/py3/tests/test_utils.py
@@ -0,0 +1,76 @@
+import itertools
+
+import pytest
+
+from prompt_toolkit.utils import take_using_weights
+
+
+def test_using_weights():
+ def take(generator, count):
+ return list(itertools.islice(generator, 0, count))
+
+ # Check distribution.
+ data = take(take_using_weights(["A", "B", "C"], [5, 10, 20]), 35)
+ assert data.count("A") == 5
+ assert data.count("B") == 10
+ assert data.count("C") == 20
+
+ assert data == [
+ "A",
+ "B",
+ "C",
+ "C",
+ "B",
+ "C",
+ "C",
+ "A",
+ "B",
+ "C",
+ "C",
+ "B",
+ "C",
+ "C",
+ "A",
+ "B",
+ "C",
+ "C",
+ "B",
+ "C",
+ "C",
+ "A",
+ "B",
+ "C",
+ "C",
+ "B",
+ "C",
+ "C",
+ "A",
+ "B",
+ "C",
+ "C",
+ "B",
+ "C",
+ "C",
+ ]
+
+ # Another order.
+ data = take(take_using_weights(["A", "B", "C"], [20, 10, 5]), 35)
+ assert data.count("A") == 20
+ assert data.count("B") == 10
+ assert data.count("C") == 5
+
+ # Bigger numbers.
+ data = take(take_using_weights(["A", "B", "C"], [20, 10, 5]), 70)
+ assert data.count("A") == 40
+ assert data.count("B") == 20
+ assert data.count("C") == 10
+
+ # Negative numbers.
+ data = take(take_using_weights(["A", "B", "C"], [-20, 10, 0]), 70)
+ assert data.count("A") == 0
+ assert data.count("B") == 70
+ assert data.count("C") == 0
+
+ # All zero-weight items.
+ with pytest.raises(ValueError):
+ take(take_using_weights(["A", "B", "C"], [0, 0, 0]), 70)