1 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.EndOfFile; 16 import net.sourceforge.chaperon.build.FirstSetCollection; 17 import net.sourceforge.chaperon.build.ItemSet; 18 import net.sourceforge.chaperon.model.grammar.Grammar; 19 import net.sourceforge.chaperon.model.grammar.Production; 20 import net.sourceforge.chaperon.model.symbol.Nonterminal; 21 import net.sourceforge.chaperon.model.symbol.Terminal; 22 23 public class ItemSetTestCase extends TestCase 24 { 25 private Terminal c; 26 private Terminal d; 27 private EndOfFile eof; 28 private Nonterminal S; 29 private Nonterminal C; 30 private Grammar grammar; 31 private FirstSetCollection firstsets; 32 33 public ItemSetTestCase(String name) 34 { 35 super(name); 36 37 } 39 40 public void setUp() 41 { 42 c = new Terminal("c"); 43 d = new Terminal("d"); 44 45 eof = new EndOfFile(); 46 47 S = new Nonterminal("S"); 48 C = new Nonterminal("C"); 49 50 grammar = new Grammar(); 51 52 Production production = new Production(S); 53 production.getDefinition().addSymbol(C); 54 production.getDefinition().addSymbol(C); 55 grammar.addProduction(production); 56 57 production = new Production(C); 58 production.getDefinition().addSymbol(c); 59 production.getDefinition().addSymbol(C); 60 grammar.addProduction(production); 61 62 production = new Production(C); 63 production.getDefinition().addSymbol(d); 64 grammar.addProduction(production); 65 66 grammar.setStartSymbol(S); 67 68 firstsets = new FirstSetCollection(grammar); 69 } 70 71 public static void assertNotEquals(String message, Object a, Object b) 72 { 73 if ((a==null) || (a==null)) 74 return; 75 76 if (a.equals(b)) 77 { 78 String formatted = ""; 79 if (message!=null) 80 formatted = message+" "; 81 82 fail(formatted+"<"+a+"> equals <"+b+">"); 83 } 84 } 85 86 public void testAddItem() 87 { 88 ItemSet set = new ItemSet(grammar, firstsets); 89 assertEquals("Test if count of item are equal", set.getItemCount(), 0); 90 91 set.addItem(0, 0, eof); 92 assertEquals("Test if count of item are equal", set.getItemCount(), 1); 93 94 set.addItem(1, 0, c); 95 assertEquals("Test if count of item are equal", set.getItemCount(), 2); 96 97 set.addItem(1, 1, d); 98 assertEquals("Test if count of item are equal", set.getItemCount(), 3); 99 100 set.addItem(2, 0, c); 101 assertEquals("Test if count of item are equal", set.getItemCount(), 4); 102 103 assertTrue("Test if item was added", set.addItem(1, 2, d)); 104 assertEquals("Test if count of item are equal", set.getItemCount(), 5); 105 106 assertTrue("Test if item was added", set.addItem(2, 1, d)); 107 assertEquals("Test if count of item are equal", set.getItemCount(), 6); 108 109 assertTrue("Test if item was added", set.addItem(1, 1, eof)); 110 assertEquals("Test if count of item are equal", set.getItemCount(), 6); 111 112 assertTrue("Test if item was added", !set.addItem(1, 1, d)); 113 assertEquals("Test if count of item are equal", set.getItemCount(), 6); 114 115 ItemSet set2 = new ItemSet(grammar, firstsets); 116 set2.addItem(1, 1, eof); 117 set2.addItem(2, 1, d); 118 set2.addItem(1, 2, d); 119 set2.addItem(2, 0, c); 120 set2.addItem(1, 1, d); 121 set2.addItem(1, 0, c); 122 set2.addItem(0, 0, eof); 123 124 assertEquals("Test if sets are equal", set, set2); 125 } 126 127 public void testAddItemSet() 128 { 129 ItemSet set = new ItemSet(grammar, firstsets); 130 assertEquals("Test if count of item are equal", set.getItemCount(), 0); 131 132 set.addItem(0, 0, eof); 133 assertEquals("Test if count of item are equal", set.getItemCount(), 1); 134 135 set.addItem(1, 0, c); 136 assertEquals("Test if count of item are equal", set.getItemCount(), 2); 137 138 set.addItem(1, 1, d); 139 assertEquals("Test if count of item are equal", set.getItemCount(), 3); 140 141 set.addItem(2, 0, c); 142 assertEquals("Test if count of item are equal", set.getItemCount(), 4); 143 144 ItemSet set2 = new ItemSet(grammar, firstsets); 145 set2.addItem(1, 2, d); 146 set2.addItem(2, 1, d); 147 set2.addItem(1, 1, eof); 148 set2.addItem(1, 1, d); 149 150 assertTrue("Test if set was added", set.addItemSet(set2)); 151 152 assertEquals("Test if count of item are equal", set.getItemCount(), 6); 153 154 ItemSet set3 = new ItemSet(grammar, firstsets); 155 set3.addItem(1, 1, eof); 156 set3.addItem(2, 1, d); 157 set3.addItem(1, 2, d); 158 set3.addItem(2, 0, c); 159 set3.addItem(1, 1, d); 160 set3.addItem(1, 0, c); 161 set3.addItem(0, 0, eof); 162 163 assertEquals("Test if sets are equal", set, set3); 164 165 assertTrue("Test if set was added", !set.addItemSet(set2)); 166 } 167 168 public void testContains() 169 { 170 ItemSet set = new ItemSet(grammar, firstsets); 171 set.addItem(0, 0, eof); 172 set.addItem(1, 0, c); 173 set.addItem(1, 0, d); 174 set.addItem(2, 0, c); 175 176 ItemSet set2 = new ItemSet(grammar, firstsets); 177 set2.addItem(1, 0, c); 178 set2.addItem(1, 0, d); 179 180 assertTrue("Test if set contains another set", set.contains(set2)); 181 assertTrue("Test if set contains another set", !set2.contains(set)); 182 } 183 184 public void testEquals() 185 { 186 ItemSet set = new ItemSet(grammar, firstsets); 187 set.addItem(0, 0, eof); 188 set.addItem(1, 0, c); 189 set.addItem(1, 0, d); 190 set.addItem(2, 0, c); 191 set.addItem(2, 0, d); 192 193 ItemSet set2 = new ItemSet(grammar, firstsets); 194 set2.addItem(0, 0, eof); 195 set2.addItem(1, 0, c); 196 set2.addItem(1, 0, d); 197 set2.addItem(2, 0, c); 198 set2.addItem(2, 0, d); 199 200 assertEquals("Test if sets are equal", set, set2); 201 202 set = new ItemSet(grammar, firstsets); 203 set.addItem(0, 0, eof); 204 set.addItem(1, 0, c); 205 set.addItem(1, 0, d); 206 set.addItem(2, 0, c); 207 set.addItem(2, 0, d); 208 209 set2 = new ItemSet(grammar, firstsets); 210 set2.addItem(1, 0, d); 211 set2.addItem(2, 0, c); 212 set2.addItem(2, 0, d); 213 set2.addItem(0, 0, eof); 214 set2.addItem(1, 0, c); 215 216 assertEquals("Test if sets are equal", set, set2); 217 218 set = new ItemSet(grammar, firstsets); 219 set.addItem(0, 0, eof); 220 set.addItem(1, 0, c); 221 set.addItem(1, 0, d); 222 set.addItem(2, 0, c); 223 set.addItem(2, 0, d); 224 225 set2 = new ItemSet(grammar, firstsets); 226 set2.addItem(0, 0, eof); 227 set2.addItem(1, 0, c); 228 set2.addItem(1, 0, eof); 229 set2.addItem(2, 0, c); 230 set2.addItem(2, 0, d); 231 232 assertNotEquals("Test if sets are not equal", set, set2); 233 234 set = new ItemSet(grammar, firstsets); 235 set.addItem(0, 0, eof); 236 set.addItem(1, 0, c); 237 set.addItem(1, 0, d); 238 set.addItem(2, 0, c); 239 set.addItem(2, 0, d); 240 241 set2 = new ItemSet(grammar, firstsets); 242 set2.addItem(0, 0, eof); 243 set2.addItem(0, 0, c); 244 set2.addItem(1, 0, d); 245 set2.addItem(2, 0, c); 246 set2.addItem(2, 0, d); 247 248 assertNotEquals("Test if sets are not equal", set, set2); 249 } 250 251 public void testEqualsCore() 252 { 253 ItemSet set = new ItemSet(grammar, firstsets); 254 set.addItem(0, 0, eof); 255 set.addItem(1, 0, c); 256 set.addItem(1, 0, d); 257 set.addItem(2, 0, c); 258 set.addItem(2, 0, d); 259 260 ItemSet set2 = new ItemSet(grammar, firstsets); 261 set2.addItem(0, 0, eof); 262 set2.addItem(1, 0, c); 263 set2.addItem(2, 0, d); 264 265 assertTrue("Test if sets have the same core", set.equalsCore(set2)); 266 267 set = new ItemSet(grammar, firstsets); 268 set.addItem(0, 0, eof); 269 set.addItem(1, 0, c); 270 set.addItem(1, 0, d); 271 set.addItem(2, 0, c); 272 set.addItem(2, 0, d); 273 274 set2 = new ItemSet(grammar, firstsets); 275 set2.addItem(2, 0, d); 276 set2.addItem(0, 0, eof); 277 set2.addItem(1, 0, c); 278 279 assertTrue("Test if sets have the same core", set.equalsCore(set2)); 280 281 set = new ItemSet(grammar, firstsets); 282 set.addItem(0, 0, eof); 283 set.addItem(1, 1, c); 284 set.addItem(1, 1, d); 285 set.addItem(2, 1, c); 286 set.addItem(2, 1, d); 287 288 set2 = new ItemSet(grammar, firstsets); 289 set2.addItem(2, 1, d); 290 set2.addItem(0, 0, eof); 291 set2.addItem(1, 1, c); 292 293 assertTrue("Test if sets have the same core", set.equalsCore(set2)); 294 295 set2 = new ItemSet(grammar, firstsets); 296 set2.addItem(1, 1, d); 297 set2.addItem(0, 0, eof); 298 set2.addItem(1, 1, c); 299 300 assertTrue("Test if sets don't have the same core", !set.equalsCore(set2)); 301 } 302 303 public void testClosure() 304 { 305 ItemSet set = new ItemSet(grammar, firstsets); 307 set.addItem(0, 0, eof); 308 309 ItemSet result = new ItemSet(grammar, firstsets); 310 result.addItem(0, 0, eof); 311 result.addItem(1, 0, c); 312 result.addItem(1, 0, d); 313 result.addItem(2, 0, c); 314 result.addItem(2, 0, d); 315 316 assertEquals("Test if sets are equal", result, set.closure()); 317 318 set = new ItemSet(grammar, firstsets); 319 set.addItem(0, 1, eof); 320 321 result = new ItemSet(grammar, firstsets); 322 result.addItem(0, 1, eof); 323 result.addItem(1, 0, eof); 324 result.addItem(2, 0, eof); 325 326 assertEquals("Test if sets are equal", result, set.closure()); 327 328 set = new ItemSet(grammar, firstsets); 329 set.addItem(0, 2, eof); 330 331 result = new ItemSet(grammar, firstsets); 332 result.addItem(0, 2, eof); 333 334 assertEquals("Test if sets are equal", result, set.closure()); 335 336 set = new ItemSet(grammar, firstsets); 337 set.addItem(1, 1, c); 338 set.addItem(1, 1, d); 339 340 result = new ItemSet(grammar, firstsets); 341 result.addItem(1, 1, c); 342 result.addItem(1, 1, d); 343 result.addItem(1, 0, c); 344 result.addItem(1, 0, d); 345 result.addItem(2, 0, c); 346 result.addItem(2, 0, d); 347 348 assertEquals("Test if sets are equal", result, set.closure()); 349 350 Terminal a = new Terminal("a"); 352 Terminal b = new Terminal("b"); 353 Terminal c = new Terminal("c"); 354 355 Nonterminal A = new Nonterminal("A"); 356 Nonterminal B = new Nonterminal("B"); 357 Nonterminal C = new Nonterminal("C"); 358 Nonterminal D = new Nonterminal("D"); 359 360 grammar = new Grammar(); 361 362 Production production = new Production(A); 363 production.getDefinition().addSymbol(B); 364 production.getDefinition().addSymbol(a); 365 grammar.addProduction(production); 366 367 production = new Production(B); 368 production.getDefinition().addSymbol(b); 369 grammar.addProduction(production); 370 371 production = new Production(C); 372 production.getDefinition().addSymbol(B); 373 production.getDefinition().addSymbol(D); 374 grammar.addProduction(production); 375 376 production = new Production(D); 377 378 grammar.addProduction(production); 380 381 firstsets = new FirstSetCollection(grammar); 382 383 set = new ItemSet(grammar, firstsets); 384 set.addItem(0, 0, c); 385 386 result = new ItemSet(grammar, firstsets); 387 result.addItem(0, 0, c); 388 result.addItem(1, 0, a); 389 390 assertEquals("Test if sets are equal", result, set.closure()); 391 392 set = new ItemSet(grammar, firstsets); 393 set.addItem(2, 0, a); 394 395 result = new ItemSet(grammar, firstsets); 396 result.addItem(2, 0, a); 397 result.addItem(1, 0, a); 398 399 assertEquals("Test if sets are equal", result, set.closure()); 400 401 grammar = new Grammar(); 403 404 production = new Production(A); 405 production.getDefinition().addSymbol(B); 406 production.getDefinition().addSymbol(D); 407 grammar.addProduction(production); 408 409 production = new Production(B); 410 production.getDefinition().addSymbol(C); 411 grammar.addProduction(production); 412 413 production = new Production(C); 414 production.getDefinition().addSymbol(d); 415 grammar.addProduction(production); 416 417 production = new Production(D); 418 production.getDefinition().addSymbol(b); 419 grammar.addProduction(production); 420 421 firstsets = new FirstSetCollection(grammar); 422 423 set = new ItemSet(grammar, firstsets); 424 set.addItem(0, 0, c); 425 426 result = new ItemSet(grammar, firstsets); 427 result.addItem(0, 0, c); 428 result.addItem(1, 0, b); 429 result.addItem(2, 0, b); 430 431 assertEquals("Test if sets are equal", result, set.closure()); 432 } 433 434 467 public static Test suite() 468 { 469 return new TestSuite(ItemSetTestCase.class); 470 } 471 } 472 | Popular Tags |