diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-14 20:18:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-14 20:18:28 +0000 |
commit | f8363b456f1ab31ee56abad579b215af195093d5 (patch) | |
tree | b1500c675c2e0a55fb75721a854e1510acf7c862 /tests/test_ratio.py | |
parent | Initial commit. (diff) | |
download | rich-f8363b456f1ab31ee56abad579b215af195093d5.tar.xz rich-f8363b456f1ab31ee56abad579b215af195093d5.zip |
Adding upstream version 9.11.0.upstream/9.11.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/test_ratio.py')
-rw-r--r-- | tests/test_ratio.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/tests/test_ratio.py b/tests/test_ratio.py new file mode 100644 index 0000000..8a44e8c --- /dev/null +++ b/tests/test_ratio.py @@ -0,0 +1,50 @@ +import pytest +from typing import NamedTuple, Optional + +from rich._ratio import ratio_reduce, ratio_resolve + + +class Edge(NamedTuple): + size: Optional[int] = None + ratio: int = 1 + minimum_size: int = 1 + + +@pytest.mark.parametrize( + "total,ratios,maximums,values,result", + [ + (20, [2, 4], [20, 20], [5, 5], [-2, -8]), + (20, [2, 4], [1, 1], [5, 5], [4, 4]), + (20, [2, 4], [1, 1], [2, 2], [1, 1]), + (3, [2, 4], [3, 3], [2, 2], [1, 0]), + (3, [2, 4], [3, 3], [0, 0], [-1, -2]), + (3, [0, 0], [3, 3], [4, 4], [4, 4]), + ], +) +def test_ratio_reduce(total, ratios, maximums, values, result): + assert ratio_reduce(total, ratios, maximums, values) == result + + +def test_ratio_resolve(): + assert ratio_resolve(100, []) == [] + assert ratio_resolve(100, [Edge(size=100), Edge(ratio=1)]) == [100, 1] + assert ratio_resolve(100, [Edge(ratio=1)]) == [100] + assert ratio_resolve(100, [Edge(ratio=1), Edge(ratio=1)]) == [50, 50] + assert ratio_resolve(100, [Edge(size=20), Edge(ratio=1), Edge(ratio=1)]) == [ + 20, + 40, + 40, + ] + assert ratio_resolve(100, [Edge(size=40), Edge(ratio=2), Edge(ratio=1)]) == [ + 40, + 40, + 20, + ] + assert ratio_resolve( + 100, [Edge(size=40), Edge(ratio=2), Edge(ratio=1, minimum_size=25)] + ) == [40, 35, 25] + assert ratio_resolve(100, [Edge(ratio=1), Edge(ratio=1), Edge(ratio=1)]) == [ + 33, + 33, + 34, + ] |