You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TopoSorterTest.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. Copyright (c) 2018 James Ahlborn
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.impl;
  14. import java.util.ArrayList;
  15. import java.util.Arrays;
  16. import java.util.Collections;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. import junit.framework.TestCase;
  21. /**
  22. *
  23. * @author James Ahlborn
  24. */
  25. public class TopoSorterTest extends TestCase
  26. {
  27. public TopoSorterTest(String name) {
  28. super(name);
  29. }
  30. public void testTopoSort() throws Exception
  31. {
  32. doTopoTest(Arrays.asList("A", "B", "C"),
  33. Arrays.asList("A", "B", "C"));
  34. doTopoTest(Arrays.asList("B", "A", "C"),
  35. Arrays.asList("A", "B", "C"),
  36. "B", "C",
  37. "A", "B");
  38. try {
  39. doTopoTest(Arrays.asList("B", "A", "C"),
  40. Arrays.asList("C", "B", "A"),
  41. "B", "C",
  42. "A", "B",
  43. "C", "A");
  44. fail("IllegalStateException should have been thrown");
  45. } catch(IllegalStateException expected) {
  46. // success
  47. assertTrue(expected.getMessage().startsWith("Cycle"));
  48. }
  49. try {
  50. doTopoTest(Arrays.asList("B", "A", "C"),
  51. Arrays.asList("C", "B", "A"),
  52. "B", "D");
  53. fail("IllegalStateException should have been thrown");
  54. } catch(IllegalStateException expected) {
  55. // success
  56. assertTrue(expected.getMessage().startsWith("Unknown descendent"));
  57. }
  58. doTopoTest(Arrays.asList("B", "D", "A", "C"),
  59. Arrays.asList("D", "A", "B", "C"),
  60. "B", "C",
  61. "A", "B");
  62. doTopoTest(Arrays.asList("B", "D", "A", "C"),
  63. Arrays.asList("A", "D", "B", "C"),
  64. "B", "C",
  65. "A", "B",
  66. "A", "D");
  67. doTopoTest(Arrays.asList("B", "D", "A", "C"),
  68. Arrays.asList("D", "A", "C", "B"),
  69. "D", "A",
  70. "C", "B");
  71. doTopoTest(Arrays.asList("B", "D", "A", "C"),
  72. Arrays.asList("D", "C", "A", "B"),
  73. "D", "A",
  74. "C", "B",
  75. "C", "A");
  76. doTopoTest(Arrays.asList("B", "D", "A", "C"),
  77. Arrays.asList("C", "D", "A", "B"),
  78. "D", "A",
  79. "C", "B",
  80. "C", "D");
  81. doTopoTest(Arrays.asList("B", "D", "A", "C"),
  82. Arrays.asList("D", "A", "C", "B"),
  83. "D", "A",
  84. "C", "B",
  85. "D", "B");
  86. }
  87. private static void doTopoTest(List<String> original,
  88. List<String> expected,
  89. String... descs) {
  90. List<String> values = new ArrayList<String>();
  91. values.addAll(original);
  92. TestTopoSorter tsorter = new TestTopoSorter(values, false);
  93. for(int i = 0; i < descs.length; i+=2) {
  94. tsorter.addDescendents(descs[i], descs[i+1]);
  95. }
  96. tsorter.sort();
  97. assertEquals(expected, values);
  98. values = new ArrayList<String>();
  99. values.addAll(original);
  100. tsorter = new TestTopoSorter(values, true);
  101. for(int i = 0; i < descs.length; i+=2) {
  102. tsorter.addDescendents(descs[i], descs[i+1]);
  103. }
  104. tsorter.sort();
  105. List<String> expectedReverse = new ArrayList<String>(expected);
  106. Collections.reverse(expectedReverse);
  107. assertEquals(expectedReverse, values);
  108. }
  109. private static class TestTopoSorter extends TopoSorter<String>
  110. {
  111. private final Map<String,List<String>> _descMap =
  112. new HashMap<String,List<String>>();
  113. protected TestTopoSorter(List<String> values, boolean reverse) {
  114. super(values, reverse);
  115. }
  116. public void addDescendents(String from, String... tos) {
  117. List<String> descs = _descMap.get(from);
  118. if(descs == null) {
  119. descs = new ArrayList<String>();
  120. _descMap.put(from, descs);
  121. }
  122. descs.addAll(Arrays.asList(tos));
  123. }
  124. @Override
  125. protected void getDescendents(String from, List<String> descendents) {
  126. List<String> descs = _descMap.get(from);
  127. if(descs != null) {
  128. descendents.addAll(descs);
  129. }
  130. }
  131. }
  132. }