/*
* Copyright (C) Chaperon. All rights reserved.
* -------------------------------------------------------------------------
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package net.sourceforge.chaperon.test;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import net.sourceforge.chaperon.build.EndOfFile;
import net.sourceforge.chaperon.build.FirstSetCollection;
import net.sourceforge.chaperon.build.ItemSet;
import net.sourceforge.chaperon.model.grammar.Grammar;
import net.sourceforge.chaperon.model.grammar.Production;
import net.sourceforge.chaperon.model.symbol.Nonterminal;
import net.sourceforge.chaperon.model.symbol.Terminal;
public class ItemSetTestCase extends TestCase
{
private Terminal c;
private Terminal d;
private EndOfFile eof;
private Nonterminal S;
private Nonterminal C;
private Grammar grammar;
private FirstSetCollection firstsets;
public ItemSetTestCase(String name)
{
super(name);
//super("ItemSetTestCase");
}
public void setUp()
{
c = new Terminal("c");
d = new Terminal("d");
eof = new EndOfFile();
S = new Nonterminal("S");
C = new Nonterminal("C");
grammar = new Grammar();
Production production = new Production(S);
production.getDefinition().addSymbol(C);
production.getDefinition().addSymbol(C);
grammar.addProduction(production);
production = new Production(C);
production.getDefinition().addSymbol(c);
production.getDefinition().addSymbol(C);
grammar.addProduction(production);
production = new Production(C);
production.getDefinition().addSymbol(d);
grammar.addProduction(production);
grammar.setStartSymbol(S);
firstsets = new FirstSetCollection(grammar);
}
public static void assertNotEquals(String message, Object a, Object b)
{
if ((a==null) || (a==null))
return;
if (a.equals(b))
{
String formatted = "";
if (message!=null)
formatted = message+" ";
fail(formatted+"<"+a+"> equals <"+b+">");
}
}
public void testAddItem()
{
ItemSet set = new ItemSet(grammar, firstsets);
assertEquals("Test if count of item are equal", set.getItemCount(), 0);
set.addItem(0, 0, eof);
assertEquals("Test if count of item are equal", set.getItemCount(), 1);
set.addItem(1, 0, c);
assertEquals("Test if count of item are equal", set.getItemCount(), 2);
set.addItem(1, 1, d);
assertEquals("Test if count of item are equal", set.getItemCount(), 3);
set.addItem(2, 0, c);
assertEquals("Test if count of item are equal", set.getItemCount(), 4);
assertTrue("Test if item was added", set.addItem(1, 2, d));
assertEquals("Test if count of item are equal", set.getItemCount(), 5);
assertTrue("Test if item was added", set.addItem(2, 1, d));
assertEquals("Test if count of item are equal", set.getItemCount(), 6);
assertTrue("Test if item was added", set.addItem(1, 1, eof));
assertEquals("Test if count of item are equal", set.getItemCount(), 6);
assertTrue("Test if item was added", !set.addItem(1, 1, d));
assertEquals("Test if count of item are equal", set.getItemCount(), 6);
ItemSet set2 = new ItemSet(grammar, firstsets);
set2.addItem(1, 1, eof);
set2.addItem(2, 1, d);
set2.addItem(1, 2, d);
set2.addItem(2, 0, c);
set2.addItem(1, 1, d);
set2.addItem(1, 0, c);
set2.addItem(0, 0, eof);
assertEquals("Test if sets are equal", set, set2);
}
public void testAddItemSet()
{
ItemSet set = new ItemSet(grammar, firstsets);
assertEquals("Test if count of item are equal", set.getItemCount(), 0);
set.addItem(0, 0, eof);
assertEquals("Test if count of item are equal", set.getItemCount(), 1);
set.addItem(1, 0, c);
assertEquals("Test if count of item are equal", set.getItemCount(), 2);
set.addItem(1, 1, d);
assertEquals("Test if count of item are equal", set.getItemCount(), 3);
set.addItem(2, 0, c);
assertEquals("Test if count of item are equal", set.getItemCount(), 4);
ItemSet set2 = new ItemSet(grammar, firstsets);
set2.addItem(1, 2, d);
set2.addItem(2, 1, d);
set2.addItem(1, 1, eof);
set2.addItem(1, 1, d);
assertTrue("Test if set was added", set.addItemSet(set2));
assertEquals("Test if count of item are equal", set.getItemCount(), 6);
ItemSet set3 = new ItemSet(grammar, firstsets);
set3.addItem(1, 1, eof);
set3.addItem(2, 1, d);
set3.addItem(1, 2, d);
set3.addItem(2, 0, c);
set3.addItem(1, 1, d);
set3.addItem(1, 0, c);
set3.addItem(0, 0, eof);
assertEquals("Test if sets are equal", set, set3);
assertTrue("Test if set was added", !set.addItemSet(set2));
}
public void testContains()
{
ItemSet set = new ItemSet(grammar, firstsets);
set.addItem(0, 0, eof);
set.addItem(1, 0, c);
set.addItem(1, 0, d);
set.addItem(2, 0, c);
ItemSet set2 = new ItemSet(grammar, firstsets);
set2.addItem(1, 0, c);
set2.addItem(1, 0, d);
assertTrue("Test if set contains another set", set.contains(set2));
assertTrue("Test if set contains another set", !set2.contains(set));
}
public void testEquals()
{
ItemSet set = new ItemSet(grammar, firstsets);
set.addItem(0, 0, eof);
set.addItem(1, 0, c);
set.addItem(1, 0, d);
set.addItem(2, 0, c);
set.addItem(2, 0, d);
ItemSet set2 = new ItemSet(grammar, firstsets);
set2.addItem(0, 0, eof);
set2.addItem(1, 0, c);
set2.addItem(1, 0, d);
set2.addItem(2, 0, c);
set2.addItem(2, 0, d);
assertEquals("Test if sets are equal", set, set2);
set = new ItemSet(grammar, firstsets);
set.addItem(0, 0, eof);
set.addItem(1, 0, c);
set.addItem(1, 0, d);
set.addItem(2, 0, c);
set.addItem(2, 0, d);
set2 = new ItemSet(grammar, firstsets);
set2.addItem(1, 0, d);
set2.addItem(2, 0, c);
set2.addItem(2, 0, d);
set2.addItem(0, 0, eof);
set2.addItem(1, 0, c);
assertEquals("Test if sets are equal", set, set2);
set = new ItemSet(grammar, firstsets);
set.addItem(0, 0, eof);
set.addItem(1, 0, c);
set.addItem(1, 0, d);
set.addItem(2, 0, c);
set.addItem(2, 0, d);
set2 = new ItemSet(grammar, firstsets);
set2.addItem(0, 0, eof);
set2.addItem(1, 0, c);
set2.addItem(1, 0, eof);
set2.addItem(2, 0, c);
set2.addItem(2, 0, d);
assertNotEquals("Test if sets are not equal", set, set2);
set = new ItemSet(grammar, firstsets);
set.addItem(0, 0, eof);
set.addItem(1, 0, c);
set.addItem(1, 0, d);
set.addItem(2, 0, c);
set.addItem(2, 0, d);
set2 = new ItemSet(grammar, firstsets);
set2.addItem(0, 0, eof);
set2.addItem(0, 0, c);
set2.addItem(1, 0, d);
set2.addItem(2, 0, c);
set2.addItem(2, 0, d);
assertNotEquals("Test if sets are not equal", set, set2);
}
public void testEqualsCore()
{
ItemSet set = new ItemSet(grammar, firstsets);
set.addItem(0, 0, eof);
set.addItem(1, 0, c);
set.addItem(1, 0, d);
set.addItem(2, 0, c);
set.addItem(2, 0, d);
ItemSet set2 = new ItemSet(grammar, firstsets);
set2.addItem(0, 0, eof);
set2.addItem(1, 0, c);
set2.addItem(2, 0, d);
assertTrue("Test if sets have the same core", set.equalsCore(set2));
set = new ItemSet(grammar, firstsets);
set.addItem(0, 0, eof);
set.addItem(1, 0, c);
set.addItem(1, 0, d);
set.addItem(2, 0, c);
set.addItem(2, 0, d);
set2 = new ItemSet(grammar, firstsets);
set2.addItem(2, 0, d);
set2.addItem(0, 0, eof);
set2.addItem(1, 0, c);
assertTrue("Test if sets have the same core", set.equalsCore(set2));
set = new ItemSet(grammar, firstsets);
set.addItem(0, 0, eof);
set.addItem(1, 1, c);
set.addItem(1, 1, d);
set.addItem(2, 1, c);
set.addItem(2, 1, d);
set2 = new ItemSet(grammar, firstsets);
set2.addItem(2, 1, d);
set2.addItem(0, 0, eof);
set2.addItem(1, 1, c);
assertTrue("Test if sets have the same core", set.equalsCore(set2));
set2 = new ItemSet(grammar, firstsets);
set2.addItem(1, 1, d);
set2.addItem(0, 0, eof);
set2.addItem(1, 1, c);
assertTrue("Test if sets don't have the same core", !set.equalsCore(set2));
}
public void testClosure()
{
// ---- Case 1 ----
ItemSet set = new ItemSet(grammar, firstsets);
set.addItem(0, 0, eof);
ItemSet result = new ItemSet(grammar, firstsets);
result.addItem(0, 0, eof);
result.addItem(1, 0, c);
result.addItem(1, 0, d);
result.addItem(2, 0, c);
result.addItem(2, 0, d);
assertEquals("Test if sets are equal", result, set.closure());
set = new ItemSet(grammar, firstsets);
set.addItem(0, 1, eof);
result = new ItemSet(grammar, firstsets);
result.addItem(0, 1, eof);
result.addItem(1, 0, eof);
result.addItem(2, 0, eof);
assertEquals("Test if sets are equal", result, set.closure());
set = new ItemSet(grammar, firstsets);
set.addItem(0, 2, eof);
result = new ItemSet(grammar, firstsets);
result.addItem(0, 2, eof);
assertEquals("Test if sets are equal", result, set.closure());
set = new ItemSet(grammar, firstsets);
set.addItem(1, 1, c);
set.addItem(1, 1, d);
result = new ItemSet(grammar, firstsets);
result.addItem(1, 1, c);
result.addItem(1, 1, d);
result.addItem(1, 0, c);
result.addItem(1, 0, d);
result.addItem(2, 0, c);
result.addItem(2, 0, d);
assertEquals("Test if sets are equal", result, set.closure());
// ---- Case 2 ----
Terminal a = new Terminal("a");
Terminal b = new Terminal("b");
Terminal c = new Terminal("c");
Nonterminal A = new Nonterminal("A");
Nonterminal B = new Nonterminal("B");
Nonterminal C = new Nonterminal("C");
Nonterminal D = new Nonterminal("D");
grammar = new Grammar();
Production production = new Production(A);
production.getDefinition().addSymbol(B);
production.getDefinition().addSymbol(a);
grammar.addProduction(production);
production = new Production(B);
production.getDefinition().addSymbol(b);
grammar.addProduction(production);
production = new Production(C);
production.getDefinition().addSymbol(B);
production.getDefinition().addSymbol(D);
grammar.addProduction(production);
production = new Production(D);
// empty
grammar.addProduction(production);
firstsets = new FirstSetCollection(grammar);
set = new ItemSet(grammar, firstsets);
set.addItem(0, 0, c);
result = new ItemSet(grammar, firstsets);
result.addItem(0, 0, c);
result.addItem(1, 0, a);
assertEquals("Test if sets are equal", result, set.closure());
set = new ItemSet(grammar, firstsets);
set.addItem(2, 0, a);
result = new ItemSet(grammar, firstsets);
result.addItem(2, 0, a);
result.addItem(1, 0, a);
assertEquals("Test if sets are equal", result, set.closure());
// ---- Case 3 ----
grammar = new Grammar();
production = new Production(A);
production.getDefinition().addSymbol(B);
production.getDefinition().addSymbol(D);
grammar.addProduction(production);
production = new Production(B);
production.getDefinition().addSymbol(C);
grammar.addProduction(production);
production = new Production(C);
production.getDefinition().addSymbol(d);
grammar.addProduction(production);
production = new Production(D);
production.getDefinition().addSymbol(b);
grammar.addProduction(production);
firstsets = new FirstSetCollection(grammar);
set = new ItemSet(grammar, firstsets);
set.addItem(0, 0, c);
result = new ItemSet(grammar, firstsets);
result.addItem(0, 0, c);
result.addItem(1, 0, b);
result.addItem(2, 0, b);
assertEquals("Test if sets are equal", result, set.closure());
}
/* public void testJump()
{
ItemSet set = new ItemSet(grammar, firstsets);
set.addItem(0, 0, eof);
ItemSet result = new ItemSet(grammar, firstsets);
result.addItem(0, 1, eof);
result.addItem(1, 0, eof);
result.addItem(2, 0, eof);
assertEquals("Test if sets are equal", result, set.closure().jump(C));
set = new ItemSet(grammar, firstsets);
set.addItem(0, 0, eof);
result = new ItemSet(grammar, firstsets);
result.addItem(1, 1, c);
result.addItem(1, 1, d);
result.addItem(1, 0, c);
result.addItem(1, 0, d);
result.addItem(2, 0, c);
result.addItem(2, 0, d);
assertEquals("Test if sets are equal", result, set.closure().jump(c));
set = result;
result = new ItemSet(grammar, firstsets);
result.addItem(1, 2, c);
result.addItem(1, 2, d);
assertEquals("Test if sets are equal", result, set.closure().jump(C));
}*/
public static Test suite()
{
return new TestSuite(ItemSetTestCase.class);
}
}