1 21 22 package net.percederberg.grammatica.parser; 23 24 import junit.framework.TestCase; 25 26 32 public class TestProductionPattern extends TestCase { 33 34 37 private static final int T1 = 1001; 38 39 42 private static final int T2 = 1002; 43 44 47 private static final int P1 = 2001; 48 49 52 private static final int P2 = 2002; 53 54 57 private ProductionPattern pattern; 58 59 62 private ProductionPatternAlternative alt; 63 64 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 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 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 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 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 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 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 } 163 } 164 } 165 | Popular Tags |