001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestPair.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.datastructures;
009:
010: import junit.framework.TestCase;
011:
012: public class TestPair extends TestCase {
013: public TestPair(String name) {
014: super (name);
015: }
016:
017: public void testInstantiation() {
018: Pair<String, Long> pair = new Pair<String, Long>("first",
019: new Long(1434));
020: assertNotNull(pair);
021: assertEquals(pair.getFirst(), "first");
022: assertEquals(pair.getSecond(), new Long(1434));
023: }
024:
025: public void testAccessors() {
026: Pair<String, Long> pair = new Pair<String, Long>("first",
027: new Long(1434));
028: assertEquals(pair.getFirst(), "first");
029: assertEquals(pair.getSecond(), new Long(1434));
030: pair.setFirst("first2");
031: pair.setSecond(new Long(3433));
032: assertEquals(pair.getFirst(), "first2");
033: assertEquals(pair.getSecond(), new Long(3433));
034: }
035:
036: public void testEquals() {
037: Pair<String, Long> pair1 = new Pair<String, Long>("first1",
038: new Long(1434));
039: Pair<String, Long> pair2 = new Pair<String, Long>("first1",
040: new Long(1434));
041: Pair<String, Long> pair3 = new Pair<String, Long>("first2",
042: new Long(83488));
043: Pair<String, Long> pair4 = new Pair<String, Long>("first1",
044: null);
045: Pair<String, Long> pair5 = new Pair<String, Long>("first2",
046: null);
047: Pair<String, Long> pair6 = new Pair<String, Long>("first2",
048: null);
049: Pair<String, Long> pair7 = new Pair<String, Long>(null,
050: new Long(1434));
051: Pair<String, Long> pair8 = new Pair<String, Long>(null,
052: new Long(83488));
053: Pair<String, Long> pair9 = new Pair<String, Long>(null,
054: new Long(83488));
055: Pair<String, Long> pair10 = new Pair<String, Long>(null, null);
056: Pair<String, Long> pair11 = new Pair<String, Long>(null, null);
057:
058: Pair<String, Integer> pair12 = new Pair<String, Integer>(
059: "first1", new Integer(1434));
060: Pair<Long, Integer> pair13 = new Pair<Long, Integer>(new Long(
061: 1434), new Integer(1434));
062: Pair<Long, String> pair14 = new Pair<Long, String>(new Long(
063: 1434), "second1");
064:
065: assertFalse(pair1.equals("first1"));
066: assertFalse(pair1.equals(new Long(1434)));
067:
068: assertFalse(pair1.equals(null));
069:
070: assertTrue(pair1.equals(pair1));
071: assertTrue(pair1.equals(pair2));
072: assertFalse(pair1.equals(pair3));
073: assertFalse(pair1.equals(pair4));
074: assertFalse(pair1.equals(pair5));
075: assertFalse(pair1.equals(pair6));
076: assertFalse(pair1.equals(pair7));
077: assertFalse(pair1.equals(pair8));
078: assertFalse(pair1.equals(pair9));
079: assertFalse(pair1.equals(pair10));
080: assertFalse(pair1.equals(pair11));
081: assertFalse(pair1.equals(pair12));
082: assertFalse(pair1.equals(pair13));
083: assertFalse(pair1.equals(pair14));
084:
085: assertTrue(pair2.equals(pair2));
086: assertFalse(pair2.equals(pair3));
087: assertFalse(pair2.equals(pair4));
088: assertFalse(pair2.equals(pair5));
089: assertFalse(pair2.equals(pair6));
090: assertFalse(pair2.equals(pair7));
091: assertFalse(pair2.equals(pair8));
092: assertFalse(pair2.equals(pair9));
093: assertFalse(pair2.equals(pair10));
094: assertFalse(pair2.equals(pair11));
095: assertFalse(pair2.equals(pair12));
096: assertFalse(pair2.equals(pair13));
097: assertFalse(pair2.equals(pair14));
098:
099: assertTrue(pair3.equals(pair3));
100: assertFalse(pair3.equals(pair4));
101: assertFalse(pair3.equals(pair5));
102: assertFalse(pair3.equals(pair6));
103: assertFalse(pair3.equals(pair7));
104: assertFalse(pair3.equals(pair8));
105: assertFalse(pair3.equals(pair9));
106: assertFalse(pair3.equals(pair10));
107: assertFalse(pair3.equals(pair11));
108: assertFalse(pair3.equals(pair12));
109: assertFalse(pair3.equals(pair13));
110: assertFalse(pair3.equals(pair14));
111:
112: assertTrue(pair4.equals(pair4));
113: assertFalse(pair4.equals(pair5));
114: assertFalse(pair4.equals(pair6));
115: assertFalse(pair4.equals(pair7));
116: assertFalse(pair4.equals(pair8));
117: assertFalse(pair4.equals(pair9));
118: assertFalse(pair4.equals(pair10));
119: assertFalse(pair4.equals(pair11));
120: assertFalse(pair4.equals(pair12));
121: assertFalse(pair4.equals(pair13));
122: assertFalse(pair4.equals(pair14));
123:
124: assertTrue(pair5.equals(pair5));
125: assertTrue(pair5.equals(pair6));
126: assertFalse(pair5.equals(pair7));
127: assertFalse(pair5.equals(pair8));
128: assertFalse(pair5.equals(pair9));
129: assertFalse(pair5.equals(pair10));
130: assertFalse(pair5.equals(pair11));
131: assertFalse(pair5.equals(pair12));
132: assertFalse(pair5.equals(pair13));
133: assertFalse(pair5.equals(pair14));
134:
135: assertTrue(pair6.equals(pair6));
136: assertFalse(pair6.equals(pair7));
137: assertFalse(pair6.equals(pair8));
138: assertFalse(pair6.equals(pair9));
139: assertFalse(pair6.equals(pair10));
140: assertFalse(pair6.equals(pair11));
141: assertFalse(pair6.equals(pair12));
142: assertFalse(pair6.equals(pair13));
143: assertFalse(pair6.equals(pair14));
144:
145: assertTrue(pair7.equals(pair7));
146: assertFalse(pair7.equals(pair8));
147: assertFalse(pair7.equals(pair9));
148: assertFalse(pair7.equals(pair10));
149: assertFalse(pair7.equals(pair11));
150: assertFalse(pair7.equals(pair12));
151: assertFalse(pair7.equals(pair13));
152: assertFalse(pair7.equals(pair14));
153:
154: assertTrue(pair8.equals(pair8));
155: assertTrue(pair8.equals(pair9));
156: assertFalse(pair8.equals(pair10));
157: assertFalse(pair8.equals(pair11));
158: assertFalse(pair8.equals(pair12));
159: assertFalse(pair8.equals(pair13));
160: assertFalse(pair8.equals(pair14));
161:
162: assertTrue(pair9.equals(pair9));
163: assertFalse(pair9.equals(pair10));
164: assertFalse(pair9.equals(pair11));
165: assertFalse(pair9.equals(pair12));
166: assertFalse(pair9.equals(pair13));
167: assertFalse(pair9.equals(pair14));
168:
169: assertTrue(pair10.equals(pair10));
170: assertTrue(pair10.equals(pair11));
171: assertFalse(pair10.equals(pair12));
172: assertFalse(pair10.equals(pair13));
173: assertFalse(pair10.equals(pair14));
174:
175: assertTrue(pair11.equals(pair11));
176: assertFalse(pair11.equals(pair12));
177: assertFalse(pair11.equals(pair13));
178: assertFalse(pair11.equals(pair14));
179:
180: assertTrue(pair12.equals(pair12));
181: assertFalse(pair12.equals(pair13));
182: assertFalse(pair12.equals(pair14));
183:
184: assertTrue(pair13.equals(pair13));
185: assertFalse(pair13.equals(pair14));
186:
187: assertTrue(pair14.equals(pair14));
188: }
189:
190: public void testHashCode() {
191: Pair<String, Long> pair1 = new Pair<String, Long>("first",
192: new Long(53433));
193: Pair<String, Long> pair2 = new Pair<String, Long>("first",
194: new Long(53433));
195: Pair<String, Long> pair3 = new Pair<String, Long>("first2",
196: new Long(83488));
197: assertEquals(pair1.hashCode(), pair1.hashCode());
198: assertEquals(pair2.hashCode(), pair2.hashCode());
199: assertTrue(pair1.hashCode() == pair2.hashCode()
200: || pair1.equals(pair2));
201: assertTrue(pair1.hashCode() != pair3.hashCode()
202: || !pair1.equals(pair3));
203: assertTrue(pair2.hashCode() != pair3.hashCode()
204: || !pair2.equals(pair3));
205: }
206: }
|