001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.test;
010:
011: import junit.framework.Test;
012: import junit.framework.TestCase;
013: import junit.framework.TestSuite;
014:
015: import net.sourceforge.chaperon.build.EndOfFile;
016: import net.sourceforge.chaperon.build.FirstSetCollection;
017: import net.sourceforge.chaperon.build.ItemSet;
018: import net.sourceforge.chaperon.model.grammar.Grammar;
019: import net.sourceforge.chaperon.model.grammar.Production;
020: import net.sourceforge.chaperon.model.symbol.Nonterminal;
021: import net.sourceforge.chaperon.model.symbol.Terminal;
022:
023: public class ItemSetTestCase extends TestCase {
024: private Terminal c;
025: private Terminal d;
026: private EndOfFile eof;
027: private Nonterminal S;
028: private Nonterminal C;
029: private Grammar grammar;
030: private FirstSetCollection firstsets;
031:
032: public ItemSetTestCase(String name) {
033: super (name);
034:
035: //super("ItemSetTestCase");
036: }
037:
038: public void setUp() {
039: c = new Terminal("c");
040: d = new Terminal("d");
041:
042: eof = new EndOfFile();
043:
044: S = new Nonterminal("S");
045: C = new Nonterminal("C");
046:
047: grammar = new Grammar();
048:
049: Production production = new Production(S);
050: production.getDefinition().addSymbol(C);
051: production.getDefinition().addSymbol(C);
052: grammar.addProduction(production);
053:
054: production = new Production(C);
055: production.getDefinition().addSymbol(c);
056: production.getDefinition().addSymbol(C);
057: grammar.addProduction(production);
058:
059: production = new Production(C);
060: production.getDefinition().addSymbol(d);
061: grammar.addProduction(production);
062:
063: grammar.setStartSymbol(S);
064:
065: firstsets = new FirstSetCollection(grammar);
066: }
067:
068: public static void assertNotEquals(String message, Object a,
069: Object b) {
070: if ((a == null) || (a == null))
071: return;
072:
073: if (a.equals(b)) {
074: String formatted = "";
075: if (message != null)
076: formatted = message + " ";
077:
078: fail(formatted + "<" + a + "> equals <" + b + ">");
079: }
080: }
081:
082: public void testAddItem() {
083: ItemSet set = new ItemSet(grammar, firstsets);
084: assertEquals("Test if count of item are equal", set
085: .getItemCount(), 0);
086:
087: set.addItem(0, 0, eof);
088: assertEquals("Test if count of item are equal", set
089: .getItemCount(), 1);
090:
091: set.addItem(1, 0, c);
092: assertEquals("Test if count of item are equal", set
093: .getItemCount(), 2);
094:
095: set.addItem(1, 1, d);
096: assertEquals("Test if count of item are equal", set
097: .getItemCount(), 3);
098:
099: set.addItem(2, 0, c);
100: assertEquals("Test if count of item are equal", set
101: .getItemCount(), 4);
102:
103: assertTrue("Test if item was added", set.addItem(1, 2, d));
104: assertEquals("Test if count of item are equal", set
105: .getItemCount(), 5);
106:
107: assertTrue("Test if item was added", set.addItem(2, 1, d));
108: assertEquals("Test if count of item are equal", set
109: .getItemCount(), 6);
110:
111: assertTrue("Test if item was added", set.addItem(1, 1, eof));
112: assertEquals("Test if count of item are equal", set
113: .getItemCount(), 6);
114:
115: assertTrue("Test if item was added", !set.addItem(1, 1, d));
116: assertEquals("Test if count of item are equal", set
117: .getItemCount(), 6);
118:
119: ItemSet set2 = new ItemSet(grammar, firstsets);
120: set2.addItem(1, 1, eof);
121: set2.addItem(2, 1, d);
122: set2.addItem(1, 2, d);
123: set2.addItem(2, 0, c);
124: set2.addItem(1, 1, d);
125: set2.addItem(1, 0, c);
126: set2.addItem(0, 0, eof);
127:
128: assertEquals("Test if sets are equal", set, set2);
129: }
130:
131: public void testAddItemSet() {
132: ItemSet set = new ItemSet(grammar, firstsets);
133: assertEquals("Test if count of item are equal", set
134: .getItemCount(), 0);
135:
136: set.addItem(0, 0, eof);
137: assertEquals("Test if count of item are equal", set
138: .getItemCount(), 1);
139:
140: set.addItem(1, 0, c);
141: assertEquals("Test if count of item are equal", set
142: .getItemCount(), 2);
143:
144: set.addItem(1, 1, d);
145: assertEquals("Test if count of item are equal", set
146: .getItemCount(), 3);
147:
148: set.addItem(2, 0, c);
149: assertEquals("Test if count of item are equal", set
150: .getItemCount(), 4);
151:
152: ItemSet set2 = new ItemSet(grammar, firstsets);
153: set2.addItem(1, 2, d);
154: set2.addItem(2, 1, d);
155: set2.addItem(1, 1, eof);
156: set2.addItem(1, 1, d);
157:
158: assertTrue("Test if set was added", set.addItemSet(set2));
159:
160: assertEquals("Test if count of item are equal", set
161: .getItemCount(), 6);
162:
163: ItemSet set3 = new ItemSet(grammar, firstsets);
164: set3.addItem(1, 1, eof);
165: set3.addItem(2, 1, d);
166: set3.addItem(1, 2, d);
167: set3.addItem(2, 0, c);
168: set3.addItem(1, 1, d);
169: set3.addItem(1, 0, c);
170: set3.addItem(0, 0, eof);
171:
172: assertEquals("Test if sets are equal", set, set3);
173:
174: assertTrue("Test if set was added", !set.addItemSet(set2));
175: }
176:
177: public void testContains() {
178: ItemSet set = new ItemSet(grammar, firstsets);
179: set.addItem(0, 0, eof);
180: set.addItem(1, 0, c);
181: set.addItem(1, 0, d);
182: set.addItem(2, 0, c);
183:
184: ItemSet set2 = new ItemSet(grammar, firstsets);
185: set2.addItem(1, 0, c);
186: set2.addItem(1, 0, d);
187:
188: assertTrue("Test if set contains another set", set
189: .contains(set2));
190: assertTrue("Test if set contains another set", !set2
191: .contains(set));
192: }
193:
194: public void testEquals() {
195: ItemSet set = new ItemSet(grammar, firstsets);
196: set.addItem(0, 0, eof);
197: set.addItem(1, 0, c);
198: set.addItem(1, 0, d);
199: set.addItem(2, 0, c);
200: set.addItem(2, 0, d);
201:
202: ItemSet set2 = new ItemSet(grammar, firstsets);
203: set2.addItem(0, 0, eof);
204: set2.addItem(1, 0, c);
205: set2.addItem(1, 0, d);
206: set2.addItem(2, 0, c);
207: set2.addItem(2, 0, d);
208:
209: assertEquals("Test if sets are equal", set, set2);
210:
211: set = new ItemSet(grammar, firstsets);
212: set.addItem(0, 0, eof);
213: set.addItem(1, 0, c);
214: set.addItem(1, 0, d);
215: set.addItem(2, 0, c);
216: set.addItem(2, 0, d);
217:
218: set2 = new ItemSet(grammar, firstsets);
219: set2.addItem(1, 0, d);
220: set2.addItem(2, 0, c);
221: set2.addItem(2, 0, d);
222: set2.addItem(0, 0, eof);
223: set2.addItem(1, 0, c);
224:
225: assertEquals("Test if sets are equal", set, set2);
226:
227: set = new ItemSet(grammar, firstsets);
228: set.addItem(0, 0, eof);
229: set.addItem(1, 0, c);
230: set.addItem(1, 0, d);
231: set.addItem(2, 0, c);
232: set.addItem(2, 0, d);
233:
234: set2 = new ItemSet(grammar, firstsets);
235: set2.addItem(0, 0, eof);
236: set2.addItem(1, 0, c);
237: set2.addItem(1, 0, eof);
238: set2.addItem(2, 0, c);
239: set2.addItem(2, 0, d);
240:
241: assertNotEquals("Test if sets are not equal", set, set2);
242:
243: set = new ItemSet(grammar, firstsets);
244: set.addItem(0, 0, eof);
245: set.addItem(1, 0, c);
246: set.addItem(1, 0, d);
247: set.addItem(2, 0, c);
248: set.addItem(2, 0, d);
249:
250: set2 = new ItemSet(grammar, firstsets);
251: set2.addItem(0, 0, eof);
252: set2.addItem(0, 0, c);
253: set2.addItem(1, 0, d);
254: set2.addItem(2, 0, c);
255: set2.addItem(2, 0, d);
256:
257: assertNotEquals("Test if sets are not equal", set, set2);
258: }
259:
260: public void testEqualsCore() {
261: ItemSet set = new ItemSet(grammar, firstsets);
262: set.addItem(0, 0, eof);
263: set.addItem(1, 0, c);
264: set.addItem(1, 0, d);
265: set.addItem(2, 0, c);
266: set.addItem(2, 0, d);
267:
268: ItemSet set2 = new ItemSet(grammar, firstsets);
269: set2.addItem(0, 0, eof);
270: set2.addItem(1, 0, c);
271: set2.addItem(2, 0, d);
272:
273: assertTrue("Test if sets have the same core", set
274: .equalsCore(set2));
275:
276: set = new ItemSet(grammar, firstsets);
277: set.addItem(0, 0, eof);
278: set.addItem(1, 0, c);
279: set.addItem(1, 0, d);
280: set.addItem(2, 0, c);
281: set.addItem(2, 0, d);
282:
283: set2 = new ItemSet(grammar, firstsets);
284: set2.addItem(2, 0, d);
285: set2.addItem(0, 0, eof);
286: set2.addItem(1, 0, c);
287:
288: assertTrue("Test if sets have the same core", set
289: .equalsCore(set2));
290:
291: set = new ItemSet(grammar, firstsets);
292: set.addItem(0, 0, eof);
293: set.addItem(1, 1, c);
294: set.addItem(1, 1, d);
295: set.addItem(2, 1, c);
296: set.addItem(2, 1, d);
297:
298: set2 = new ItemSet(grammar, firstsets);
299: set2.addItem(2, 1, d);
300: set2.addItem(0, 0, eof);
301: set2.addItem(1, 1, c);
302:
303: assertTrue("Test if sets have the same core", set
304: .equalsCore(set2));
305:
306: set2 = new ItemSet(grammar, firstsets);
307: set2.addItem(1, 1, d);
308: set2.addItem(0, 0, eof);
309: set2.addItem(1, 1, c);
310:
311: assertTrue("Test if sets don't have the same core", !set
312: .equalsCore(set2));
313: }
314:
315: public void testClosure() {
316: // ---- Case 1 ----
317: ItemSet set = new ItemSet(grammar, firstsets);
318: set.addItem(0, 0, eof);
319:
320: ItemSet result = new ItemSet(grammar, firstsets);
321: result.addItem(0, 0, eof);
322: result.addItem(1, 0, c);
323: result.addItem(1, 0, d);
324: result.addItem(2, 0, c);
325: result.addItem(2, 0, d);
326:
327: assertEquals("Test if sets are equal", result, set.closure());
328:
329: set = new ItemSet(grammar, firstsets);
330: set.addItem(0, 1, eof);
331:
332: result = new ItemSet(grammar, firstsets);
333: result.addItem(0, 1, eof);
334: result.addItem(1, 0, eof);
335: result.addItem(2, 0, eof);
336:
337: assertEquals("Test if sets are equal", result, set.closure());
338:
339: set = new ItemSet(grammar, firstsets);
340: set.addItem(0, 2, eof);
341:
342: result = new ItemSet(grammar, firstsets);
343: result.addItem(0, 2, eof);
344:
345: assertEquals("Test if sets are equal", result, set.closure());
346:
347: set = new ItemSet(grammar, firstsets);
348: set.addItem(1, 1, c);
349: set.addItem(1, 1, d);
350:
351: result = new ItemSet(grammar, firstsets);
352: result.addItem(1, 1, c);
353: result.addItem(1, 1, d);
354: result.addItem(1, 0, c);
355: result.addItem(1, 0, d);
356: result.addItem(2, 0, c);
357: result.addItem(2, 0, d);
358:
359: assertEquals("Test if sets are equal", result, set.closure());
360:
361: // ---- Case 2 ----
362: Terminal a = new Terminal("a");
363: Terminal b = new Terminal("b");
364: Terminal c = new Terminal("c");
365:
366: Nonterminal A = new Nonterminal("A");
367: Nonterminal B = new Nonterminal("B");
368: Nonterminal C = new Nonterminal("C");
369: Nonterminal D = new Nonterminal("D");
370:
371: grammar = new Grammar();
372:
373: Production production = new Production(A);
374: production.getDefinition().addSymbol(B);
375: production.getDefinition().addSymbol(a);
376: grammar.addProduction(production);
377:
378: production = new Production(B);
379: production.getDefinition().addSymbol(b);
380: grammar.addProduction(production);
381:
382: production = new Production(C);
383: production.getDefinition().addSymbol(B);
384: production.getDefinition().addSymbol(D);
385: grammar.addProduction(production);
386:
387: production = new Production(D);
388:
389: // empty
390: grammar.addProduction(production);
391:
392: firstsets = new FirstSetCollection(grammar);
393:
394: set = new ItemSet(grammar, firstsets);
395: set.addItem(0, 0, c);
396:
397: result = new ItemSet(grammar, firstsets);
398: result.addItem(0, 0, c);
399: result.addItem(1, 0, a);
400:
401: assertEquals("Test if sets are equal", result, set.closure());
402:
403: set = new ItemSet(grammar, firstsets);
404: set.addItem(2, 0, a);
405:
406: result = new ItemSet(grammar, firstsets);
407: result.addItem(2, 0, a);
408: result.addItem(1, 0, a);
409:
410: assertEquals("Test if sets are equal", result, set.closure());
411:
412: // ---- Case 3 ----
413: grammar = new Grammar();
414:
415: production = new Production(A);
416: production.getDefinition().addSymbol(B);
417: production.getDefinition().addSymbol(D);
418: grammar.addProduction(production);
419:
420: production = new Production(B);
421: production.getDefinition().addSymbol(C);
422: grammar.addProduction(production);
423:
424: production = new Production(C);
425: production.getDefinition().addSymbol(d);
426: grammar.addProduction(production);
427:
428: production = new Production(D);
429: production.getDefinition().addSymbol(b);
430: grammar.addProduction(production);
431:
432: firstsets = new FirstSetCollection(grammar);
433:
434: set = new ItemSet(grammar, firstsets);
435: set.addItem(0, 0, c);
436:
437: result = new ItemSet(grammar, firstsets);
438: result.addItem(0, 0, c);
439: result.addItem(1, 0, b);
440: result.addItem(2, 0, b);
441:
442: assertEquals("Test if sets are equal", result, set.closure());
443: }
444:
445: /* public void testJump()
446: {
447: ItemSet set = new ItemSet(grammar, firstsets);
448: set.addItem(0, 0, eof);
449:
450: ItemSet result = new ItemSet(grammar, firstsets);
451: result.addItem(0, 1, eof);
452: result.addItem(1, 0, eof);
453: result.addItem(2, 0, eof);
454:
455: assertEquals("Test if sets are equal", result, set.closure().jump(C));
456:
457: set = new ItemSet(grammar, firstsets);
458: set.addItem(0, 0, eof);
459:
460: result = new ItemSet(grammar, firstsets);
461: result.addItem(1, 1, c);
462: result.addItem(1, 1, d);
463: result.addItem(1, 0, c);
464: result.addItem(1, 0, d);
465: result.addItem(2, 0, c);
466: result.addItem(2, 0, d);
467:
468: assertEquals("Test if sets are equal", result, set.closure().jump(c));
469:
470: set = result;
471:
472: result = new ItemSet(grammar, firstsets);
473: result.addItem(1, 2, c);
474: result.addItem(1, 2, d);
475:
476: assertEquals("Test if sets are equal", result, set.closure().jump(C));
477: }*/
478: public static Test suite() {
479: return new TestSuite(ItemSetTestCase.class);
480: }
481: }
|