1
0

test_itertools.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright 2020 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Dict, Iterable, List, Sequence
  15. from synapse.util.iterutils import chunk_seq, sorted_topologically
  16. from tests.unittest import TestCase
  17. class ChunkSeqTests(TestCase):
  18. def test_short_seq(self) -> None:
  19. parts = chunk_seq("123", 8)
  20. self.assertEqual(
  21. list(parts),
  22. ["123"],
  23. )
  24. def test_long_seq(self) -> None:
  25. parts = chunk_seq("abcdefghijklmnop", 8)
  26. self.assertEqual(
  27. list(parts),
  28. ["abcdefgh", "ijklmnop"],
  29. )
  30. def test_uneven_parts(self) -> None:
  31. parts = chunk_seq("abcdefghijklmnop", 5)
  32. self.assertEqual(
  33. list(parts),
  34. ["abcde", "fghij", "klmno", "p"],
  35. )
  36. def test_empty_input(self) -> None:
  37. parts: Iterable[Sequence] = chunk_seq([], 5)
  38. self.assertEqual(
  39. list(parts),
  40. [],
  41. )
  42. class SortTopologically(TestCase):
  43. def test_empty(self) -> None:
  44. "Test that an empty graph works correctly"
  45. graph: Dict[int, List[int]] = {}
  46. self.assertEqual(list(sorted_topologically([], graph)), [])
  47. def test_handle_empty_graph(self) -> None:
  48. "Test that a graph where a node doesn't have an entry is treated as empty"
  49. graph: Dict[int, List[int]] = {}
  50. # For disconnected nodes the output is simply sorted.
  51. self.assertEqual(list(sorted_topologically([1, 2], graph)), [1, 2])
  52. def test_disconnected(self) -> None:
  53. "Test that a graph with no edges work"
  54. graph: Dict[int, List[int]] = {1: [], 2: []}
  55. # For disconnected nodes the output is simply sorted.
  56. self.assertEqual(list(sorted_topologically([1, 2], graph)), [1, 2])
  57. def test_linear(self) -> None:
  58. "Test that a simple `4 -> 3 -> 2 -> 1` graph works"
  59. graph: Dict[int, List[int]] = {1: [], 2: [1], 3: [2], 4: [3]}
  60. self.assertEqual(list(sorted_topologically([4, 3, 2, 1], graph)), [1, 2, 3, 4])
  61. def test_subset(self) -> None:
  62. "Test that only sorting a subset of the graph works"
  63. graph: Dict[int, List[int]] = {1: [], 2: [1], 3: [2], 4: [3]}
  64. self.assertEqual(list(sorted_topologically([4, 3], graph)), [3, 4])
  65. def test_fork(self) -> None:
  66. "Test that a forked graph works"
  67. graph: Dict[int, List[int]] = {1: [], 2: [1], 3: [1], 4: [2, 3]}
  68. # Valid orderings are `[1, 3, 2, 4]` or `[1, 2, 3, 4]`, but we should
  69. # always get the same one.
  70. self.assertEqual(list(sorted_topologically([4, 3, 2, 1], graph)), [1, 2, 3, 4])
  71. def test_duplicates(self) -> None:
  72. "Test that a graph with duplicate edges work"
  73. graph: Dict[int, List[int]] = {1: [], 2: [1, 1], 3: [2, 2], 4: [3]}
  74. self.assertEqual(list(sorted_topologically([4, 3, 2, 1], graph)), [1, 2, 3, 4])
  75. def test_multiple_paths(self) -> None:
  76. "Test that a graph with multiple paths between two nodes work"
  77. graph: Dict[int, List[int]] = {1: [], 2: [1], 3: [2], 4: [3, 2, 1]}
  78. self.assertEqual(list(sorted_topologically([4, 3, 2, 1], graph)), [1, 2, 3, 4])