KickJava   Java API By Example, From Geeks To Geeks.

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


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 MinimalDfaStateTest {
32
33     MinimalDfaState<Integer JavaDoc> minimalDfaStartState;
34
35     MinimalDfa<Integer JavaDoc> minimalDfa;
36
37     Dfa<Integer JavaDoc> dfa;
38
39     Symbol<Integer JavaDoc> symbol;
40
41     Interval<Integer JavaDoc> interval;
42
43     @SuppressWarnings JavaDoc("unchecked")
44     @Before
45     public void setUp()
46             throws Exception JavaDoc {
47
48         this.interval = Realms.getInteger().createInterval(50);
49
50         this.symbol = new Symbol(this.interval);
51
52         this.dfa = new Dfa<Integer JavaDoc>(new Nfa<Integer JavaDoc>(this.interval).oneOrMore());
53
54         this.minimalDfa = new MinimalDfa<Integer JavaDoc>(this.dfa);
55
56         this.minimalDfaStartState = this.minimalDfa.getStartState();
57     }
58
59     @SuppressWarnings JavaDoc("unused")
60     @Test
61     public void testMinimalDfaState() {
62
63         // Case with null minimalDfa
64
MinimalDfa<Integer JavaDoc> nullMinimalDfa = null;
65         try {
66             MinimalDfaState<Integer JavaDoc> minimalDfaState = new MinimalDfaState<Integer JavaDoc>(
67                     nullMinimalDfa);
68             fail("minimalDfa may not be null");
69         }
70         catch (InternalException e) {
71             // Expected
72
}
73
74         // Typical Case
75
assertEquals("the state dont have the correct minimalDfa",
76                 this.minimalDfa, this.minimalDfaStartState.getMinimalDfa());
77         assertTrue(
78                 "from the startState, the correct symbol should lead to an acceptionState",
79                 this.minimalDfaStartState.getTarget(this.symbol) == this.minimalDfa
80                         .getAcceptStates().first());
81     }
82
83     @Test
84     public void testGetTarget() {
85
86         // Case with null symbol
87
Symbol<Integer JavaDoc> nullSymbol = null;
88         try {
89             this.minimalDfaStartState.getTarget(nullSymbol);
90             fail("symbol may not be null");
91         }
92         catch (InternalException e) {
93             // Expected
94
}
95
96         // Case with incorrect symbol
97
Symbol<Integer JavaDoc> newSymbol = new Symbol<Integer JavaDoc>(Realms.getInteger()
98                 .createInterval(999));
99         try {
100             this.minimalDfaStartState.getTarget(newSymbol);
101             fail("invalid symbol");
102         }
103         catch (InternalException e) {
104             // Expected
105
}
106     }
107
108     @Test
109     public void testEqualsObject() {
110
111         assertTrue("a minimalState should be equals to himself",
112                 this.minimalDfaStartState.equals(this.minimalDfaStartState));
113
114         assertTrue("the two minimalState should be equals",
115                 this.minimalDfaStartState.equals(this.minimalDfa
116                         .getStartState()));
117     }
118
119     @Test
120     public void testCompareTo() {
121
122         // Case with lower minimalState
123
MinimalDfaState<Integer JavaDoc> lowerMinimalState = this.minimalDfa
124                 .getStates().first();
125         assertTrue(
126                 "minimalDfaStartState should be greater than lowerMinimalState",
127                 this.minimalDfaStartState.compareTo(lowerMinimalState) > 0);
128
129         // Case with equal state
130
assertTrue("the two minimalState should be equals",
131                 this.minimalDfaStartState.compareTo(this.minimalDfa
132                         .getStartState()) == 0);
133
134         // Case with greater state
135
MinimalDfaState<Integer JavaDoc> greaterMinimalState = this.minimalDfa
136                 .getStates().last();
137         assertTrue(
138                 "minimalDfaStartState should be lower than greaterMinimalState",
139                 this.minimalDfaStartState.compareTo(greaterMinimalState) < 0);
140     }
141
142     @Test
143     public void testAddTransition() {
144
145         // Case with an already stable minimalState
146
try {
147             this.minimalDfaStartState.addTransition(this.symbol,
148                     this.minimalDfaStartState);
149             fail("a stable state may not be modified");
150         }
151         catch (InternalException e) {
152             // Expected
153
}
154     }
155
156     @Test
157     public void testStabilize() {
158
159         // Case with an already stable minimalState
160
try {
161             this.minimalDfaStartState.stabilize();
162             fail("the minimalState is already stable");
163         }
164         catch (InternalException e) {
165             // Expected
166
}
167     }
168
169 }
170
Popular Tags