test_itertools.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from synapse.util.iterutils import chunk_seq
  16. from tests.unittest import TestCase
  17. class ChunkSeqTests(TestCase):
  18. def test_short_seq(self):
  19. parts = chunk_seq("123", 8)
  20. self.assertEqual(
  21. list(parts), ["123"],
  22. )
  23. def test_long_seq(self):
  24. parts = chunk_seq("abcdefghijklmnop", 8)
  25. self.assertEqual(
  26. list(parts), ["abcdefgh", "ijklmnop"],
  27. )
  28. def test_uneven_parts(self):
  29. parts = chunk_seq("abcdefghijklmnop", 5)
  30. self.assertEqual(
  31. list(parts), ["abcde", "fghij", "klmno", "p"],
  32. )
  33. def test_empty_input(self):
  34. parts = chunk_seq([], 5)
  35. self.assertEqual(
  36. list(parts), [],
  37. )