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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  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 testGetNumArgs() {
  28. final String target = "((0&1&2)|3)&(!4)";
  29. final ConditionTree tree = ConditionTree.parseString(target);
  30. assertNotNull(tree);
  31. assertEquals(4, tree.getMaximumArgument());
  32. final String target2 = "";
  33. assertEquals(0, ConditionTree.parseString(target2).getMaximumArgument());
  34. }
  35. @Test
  36. public void testCreateConjunction() {
  37. final String expected = "(((0&1)&2)&3)";
  38. final ConditionTree tree = ConditionTree.createConjunction(4);
  39. assertNotNull(tree);
  40. assertEquals(expected, tree.toString());
  41. }
  42. @Test
  43. public void testCreateDisjunction() {
  44. final String expected = "(((0|1)|2)|3)";
  45. final ConditionTree tree = ConditionTree.createDisjunction(4);
  46. assertNotNull(tree);
  47. assertEquals(expected, tree.toString());
  48. }
  49. @Test
  50. public void testMismatchedBrackets() {
  51. final ConditionTree tree = ConditionTree.parseString("(0");
  52. assertNull(tree);
  53. }
  54. @Test
  55. public void testMismatchedBrackets2() {
  56. final ConditionTree tree = ConditionTree.parseString("0)");
  57. assertNull(tree);
  58. }
  59. @Test
  60. public void testMissingUnaryArg() {
  61. final ConditionTree tree = ConditionTree.parseString("!");
  62. assertNull(tree);
  63. }
  64. @Test
  65. public void testGarbageUnaryArg() {
  66. final ConditionTree tree = ConditionTree.parseString("!xy");
  67. assertNull(tree);
  68. }
  69. @Test
  70. public void testMissingBinaryArg() {
  71. final ConditionTree tree = ConditionTree.parseString("0|");
  72. assertNull(tree);
  73. }
  74. @Test
  75. public void testMissingBinaryArg2() {
  76. final ConditionTree tree = ConditionTree.parseString("0|!");
  77. assertNull(tree);
  78. }
  79. @Test
  80. public void testNonExistantOp() {
  81. final ConditionTree tree = ConditionTree.parseString("0/1");
  82. assertNull(tree);
  83. }
  84. @Test
  85. public void testHugeNumber() {
  86. final ConditionTree tree = ConditionTree.parseString("9999999999999999");
  87. assertNull(tree);
  88. }
  89. @Test
  90. public void testNoopEvaluation() {
  91. final ConditionTree tree = ConditionTree.parseString("");
  92. assertTrue(tree.evaluate(new boolean[]{true, false, true}));
  93. }
  94. @Test
  95. public void testBracketedUnary() {
  96. final ConditionTree tree = ConditionTree.parseString("(+)");
  97. assertNull(tree);
  98. }
  99. @Test
  100. public void testEquals() {
  101. final ConditionTree tree1 = ConditionTree.parseString("(1&3)");
  102. final ConditionTree tree2 = ConditionTree.parseString("((1&(3)))");
  103. assertTrue(tree1.equals(tree2));
  104. assertTrue(tree2.equals(tree1));
  105. assertEquals(tree1.hashCode(), tree2.hashCode());
  106. }
  107. @Test
  108. public void testNotEquals() {
  109. final ConditionTree tree1 = ConditionTree.parseString("(1&3)");
  110. final ConditionTree tree2 = ConditionTree.parseString("((1&(2)))");
  111. assertFalse(tree1.equals(tree2));
  112. assertFalse(tree2.equals(tree1));
  113. }
  114. }