您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

87 行
2.7 KiB

  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), ["123"],
  23. )
  24. def test_long_seq(self):
  25. parts = chunk_seq("abcdefghijklmnop", 8)
  26. self.assertEqual(
  27. list(parts), ["abcdefgh", "ijklmnop"],
  28. )
  29. def test_uneven_parts(self):
  30. parts = chunk_seq("abcdefghijklmnop", 5)
  31. self.assertEqual(
  32. list(parts), ["abcde", "fghij", "klmno", "p"],
  33. )
  34. def test_empty_input(self):
  35. parts = chunk_seq([], 5)
  36. self.assertEqual(
  37. list(parts), [],
  38. )
  39. class SortTopologically(TestCase):
  40. def test_empty(self):
  41. "Test that an empty graph works correctly"
  42. graph = {} # type: Dict[int, List[int]]
  43. self.assertEqual(list(sorted_topologically([], graph)), [])
  44. def test_disconnected(self):
  45. "Test that a graph with no edges work"
  46. graph = {1: [], 2: []} # type: Dict[int, List[int]]
  47. # For disconnected nodes the output is simply sorted.
  48. self.assertEqual(list(sorted_topologically([1, 2], graph)), [1, 2])
  49. def test_linear(self):
  50. "Test that a simple `4 -> 3 -> 2 -> 1` graph works"
  51. graph = {1: [], 2: [1], 3: [2], 4: [3]} # type: Dict[int, List[int]]
  52. self.assertEqual(list(sorted_topologically([4, 3, 2, 1], graph)), [1, 2, 3, 4])
  53. def test_subset(self):
  54. "Test that only sorting a subset of the graph works"
  55. graph = {1: [], 2: [1], 3: [2], 4: [3]} # type: Dict[int, List[int]]
  56. self.assertEqual(list(sorted_topologically([4, 3], graph)), [3, 4])
  57. def test_fork(self):
  58. "Test that a forked graph works"
  59. graph = {1: [], 2: [1], 3: [1], 4: [2, 3]} # type: Dict[int, List[int]]
  60. # Valid orderings are `[1, 3, 2, 4]` or `[1, 2, 3, 4]`, but we should
  61. # always get the same one.
  62. self.assertEqual(list(sorted_topologically([4, 3, 2, 1], graph)), [1, 2, 3, 4])