KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > test > GrammarTestCase


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.test;
10
11 import junit.framework.Test;
12 import junit.framework.TestCase;
13 import junit.framework.TestSuite;
14
15 import net.sourceforge.chaperon.model.grammar.Associativity;
16 import net.sourceforge.chaperon.model.grammar.Grammar;
17 import net.sourceforge.chaperon.model.grammar.Production;
18 import net.sourceforge.chaperon.model.symbol.Nonterminal;
19 import net.sourceforge.chaperon.model.symbol.Terminal;
20
21 public class GrammarTestCase extends TestCase
22 {
23   private Terminal a;
24   private Terminal b;
25   private Nonterminal E;
26   private Nonterminal F;
27   private Nonterminal G;
28   private Production p1;
29   private Production p2;
30   private Production p3;
31   private Grammar grammar;
32
33   public GrammarTestCase(String JavaDoc name)
34   {
35     super(name);
36   }
37
38   public void setUp()
39   {
40     a = new Terminal("a");
41     b = new Terminal("b");
42     E = new Nonterminal("E");
43     F = new Nonterminal("F");
44     G = new Nonterminal("G");
45
46     grammar = new Grammar();
47
48     p1 = new Production(F);
49     p1.getDefinition().addSymbol(E);
50     p1.getDefinition().addSymbol(a);
51     p1.getDefinition().addSymbol(E);
52     grammar.addProduction(p1);
53
54     p2 = new Production(F);
55     p2.getDefinition().addSymbol(E);
56     p2.getDefinition().addSymbol(b);
57     p2.getDefinition().addSymbol(E);
58     grammar.addProduction(p2);
59
60     p3 = new Production(G);
61     p3.getDefinition().addSymbol(E);
62     p3.getDefinition().addSymbol(F);
63     grammar.addProduction(p3);
64   }
65
66   public void testContent()
67   {
68     assertTrue("Test if grammar contains productions", grammar.contains(p1));
69     assertTrue("Test if grammar contains productions", grammar.contains(p2));
70
71     assertTrue("Test if production are not equal", !p1.equals(p2));
72
73     Production p3 = new Production(F);
74     p3.getDefinition().addSymbol(E);
75     p3.getDefinition().addSymbol(a);
76     p3.getDefinition().addSymbol(E);
77
78     assertEquals("Test if production are equal", p1, p3);
79     assertTrue("Test if grammar contains productions", grammar.contains(p3));
80
81     assertEquals("Test production indices", 0, grammar.indexOf(p1));
82     assertEquals("Test production indices", 1, grammar.indexOf(p2));
83     assertEquals("Test production indices", 0, grammar.indexOf(p3));
84   }
85
86   public void testPriorities()
87   {
88     assertEquals("Test terminal priority", 0, grammar.getPriority(a));
89     assertEquals("Test terminal priority", 0, grammar.getPriority(b));
90     assertEquals("Test production priority", grammar.getPriority(a), grammar.getPriority(p1));
91     assertEquals("Test production priority", grammar.getPriority(b), grammar.getPriority(p2));
92     assertEquals("Test production priority", 0, grammar.getPriority(p2));
93
94     grammar.setPriority(a, 4);
95     grammar.setPriority(b, 3);
96
97     assertEquals("Test terminal priority", 4, grammar.getPriority(a));
98     assertEquals("Test terminal priority", 3, grammar.getPriority(b));
99     assertEquals("Test production priority", grammar.getPriority(a), grammar.getPriority(p1));
100     assertEquals("Test production priority", grammar.getPriority(b), grammar.getPriority(p2));
101
102     grammar.setPriority(a, 3);
103     grammar.setPriority(b, 4);
104
105     assertEquals("Test terminal priority", 3, grammar.getPriority(a));
106     assertEquals("Test terminal priority", 4, grammar.getPriority(b));
107     assertEquals("Test production priority", grammar.getPriority(a), grammar.getPriority(p1));
108     assertEquals("Test production priority", grammar.getPriority(b), grammar.getPriority(p2));
109
110     p1.setPrecedence(b);
111     assertEquals("Test production priority", grammar.getPriority(b), grammar.getPriority(p1));
112   }
113
114   public void testAssociativities()
115   {
116     Associativity left = Associativity.LEFT;
117     Associativity right = Associativity.RIGHT;
118     Associativity nonassoc = Associativity.NONASSOC;
119
120     assertEquals("Test if associativities are equal", left, new Associativity("left"));
121     assertEquals("Test if associativities are equal", right, new Associativity("right"));
122     assertEquals("Test if associativities are equal", nonassoc, new Associativity("nonassoc"));
123
124     assertTrue("Test if associativities are not equal", !left.equals(right));
125     assertTrue("Test if associativities are not equal", !right.equals(nonassoc));
126     assertTrue("Test if associativities are not equal", !nonassoc.equals(left));
127
128     assertEquals("Test production associativities", nonassoc, grammar.getAssociativity(p1));
129     assertEquals("Test production associativities", nonassoc, grammar.getAssociativity(p2));
130     assertEquals("Test production associativities", nonassoc, grammar.getAssociativity(p3));
131
132     grammar.setAssociativity(b, right);
133
134     assertEquals("Test terminal associativities", right, grammar.getAssociativity(b));
135     assertEquals("Test production associativities", nonassoc, grammar.getAssociativity(p1));
136     assertEquals("Test production associativities", right, grammar.getAssociativity(p2));
137     assertEquals("Test production associativities", nonassoc, grammar.getAssociativity(p3));
138
139     grammar.setAssociativity(a, left);
140
141     assertEquals("Test terminal associativities", left, grammar.getAssociativity(a));
142     assertEquals("Test production associativities", left, grammar.getAssociativity(p1));
143     assertEquals("Test production associativities", right, grammar.getAssociativity(p2));
144     assertEquals("Test production associativities", nonassoc, grammar.getAssociativity(p3));
145
146     p1.setPrecedence(b);
147     assertEquals("Test production priority", right, grammar.getAssociativity(p1));
148   }
149
150   public static Test suite()
151   {
152     return new TestSuite(GrammarTestCase.class);
153   }
154 }
155
Popular Tags