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.grammar.Associativity;
016: import net.sourceforge.chaperon.model.grammar.Grammar;
017: import net.sourceforge.chaperon.model.grammar.Production;
018: import net.sourceforge.chaperon.model.symbol.Nonterminal;
019: import net.sourceforge.chaperon.model.symbol.Terminal;
020:
021: public class GrammarTestCase extends TestCase {
022: private Terminal a;
023: private Terminal b;
024: private Nonterminal E;
025: private Nonterminal F;
026: private Nonterminal G;
027: private Production p1;
028: private Production p2;
029: private Production p3;
030: private Grammar grammar;
031:
032: public GrammarTestCase(String name) {
033: super (name);
034: }
035:
036: public void setUp() {
037: a = new Terminal("a");
038: b = new Terminal("b");
039: E = new Nonterminal("E");
040: F = new Nonterminal("F");
041: G = new Nonterminal("G");
042:
043: grammar = new Grammar();
044:
045: p1 = new Production(F);
046: p1.getDefinition().addSymbol(E);
047: p1.getDefinition().addSymbol(a);
048: p1.getDefinition().addSymbol(E);
049: grammar.addProduction(p1);
050:
051: p2 = new Production(F);
052: p2.getDefinition().addSymbol(E);
053: p2.getDefinition().addSymbol(b);
054: p2.getDefinition().addSymbol(E);
055: grammar.addProduction(p2);
056:
057: p3 = new Production(G);
058: p3.getDefinition().addSymbol(E);
059: p3.getDefinition().addSymbol(F);
060: grammar.addProduction(p3);
061: }
062:
063: public void testContent() {
064: assertTrue("Test if grammar contains productions", grammar
065: .contains(p1));
066: assertTrue("Test if grammar contains productions", grammar
067: .contains(p2));
068:
069: assertTrue("Test if production are not equal", !p1.equals(p2));
070:
071: Production p3 = new Production(F);
072: p3.getDefinition().addSymbol(E);
073: p3.getDefinition().addSymbol(a);
074: p3.getDefinition().addSymbol(E);
075:
076: assertEquals("Test if production are equal", p1, p3);
077: assertTrue("Test if grammar contains productions", grammar
078: .contains(p3));
079:
080: assertEquals("Test production indices", 0, grammar.indexOf(p1));
081: assertEquals("Test production indices", 1, grammar.indexOf(p2));
082: assertEquals("Test production indices", 0, grammar.indexOf(p3));
083: }
084:
085: public void testPriorities() {
086: assertEquals("Test terminal priority", 0, grammar
087: .getPriority(a));
088: assertEquals("Test terminal priority", 0, grammar
089: .getPriority(b));
090: assertEquals("Test production priority",
091: grammar.getPriority(a), grammar.getPriority(p1));
092: assertEquals("Test production priority",
093: grammar.getPriority(b), grammar.getPriority(p2));
094: assertEquals("Test production priority", 0, grammar
095: .getPriority(p2));
096:
097: grammar.setPriority(a, 4);
098: grammar.setPriority(b, 3);
099:
100: assertEquals("Test terminal priority", 4, grammar
101: .getPriority(a));
102: assertEquals("Test terminal priority", 3, grammar
103: .getPriority(b));
104: assertEquals("Test production priority",
105: grammar.getPriority(a), grammar.getPriority(p1));
106: assertEquals("Test production priority",
107: grammar.getPriority(b), grammar.getPriority(p2));
108:
109: grammar.setPriority(a, 3);
110: grammar.setPriority(b, 4);
111:
112: assertEquals("Test terminal priority", 3, grammar
113: .getPriority(a));
114: assertEquals("Test terminal priority", 4, grammar
115: .getPriority(b));
116: assertEquals("Test production priority",
117: grammar.getPriority(a), grammar.getPriority(p1));
118: assertEquals("Test production priority",
119: grammar.getPriority(b), grammar.getPriority(p2));
120:
121: p1.setPrecedence(b);
122: assertEquals("Test production priority",
123: grammar.getPriority(b), grammar.getPriority(p1));
124: }
125:
126: public void testAssociativities() {
127: Associativity left = Associativity.LEFT;
128: Associativity right = Associativity.RIGHT;
129: Associativity nonassoc = Associativity.NONASSOC;
130:
131: assertEquals("Test if associativities are equal", left,
132: new Associativity("left"));
133: assertEquals("Test if associativities are equal", right,
134: new Associativity("right"));
135: assertEquals("Test if associativities are equal", nonassoc,
136: new Associativity("nonassoc"));
137:
138: assertTrue("Test if associativities are not equal", !left
139: .equals(right));
140: assertTrue("Test if associativities are not equal", !right
141: .equals(nonassoc));
142: assertTrue("Test if associativities are not equal", !nonassoc
143: .equals(left));
144:
145: assertEquals("Test production associativities", nonassoc,
146: grammar.getAssociativity(p1));
147: assertEquals("Test production associativities", nonassoc,
148: grammar.getAssociativity(p2));
149: assertEquals("Test production associativities", nonassoc,
150: grammar.getAssociativity(p3));
151:
152: grammar.setAssociativity(b, right);
153:
154: assertEquals("Test terminal associativities", right, grammar
155: .getAssociativity(b));
156: assertEquals("Test production associativities", nonassoc,
157: grammar.getAssociativity(p1));
158: assertEquals("Test production associativities", right, grammar
159: .getAssociativity(p2));
160: assertEquals("Test production associativities", nonassoc,
161: grammar.getAssociativity(p3));
162:
163: grammar.setAssociativity(a, left);
164:
165: assertEquals("Test terminal associativities", left, grammar
166: .getAssociativity(a));
167: assertEquals("Test production associativities", left, grammar
168: .getAssociativity(p1));
169: assertEquals("Test production associativities", right, grammar
170: .getAssociativity(p2));
171: assertEquals("Test production associativities", nonassoc,
172: grammar.getAssociativity(p3));
173:
174: p1.setPrecedence(b);
175: assertEquals("Test production priority", right, grammar
176: .getAssociativity(p1));
177: }
178:
179: public static Test suite() {
180: return new TestSuite(GrammarTestCase.class);
181: }
182: }
|