KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sablecc > sablecc > automaton > DfaStateTest


1 /* This file is part of SableCC ( http://sablecc.org ).
2  *
3  * Copyright 2007 Raymond Audet <raymond.audet@gmail.com>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 JavaDoc> dfa;
34
35     DfaState<Integer JavaDoc> dfaStartState;
36
37     Symbol<Integer JavaDoc> symbol;
38
39     Interval<Integer JavaDoc> interval;
40
41     @SuppressWarnings JavaDoc("unchecked")
42     @Before
43     public void setUp()
44             throws Exception JavaDoc {
45
46         this.interval = Realms.getInteger().createInterval(50);
47
48         this.symbol = new Symbol(this.interval);
49
50         this.dfa = new Dfa<Integer JavaDoc>(new Nfa<Integer JavaDoc>(this.interval).oneOrMore());
51
52         this.dfaStartState = this.dfa.getStartState();
53     }
54
55     @SuppressWarnings JavaDoc("unused")
56     @Test
57     public void testDfaState() {
58
59         // Case with null Dfa
60
Dfa<Integer JavaDoc> nullDfa = null;
61         try {
62             DfaState<Integer JavaDoc> dfaState = new DfaState<Integer JavaDoc>(nullDfa);
63             fail("dfa may not be null");
64         }
65         catch (InternalException e) {
66             // Expected
67
}
68
69         // Typical Case
70
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         // Case with null symbol
82
Symbol<Integer JavaDoc> nullSymbol = null;
83         try {
84             this.dfaStartState.getTarget(nullSymbol);
85             fail("symbol may not be null");
86         }
87         catch (InternalException e) {
88             // Expected
89
}
90
91         // Case with incorrect symbol
92
Symbol<Integer JavaDoc> newSymbol = new Symbol<Integer JavaDoc>(Realms.getInteger()
93                 .createInterval(999));
94         try {
95             this.dfaStartState.getTarget(newSymbol);
96             fail("invalid symbol");
97         }
98         catch (InternalException e) {
99             // Expected
100
}
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         // Case with lower state
117
DfaState<Integer JavaDoc> lowerState = this.dfa.getStates().first();
118         assertTrue("dfaStartState should be greater than lowerState",
119                 this.dfaStartState.compareTo(lowerState) > 0);
120
121         // Case with equal state
122
assertTrue("the two states should be equals", this.dfaStartState
123                 .compareTo(this.dfa.getStartState()) == 0);
124
125         // Case with greater state
126
DfaState<Integer JavaDoc> 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         // Case with an already stable State
135
try {
136             this.dfaStartState.addTransition(this.symbol, this.dfaStartState);
137             fail("a stable state may not be modified");
138         }
139         catch (InternalException e) {
140             // Expected
141
}
142     }
143
144     @Test
145     public void testRemoveTransitions() {
146
147         // Case with an already stable State
148
try {
149             this.dfaStartState.addTransition(this.symbol, this.dfaStartState);
150             fail("a stable state may not be modified");
151         }
152         catch (InternalException e) {
153             // Expected
154
}
155     }
156
157     @Test
158     public void testStabilize() {
159
160         // Case with an already stable State
161
try {
162             this.dfaStartState.addTransition(this.symbol, this.dfaStartState);
163             fail("this state is already stable");
164         }
165         catch (InternalException e) {
166             // Expected
167
}
168     }
169
170 }
171
Popular Tags