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.SymbolSet;
017: import net.sourceforge.chaperon.model.symbol.Terminal;
018:
019: public class SymbolSetTestCase 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 SymbolSetTestCase(String name) {
028: super (name);
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 testAddSymbol() {
056: SymbolSet set = new SymbolSet();
057:
058: assertEquals("Test if set is empty", 0, set.getSymbolCount());
059: assertEquals("Test if set is empty", true, set.isEmpty());
060:
061: set.addSymbol(a);
062:
063: assertEquals("Test if set is not empty", 1, set
064: .getSymbolCount());
065: assertEquals("Test if set is not empty", false, set.isEmpty());
066:
067: assertEquals("Test if symbols are equal", a, set.getSymbol(0));
068:
069: set.addSymbol(a);
070: set.addSymbol(A);
071: set.addSymbol(b);
072: set.addSymbol(B);
073: set.addSymbol(c);
074: set.addSymbol(C);
075:
076: assertEquals("Test if set is not empty", 6, set
077: .getSymbolCount());
078: assertEquals("Test if set is not empty", false, set.isEmpty());
079:
080: assertEquals("Test if symbols are equal", a, set.getSymbol(0));
081: assertEquals("Test if symbols are equal", B, set.getSymbol(3));
082: assertEquals("Test if symbols are equal", C, set.getSymbol(5));
083:
084: assertEquals("Test if indices are equal", 0, set.indexOf(a));
085: assertEquals("Test if indices are equal", 4, set.indexOf(c));
086:
087: SymbolSet set2 = new SymbolSet();
088:
089: set2.addSymbol(a);
090: set2.addSymbol(a);
091: set2.addSymbol(A);
092: set2.addSymbol(b);
093:
094: assertNotEquals("Test if sets are not equal", set, set2);
095:
096: set2.addSymbol(B);
097: set2.addSymbol(c);
098: set2.addSymbol(C);
099:
100: assertEquals("Test if sets are equal", set, set2);
101:
102: SymbolSet set3 = new SymbolSet();
103:
104: set3.addSymbol(a);
105: set3.addSymbol(A);
106: set3.addSymbol(b);
107: set3.addSymbol(B);
108: set3.addSymbol(c);
109: set3.addSymbol(C);
110: set3.addSymbol(a);
111:
112: assertEquals("Test if sets are equal", set, set3);
113: }
114:
115: public void testEquals() {
116: SymbolSet set = new SymbolSet();
117: set.addSymbol(A);
118: set.addSymbol(B);
119: set.addSymbol(C);
120:
121: SymbolSet set2 = new SymbolSet();
122: set2.addSymbol(A);
123: set2.addSymbol(C);
124: set2.addSymbol(B);
125:
126: assertEquals("Test if symbols are equal", set, set2);
127:
128: set2 = new SymbolSet();
129: set2.addSymbol(A);
130: set2.addSymbol(B);
131:
132: assertNotEquals("Test if sets are not equal", set, set2);
133: }
134:
135: public static Test suite() {
136: return new TestSuite(SymbolSetTestCase.class);
137: }
138: }
|