Commit | Line | Data |
---|---|---|
d18dd09b | 1 | /******************************************************************************* |
cbd4ad82 | 2 | * Copyright (c) 2009, 2010 Ericsson |
d18dd09b ASL |
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> | |
cbd4ad82 | 23 | * Test suite for the TmfTimeRange class. |
d18dd09b ASL |
24 | */ |
25 | public class TmfTimeRangeTest extends TestCase { | |
26 | ||
cbd4ad82 FC |
27 | |
28 | // ------------------------------------------------------------------------ | |
d18dd09b | 29 | // Housekeeping |
cbd4ad82 | 30 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
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 | ||
cbd4ad82 | 46 | // ------------------------------------------------------------------------ |
d18dd09b | 47 | // Constructors |
cbd4ad82 | 48 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
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()); | |
cbd4ad82 FC |
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 | } | |
d18dd09b ASL |
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()); | |
cbd4ad82 | 80 | assertEquals("endTime", ts2, range.getEndTime()); |
d18dd09b ASL |
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()); | |
cbd4ad82 | 87 | assertEquals("endTime", TmfTimestamp.BigCrunch, range.getEndTime()); |
d18dd09b ASL |
88 | } |
89 | ||
90 | public void testOpenRange3() throws Exception { | |
91 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); | |
cbd4ad82 FC |
92 | assertEquals("startTime", TmfTimestamp.BigBang, range.getStartTime()); |
93 | assertEquals("endTime", TmfTimestamp.BigCrunch, range.getEndTime()); | |
d18dd09b ASL |
94 | } |
95 | ||
cbd4ad82 FC |
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); | |
d18dd09b | 101 | |
cbd4ad82 FC |
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 { | |
d18dd09b ASL |
126 | TmfTimestamp ts1 = new TmfTimestamp(12345); |
127 | TmfTimestamp ts2 = new TmfTimestamp(12350); | |
cbd4ad82 FC |
128 | TmfTimeRange range1 = new TmfTimeRange(ts1, ts2); |
129 | TmfTimeRange range2 = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); | |
d18dd09b | 130 | |
cbd4ad82 FC |
131 | assertTrue("equals", range1.equals(range1)); |
132 | assertTrue("equals", range2.equals(range2)); | |
d18dd09b | 133 | |
cbd4ad82 FC |
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)); | |
d18dd09b | 164 | } |
cbd4ad82 | 165 | |
cbd4ad82 FC |
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 | ||
2fb2eb37 FC |
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 | ||
cbd4ad82 | 192 | // ------------------------------------------------------------------------ |
d18dd09b | 193 | // toString |
cbd4ad82 | 194 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
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 | ||
cbd4ad82 | 201 | String expected = "[TmfTimeRange(" + ts1 + ":" + ts2 + ")]"; |
d18dd09b ASL |
202 | assertEquals("toString", expected, range.toString()); |
203 | } | |
204 | ||
cbd4ad82 FC |
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 | ||
d18dd09b | 222 | } |