KickJava   Java API By Example, From Geeks To Geeks.

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


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.symbol.Nonterminal;
16 import net.sourceforge.chaperon.model.symbol.SymbolList;
17 import net.sourceforge.chaperon.model.symbol.Terminal;
18
19 public class SymbolListTestCase extends TestCase
20 {
21   private Terminal a;
22   private Terminal b;
23   private Terminal c;
24   private Nonterminal A;
25   private Nonterminal B;
26   private Nonterminal C;
27
28   public SymbolListTestCase()
29   {
30     super("SymbolListTestCase");
31   }
32
33   public void setUp()
34   {
35     a = new Terminal("a");
36     b = new Terminal("b");
37     c = new Terminal("c");
38
39     A = new Nonterminal("A");
40     B = new Nonterminal("B");
41     C = new Nonterminal("C");
42   }
43
44   public static void assertNotEquals(String JavaDoc message, Object JavaDoc a, Object JavaDoc b)
45   {
46     if ((a==null) || (a==null))
47       return;
48
49     if (a.equals(b))
50     {
51       String JavaDoc formatted = "";
52       if (message!=null)
53         formatted = message+" ";
54
55       fail(formatted+"<"+a+"> equals <"+b+">");
56     }
57   }
58
59   public void runTest() throws Throwable JavaDoc
60   {
61     SymbolList list = new SymbolList();
62
63     assertEquals("Test if list is empty", 0, list.getSymbolCount());
64     assertEquals("Test if list is empty", true, list.isEmpty());
65
66     list.addSymbol(a);
67
68     assertEquals("Test if list is not empty", 1, list.getSymbolCount());
69     assertEquals("Test if list is not empty", false, list.isEmpty());
70
71     assertEquals("Test if symbols are equal", a, list.getSymbol(0));
72
73     list.addSymbol(a);
74     list.addSymbol(A);
75     list.addSymbol(b);
76     list.addSymbol(B);
77     list.addSymbol(c);
78     list.addSymbol(C);
79
80     assertEquals("Test if list is not empty", 7, list.getSymbolCount());
81     assertEquals("Test if list is not empty", false, list.isEmpty());
82
83     assertEquals("Test if symbols are equal", a, list.getSymbol(0));
84     assertEquals("Test if symbols are equal", a, list.getSymbol(1));
85     assertEquals("Test if symbols are equal", B, list.getSymbol(4));
86     assertEquals("Test if symbols are equal", C, list.getSymbol(6));
87
88     assertEquals("Test if indices are equal", 0, list.indexOf(a));
89     assertEquals("Test if indices are equal", 5, list.indexOf(c));
90
91     SymbolList list2 = new SymbolList();
92
93     list2.addSymbol(a);
94     list2.addSymbol(a);
95     list2.addSymbol(A);
96     list2.addSymbol(b);
97
98     assertNotEquals("Test if lists are not equal", list, list2);
99
100     list2.addSymbol(B);
101     list2.addSymbol(c);
102     list2.addSymbol(C);
103
104     assertEquals("Test if lists are equal", list, list2);
105
106     SymbolList list3 = new SymbolList();
107
108     list3.addSymbol(a);
109     list3.addSymbol(A);
110     list3.addSymbol(b);
111     list3.addSymbol(B);
112     list3.addSymbol(c);
113     list3.addSymbol(C);
114     list3.addSymbol(a);
115
116     assertNotEquals("Test if lists are not equal", list, list3);
117   }
118
119   public static Test suite()
120   {
121     TestSuite suite = new TestSuite("Symbol list tests");
122     suite.addTest(new SymbolListTestCase());
123     return suite;
124   }
125 }
126
Popular Tags