KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > parser > TestProductionPattern


1 /*
2  * TestProductionPattern.java
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2.1
7  * of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17  * MA 02111-1307, USA.
18  *
19  * Copyright (c) 2003-2005 Per Cederberg. All rights reserved.
20  */

21
22 package net.percederberg.grammatica.parser;
23
24 import junit.framework.TestCase;
25
26 /**
27  * A test case for the ProductionPattern class.
28  *
29  * @author Per Cederberg, <per at percederberg dot net>
30  * @version 1.0
31  */

32 public class TestProductionPattern extends TestCase {
33
34     /**
35      * A token constant.
36      */

37     private static final int T1 = 1001;
38
39     /**
40      * A token constant.
41      */

42     private static final int T2 = 1002;
43
44     /**
45      * A production constant.
46      */

47     private static final int P1 = 2001;
48
49     /**
50      * A production constant.
51      */

52     private static final int P2 = 2002;
53
54     /**
55      * The production pattern variable used in tests.
56      */

57     private ProductionPattern pattern;
58
59     /**
60      * The production pattern alternative variable used in tests.
61      */

62     private ProductionPatternAlternative alt;
63
64     /**
65      * Tests the pattern left-recursive detection.
66      */

67     public void testLeftRecursive() {
68         pattern = new ProductionPattern(P1, "P1");
69         alt = new ProductionPatternAlternative();
70         alt.addProduction(P2, 0, -1);
71         alt.addToken(T1, 0, 1);
72         alt.addProduction(P1, 0, 1);
73         alt.addToken(T2, 1, 1);
74         addAlternative(pattern, alt);
75         assertTrue(pattern.isLeftRecursive());
76     }
77
78     /**
79      * Tests the pattern right-recursive detection.
80      */

81     public void testRightRecursive() {
82         pattern = new ProductionPattern(P1, "P1");
83         alt = new ProductionPatternAlternative();
84         alt.addToken(T2, 1, 1);
85         alt.addProduction(P1, 0, 1);
86         alt.addProduction(P2, 0, -1);
87         alt.addToken(T1, 0, 1);
88         addAlternative(pattern, alt);
89         assertTrue(pattern.isRightRecursive());
90     }
91
92     /**
93      * Tests the pattern empty matching detection.
94      */

95     public void testMatchingEmpty() {
96         pattern = new ProductionPattern(P1, "P1");
97         alt = new ProductionPatternAlternative();
98         alt.addProduction(P2, 0, -1);
99         alt.addToken(T1, 0, 1);
100         alt.addProduction(P1, 0, 1);
101         addAlternative(pattern, alt);
102         assertTrue(pattern.isMatchingEmpty());
103     }
104
105     /**
106      * Tests adding a single pattern alternative twice.
107      */

108     public void testDuplicateAlternative() {
109         pattern = new ProductionPattern(P1, "P1");
110         alt = new ProductionPatternAlternative();
111         alt.addToken(T1, 1, 1);
112         addAlternative(pattern, alt);
113         failAddAlternative(pattern, alt);
114     }
115
116     /**
117      * Tests adding a duplicate pattern alternative.
118      */

119     public void testIdenticalAlternative() {
120         pattern = new ProductionPattern(P1, "P1");
121         alt = new ProductionPatternAlternative();
122         alt.addToken(T1, 1, 1);
123         addAlternative(pattern, alt);
124         alt = new ProductionPatternAlternative();
125         alt.addToken(T1, 1, 1);
126         failAddAlternative(pattern, alt);
127     }
128
129     /**
130      * Adds a pattern alternative. This method reports a test failure
131      * if an exception was thrown.
132      *
133      * @param pattern the production pattern
134      * @param alt the pattern alternative to add
135      */

136     private void addAlternative(ProductionPattern pattern,
137                                 ProductionPatternAlternative alt) {
138
139         try {
140             pattern.addAlternative(alt);
141         } catch (ParserCreationException e) {
142             fail("couldn't add alternative to " + pattern.getName() +
143                  ": " + e.getMessage());
144         }
145     }
146
147     /**
148      * Adds a pattern alternative. This method reports a test failure
149      * if no exception was thrown.
150      *
151      * @param pattern the production pattern
152      * @param alt the pattern alternative to add
153      */

154     private void failAddAlternative(ProductionPattern pattern,
155                                     ProductionPatternAlternative alt) {
156
157         try {
158             pattern.addAlternative(alt);
159             fail("could add alternative to " + pattern.getName());
160         } catch (ParserCreationException e) {
161             // Failure was expected
162
}
163     }
164 }
165
Popular Tags