1 17 18 package org.sablecc.sablecc.automaton; 19 20 import static org.junit.Assert.assertEquals; 21 import static org.junit.Assert.assertTrue; 22 import static org.junit.Assert.fail; 23 24 import java.util.SortedSet ; 25 import java.util.TreeSet ; 26 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.sablecc.sablecc.alphabet.AdjacencyRealm; 30 import org.sablecc.sablecc.alphabet.Realms; 31 import org.sablecc.sablecc.alphabet.Symbol; 32 import org.sablecc.sablecc.exception.InternalException; 33 34 public class NfaStateTest { 35 36 Nfa<Integer > stableNfa; 37 38 NfaCombineResult<Integer > result; 39 40 Nfa<Integer > nfa; 41 42 NfaState<Integer > nfaState; 43 44 @Before 45 public void setUp() 46 throws Exception { 47 48 this.stableNfa = new Nfa<Integer >(); 50 this.result = this.stableNfa.combineWith(this.stableNfa); 51 52 this.nfa = this.result.getNewNfa(); 53 this.nfaState = new NfaState<Integer >(this.nfa); 54 } 55 56 @SuppressWarnings ("unused") 57 @Test 58 public void testNfaState() { 59 60 try { 62 NfaState<Integer > nullNfaState = new NfaState<Integer >(null); 63 fail("nfa may not be null"); 64 } 65 catch (InternalException e) { 66 } 68 69 71 assertEquals("nfa is not correct.", this.nfa, this.nfaState.getNfa()); 72 } 73 74 @Test 75 public void testGetTransitions() { 76 77 try { 79 this.nfaState.getTransitions(); 80 fail("the state is not stable yet"); 81 } 82 catch (InternalException e) { 83 } 85 86 NfaState<Integer > newNfaState = new NfaState<Integer >(this.nfa); 88 89 this.nfaState.addTransition(null, newNfaState); 90 this.nfaState.stabilize(); 91 assertEquals("nfaState don't have one new transition", 1, this.nfaState 92 .getTransitions().size()); 93 } 94 95 @Test 96 public void testGetTargets() { 97 98 try { 100 this.nfaState.getTargets(null); 101 fail("the state is not stable yet"); 102 } 103 catch (InternalException e) { 104 } 106 107 this.nfaState.stabilize(); 109 final SortedSet <NfaState<Integer >> emptyNfaStateSet = new TreeSet <NfaState<Integer >>(); 110 111 assertEquals("the target should be an empty NfaState", 112 emptyNfaStateSet, this.nfaState.getTargets(null)); 113 114 this.nfaState = new NfaState<Integer >(this.nfa); 115 NfaState<Integer > newNfaState = new NfaState<Integer >(this.nfa); 116 this.nfaState.addTransition(null, newNfaState); 117 this.nfaState.stabilize(); 118 119 assertEquals("the target should be newSfaState", newNfaState, 120 this.nfaState.getTargets(null).first()); 121 } 122 123 @Test 124 public void testEqualsObject() { 125 126 assertTrue("a state should be equals to itself", this.nfaState 127 .equals(this.nfaState)); 128 } 129 130 @Test 131 public void testCompareTo() { 132 133 Nfa<Integer > secondStableNfa = new Nfa<Integer >(); 135 this.result = secondStableNfa.combineWith(secondStableNfa); 136 137 Nfa<Integer > secondNfa = this.result.getNewNfa(); 138 NfaState<Integer > secondNfaState = new NfaState<Integer >(secondNfa); 139 140 try { 141 secondNfaState.compareTo(this.nfaState); 142 fail("cannot compare states from distinct NFAs"); 143 } 144 catch (InternalException e) { 145 } 147 148 NfaState<Integer > greaterNfaState = new NfaState<Integer >(this.nfa); 150 151 assertTrue("greatetNfaState should be greater than nfaState", 152 greaterNfaState.compareTo(this.nfaState) > 0); 153 154 assertTrue("the two states are equals", this.nfaState 156 .compareTo(this.nfaState) == 0); 157 } 158 159 @Test 160 public void testAddTransition() { 161 162 try { 164 this.nfaState.addTransition(null, null); 165 fail("nfaState may not be null"); 166 } 167 catch (InternalException e) { 168 } 170 171 AdjacencyRealm<Integer > integerRealm = Realms.getInteger(); 173 Symbol<Integer > symbol = new Symbol<Integer >(integerRealm 174 .createInterval(10, 20)); 175 176 try { 177 this.nfaState.addTransition(symbol, this.nfaState); 178 fail("invalid symbol"); 179 } 180 catch (RuntimeException e1) { 181 } 183 184 Nfa<Integer > secondStableNfa = new Nfa<Integer >(); 186 this.result = secondStableNfa.combineWith(secondStableNfa); 187 188 Nfa<Integer > secondNfa = this.result.getNewNfa(); 189 NfaState<Integer > secondNfaState = new NfaState<Integer >(secondNfa); 190 191 try { 192 this.nfaState.addTransition(null, secondNfaState); 193 fail("invalid nfaState"); 194 } 195 catch (InternalException e) { 196 } 198 199 this.nfaState.addTransition(null, this.nfaState); 201 202 this.nfaState.stabilize(); 203 assertTrue("there should be only one transition", this.nfaState 204 .getTransitions().size() == 1); 205 206 this.nfaState = new NfaState<Integer >(this.nfa); 207 NfaState<Integer > newNfaState = new NfaState<Integer >(this.nfa); 208 this.nfaState.addTransition(null, this.nfaState); 209 this.nfaState.addTransition(null, newNfaState); 210 211 this.nfaState.stabilize(); 212 213 assertTrue("there should be two targets for null transition", 214 this.nfaState.getTargets(null).size() == 2); 215 216 try { 218 this.nfaState.addTransition(null, this.nfaState); 219 fail("a stable state may not be modified"); 220 } 221 catch (InternalException e) { 222 } 224 } 225 226 @Test 227 public void testStabilize() { 228 229 this.nfaState.stabilize(); 231 try { 232 this.nfaState.stabilize(); 233 fail("state is already stabilize"); 234 } 235 catch (InternalException e) { 236 } 238 } 239 240 @Test 241 public void testGetEpsilonReach() { 242 243 try { 245 this.nfaState.getEpsilonReach(); 246 fail("the state is not stable yet"); 247 } 248 catch (InternalException e) { 249 } 251 252 this.nfaState.stabilize(); 254 assertTrue("the epsilonReach should be nfaState only", this.nfaState 255 .getEpsilonReach().size() == 1); 256 257 this.nfaState = new NfaState<Integer >(this.nfa); 259 NfaState<Integer > secondState = new NfaState<Integer >(this.nfa); 260 NfaState<Integer > thirdState = new NfaState<Integer >(this.nfa); 261 262 secondState.addTransition(null, thirdState); 263 this.nfaState.addTransition(null, secondState); 264 this.nfaState.stabilize(); 265 secondState.stabilize(); 266 thirdState.stabilize(); 267 assertTrue( 268 "the epsilonReach should be to nfaState, second and third state", 269 this.nfaState.getEpsilonReach().size() == 3); 270 } 271 272 } 273 | Popular Tags |