diff options
| author | robot-piglet <[email protected]> | 2025-05-07 16:01:33 +0300 |
|---|---|---|
| committer | robot-piglet <[email protected]> | 2025-05-07 16:40:48 +0300 |
| commit | bab8f9e181aeff66a037d892252ff3d972922bd6 (patch) | |
| tree | 560f3a6e6e512e2b8e303de2d0a49bad9c9fc9d5 /contrib/python/more-itertools/py3/tests | |
| parent | 6c0a53e80dc1a7177112f16be38041f54df54af7 (diff) | |
Intermediate changes
commit_hash:b9d0dab354258c30c639c4cd045b559a38836838
Diffstat (limited to 'contrib/python/more-itertools/py3/tests')
| -rw-r--r-- | contrib/python/more-itertools/py3/tests/test_more.py | 35 | ||||
| -rw-r--r-- | contrib/python/more-itertools/py3/tests/test_recipes.py | 55 |
2 files changed, 62 insertions, 28 deletions
diff --git a/contrib/python/more-itertools/py3/tests/test_more.py b/contrib/python/more-itertools/py3/tests/test_more.py index bfbf583f28f..f30e747d190 100644 --- a/contrib/python/more-itertools/py3/tests/test_more.py +++ b/contrib/python/more-itertools/py3/tests/test_more.py @@ -1,5 +1,4 @@ import cmath -import warnings from collections import Counter, abc from collections.abc import Set @@ -29,7 +28,6 @@ from statistics import mean from string import ascii_letters from sys import version_info from time import sleep -from traceback import format_exc from unittest import skipIf, TestCase import more_itertools as mi @@ -615,24 +613,12 @@ class OneTests(TestCase): it = iter(['item']) self.assertEqual(mi.one(it), 'item') - def test_too_short(self): + def test_too_short_new(self): it = iter([]) - for too_short, exc_type in [ - (None, ValueError), - (IndexError, IndexError), - ]: - with self.subTest(too_short=too_short): - try: - mi.one(it, too_short=too_short) - except exc_type: - formatted_exc = format_exc() - self.assertIn('StopIteration', formatted_exc) - self.assertIn( - 'The above exception was the direct cause', - formatted_exc, - ) - else: - self.fail() + self.assertRaises(ValueError, lambda: mi.one(it)) + self.assertRaises( + OverflowError, lambda: mi.one(it, too_short=OverflowError) + ) def test_too_long(self): it = count() @@ -1908,15 +1894,11 @@ class StaggerTest(TestCase): class ZipEqualTest(TestCase): @skipIf(version_info[:2] < (3, 10), 'zip_equal deprecated for 3.10+') def test_deprecation(self): - with warnings.catch_warnings(record=True) as caught: - warnings.simplefilter('always') + with self.assertWarns(DeprecationWarning): self.assertEqual( list(mi.zip_equal([1, 2], [3, 4])), [(1, 3), (2, 4)] ) - (warning,) = caught - assert warning.category == DeprecationWarning - def test_equal(self): lists = [0, 1, 2], [2, 3, 4] @@ -4137,7 +4119,7 @@ class FilterExceptTests(TestCase): list(mi.filter_except(int, iterable)) def test_raise(self): - iterable = ['0', '1' '2', 'three', None] + iterable = ['0', '12', 'three', None] with self.assertRaises(TypeError): list(mi.filter_except(int, iterable, ValueError)) @@ -4169,7 +4151,7 @@ class MapExceptTests(TestCase): list(mi.map_except(int, iterable)) def test_raise(self): - iterable = ['0', '1' '2', 'three', None] + iterable = ['0', '12', 'three', None] with self.assertRaises(TypeError): list(mi.map_except(int, iterable, ValueError)) @@ -4322,7 +4304,6 @@ class SampleTests(TestCase): self.assertTrue(difference_in_means < 4.4) def test_error_cases(self): - # weights and counts are mutally exclusive with self.assertRaises(TypeError): mi.sample( diff --git a/contrib/python/more-itertools/py3/tests/test_recipes.py b/contrib/python/more-itertools/py3/tests/test_recipes.py index a810b8de1e0..63be39426eb 100644 --- a/contrib/python/more-itertools/py3/tests/test_recipes.py +++ b/contrib/python/more-itertools/py3/tests/test_recipes.py @@ -1,10 +1,11 @@ +from collections import Counter from decimal import Decimal from doctest import DocTestSuite from fractions import Fraction from functools import reduce from itertools import combinations, count, groupby, permutations from operator import mul -from math import comb, factorial +from math import comb, prod, factorial from sys import version_info from unittest import TestCase, skipIf from unittest.mock import patch @@ -1385,3 +1386,55 @@ class LoopsTests(TestCase): self.assertTrue( all(list(mi.loops(n)) == [None] * n for n in range(-10, 10)) ) + + +class MultinomialTests(TestCase): + def test_basic(self): + multinomial = mi.multinomial + + # Case M(11; 5, 2, 1, 1, 2) = 83160 + # https://www.wolframalpha.com/input?i=Multinomia%285%2C+2%2C+1%2C+1%2C+2%29 + self.assertEqual(multinomial(5, 2, 1, 1, 2), 83160) + + # Commutative + self.assertEqual(multinomial(2, 1, 1, 2, 5), 83160) + + # Unaffected by zero-sized bins + self.assertEqual(multinomial(2, 0, 1, 0, 1, 2, 5, 0), 83160) + + # Matches definition + self.assertEqual( + multinomial(5, 2, 1, 1, 2), + ( + factorial(sum([5, 2, 1, 1, 2])) + // prod(map(factorial, [5, 2, 1, 1, 2])) + ), + ) + + # Corner cases and identities + self.assertEqual(multinomial(), 1) + self.assertEqual(multinomial(5), 1) + self.assertEqual(multinomial(5, 7), comb(12, 5)) + self.assertEqual(multinomial(1, 1, 1, 1, 1, 1, 1), factorial(7)) + + # Relationship to distinct_permuations() and permutations() + for word in ['plain', 'pizza', 'coffee', 'honolulu', 'assists']: + with self.subTest(word=word): + self.assertEqual( + multinomial(*Counter(word).values()), + mi.ilen(mi.distinct_permutations(word)), + ) + self.assertEqual( + multinomial(*Counter(word).values()), + len(set(permutations(word))), + ) + + # Error cases + with self.assertRaises(ValueError): + multinomial(-5, 7) # No negative inputs + with self.assertRaises(TypeError): + multinomial(5, 7.25) # No float inputs + with self.assertRaises(TypeError): + multinomial(5, 'x') # No non-numeric inputs + with self.assertRaises(TypeError): + multinomial([5, 7]) # No sequence inputs |
