KickJava   Java API By Example, From Geeks To Geeks.

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


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.fail;
21
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.sablecc.sablecc.alphabet.Realms;
25 import org.sablecc.sablecc.exception.InternalException;
26
27 public class PartitionTest {
28
29     Partition<Integer JavaDoc> partition;
30
31     Dfa<Integer JavaDoc> dfa;
32
33     Nfa<Integer JavaDoc> nfa;
34
35     @Before
36     public void setUp()
37             throws Exception JavaDoc {
38
39         this.nfa = new Nfa<Integer JavaDoc>(Realms.getInteger().createInterval(50))
40                 .zeroOrMore();
41
42         this.dfa = new Dfa<Integer JavaDoc>(this.nfa);
43
44         this.partition = new Partition<Integer JavaDoc>(this.dfa);
45
46     }
47
48     @Test
49     public void testPartition() {
50
51         // Case with null dfa
52
Dfa<Integer JavaDoc> nullDfa = null;
53         try {
54             this.partition = new Partition<Integer JavaDoc>(nullDfa);
55             fail("dfa may not be null");
56         }
57         catch (InternalException e) {
58             // Expected
59
}
60     }
61
62     @Test
63     public void testAddGroup() {
64
65         // Case with null Group
66
Group<Integer JavaDoc> nullGroup = null;
67         try {
68             this.partition.addGroup(nullGroup);
69             fail("group may not be null");
70         }
71         catch (InternalException e) {
72             // Expected
73
}
74
75         // Case with invalid Group
76
Partition<Integer JavaDoc> newPartition = new Partition<Integer JavaDoc>(this.dfa);
77         Group<Integer JavaDoc> invalidGroup = new Group<Integer JavaDoc>(newPartition);
78         try {
79             this.partition.addGroup(invalidGroup);
80             fail("invalid group");
81         }
82         catch (InternalException e) {
83             // Expected
84
}
85
86         // Case with a group already added
87
Group<Integer JavaDoc> group = new Group<Integer JavaDoc>(this.partition);
88         try {
89             this.partition.addGroup(group);
90             fail("group is already in this partiton");
91         }
92         catch (InternalException e) {
93             // Expected
94
}
95     }
96
97     @Test
98     public void testGetElement() {
99
100         // Case with null DfaState
101
DfaState<Integer JavaDoc> nullState = null;
102         try {
103             this.partition.getElement(nullState);
104             fail("state may not be null");
105         }
106         catch (InternalException e) {
107             // Expected
108
}
109
110         // Case with invalid DfaState
111
Dfa<Integer JavaDoc> newDfa = new Dfa<Integer JavaDoc>(this.nfa);
112         DfaState<Integer JavaDoc> dfaState = newDfa.getStartState();
113         try {
114             this.partition.getElement(dfaState);
115             fail("invalid state");
116         }
117         catch (InternalException e) {
118             // Expected
119
}
120         dfaState = this.dfa.getStartState();
121         this.partition.getElement(dfaState);
122     }
123
124     @Test
125     public void testAddElement() {
126
127         Element<Integer JavaDoc> element;
128         // Case with null Element
129
Element<Integer JavaDoc> nullElement = null;
130         try {
131             this.partition.addElement(nullElement);
132             fail("element may not be null");
133         }
134         catch (InternalException e) {
135             // Expected
136
}
137
138         // Case with invalid Element
139
Partition<Integer JavaDoc> newPartition = new Partition<Integer JavaDoc>(this.dfa);
140         element = newPartition.getElement(this.dfa.getStartState());
141         try {
142             this.partition.addElement(element);
143             fail("invalid element");
144         }
145         catch (InternalException e) {
146             // Expected
147
}
148     }
149
150 }
151
Popular Tags