diff options
author | robot-piglet <robot-piglet@yandex-team.com> | 2024-08-22 12:08:26 +0300 |
---|---|---|
committer | robot-piglet <robot-piglet@yandex-team.com> | 2024-08-22 12:17:43 +0300 |
commit | 9b7b894a85081d8b88c312d5797e86e3166cea7d (patch) | |
tree | bfca0f422a9649a31bab0e9b2d1ab2ae37f5f9e7 /contrib/python/more-itertools/py3/tests | |
parent | a04076863119aed8a4fd5d4b31b42f83283a2d8d (diff) | |
download | ydb-9b7b894a85081d8b88c312d5797e86e3166cea7d.tar.gz |
Intermediate changes
Diffstat (limited to 'contrib/python/more-itertools/py3/tests')
-rw-r--r-- | contrib/python/more-itertools/py3/tests/test_more.py | 172 | ||||
-rw-r--r-- | contrib/python/more-itertools/py3/tests/test_recipes.py | 13 |
2 files changed, 150 insertions, 35 deletions
diff --git a/contrib/python/more-itertools/py3/tests/test_more.py b/contrib/python/more-itertools/py3/tests/test_more.py index fda4c0984a..1a70ea08e5 100644 --- a/contrib/python/more-itertools/py3/tests/test_more.py +++ b/contrib/python/more-itertools/py3/tests/test_more.py @@ -473,36 +473,11 @@ class ConsumerTests(TestCase): class DistinctPermutationsTests(TestCase): - def test_distinct_permutations(self): - """Make sure the output for ``distinct_permutations()`` is the same as - set(permutations(it)). - - """ + def test_basic(self): iterable = ['z', 'a', 'a', 'q', 'q', 'q', 'y'] - test_output = sorted(mi.distinct_permutations(iterable)) - ref_output = sorted(set(permutations(iterable))) - self.assertEqual(test_output, ref_output) - - def test_other_iterables(self): - """Make sure ``distinct_permutations()`` accepts a different type of - iterables. - - """ - # a generator - iterable = (c for c in ['z', 'a', 'a', 'q', 'q', 'q', 'y']) - test_output = sorted(mi.distinct_permutations(iterable)) - # "reload" it - iterable = (c for c in ['z', 'a', 'a', 'q', 'q', 'q', 'y']) - ref_output = sorted(set(permutations(iterable))) - self.assertEqual(test_output, ref_output) - - # an iterator - iterable = iter(['z', 'a', 'a', 'q', 'q', 'q', 'y']) - test_output = sorted(mi.distinct_permutations(iterable)) - # "reload" it - iterable = iter(['z', 'a', 'a', 'q', 'q', 'q', 'y']) - ref_output = sorted(set(permutations(iterable))) - self.assertEqual(test_output, ref_output) + actual = list(mi.distinct_permutations(iterable)) + expected = set(permutations(iterable)) + self.assertCountEqual(actual, expected) def test_r(self): for iterable, r in ( @@ -524,9 +499,35 @@ class DistinctPermutationsTests(TestCase): ([], 4), ): with self.subTest(iterable=iterable, r=r): - expected = sorted(set(permutations(iterable, r))) - actual = sorted(mi.distinct_permutations(iter(iterable), r)) - self.assertEqual(actual, expected) + expected = set(permutations(iterable, r)) + actual = list(mi.distinct_permutations(iter(iterable), r)) + self.assertCountEqual(actual, expected) + + def test_unsortable(self): + iterable = ['1', 2, 2, 3, 3, 3] + actual = list(mi.distinct_permutations(iterable)) + expected = set(permutations(iterable)) + self.assertCountEqual(actual, expected) + + def test_unsortable_r(self): + iterable = ['1', 2, 2, 3, 3, 3] + for r in range(len(iterable) + 1): + with self.subTest(iterable=iterable, r=r): + actual = list(mi.distinct_permutations(iterable, r=r)) + expected = set(permutations(iterable, r=r)) + self.assertCountEqual(actual, expected) + + def test_unsorted_equivalent(self): + iterable = [1, True, '3'] + actual = list(mi.distinct_permutations(iterable)) + expected = set(permutations(iterable)) + self.assertCountEqual(actual, expected) + + def test_unhashable(self): + iterable = ([1], [1], 2) + actual = list(mi.distinct_permutations(iterable)) + expected = list(mi.unique_everseen(permutations(iterable))) + self.assertCountEqual(actual, expected) class IlenTests(TestCase): @@ -2172,6 +2173,29 @@ class SortTogetherTest(TestCase): ], ) + def test_strict(self): + # Test for list of lists or tuples + self.assertRaises( + mi.UnequalIterablesError, + lambda: mi.sort_together( + [(4, 3, 2, 1), ('a', 'b', 'c')], strict=True + ), + ) + + # Test for list of iterables + self.assertRaises( + mi.UnequalIterablesError, + lambda: mi.sort_together([range(4), range(5)], strict=True), + ) + + # Test for iterable of iterables + self.assertRaises( + mi.UnequalIterablesError, + lambda: mi.sort_together( + (range(i) for i in range(4)), strict=True + ), + ) + class DivideTest(TestCase): """Tests for divide()""" @@ -3347,6 +3371,10 @@ class SeekableTest(PeekableMixinTests, TestCase): self.assertEqual(next(s), '2') s.relative_seek(-2) self.assertEqual(next(s), '1') + s.relative_seek(-2) + self.assertEqual( + next(s), '0' + ) # Seek relative to current position within the cache s.relative_seek(-10) # Lower bound self.assertEqual(next(s), '0') s.relative_seek(10) # Lower bound @@ -3488,17 +3516,43 @@ class CircularShiftsTests(TestCase): def test_simple_circular_shifts(self): # test the a simple iterator case self.assertEqual( - mi.circular_shifts(range(4)), + list(mi.circular_shifts(range(4))), [(0, 1, 2, 3), (1, 2, 3, 0), (2, 3, 0, 1), (3, 0, 1, 2)], ) def test_duplicates(self): # test non-distinct entries self.assertEqual( - mi.circular_shifts([0, 1, 0, 1]), + list(mi.circular_shifts([0, 1, 0, 1])), [(0, 1, 0, 1), (1, 0, 1, 0), (0, 1, 0, 1), (1, 0, 1, 0)], ) + def test_steps_positive(self): + actual = list(mi.circular_shifts(range(5), steps=2)) + expected = [ + (0, 1, 2, 3, 4), + (2, 3, 4, 0, 1), + (4, 0, 1, 2, 3), + (1, 2, 3, 4, 0), + (3, 4, 0, 1, 2), + ] + self.assertEqual(actual, expected) + + def test_steps_negative(self): + actual = list(mi.circular_shifts(range(5), steps=-2)) + expected = [ + (0, 1, 2, 3, 4), + (3, 4, 0, 1, 2), + (1, 2, 3, 4, 0), + (4, 0, 1, 2, 3), + (2, 3, 4, 0, 1), + ] + self.assertEqual(actual, expected) + + def test_steps_zero(self): + with self.assertRaises(ValueError): + list(mi.circular_shifts(range(5), steps=0)) + class MakeDecoratorTests(TestCase): def test_basic(self): @@ -3885,6 +3939,24 @@ class SetPartitionsTests(TestCase): def test_to_many_groups(self): self.assertEqual([], list(mi.set_partitions(range(4), 5))) + def test_min_size(self): + it = 'abc' + actual = mi.set_partitions(it, min_size=2) + expected = [['abc']] + self.assertEqual( + self._normalize_partitions(expected), + self._normalize_partitions(actual), + ) + + def test_max_size(self): + it = 'abc' + actual = mi.set_partitions(it, max_size=2) + expected = [['a', 'bc'], ['ab', 'c'], ['b', 'ac'], ['a', 'b', 'c']] + self.assertEqual( + self._normalize_partitions(expected), + self._normalize_partitions(actual), + ) + class TimeLimitedTests(TestCase): def test_basic(self): @@ -4145,6 +4217,11 @@ class SampleTests(TestCase): expected = ['f', 'e'] self.assertEqual(actual, expected) + def test_negative(self): + data = [1, 2, 3, 4, 5] + with self.assertRaises(ValueError): + mi.sample(data, k=-1) + def test_length(self): """Check that *k* elements are sampled.""" data = [1, 2, 3, 4, 5] @@ -4154,6 +4231,32 @@ class SampleTests(TestCase): expected = min(k, len(data)) self.assertEqual(actual, expected) + def test_strict(self): + data = ['1', '2', '3', '4', '5'] + self.assertEqual(set(mi.sample(data, 6, strict=False)), set(data)) + with self.assertRaises(ValueError): + mi.sample(data, 6, strict=True) + + def test_counts(self): + # Test with counts + seed(0) + iterable = ['red', 'blue'] + counts = [4, 2] + k = 5 + actual = list(mi.sample(iterable, counts=counts, k=k)) + + # Test without counts + seed(0) + decoded_iterable = (['red'] * 4) + (['blue'] * 2) + expected = list(mi.sample(decoded_iterable, k=k)) + + self.assertEqual(actual, expected) + + def test_counts_all(self): + actual = Counter(mi.sample('uwxyz', 35, counts=(1, 0, 4, 10, 20))) + expected = Counter({'u': 1, 'x': 4, 'y': 10, 'z': 20}) + self.assertEqual(actual, expected) + def test_sampling_entire_iterable(self): """If k=len(iterable), the sample contains the original elements.""" data = ["a", 2, "a", 4, (1, 2, 3)] @@ -4227,6 +4330,7 @@ class IsSortedTests(TestCase): ([1, 2, 3], {}, True), ([1, 1, 2, 3], {}, True), ([1, 10, 2, 3], {}, False), + ([3, float('nan'), 1, 2], {}, True), (['1', '10', '2', '3'], {}, True), (['1', '10', '2', '3'], {'key': int}, False), ([1, 2, 3], {'reverse': True}, False), diff --git a/contrib/python/more-itertools/py3/tests/test_recipes.py b/contrib/python/more-itertools/py3/tests/test_recipes.py index 0035e58d05..d3762d49db 100644 --- a/contrib/python/more-itertools/py3/tests/test_recipes.py +++ b/contrib/python/more-itertools/py3/tests/test_recipes.py @@ -838,7 +838,7 @@ class TriplewiseTests(TestCase): class SlidingWindowTests(TestCase): - def test_basic(self): + def test_islice_version(self): for iterable, n, expected in [ ([], 1, []), ([0], 1, [(0,)]), @@ -853,6 +853,17 @@ class SlidingWindowTests(TestCase): actual = list(mi.sliding_window(iterable, n)) self.assertEqual(actual, expected) + def test_deque_version(self): + iterable = map(str, range(100)) + all_windows = list(mi.sliding_window(iterable, 95)) + self.assertEqual(all_windows[0], tuple(map(str, range(95)))) + self.assertEqual(all_windows[-1], tuple(map(str, range(5, 100)))) + + def test_zero(self): + iterable = map(str, range(100)) + with self.assertRaises(ValueError): + list(mi.sliding_window(iterable, 0)) + class SubslicesTests(TestCase): def test_basic(self): |