| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009, 2010 Ericsson |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which |
| 6 | * accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Francois Chouinard - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.tmf.tests.event; |
| 14 | |
| 15 | import org.eclipse.linuxtools.tmf.event.TmfTimeRange; |
| 16 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; |
| 17 | |
| 18 | import junit.framework.TestCase; |
| 19 | |
| 20 | /** |
| 21 | * <b><u>TmfTimeRangeTest</u></b> |
| 22 | * <p> |
| 23 | * Test suite for the TmfTimeRange class. |
| 24 | */ |
| 25 | @SuppressWarnings("nls") |
| 26 | public class TmfTimeRangeTest extends TestCase { |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | // Housekeeping |
| 30 | // ------------------------------------------------------------------------ |
| 31 | |
| 32 | public TmfTimeRangeTest(String name) { |
| 33 | super(name); |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | protected void setUp() throws Exception { |
| 38 | super.setUp(); |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | protected void tearDown() throws Exception { |
| 43 | super.tearDown(); |
| 44 | } |
| 45 | |
| 46 | // ------------------------------------------------------------------------ |
| 47 | // Constructors |
| 48 | // ------------------------------------------------------------------------ |
| 49 | |
| 50 | public void testConstructor() throws Exception { |
| 51 | TmfTimestamp ts1 = new TmfTimestamp(12345); |
| 52 | TmfTimestamp ts2 = new TmfTimestamp(12350); |
| 53 | TmfTimeRange range = new TmfTimeRange(ts1, ts2); |
| 54 | assertEquals("startTime", ts1, range.getStartTime()); |
| 55 | assertEquals("endTime", ts2, range.getEndTime()); |
| 56 | } |
| 57 | |
| 58 | public void testBadConstructor() throws Exception { |
| 59 | try { |
| 60 | new TmfTimeRange(TmfTimestamp.BigBang, null); |
| 61 | fail("null copy"); |
| 62 | } |
| 63 | catch (IllegalArgumentException e) { |
| 64 | // Success |
| 65 | } |
| 66 | |
| 67 | try { |
| 68 | new TmfTimeRange(null, TmfTimestamp.BigCrunch); |
| 69 | fail("null copy"); |
| 70 | } |
| 71 | catch (IllegalArgumentException e) { |
| 72 | // Success |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public void testOpenRange1() throws Exception { |
| 77 | TmfTimestamp ts2 = new TmfTimestamp(12350); |
| 78 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, ts2); |
| 79 | assertEquals("startTime", TmfTimestamp.BigBang, range.getStartTime()); |
| 80 | assertEquals("endTime", ts2, range.getEndTime()); |
| 81 | } |
| 82 | |
| 83 | public void testOpenRange2() throws Exception { |
| 84 | TmfTimestamp ts1 = new TmfTimestamp(12345); |
| 85 | TmfTimeRange range = new TmfTimeRange(ts1, TmfTimestamp.BigCrunch); |
| 86 | assertEquals("startTime", ts1, range.getStartTime()); |
| 87 | assertEquals("endTime", TmfTimestamp.BigCrunch, range.getEndTime()); |
| 88 | } |
| 89 | |
| 90 | public void testOpenRange3() throws Exception { |
| 91 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); |
| 92 | assertEquals("startTime", TmfTimestamp.BigBang, range.getStartTime()); |
| 93 | assertEquals("endTime", TmfTimestamp.BigCrunch, range.getEndTime()); |
| 94 | } |
| 95 | |
| 96 | public void testCopyConstructor() throws Exception { |
| 97 | TmfTimestamp ts1 = new TmfTimestamp(12345); |
| 98 | TmfTimestamp ts2 = new TmfTimestamp(12350); |
| 99 | TmfTimeRange range0 = new TmfTimeRange(ts1, ts2); |
| 100 | TmfTimeRange range1 = new TmfTimeRange(range0); |
| 101 | |
| 102 | assertEquals("startTime", ts1, range1.getStartTime()); |
| 103 | assertEquals("endTime", ts2, range1.getEndTime()); |
| 104 | |
| 105 | TmfTimeRange range2 = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); |
| 106 | TmfTimeRange range3 = new TmfTimeRange(range2); |
| 107 | assertEquals("startTime", TmfTimestamp.BigBang, range3.getStartTime()); |
| 108 | assertEquals("endTime", TmfTimestamp.BigCrunch, range3.getEndTime()); |
| 109 | } |
| 110 | |
| 111 | public void testCopyConstructor2() throws Exception { |
| 112 | try { |
| 113 | new TmfTimeRange(null); |
| 114 | fail("null copy"); |
| 115 | } |
| 116 | catch (IllegalArgumentException e) { |
| 117 | // Success |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // ------------------------------------------------------------------------ |
| 122 | // equals |
| 123 | // ------------------------------------------------------------------------ |
| 124 | |
| 125 | public void testEqualsReflexivity() throws Exception { |
| 126 | TmfTimestamp ts1 = new TmfTimestamp(12345); |
| 127 | TmfTimestamp ts2 = new TmfTimestamp(12350); |
| 128 | TmfTimeRange range1 = new TmfTimeRange(ts1, ts2); |
| 129 | TmfTimeRange range2 = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); |
| 130 | |
| 131 | assertTrue("equals", range1.equals(range1)); |
| 132 | assertTrue("equals", range2.equals(range2)); |
| 133 | |
| 134 | assertTrue("equals", !range1.equals(range2)); |
| 135 | assertTrue("equals", !range2.equals(range1)); |
| 136 | } |
| 137 | |
| 138 | public void testEqualsSymmetry() throws Exception { |
| 139 | TmfTimestamp ts1 = new TmfTimestamp(12345); |
| 140 | TmfTimestamp ts2 = new TmfTimestamp(12350); |
| 141 | TmfTimeRange range1a = new TmfTimeRange(ts1, ts2); |
| 142 | TmfTimeRange range1b = new TmfTimeRange(ts1, ts2); |
| 143 | |
| 144 | TmfTimeRange range2a = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); |
| 145 | TmfTimeRange range2b = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); |
| 146 | |
| 147 | assertTrue("equals", range1a.equals(range1b)); |
| 148 | assertTrue("equals", range1b.equals(range1a)); |
| 149 | |
| 150 | assertTrue("equals", range2a.equals(range2b)); |
| 151 | assertTrue("equals", range2b.equals(range2a)); |
| 152 | } |
| 153 | |
| 154 | public void testEqualsTransivity() throws Exception { |
| 155 | TmfTimestamp ts1 = new TmfTimestamp(12345); |
| 156 | TmfTimestamp ts2 = new TmfTimestamp(12350); |
| 157 | TmfTimeRange range1a = new TmfTimeRange(ts1, ts2); |
| 158 | TmfTimeRange range1b = new TmfTimeRange(ts1, ts2); |
| 159 | TmfTimeRange range1c = new TmfTimeRange(ts1, ts2); |
| 160 | |
| 161 | assertTrue("equals", range1a.equals(range1b)); |
| 162 | assertTrue("equals", range1b.equals(range1c)); |
| 163 | assertTrue("equals", range1a.equals(range1c)); |
| 164 | } |
| 165 | |
| 166 | public void testEqualsNull() throws Exception { |
| 167 | TmfTimestamp ts1 = new TmfTimestamp(12345); |
| 168 | TmfTimestamp ts2 = new TmfTimestamp(12350); |
| 169 | TmfTimeRange range1 = new TmfTimeRange(ts1, ts2); |
| 170 | |
| 171 | assertTrue("equals", !range1.equals(null)); |
| 172 | } |
| 173 | |
| 174 | // ------------------------------------------------------------------------ |
| 175 | // hashCode |
| 176 | // ------------------------------------------------------------------------ |
| 177 | |
| 178 | public void testHashCode() throws Exception { |
| 179 | TmfTimestamp ts1 = new TmfTimestamp(12345); |
| 180 | TmfTimestamp ts2 = new TmfTimestamp(12350); |
| 181 | TmfTimeRange range1 = new TmfTimeRange(ts1, ts2); |
| 182 | TmfTimeRange range1b = new TmfTimeRange(range1); |
| 183 | TmfTimeRange range2 = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); |
| 184 | TmfTimeRange range2b = new TmfTimeRange(range2); |
| 185 | |
| 186 | assertTrue("hashCode", range1.hashCode() == range1b.hashCode()); |
| 187 | assertTrue("hashCode", range2.hashCode() == range2b.hashCode()); |
| 188 | |
| 189 | assertTrue("hashCode", range1.hashCode() != range2.hashCode()); |
| 190 | } |
| 191 | |
| 192 | // ------------------------------------------------------------------------ |
| 193 | // toString |
| 194 | // ------------------------------------------------------------------------ |
| 195 | |
| 196 | public void testToString() throws Exception { |
| 197 | TmfTimestamp ts1 = new TmfTimestamp(12345); |
| 198 | TmfTimestamp ts2 = new TmfTimestamp(12350); |
| 199 | TmfTimeRange range = new TmfTimeRange(ts1, ts2); |
| 200 | |
| 201 | String expected = "[TmfTimeRange(" + ts1 + ":" + ts2 + ")]"; |
| 202 | assertEquals("toString", expected, range.toString()); |
| 203 | } |
| 204 | |
| 205 | // ------------------------------------------------------------------------ |
| 206 | // contains |
| 207 | // ------------------------------------------------------------------------ |
| 208 | |
| 209 | public void testContains() throws Exception { |
| 210 | TmfTimestamp ts1 = new TmfTimestamp(12345); |
| 211 | TmfTimestamp ts2 = new TmfTimestamp(12350); |
| 212 | TmfTimeRange range = new TmfTimeRange(ts1, ts2); |
| 213 | |
| 214 | assertTrue("contains (lower bound)", range.contains(new TmfTimestamp(12345))); |
| 215 | assertTrue("contains (higher bound)", range.contains(new TmfTimestamp(12350))); |
| 216 | assertTrue("contains (within bounds)", range.contains(new TmfTimestamp(12346))); |
| 217 | |
| 218 | assertFalse("contains (low value)", range.contains(new TmfTimestamp(12340))); |
| 219 | assertFalse("contains (high value)", range.contains(new TmfTimestamp(12351))); |
| 220 | } |
| 221 | |
| 222 | // ------------------------------------------------------------------------ |
| 223 | // getIntersection |
| 224 | // ------------------------------------------------------------------------ |
| 225 | |
| 226 | public void testGetIntersection() throws Exception { |
| 227 | |
| 228 | TmfTimestamp ts1a = new TmfTimestamp(1000); |
| 229 | TmfTimestamp ts1b = new TmfTimestamp(2000); |
| 230 | TmfTimeRange range1 = new TmfTimeRange(ts1a, ts1b); |
| 231 | |
| 232 | TmfTimestamp ts2a = new TmfTimestamp(2000); |
| 233 | TmfTimestamp ts2b = new TmfTimestamp(3000); |
| 234 | TmfTimeRange range2 = new TmfTimeRange(ts2a, ts2b); |
| 235 | |
| 236 | TmfTimestamp ts3a = new TmfTimestamp(3000); |
| 237 | TmfTimestamp ts3b = new TmfTimestamp(4000); |
| 238 | TmfTimeRange range3 = new TmfTimeRange(ts3a, ts3b); |
| 239 | |
| 240 | TmfTimestamp ts4a = new TmfTimestamp(1500); |
| 241 | TmfTimestamp ts4b = new TmfTimestamp(2500); |
| 242 | TmfTimeRange range4 = new TmfTimeRange(ts4a, ts4b); |
| 243 | |
| 244 | TmfTimestamp ts5a = new TmfTimestamp(1500); |
| 245 | TmfTimestamp ts5b = new TmfTimestamp(2000); |
| 246 | TmfTimeRange range5 = new TmfTimeRange(ts5a, ts5b); |
| 247 | |
| 248 | TmfTimestamp ts6a = new TmfTimestamp(2000); |
| 249 | TmfTimestamp ts6b = new TmfTimestamp(2500); |
| 250 | TmfTimeRange range6 = new TmfTimeRange(ts6a, ts6b); |
| 251 | |
| 252 | TmfTimestamp ts7a = new TmfTimestamp(1500); |
| 253 | TmfTimestamp ts7b = new TmfTimestamp(3500); |
| 254 | TmfTimeRange range7 = new TmfTimeRange(ts7a, ts7b); |
| 255 | |
| 256 | TmfTimestamp ts8a = new TmfTimestamp(2250); |
| 257 | TmfTimestamp ts8b = new TmfTimestamp(2750); |
| 258 | TmfTimeRange range8 = new TmfTimeRange(ts8a, ts8b); |
| 259 | |
| 260 | assertEquals("getIntersection (below - not contiguous)", null, range1.getIntersection(range3)); |
| 261 | assertEquals("getIntersection (above - not contiguous)", null, range3.getIntersection(range1)); |
| 262 | |
| 263 | assertEquals("getIntersection (below - contiguous)", new TmfTimeRange(ts1b, ts1b), range1.getIntersection(range2)); |
| 264 | assertEquals("getIntersection (above - contiguous)", new TmfTimeRange(ts3a, ts3a), range3.getIntersection(range2)); |
| 265 | |
| 266 | assertEquals("getIntersection (below - overlap)", new TmfTimeRange(ts2a, ts4b), range2.getIntersection(range4)); |
| 267 | assertEquals("getIntersection (above - overlap)", new TmfTimeRange(ts2a, ts4b), range4.getIntersection(range2)); |
| 268 | |
| 269 | assertEquals("getIntersection (within - overlap1)", range6, range2.getIntersection(range6)); |
| 270 | assertEquals("getIntersection (within - overlap2)", range6, range6.getIntersection(range2)); |
| 271 | |
| 272 | assertEquals("getIntersection (within - overlap3)", range5, range1.getIntersection(range5)); |
| 273 | assertEquals("getIntersection (within - overlap4)", range5, range5.getIntersection(range1)); |
| 274 | |
| 275 | assertEquals("getIntersection (within - overlap5)", range8, range2.getIntersection(range8)); |
| 276 | assertEquals("getIntersection (within - overlap6)", range8, range8.getIntersection(range2)); |
| 277 | |
| 278 | assertEquals("getIntersection (accross1)", range2, range2.getIntersection(range7)); |
| 279 | assertEquals("getIntersection (accross2)", range2, range7.getIntersection(range2)); |
| 280 | } |
| 281 | |
| 282 | } |