test_itertools.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 typing import Dict, List
  16. from synapse.util.iterutils import chunk_seq, sorted_topologically
  17. from tests.unittest import TestCase
  18. class ChunkSeqTests(TestCase):
  19. def test_short_seq(self):
  20. parts = chunk_seq("123", 8)
  21. self.assertEqual(
  22. list(parts),
  23. ["123"],
  24. )
  25. def test_long_seq(self):
  26. parts = chunk_seq("abcdefghijklmnop", 8)
  27. self.assertEqual(
  28. list(parts),
  29. ["abcdefgh", "ijklmnop"],
  30. )
  31. def test_uneven_parts(self):
  32. parts = chunk_seq("abcdefghijklmnop", 5)
  33. self.assertEqual(
  34. list(parts),
  35. ["abcde", "fghij", "klmno", "p"],
  36. )
  37. def test_empty_input(self):
  38. parts = chunk_seq([], 5)
  39. self.assertEqual(
  40. list(parts),
  41. [],
  42. )
  43. class SortTopologically(TestCase):
  44. def test_empty(self):
  45. "Test that an empty graph works correctly"
  46. graph = {} # type: Dict[int, List[int]]
  47. self.assertEqual(list(sorted_topologically([], graph)), [])
  48. def test_handle_empty_graph(self):
  49. "Test that a graph where a node doesn't have an entry is treated as empty"
  50. graph = {} # type: Dict[int, List[int]]
  51. # For disconnected nodes the output is simply sorted.
  52. self.assertEqual(list(sorted_topologically([1, 2], graph)), [1, 2])
  53. def test_disconnected(self):
  54. "Test that a graph with no edges work"
  55. graph = {1: [], 2: []} # type: Dict[int, List[int]]
  56. # For disconnected nodes the output is simply sorted.
  57. self.assertEqual(list(sorted_topologically([1, 2], graph)), [1, 2])
  58. def test_linear(self):
  59. "Test that a simple `4 -> 3 -> 2 -> 1` graph works"
  60. graph = {1: [], 2: [1], 3: [2], 4: [3]} # type: Dict[int, List[int]]
  61. self.assertEqual(list(sorted_topologically([4, 3, 2, 1], graph)), [1, 2, 3, 4])
  62. def test_subset(self):
  63. "Test that only sorting a subset of the graph works"
  64. graph = {1: [], 2: [1], 3: [2], 4: [3]} # type: Dict[int, List[int]]
  65. self.assertEqual(list(sorted_topologically([4, 3], graph)), [3, 4])
  66. def test_fork(self):
  67. "Test that a forked graph works"
  68. graph = {1: [], 2: [1], 3: [1], 4: [2, 3]} # type: Dict[int, List[int]]
  69. # Valid orderings are `[1, 3, 2, 4]` or `[1, 2, 3, 4]`, but we should
  70. # always get the same one.
  71. self.assertEqual(list(sorted_topologically([4, 3, 2, 1], graph)), [1, 2, 3, 4])
  72. def test_duplicates(self):
  73. "Test that a graph with duplicate edges work"
  74. graph = {1: [], 2: [1, 1], 3: [2, 2], 4: [3]} # type: Dict[int, List[int]]
  75. self.assertEqual(list(sorted_topologically([4, 3, 2, 1], graph)), [1, 2, 3, 4])
  76. def test_multiple_paths(self):
  77. "Test that a graph with multiple paths between two nodes work"
  78. graph = {1: [], 2: [1], 3: [2], 4: [3, 2, 1]} # type: Dict[int, List[int]]
  79. self.assertEqual(list(sorted_topologically([4, 3, 2, 1], graph)), [1, 2, 3, 4])