KickJava   Java API By Example, From Geeks To Geeks.

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


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.SymbolSet;
17 import net.sourceforge.chaperon.model.symbol.Terminal;
18
19 public class SymbolSetTestCase 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 SymbolSetTestCase(String JavaDoc name)
29   {
30     super(name);
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 testAddSymbol()
60   {
61     SymbolSet set = new SymbolSet();
62
63     assertEquals("Test if set is empty", 0, set.getSymbolCount());
64     assertEquals("Test if set is empty", true, set.isEmpty());
65
66     set.addSymbol(a);
67
68     assertEquals("Test if set is not empty", 1, set.getSymbolCount());
69     assertEquals("Test if set is not empty", false, set.isEmpty());
70
71     assertEquals("Test if symbols are equal", a, set.getSymbol(0));
72
73     set.addSymbol(a);
74     set.addSymbol(A);
75     set.addSymbol(b);
76     set.addSymbol(B);
77     set.addSymbol(c);
78     set.addSymbol(C);
79
80     assertEquals("Test if set is not empty", 6, set.getSymbolCount());
81     assertEquals("Test if set is not empty", false, set.isEmpty());
82
83     assertEquals("Test if symbols are equal", a, set.getSymbol(0));
84     assertEquals("Test if symbols are equal", B, set.getSymbol(3));
85     assertEquals("Test if symbols are equal", C, set.getSymbol(5));
86
87     assertEquals("Test if indices are equal", 0, set.indexOf(a));
88     assertEquals("Test if indices are equal", 4, set.indexOf(c));
89
90     SymbolSet set2 = new SymbolSet();
91
92     set2.addSymbol(a);
93     set2.addSymbol(a);
94     set2.addSymbol(A);
95     set2.addSymbol(b);
96
97     assertNotEquals("Test if sets are not equal", set, set2);
98
99     set2.addSymbol(B);
100     set2.addSymbol(c);
101     set2.addSymbol(C);
102
103     assertEquals("Test if sets are equal", set, set2);
104
105     SymbolSet set3 = new SymbolSet();
106
107     set3.addSymbol(a);
108     set3.addSymbol(A);
109     set3.addSymbol(b);
110     set3.addSymbol(B);
111     set3.addSymbol(c);
112     set3.addSymbol(C);
113     set3.addSymbol(a);
114
115     assertEquals("Test if sets are equal", set, set3);
116   }
117
118   public void testEquals()
119   {
120     SymbolSet set = new SymbolSet();
121     set.addSymbol(A);
122     set.addSymbol(B);
123     set.addSymbol(C);
124
125     SymbolSet set2 = new SymbolSet();
126     set2.addSymbol(A);
127     set2.addSymbol(C);
128     set2.addSymbol(B);
129
130     assertEquals("Test if symbols are equal", set, set2);
131
132     set2 = new SymbolSet();
133     set2.addSymbol(A);
134     set2.addSymbol(B);
135
136     assertNotEquals("Test if sets are not equal", set, set2);
137   }
138
139   public static Test suite()
140   {
141     return new TestSuite(SymbolSetTestCase.class);
142   }
143 }
144
Popular Tags