Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ConditionTreeTest.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.actions;
  23. import org.junit.Test;
  24. import static org.junit.Assert.*;
  25. public class ConditionTreeTest {
  26. @Test
  27. public void testParseString() {
  28. String[][] testCases = {
  29. {"1", "1"},
  30. {"50", "50"},
  31. {"!50", "!50"},
  32. {"1&1", "(1&1)"},
  33. {"1&!1", "(1&!1)"},
  34. {"!1&1", "(!1&1)"},
  35. {"(1)", "1"},
  36. {"((((1))))", "1"},
  37. {"(1&1)&1", "((1&1)&1)"},
  38. {"1&2&3", "((1&2)&3)"},
  39. {"(1&2&3)|4", "(((1&2)&3)|4)"},
  40. {"!(1|!1)", "!(1|!1)"},
  41. {"!!1", "!!1"},
  42. {"1 & !(2 | (3 & !4))", "(1&!(2|(3&!4)))"},
  43. {"", ""},
  44. {"((1|((2&!3)&(!4))|6)&7)|!8", "((((1|((2&!3)&!4))|6)&7)|!8)"},
  45. };
  46. for (String[] testCase : testCases) {
  47. System.out.println("\nInput: " + testCase[0]);
  48. final ConditionTree res = ConditionTree.parseString(testCase[0]);
  49. assertNotNull(res);
  50. System.out.println(" Expected: " + testCase[1]);
  51. System.out.println(" Result: " + res);
  52. assertEquals(testCase[1], res.toString());
  53. }
  54. }
  55. @Test
  56. public void testEvaluate() {
  57. System.out.println();
  58. final String target = "((0&1&2)|3)&(!4)";
  59. final ConditionTree tree = ConditionTree.parseString(target);
  60. assertNotNull(tree);
  61. boolean[][] testCases = {
  62. {true, true, true, true, true},
  63. {true, true, true, true, false},
  64. {true, true, true, false, true},
  65. {true, true, true, false, false},
  66. {true, true, false, true, true},
  67. {true, true, false, true, false},
  68. {true, true, false, false, true},
  69. {true, true, false, false, false},
  70. {true, false, true, true, true},
  71. {true, false, true, true, false},
  72. {true, false, true, false, true},
  73. {true, false, true, false, false},
  74. {true, false, false, true, true},
  75. {true, false, false, true, false},
  76. {true, false, false, false, true},
  77. {true, false, false, false, false},
  78. {false, true, true, true, true},
  79. {false, true, true, true, false},
  80. {false, true, true, false, true},
  81. {false, true, true, false, false},
  82. {false, true, false, true, true},
  83. {false, true, false, true, false},
  84. {false, true, false, false, true},
  85. {false, true, false, false, false},
  86. {false, false, true, true, true},
  87. {false, false, true, true, false},
  88. {false, false, true, false, true},
  89. {false, false, true, false, false},
  90. {false, false, false, true, true},
  91. {false, false, false, true, false},
  92. {false, false, false, false, true},
  93. {false, false, false, false, false},
  94. };
  95. //final String target = "((0&1&2)|3)&(!4)";
  96. for (boolean[] testCase : testCases) {
  97. final boolean expected =
  98. ((testCase[0] && testCase[1] && testCase[2]) || testCase[3])
  99. && !testCase[4];
  100. final boolean actual = tree.evaluate(testCase);
  101. assertEquals(expected, actual);
  102. }
  103. }
  104. @Test
  105. public void testGetNumArgs() {
  106. final String target = "((0&1&2)|3)&(!4)";
  107. final ConditionTree tree = ConditionTree.parseString(target);
  108. assertNotNull(tree);
  109. assertEquals(4, tree.getMaximumArgument());
  110. final String target2 = "";
  111. assertEquals(0, ConditionTree.parseString(target2).getMaximumArgument());
  112. }
  113. @Test
  114. public void testCreateConjunction() {
  115. final String expected = "(((0&1)&2)&3)";
  116. final ConditionTree tree = ConditionTree.createConjunction(4);
  117. assertNotNull(tree);
  118. assertEquals(expected, tree.toString());
  119. }
  120. @Test
  121. public void testCreateDisjunction() {
  122. final String expected = "(((0|1)|2)|3)";
  123. final ConditionTree tree = ConditionTree.createDisjunction(4);
  124. assertNotNull(tree);
  125. assertEquals(expected, tree.toString());
  126. }
  127. @Test
  128. public void testMismatchedBrackets() {
  129. final ConditionTree tree = ConditionTree.parseString("(0");
  130. assertNull(tree);
  131. }
  132. @Test
  133. public void testMismatchedBrackets2() {
  134. final ConditionTree tree = ConditionTree.parseString("0)");
  135. assertNull(tree);
  136. }
  137. @Test
  138. public void testMissingUnaryArg() {
  139. final ConditionTree tree = ConditionTree.parseString("!");
  140. assertNull(tree);
  141. }
  142. @Test
  143. public void testGarbageUnaryArg() {
  144. final ConditionTree tree = ConditionTree.parseString("!xy");
  145. assertNull(tree);
  146. }
  147. @Test
  148. public void testMissingBinaryArg() {
  149. final ConditionTree tree = ConditionTree.parseString("0|");
  150. assertNull(tree);
  151. }
  152. @Test
  153. public void testMissingBinaryArg2() {
  154. final ConditionTree tree = ConditionTree.parseString("0|!");
  155. assertNull(tree);
  156. }
  157. @Test
  158. public void testNonExistantOp() {
  159. final ConditionTree tree = ConditionTree.parseString("0/1");
  160. assertNull(tree);
  161. }
  162. @Test
  163. public void testNoopEvaluation() {
  164. final ConditionTree tree = ConditionTree.parseString("");
  165. assertTrue(tree.evaluate(new boolean[]{true, false, true}));
  166. }
  167. @Test
  168. public void testBracketedUnary() {
  169. final ConditionTree tree = ConditionTree.parseString("(+)");
  170. assertNull(tree);
  171. }
  172. }