KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.SortedSet JavaDoc;
25 import java.util.TreeSet JavaDoc;
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 JavaDoc> stableNfa;
37
38     NfaCombineResult<Integer JavaDoc> result;
39
40     Nfa<Integer JavaDoc> nfa;
41
42     NfaState<Integer JavaDoc> nfaState;
43
44     @Before
45     public void setUp()
46             throws Exception JavaDoc {
47
48         // In order to get an unstable Nfa
49
this.stableNfa = new Nfa<Integer JavaDoc>();
50         this.result = this.stableNfa.combineWith(this.stableNfa);
51
52         this.nfa = this.result.getNewNfa();
53         this.nfaState = new NfaState<Integer JavaDoc>(this.nfa);
54     }
55
56     @SuppressWarnings JavaDoc("unused")
57     @Test
58     public void testNfaState() {
59
60         // Case with null Nfa.
61
try {
62             NfaState<Integer JavaDoc> nullNfaState = new NfaState<Integer JavaDoc>(null);
63             fail("nfa may not be null");
64         }
65         catch (InternalException e) {
66             // Expected
67
}
68
69         // Typical case.
70

71         assertEquals("nfa is not correct.", this.nfa, this.nfaState.getNfa());
72     }
73
74     @Test
75     public void testGetTransitions() {
76
77         // Case with unstable NfaState.
78
try {
79             this.nfaState.getTransitions();
80             fail("the state is not stable yet");
81         }
82         catch (InternalException e) {
83             // Expected
84
}
85
86         // Case with one added transition
87
NfaState<Integer JavaDoc> newNfaState = new NfaState<Integer JavaDoc>(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         // Case with unstable NfaState.
99
try {
100             this.nfaState.getTargets(null);
101             fail("the state is not stable yet");
102         }
103         catch (InternalException e) {
104             // Expected
105
}
106
107         // Typical case.
108
this.nfaState.stabilize();
109         final SortedSet JavaDoc<NfaState<Integer JavaDoc>> emptyNfaStateSet = new TreeSet JavaDoc<NfaState<Integer JavaDoc>>();
110
111         assertEquals("the target should be an empty NfaState",
112                 emptyNfaStateSet, this.nfaState.getTargets(null));
113
114         this.nfaState = new NfaState<Integer JavaDoc>(this.nfa);
115         NfaState<Integer JavaDoc> newNfaState = new NfaState<Integer JavaDoc>(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         // Case with wrong Nfa.
134
Nfa<Integer JavaDoc> secondStableNfa = new Nfa<Integer JavaDoc>();
135         this.result = secondStableNfa.combineWith(secondStableNfa);
136
137         Nfa<Integer JavaDoc> secondNfa = this.result.getNewNfa();
138         NfaState<Integer JavaDoc> secondNfaState = new NfaState<Integer JavaDoc>(secondNfa);
139
140         try {
141             secondNfaState.compareTo(this.nfaState);
142             fail("cannot compare states from distinct NFAs");
143         }
144         catch (InternalException e) {
145             // Expected
146
}
147
148         // Case with greater state.
149
NfaState<Integer JavaDoc> greaterNfaState = new NfaState<Integer JavaDoc>(this.nfa);
150
151         assertTrue("greatetNfaState should be greater than nfaState",
152                 greaterNfaState.compareTo(this.nfaState) > 0);
153
154         // Case with same state
155
assertTrue("the two states are equals", this.nfaState
156                 .compareTo(this.nfaState) == 0);
157     }
158
159     @Test
160     public void testAddTransition() {
161
162         // Case with null state.
163
try {
164             this.nfaState.addTransition(null, null);
165             fail("nfaState may not be null");
166         }
167         catch (InternalException e) {
168             // Expected
169
}
170
171         // Case with invalid Symbol
172
AdjacencyRealm<Integer JavaDoc> integerRealm = Realms.getInteger();
173         Symbol<Integer JavaDoc> symbol = new Symbol<Integer JavaDoc>(integerRealm
174                 .createInterval(10, 20));
175
176         try {
177             this.nfaState.addTransition(symbol, this.nfaState);
178             fail("invalid symbol");
179         }
180         catch (RuntimeException JavaDoc e1) {
181             // Expected
182
}
183
184         // Case with wrong nfa.
185
Nfa<Integer JavaDoc> secondStableNfa = new Nfa<Integer JavaDoc>();
186         this.result = secondStableNfa.combineWith(secondStableNfa);
187
188         Nfa<Integer JavaDoc> secondNfa = this.result.getNewNfa();
189         NfaState<Integer JavaDoc> secondNfaState = new NfaState<Integer JavaDoc>(secondNfa);
190
191         try {
192             this.nfaState.addTransition(null, secondNfaState);
193             fail("invalid nfaState");
194         }
195         catch (InternalException e) {
196             // Expected
197
}
198
199         // Typical case.
200
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 JavaDoc>(this.nfa);
207         NfaState<Integer JavaDoc> newNfaState = new NfaState<Integer JavaDoc>(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         // Case with already stable state.
217
try {
218             this.nfaState.addTransition(null, this.nfaState);
219             fail("a stable state may not be modified");
220         }
221         catch (InternalException e) {
222             // Expected
223
}
224     }
225
226     @Test
227     public void testStabilize() {
228
229         // Case with already stable state.
230
this.nfaState.stabilize();
231         try {
232             this.nfaState.stabilize();
233             fail("state is already stabilize");
234         }
235         catch (InternalException e) {
236             // Expected
237
}
238     }
239
240     @Test
241     public void testGetEpsilonReach() {
242
243         // Case with unstable state.
244
try {
245             this.nfaState.getEpsilonReach();
246             fail("the state is not stable yet");
247         }
248         catch (InternalException e) {
249             // Expected
250
}
251
252         // Typical Case.
253
this.nfaState.stabilize();
254         assertTrue("the epsilonReach should be nfaState only", this.nfaState
255                 .getEpsilonReach().size() == 1);
256
257         // Case with multiple transitions
258
this.nfaState = new NfaState<Integer JavaDoc>(this.nfa);
259         NfaState<Integer JavaDoc> secondState = new NfaState<Integer JavaDoc>(this.nfa);
260         NfaState<Integer JavaDoc> thirdState = new NfaState<Integer JavaDoc>(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