/*
* 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.model.symbol.Nonterminal;
import net.sourceforge.chaperon.model.symbol.SymbolSet;
import net.sourceforge.chaperon.model.symbol.Terminal;
public class SymbolSetTestCase extends TestCase
{
private Terminal a;
private Terminal b;
private Terminal c;
private Nonterminal A;
private Nonterminal B;
private Nonterminal C;
public SymbolSetTestCase(String name)
{
super(name);
}
public void setUp()
{
a = new Terminal("a");
b = new Terminal("b");
c = new Terminal("c");
A = new Nonterminal("A");
B = new Nonterminal("B");
C = new Nonterminal("C");
}
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 testAddSymbol()
{
SymbolSet set = new SymbolSet();
assertEquals("Test if set is empty", 0, set.getSymbolCount());
assertEquals("Test if set is empty", true, set.isEmpty());
set.addSymbol(a);
assertEquals("Test if set is not empty", 1, set.getSymbolCount());
assertEquals("Test if set is not empty", false, set.isEmpty());
assertEquals("Test if symbols are equal", a, set.getSymbol(0));
set.addSymbol(a);
set.addSymbol(A);
set.addSymbol(b);
set.addSymbol(B);
set.addSymbol(c);
set.addSymbol(C);
assertEquals("Test if set is not empty", 6, set.getSymbolCount());
assertEquals("Test if set is not empty", false, set.isEmpty());
assertEquals("Test if symbols are equal", a, set.getSymbol(0));
assertEquals("Test if symbols are equal", B, set.getSymbol(3));
assertEquals("Test if symbols are equal", C, set.getSymbol(5));
assertEquals("Test if indices are equal", 0, set.indexOf(a));
assertEquals("Test if indices are equal", 4, set.indexOf(c));
SymbolSet set2 = new SymbolSet();
set2.addSymbol(a);
set2.addSymbol(a);
set2.addSymbol(A);
set2.addSymbol(b);
assertNotEquals("Test if sets are not equal", set, set2);
set2.addSymbol(B);
set2.addSymbol(c);
set2.addSymbol(C);
assertEquals("Test if sets are equal", set, set2);
SymbolSet set3 = new SymbolSet();
set3.addSymbol(a);
set3.addSymbol(A);
set3.addSymbol(b);
set3.addSymbol(B);
set3.addSymbol(c);
set3.addSymbol(C);
set3.addSymbol(a);
assertEquals("Test if sets are equal", set, set3);
}
public void testEquals()
{
SymbolSet set = new SymbolSet();
set.addSymbol(A);
set.addSymbol(B);
set.addSymbol(C);
SymbolSet set2 = new SymbolSet();
set2.addSymbol(A);
set2.addSymbol(C);
set2.addSymbol(B);
assertEquals("Test if symbols are equal", set, set2);
set2 = new SymbolSet();
set2.addSymbol(A);
set2.addSymbol(B);
assertNotEquals("Test if sets are not equal", set, set2);
}
public static Test suite()
{
return new TestSuite(SymbolSetTestCase.class);
}
}