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.

ConditionTreeTest.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2006-2008 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 extends junit.framework.TestCase {
  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. }
  111. @Test
  112. public void testCreateConjunction() {
  113. final String expected = "(((0&1)&2)&3)";
  114. final ConditionTree tree = ConditionTree.createConjunction(4);
  115. assertNotNull(tree);
  116. assertEquals(expected, tree.toString());
  117. }
  118. @Test
  119. public void testCreateDisjunction() {
  120. final String expected = "(((0|1)|2)|3)";
  121. final ConditionTree tree = ConditionTree.createDisjunction(4);
  122. assertNotNull(tree);
  123. assertEquals(expected, tree.toString());
  124. }
  125. }