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 org.junit.Before; 25 import org.junit.Test; 26 import org.sablecc.sablecc.alphabet.Interval; 27 import org.sablecc.sablecc.alphabet.Realms; 28 import org.sablecc.sablecc.alphabet.Symbol; 29 import org.sablecc.sablecc.exception.InternalException; 30 31 public class DfaStateTest { 32 33 Dfa<Integer > dfa; 34 35 DfaState<Integer > dfaStartState; 36 37 Symbol<Integer > symbol; 38 39 Interval<Integer > interval; 40 41 @SuppressWarnings ("unchecked") 42 @Before 43 public void setUp() 44 throws Exception { 45 46 this.interval = Realms.getInteger().createInterval(50); 47 48 this.symbol = new Symbol(this.interval); 49 50 this.dfa = new Dfa<Integer >(new Nfa<Integer >(this.interval).oneOrMore()); 51 52 this.dfaStartState = this.dfa.getStartState(); 53 } 54 55 @SuppressWarnings ("unused") 56 @Test 57 public void testDfaState() { 58 59 Dfa<Integer > nullDfa = null; 61 try { 62 DfaState<Integer > dfaState = new DfaState<Integer >(nullDfa); 63 fail("dfa may not be null"); 64 } 65 catch (InternalException e) { 66 } 68 69 assertEquals("the state dont have the correct dfa", this.dfa, 71 this.dfaStartState.getDfa()); 72 assertTrue( 73 "from the startState, the correct symbol should lead to an acceptionState", 74 this.dfaStartState.getTarget(this.symbol) == this.dfa 75 .getAcceptStates().first()); 76 } 77 78 @Test 79 public void testGetTarget() { 80 81 Symbol<Integer > nullSymbol = null; 83 try { 84 this.dfaStartState.getTarget(nullSymbol); 85 fail("symbol may not be null"); 86 } 87 catch (InternalException e) { 88 } 90 91 Symbol<Integer > newSymbol = new Symbol<Integer >(Realms.getInteger() 93 .createInterval(999)); 94 try { 95 this.dfaStartState.getTarget(newSymbol); 96 fail("invalid symbol"); 97 } 98 catch (InternalException e) { 99 } 101 } 102 103 @Test 104 public void testEqualsObject() { 105 106 assertTrue("a state should be equals to himself", this.dfaStartState 107 .equals(this.dfaStartState)); 108 109 assertTrue("the two states should be equals", this.dfaStartState 110 .equals(this.dfa.getStartState())); 111 } 112 113 @Test 114 public void testCompareTo() { 115 116 DfaState<Integer > lowerState = this.dfa.getStates().first(); 118 assertTrue("dfaStartState should be greater than lowerState", 119 this.dfaStartState.compareTo(lowerState) > 0); 120 121 assertTrue("the two states should be equals", this.dfaStartState 123 .compareTo(this.dfa.getStartState()) == 0); 124 125 DfaState<Integer > greaterState = this.dfa.getStates().last(); 127 assertTrue("dfaStartState should be lower than greaterState", 128 this.dfaStartState.compareTo(greaterState) < 0); 129 } 130 131 @Test 132 public void testAddTransition() { 133 134 try { 136 this.dfaStartState.addTransition(this.symbol, this.dfaStartState); 137 fail("a stable state may not be modified"); 138 } 139 catch (InternalException e) { 140 } 142 } 143 144 @Test 145 public void testRemoveTransitions() { 146 147 try { 149 this.dfaStartState.addTransition(this.symbol, this.dfaStartState); 150 fail("a stable state may not be modified"); 151 } 152 catch (InternalException e) { 153 } 155 } 156 157 @Test 158 public void testStabilize() { 159 160 try { 162 this.dfaStartState.addTransition(this.symbol, this.dfaStartState); 163 fail("this state is already stable"); 164 } 165 catch (InternalException e) { 166 } 168 } 169 170 } 171 | Popular Tags |