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.model.symbol.Nonterminal;
016: import net.sourceforge.chaperon.model.symbol.SymbolList;
017: import net.sourceforge.chaperon.model.symbol.Terminal;
018:
019: public class SymbolListTestCase extends TestCase {
020: private Terminal a;
021: private Terminal b;
022: private Terminal c;
023: private Nonterminal A;
024: private Nonterminal B;
025: private Nonterminal C;
026:
027: public SymbolListTestCase() {
028: super ("SymbolListTestCase");
029: }
030:
031: public void setUp() {
032: a = new Terminal("a");
033: b = new Terminal("b");
034: c = new Terminal("c");
035:
036: A = new Nonterminal("A");
037: B = new Nonterminal("B");
038: C = new Nonterminal("C");
039: }
040:
041: public static void assertNotEquals(String message, Object a,
042: Object b) {
043: if ((a == null) || (a == null))
044: return;
045:
046: if (a.equals(b)) {
047: String formatted = "";
048: if (message != null)
049: formatted = message + " ";
050:
051: fail(formatted + "<" + a + "> equals <" + b + ">");
052: }
053: }
054:
055: public void runTest() throws Throwable {
056: SymbolList list = new SymbolList();
057:
058: assertEquals("Test if list is empty", 0, list.getSymbolCount());
059: assertEquals("Test if list is empty", true, list.isEmpty());
060:
061: list.addSymbol(a);
062:
063: assertEquals("Test if list is not empty", 1, list
064: .getSymbolCount());
065: assertEquals("Test if list is not empty", false, list.isEmpty());
066:
067: assertEquals("Test if symbols are equal", a, list.getSymbol(0));
068:
069: list.addSymbol(a);
070: list.addSymbol(A);
071: list.addSymbol(b);
072: list.addSymbol(B);
073: list.addSymbol(c);
074: list.addSymbol(C);
075:
076: assertEquals("Test if list is not empty", 7, list
077: .getSymbolCount());
078: assertEquals("Test if list is not empty", false, list.isEmpty());
079:
080: assertEquals("Test if symbols are equal", a, list.getSymbol(0));
081: assertEquals("Test if symbols are equal", a, list.getSymbol(1));
082: assertEquals("Test if symbols are equal", B, list.getSymbol(4));
083: assertEquals("Test if symbols are equal", C, list.getSymbol(6));
084:
085: assertEquals("Test if indices are equal", 0, list.indexOf(a));
086: assertEquals("Test if indices are equal", 5, list.indexOf(c));
087:
088: SymbolList list2 = new SymbolList();
089:
090: list2.addSymbol(a);
091: list2.addSymbol(a);
092: list2.addSymbol(A);
093: list2.addSymbol(b);
094:
095: assertNotEquals("Test if lists are not equal", list, list2);
096:
097: list2.addSymbol(B);
098: list2.addSymbol(c);
099: list2.addSymbol(C);
100:
101: assertEquals("Test if lists are equal", list, list2);
102:
103: SymbolList list3 = new SymbolList();
104:
105: list3.addSymbol(a);
106: list3.addSymbol(A);
107: list3.addSymbol(b);
108: list3.addSymbol(B);
109: list3.addSymbol(c);
110: list3.addSymbol(C);
111: list3.addSymbol(a);
112:
113: assertNotEquals("Test if lists are not equal", list, list3);
114: }
115:
116: public static Test suite() {
117: TestSuite suite = new TestSuite("Symbol list tests");
118: suite.addTest(new SymbolListTestCase());
119: return suite;
120: }
121: }
|