KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sablecc > sablecc > alphabet > AlphabetTest


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

18
19 package org.sablecc.sablecc.alphabet;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 import java.math.BigInteger JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.LinkedList JavaDoc;
29
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.sablecc.sablecc.exception.InternalException;
33
34 public class AlphabetTest {
35
36     private Alphabet<Integer JavaDoc> alphaInt;
37
38     private Collection JavaDoc<Symbol<Integer JavaDoc>> symbolsInt = new LinkedList JavaDoc<Symbol<Integer JavaDoc>>();
39
40     private Collection JavaDoc<Interval<Integer JavaDoc>> intervalsInt = new LinkedList JavaDoc<Interval<Integer JavaDoc>>();
41
42     private Symbol<Integer JavaDoc> firstSymbolInt;
43
44     private Symbol<Integer JavaDoc> secondSymbolInt;
45
46     private AdjacencyRealm<Integer JavaDoc> integerAdjacencyRealm = Realms.getInteger();
47
48     private Alphabet<BigInteger JavaDoc> alphaBig;
49
50     private Collection JavaDoc<Symbol<BigInteger JavaDoc>> symbolsBig = new LinkedList JavaDoc<Symbol<BigInteger JavaDoc>>();
51
52     private Collection JavaDoc<Interval<BigInteger JavaDoc>> intervalsBig = new LinkedList JavaDoc<Interval<BigInteger JavaDoc>>();
53
54     private Symbol<BigInteger JavaDoc> firstSymbolBig;
55
56     private Symbol<BigInteger JavaDoc> secondSymbolBig;
57
58     private AdjacencyRealm<BigInteger JavaDoc> bigIntegerAdjacencyRealm = Realms
59             .getBigInteger();
60
61     @Before
62     public void setUp()
63             throws Exception JavaDoc {
64
65         // For Integer
66
this.intervalsInt.clear();
67         this.intervalsInt.add(new Interval<Integer JavaDoc>(10, 20,
68                 this.integerAdjacencyRealm));
69         this.intervalsInt.add(new Interval<Integer JavaDoc>(50, 100,
70                 this.integerAdjacencyRealm));
71         this.intervalsInt.add(new Interval<Integer JavaDoc>(200, 400,
72                 this.integerAdjacencyRealm));
73
74         this.firstSymbolInt = new Symbol<Integer JavaDoc>(this.intervalsInt);
75
76         this.intervalsInt.clear();
77         this.intervalsInt.add(new Interval<Integer JavaDoc>(30, 40,
78                 this.integerAdjacencyRealm));
79         this.intervalsInt.add(new Interval<Integer JavaDoc>(450, 500,
80                 this.integerAdjacencyRealm));
81         this.intervalsInt.add(new Interval<Integer JavaDoc>(600, 800,
82                 this.integerAdjacencyRealm));
83
84         this.secondSymbolInt = new Symbol<Integer JavaDoc>(this.intervalsInt);
85
86         this.symbolsInt.clear();
87         this.symbolsInt.add(this.firstSymbolInt);
88         this.symbolsInt.add(this.secondSymbolInt);
89         this.alphaInt = new Alphabet<Integer JavaDoc>(this.symbolsInt);
90
91         // For BigInteger
92
this.intervalsBig.clear();
93         this.intervalsBig.add(new Interval<BigInteger JavaDoc>(BigInteger.ZERO,
94                 BigInteger.TEN, this.bigIntegerAdjacencyRealm));
95         this.intervalsBig.add(new Interval<BigInteger JavaDoc>(new BigInteger JavaDoc("50"),
96                 new BigInteger JavaDoc("100"), this.bigIntegerAdjacencyRealm));
97         this.intervalsBig.add(new Interval<BigInteger JavaDoc>(new BigInteger JavaDoc("200"),
98                 new BigInteger JavaDoc("400"), this.bigIntegerAdjacencyRealm));
99
100         this.firstSymbolBig = new Symbol<BigInteger JavaDoc>(this.intervalsBig);
101
102         this.intervalsBig.clear();
103         this.intervalsBig.add(new Interval<BigInteger JavaDoc>(new BigInteger JavaDoc("30"),
104                 new BigInteger JavaDoc("40"), this.bigIntegerAdjacencyRealm));
105         this.intervalsBig.add(new Interval<BigInteger JavaDoc>(new BigInteger JavaDoc("600"),
106                 new BigInteger JavaDoc("800"), this.bigIntegerAdjacencyRealm));
107         this.intervalsBig.add(new Interval<BigInteger JavaDoc>(new BigInteger JavaDoc("150"),
108                 new BigInteger JavaDoc("170"), this.bigIntegerAdjacencyRealm));
109
110         this.secondSymbolBig = new Symbol<BigInteger JavaDoc>(this.intervalsBig);
111
112         this.symbolsBig.clear();
113         this.symbolsBig.add(this.firstSymbolBig);
114         this.symbolsBig.add(this.secondSymbolBig);
115         this.alphaBig = new Alphabet<BigInteger JavaDoc>(this.symbolsBig);
116     }
117
118     @SuppressWarnings JavaDoc("unused")
119     @Test
120     public void testAlphabetCollectionOfSymbolOfT() {
121
122         // Case with null Collection.
123
Collection JavaDoc<Symbol<Integer JavaDoc>> nullCollection = null;
124         try {
125
126             Alphabet<Integer JavaDoc> alphabetNullCollection = new Alphabet<Integer JavaDoc>(
127                     nullCollection);
128             fail("Symbols may not be null.");
129         }
130         catch (InternalException e) {
131             // exception
132
}
133
134         // Case with overlapping intervals with Integer.
135
this.intervalsInt.clear();
136         this.intervalsInt.add(new Interval<Integer JavaDoc>(15, 70,
137                 this.integerAdjacencyRealm));
138         this.intervalsInt.add(new Interval<Integer JavaDoc>(120, 150,
139                 this.integerAdjacencyRealm));
140         Symbol<Integer JavaDoc> crossingIntervalSymbolInt = new Symbol<Integer JavaDoc>(
141                 this.intervalsInt);
142
143         Collection JavaDoc<Symbol<Integer JavaDoc>> symbolsOverLappingInt = new LinkedList JavaDoc<Symbol<Integer JavaDoc>>();
144         symbolsOverLappingInt.add(this.firstSymbolInt);
145         symbolsOverLappingInt.add(crossingIntervalSymbolInt);
146
147         try {
148             this.alphaInt = new Alphabet<Integer JavaDoc>(symbolsOverLappingInt);
149             fail("Distinct symbols may not have overlapping intervals.");
150         }
151         catch (InternalException e) {
152             // expected
153
}
154
155         // Case with overlapping intervals with BigInteger.
156
this.intervalsBig.clear();
157         this.intervalsBig.add(new Interval<BigInteger JavaDoc>(new BigInteger JavaDoc("5"),
158                 new BigInteger JavaDoc("70"), this.bigIntegerAdjacencyRealm));
159         this.intervalsBig.add(new Interval<BigInteger JavaDoc>(new BigInteger JavaDoc("160"),
160                 new BigInteger JavaDoc("700"), this.bigIntegerAdjacencyRealm));
161         Symbol<BigInteger JavaDoc> crossingIntervalSymbolBig = new Symbol<BigInteger JavaDoc>(
162                 this.intervalsBig);
163
164         Collection JavaDoc<Symbol<BigInteger JavaDoc>> symbolsOverLappingBig = new LinkedList JavaDoc<Symbol<BigInteger JavaDoc>>();
165         symbolsOverLappingBig.add(this.firstSymbolBig);
166         symbolsOverLappingBig.add(crossingIntervalSymbolBig);
167
168         try {
169             this.alphaBig = new Alphabet<BigInteger JavaDoc>(symbolsOverLappingBig);
170             fail("Distinct symbols may not have overlapping intervals.");
171         }
172         catch (InternalException e) {
173             // expected
174
}
175
176         // Typical case with Integer.
177
assertTrue("alphaInt should contains firstSymbolInt. ", this.alphaInt
178                 .getSymbols().contains(this.firstSymbolInt));
179         assertTrue("alphaInt should contains secondSymbolInt. ", this.alphaInt
180                 .getSymbols().contains(this.secondSymbolInt));
181
182         // Typical case with BigInteger.
183
assertTrue("alphaBig should contains firstSymbolBig. ", this.alphaBig
184                 .getSymbols().contains(this.firstSymbolBig));
185         assertTrue("alphaBig should contains secondSymbolBig. ", this.alphaBig
186                 .getSymbols().contains(this.secondSymbolBig));
187
188         // Case with multiple complement symbol
189
this.symbolsInt.clear();
190         this.symbolsInt.add(new Symbol<Integer JavaDoc>());
191         this.symbolsInt.add(new Symbol<Integer JavaDoc>());
192
193         try {
194             this.alphaInt = new Alphabet<Integer JavaDoc>(this.symbolsInt);
195             fail("an alphabet may not contain multiple complements symbols");
196         }
197         catch (InternalException e) {
198             // Expected
199
}
200
201         assertFalse("this alphabet does not contain a complement symbol",
202                 this.alphaInt.containsComplementSymbol());
203
204     }
205
206     @Test
207     public void testAlphabetSymbolOfT() {
208
209         // Case with null Symbol.
210
Symbol<Integer JavaDoc> nullSymbol = null;
211         try {
212             this.alphaInt = new Alphabet<Integer JavaDoc>(nullSymbol);
213             fail("symbol may not be null");
214         }
215         catch (InternalException e) {
216             // expected
217
}
218         // Typical Case with Integer.
219
this.alphaInt = new Alphabet<Integer JavaDoc>(this.firstSymbolInt);
220
221         assertTrue("alphaInt should contains firstSymbolInt.", this.alphaInt
222                 .getSymbols().contains(this.firstSymbolInt));
223
224         // Typical Case with BigInteger.
225
this.alphaBig = new Alphabet<BigInteger JavaDoc>(this.firstSymbolBig);
226
227         assertTrue("alphaBig should contains firstSymbolBig.", this.alphaBig
228                 .getSymbols().contains(this.firstSymbolBig));
229     }
230
231     @Test
232     public void testAlphabetIntervalOfT() {
233
234         // Case with null Interval.
235
Interval<Integer JavaDoc> nullInterval = null;
236         try {
237             this.alphaInt = new Alphabet<Integer JavaDoc>(nullInterval);
238             fail("interval may not be null");
239         }
240         catch (InternalException e) {
241             // expected
242
}
243
244         // Typical case with Integer.
245
this.alphaInt = new Alphabet<Integer JavaDoc>(new Interval<Integer JavaDoc>(10, 20,
246                 this.integerAdjacencyRealm));
247
248         // Typical case with BigInteger.
249
this.alphaBig = new Alphabet<BigInteger JavaDoc>(new Interval<BigInteger JavaDoc>(
250                 new BigInteger JavaDoc("10"), new BigInteger JavaDoc("20"),
251                 this.bigIntegerAdjacencyRealm));
252     }
253
254     @Test
255     public void testGetComplementSymbol() {
256
257         // Case with no complement Symbol
258
try {
259             this.alphaInt.getComplementSymbol();
260             fail("this alphabet does not contain a complement symbol");
261         }
262         catch (InternalException e) {
263             // Expected
264
}
265
266     }
267
268     @Test
269     public void testMerge() {
270
271         // With Integer
272
Alphabet<Integer JavaDoc> firstPartAlphabetInt;
273         Alphabet<Integer JavaDoc> secondPartAlphabetInt;
274
275         firstPartAlphabetInt = new Alphabet<Integer JavaDoc>(this.firstSymbolInt);
276         secondPartAlphabetInt = new Alphabet<Integer JavaDoc>(this.secondSymbolInt);
277         AlphabetMergeResult<Integer JavaDoc> mergeResultInt = firstPartAlphabetInt
278                 .mergeWith(secondPartAlphabetInt);
279
280         assertEquals("The merge result should equals the complete alphabet.",
281                 this.alphaInt.toString(), mergeResultInt.getNewAlphabet()
282                         .toString());
283
284         // With BigInteger
285
Alphabet<BigInteger JavaDoc> firstPartAlphabetBig;
286         Alphabet<BigInteger JavaDoc> secondPartAlphabetBig;
287
288         firstPartAlphabetBig = new Alphabet<BigInteger JavaDoc>(this.firstSymbolBig);
289         secondPartAlphabetBig = new Alphabet<BigInteger JavaDoc>(this.secondSymbolBig);
290         AlphabetMergeResult<BigInteger JavaDoc> mergeResultBig = firstPartAlphabetBig
291                 .mergeWith(secondPartAlphabetBig);
292
293         assertEquals("The merge result should equals the complete alphabet.",
294                 this.alphaBig.toString(), mergeResultBig.getNewAlphabet()
295                         .toString());
296
297         // Case with complement symbol
298
this.symbolsInt.clear();
299         this.symbolsInt.add(new Symbol<Integer JavaDoc>());
300         firstPartAlphabetInt = new Alphabet<Integer JavaDoc>(this.symbolsInt);
301
302         mergeResultInt = firstPartAlphabetInt.mergeWith(secondPartAlphabetInt);
303
304         Alphabet<Integer JavaDoc> alphabet = mergeResultInt.getNewAlphabet();
305         assertTrue("the merged result should have a complement Symbol",
306                 alphabet.containsComplementSymbol());
307     }
308
309 }
310
Popular Tags