KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sablecc > sablecc > alphabet > SymbolPairTest


1 /* This file is part of SableCC ( http://sablecc.org ).
2  *
3  * Copyright 2007 Patrick Pelletier <pp.pelletier@gmail.com>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.sablecc.sablecc.alphabet;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23
24 import java.math.BigInteger JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.LinkedList JavaDoc;
27
28 import org.junit.Before;
29 import org.junit.Test;
30
31 public class SymbolPairTest {
32
33     private Symbol<Integer JavaDoc> symbol1Int;
34
35     private Symbol<Integer JavaDoc> symbol2Int;
36
37     private Collection JavaDoc<Interval<Integer JavaDoc>> intervalsInt = new LinkedList JavaDoc<Interval<Integer JavaDoc>>();
38
39     private Symbol<BigInteger JavaDoc> symbol1BigInt;
40
41     private Symbol<BigInteger JavaDoc> symbol2BigInt;
42
43     private Collection JavaDoc<Interval<BigInteger JavaDoc>> intervalsBigInt = new LinkedList JavaDoc<Interval<BigInteger JavaDoc>>();
44
45     @Before
46     public void setUp() {
47
48         // For Integer.
49
this.intervalsInt.clear();
50         this.intervalsInt
51                 .add(new Interval<Integer JavaDoc>(10, 20, Realms.getInteger()));
52         this.intervalsInt.add(new Interval<Integer JavaDoc>(50, 100, Realms
53                 .getInteger()));
54         this.intervalsInt.add(new Interval<Integer JavaDoc>(200, 400, Realms
55                 .getInteger()));
56
57         this.symbol1Int = new Symbol<Integer JavaDoc>(this.intervalsInt);
58
59         this.intervalsInt.clear();
60         this.intervalsInt
61                 .add(new Interval<Integer JavaDoc>(30, 40, Realms.getInteger()));
62         this.intervalsInt
63                 .add(new Interval<Integer JavaDoc>(75, 80, Realms.getInteger()));
64         this.intervalsInt.add(new Interval<Integer JavaDoc>(150, 175, Realms
65                 .getInteger()));
66
67         this.symbol2Int = new Symbol<Integer JavaDoc>(this.intervalsInt);
68
69         // For BigInteger.
70
this.intervalsBigInt.clear();
71         this.intervalsBigInt.add(new Interval<BigInteger JavaDoc>(BigInteger.TEN,
72                 new BigInteger JavaDoc("20"), Realms.getBigInteger()));
73         this.intervalsBigInt.add(new Interval<BigInteger JavaDoc>(new BigInteger JavaDoc("50"),
74                 new BigInteger JavaDoc("100"), Realms.getBigInteger()));
75         this.intervalsBigInt.add(new Interval<BigInteger JavaDoc>(
76                 new BigInteger JavaDoc("200"), new BigInteger JavaDoc("400"), Realms
77                         .getBigInteger()));
78
79         this.symbol1BigInt = new Symbol<BigInteger JavaDoc>(this.intervalsBigInt);
80
81         this.intervalsBigInt.clear();
82         this.intervalsBigInt.add(new Interval<BigInteger JavaDoc>(new BigInteger JavaDoc("30"),
83                 new BigInteger JavaDoc("40"), Realms.getBigInteger()));
84         this.intervalsBigInt.add(new Interval<BigInteger JavaDoc>(new BigInteger JavaDoc("75"),
85                 new BigInteger JavaDoc("80"), Realms.getBigInteger()));
86         this.intervalsBigInt.add(new Interval<BigInteger JavaDoc>(
87                 new BigInteger JavaDoc("150"), new BigInteger JavaDoc("175"), Realms
88                         .getBigInteger()));
89
90         this.symbol2BigInt = new Symbol<BigInteger JavaDoc>(this.intervalsBigInt);
91
92     }
93
94     @Test
95     public void testSymbolPair() {
96
97         // Testing constructor with type Integer
98
SymbolPair<Integer JavaDoc> symbolPairInt = new SymbolPair<Integer JavaDoc>(
99                 this.symbol1Int, this.symbol2Int);
100         assertEquals("Constructed SymbolPair doesn't return good symbol1Int",
101                 this.symbol1Int, symbolPairInt.getSymbol1());
102         assertEquals("Constructed SymbolPair doesn't return good symbol1Int",
103                 this.symbol2Int, symbolPairInt.getSymbol2());
104
105         // Testing constructor with type BigInteger
106
SymbolPair<BigInteger JavaDoc> symbolPairBigInt = new SymbolPair<BigInteger JavaDoc>(
107                 this.symbol1BigInt, this.symbol2BigInt);
108         assertEquals(
109                 "Constructed SymbolPair doesn't return good symbol1BigInt",
110                 this.symbol1BigInt, symbolPairBigInt.getSymbol1());
111         assertEquals(
112                 "Constructed SymbolPair doesn't return good symbol1BIgInt",
113                 this.symbol2BigInt, symbolPairBigInt.getSymbol2());
114
115     }
116
117     @Test
118     public void testEqualsObject() {
119
120         // =====================
121
// Testing with integer.
122
SymbolPair<Integer JavaDoc> symbolPairInt = new SymbolPair<Integer JavaDoc>(
123                 this.symbol1Int, this.symbol2Int);
124
125         // Case with null object.
126
assertFalse("A symbol pair and a null Object should not be equals.",
127                 symbolPairInt.equals(null));
128
129         // Case with non symbol pair object.
130
assertFalse(
131                 "A symbol pair and a non symbol pair object should not be equals.",
132                 symbolPairInt.equals(0));
133
134         SymbolPair<Integer JavaDoc> symbolPairInt2 = new SymbolPair<Integer JavaDoc>(
135                 this.symbol1Int, this.symbol2Int);
136
137         // Cases with null symbols in a pair.
138
symbolPairInt = new SymbolPair<Integer JavaDoc>(null, this.symbol1Int);
139         assertFalse(
140                 "A symbol pair with a null symbol and a non null object of type symbol pair should not be equals.",
141                 symbolPairInt.equals(symbolPairInt2));
142
143         symbolPairInt = new SymbolPair<Integer JavaDoc>(this.symbol1Int, null);
144         assertFalse(
145                 "A symbol pair with a null symbol and a non null object of type symbol pair should not be equals.",
146                 symbolPairInt.equals(symbolPairInt2));
147
148         // Case with different symbol pairs.
149
symbolPairInt = new SymbolPair<Integer JavaDoc>(this.symbol1Int,
150                 this.symbol2Int);
151         symbolPairInt2 = new SymbolPair<Integer JavaDoc>(this.symbol2Int,
152                 this.symbol1Int);
153         assertFalse("Two different symbol pairs should not be equals,",
154                 symbolPairInt.equals(symbolPairInt2));
155
156         // Case with same symbol pairs.
157
symbolPairInt2 = new SymbolPair<Integer JavaDoc>(this.symbol1Int,
158                 this.symbol2Int);
159         assertTrue("Two identical symbol pairs should be equals.",
160                 symbolPairInt.equals(symbolPairInt2));
161
162         // ========================
163
// Testing with BigInteger.
164
SymbolPair<BigInteger JavaDoc> symbolPairBigInt = new SymbolPair<BigInteger JavaDoc>(
165                 this.symbol1BigInt, this.symbol2BigInt);
166
167         // Case with null object.
168
assertFalse("A symbol pair and a null Object should not be equals.",
169                 symbolPairBigInt.equals(null));
170
171         // Case with non symbol pair object.
172
assertFalse(
173                 "A symbol pair and a non symbol pair object should not be equals.",
174                 symbolPairBigInt.equals(0));
175
176         SymbolPair<BigInteger JavaDoc> symbolPairBigInt2 = new SymbolPair<BigInteger JavaDoc>(
177                 this.symbol1BigInt, this.symbol2BigInt);
178
179         // Cases with null symbols in a pair.
180
symbolPairBigInt = new SymbolPair<BigInteger JavaDoc>(null, this.symbol1BigInt);
181         assertFalse(
182                 "A symbol pair with a null symbol and a non null object of type symbol pair should not be equals.",
183                 symbolPairBigInt.equals(symbolPairBigInt2));
184
185         symbolPairBigInt = new SymbolPair<BigInteger JavaDoc>(this.symbol1BigInt, null);
186         assertFalse(
187                 "A symbol pair with a null symbol and a non null object of type symbol pair should not be equals.",
188                 symbolPairBigInt.equals(symbolPairBigInt2));
189
190         // Case with different symbol pairs.
191
symbolPairBigInt = new SymbolPair<BigInteger JavaDoc>(this.symbol1BigInt,
192                 this.symbol2BigInt);
193         symbolPairBigInt2 = new SymbolPair<BigInteger JavaDoc>(this.symbol2BigInt,
194                 this.symbol1BigInt);
195         assertFalse("Two different symbol pairs should not be equals,",
196                 symbolPairBigInt.equals(symbolPairBigInt2));
197
198         // Case with same symbol pairs.
199
symbolPairBigInt2 = new SymbolPair<BigInteger JavaDoc>(this.symbol1BigInt,
200                 this.symbol2BigInt);
201         assertTrue("Two identical symbol pairs should be equals.",
202                 symbolPairBigInt.equals(symbolPairBigInt2));
203
204     }
205
206 }
207
Popular Tags