KickJava   Java API By Example, From Geeks To Geeks.

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


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.build.EmptyList;
16 import net.sourceforge.chaperon.build.FirstSetCollection;
17 import net.sourceforge.chaperon.model.grammar.Grammar;
18 import net.sourceforge.chaperon.model.grammar.Production;
19 import net.sourceforge.chaperon.model.symbol.Nonterminal;
20 import net.sourceforge.chaperon.model.symbol.SymbolList;
21 import net.sourceforge.chaperon.model.symbol.SymbolSet;
22 import net.sourceforge.chaperon.model.symbol.Terminal;
23
24 public class FirstSetTestCase extends TestCase
25 {
26   private Terminal plus;
27   private Terminal mult;
28   private Terminal bopen;
29   private Terminal bclose;
30   private Terminal id;
31   private Nonterminal E;
32   private Nonterminal Eprime;
33   private Nonterminal T;
34   private Nonterminal Tprime;
35   private Nonterminal F;
36   private EmptyList emptylist;
37   private Grammar grammar;
38
39   public FirstSetTestCase(String JavaDoc name)
40   {
41     super(name);
42   }
43
44   public void setUp()
45   {
46     emptylist = new EmptyList();
47
48     plus = new Terminal("plus");
49     mult = new Terminal("mult");
50     bopen = new Terminal("bopen");
51     bclose = new Terminal("bclose");
52     id = new Terminal("id");
53
54     E = new Nonterminal("E");
55     Eprime = new Nonterminal("E'");
56     T = new Nonterminal("T");
57     Tprime = new Nonterminal("T'");
58     F = new Nonterminal("F");
59
60     grammar = new Grammar();
61
62     // E -> T E'
63
Production production = new Production(E);
64     production.getDefinition().addSymbol(T);
65     production.getDefinition().addSymbol(Eprime);
66     grammar.addProduction(production);
67
68     // E' -> + T E'
69
production = new Production(Eprime);
70     production.getDefinition().addSymbol(plus);
71     production.getDefinition().addSymbol(T);
72     production.getDefinition().addSymbol(Eprime);
73     grammar.addProduction(production);
74
75     // E' ->
76
production = new Production(Eprime);
77     grammar.addProduction(production);
78
79     // T -> F T'
80
production = new Production(T);
81     production.getDefinition().addSymbol(F);
82     production.getDefinition().addSymbol(Tprime);
83     grammar.addProduction(production);
84
85     // T' -> * F T'
86
production = new Production(Tprime);
87     production.getDefinition().addSymbol(mult);
88     production.getDefinition().addSymbol(F);
89     production.getDefinition().addSymbol(Tprime);
90     grammar.addProduction(production);
91
92     // T' ->
93
production = new Production(Tprime);
94     grammar.addProduction(production);
95
96     // F -> bopen E bclose
97
production = new Production(F);
98     production.getDefinition().addSymbol(bopen);
99     production.getDefinition().addSymbol(E);
100     production.getDefinition().addSymbol(bclose);
101     grammar.addProduction(production);
102
103     // F -> id
104
production = new Production(F);
105     production.getDefinition().addSymbol(id);
106     grammar.addProduction(production);
107   }
108
109   public void testFirstOfASymbol()
110   {
111     FirstSetCollection sets = new FirstSetCollection(grammar); /*, new ConsoleLogger());*/
112
113     SymbolSet result = new SymbolSet();
114     result.addSymbol(id);
115     result.addSymbol(bopen);
116     assertEquals("Test if sets are equal", result, sets.getFirstSet(E));
117     assertEquals("Test if sets are equal", result, sets.getFirstSet(T));
118     assertEquals("Test if sets are equal", result, sets.getFirstSet(F));
119
120     result = new SymbolSet();
121     result.addSymbol(plus);
122     result.addSymbol(emptylist);
123     assertEquals("Test if sets are equal", result, sets.getFirstSet(Eprime));
124
125     result = new SymbolSet();
126     result.addSymbol(mult);
127     result.addSymbol(emptylist);
128     assertEquals("Test if sets are equal", result, sets.getFirstSet(Tprime));
129   }
130
131   public void testFirstOfASymbolList()
132   {
133     FirstSetCollection sets = new FirstSetCollection(grammar); /*, new ConsoleLogger());*/
134
135     SymbolList list = new SymbolList();
136     list.addSymbol(Eprime);
137
138     SymbolSet result = new SymbolSet();
139     result.addSymbol(plus);
140     result.addSymbol(emptylist);
141     assertEquals("Test if sets are equal", result, sets.getFirstSet(list));
142
143     list = new SymbolList();
144     list.addSymbol(Eprime);
145     list.addSymbol(id);
146     list.addSymbol(Tprime);
147
148     result = new SymbolSet();
149     result.addSymbol(plus);
150     result.addSymbol(id);
151     assertEquals("Test if sets are equal", result, sets.getFirstSet(list));
152
153     list = new SymbolList();
154     list.addSymbol(Eprime);
155     list.addSymbol(Tprime);
156
157     result = new SymbolSet();
158     result.addSymbol(plus);
159     result.addSymbol(mult);
160     result.addSymbol(emptylist);
161     assertEquals("Test if sets are equal", result, sets.getFirstSet(list));
162
163     list = new SymbolList();
164
165     result = new SymbolSet();
166     result.addSymbol(emptylist);
167     assertEquals("Test if sets are equal", result, sets.getFirstSet(list));
168   }
169
170   public static Test suite()
171   {
172     return new TestSuite(FirstSetTestCase.class);
173   }
174 }
175
Popular Tags