tmf: Add equals/hashCode to TmfConstantTransform
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / synchronization / TsTransformTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * 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 * Geneviève Bastien - Initial implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.core.tests.synchronization;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertFalse;
17 import static org.junit.Assert.assertNotEquals;
18 import static org.junit.Assert.assertTrue;
19
20 import java.math.BigDecimal;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.eclipse.tracecompass.internal.tmf.core.synchronization.TmfConstantTransform;
26 import org.eclipse.tracecompass.internal.tmf.core.synchronization.TmfTimestampTransform;
27 import org.eclipse.tracecompass.internal.tmf.core.synchronization.TmfTimestampTransformLinear;
28 import org.eclipse.tracecompass.tmf.core.synchronization.ITmfTimestampTransform;
29 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
30 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
31 import org.junit.Test;
32
33 /**
34 * Tests for {@link TmfTimestampTransform} and its descendants
35 *
36 * @author Geneviève Bastien
37 */
38 @SuppressWarnings("nls")
39 public class TsTransformTest {
40
41 private static final long ts = 1361657893526374091L;
42 private static final @NonNull ITmfTimestamp oTs = TmfTimestamp.fromSeconds(ts);
43
44 /**
45 * Test the linear transform
46 */
47 @Test
48 public void testLinearTransform() {
49 /* Default constructor */
50 TmfTimestampTransformLinear ttl = new TmfTimestampTransformLinear();
51 assertEquals(1361657893526374091L, ttl.transform(ts));
52 assertEquals(1361657893526374091L, ttl.transform(oTs).getValue());
53
54 /* Just an offset */
55 ttl = new TmfTimestampTransformLinear(BigDecimal.valueOf(1.0), BigDecimal.valueOf(3));
56 assertEquals(1361657893526374094L, ttl.transform(ts));
57 assertEquals(1361657893526374094L, ttl.transform(oTs).getValue());
58
59 /* Just a slope */
60 ttl = new TmfTimestampTransformLinear(BigDecimal.valueOf(2.0), BigDecimal.valueOf(0));
61 assertEquals(2723315787052748182L, ttl.transform(ts));
62 assertEquals(2723315787052748182L, ttl.transform(oTs).getValue());
63
64 /* Offset and slope */
65 ttl = new TmfTimestampTransformLinear(BigDecimal.valueOf(2.0), BigDecimal.valueOf(3));
66 assertEquals(2723315787052748185L, ttl.transform(ts));
67 assertEquals(2723315787052748185L, ttl.transform(oTs).getValue());
68
69 /* Offset and slope */
70 ttl = new TmfTimestampTransformLinear(BigDecimal.valueOf(0.5), BigDecimal.valueOf(0));
71 assertEquals(680828946763187045L, ttl.transform(ts));
72 assertEquals(680828946763187045L, ttl.transform(oTs).getValue());
73 }
74
75 /**
76 * Test for the identity transform
77 */
78 @Test
79 public void testIdentityTransform() {
80 ITmfTimestampTransform tt = TmfTimestampTransform.IDENTITY;
81 assertEquals(ts, tt.transform(ts));
82 assertEquals(oTs, tt.transform(oTs));
83 }
84
85 /**
86 * Test hash and equals function
87 */
88 @Test
89 public void testEquality() {
90 Map<ITmfTimestampTransform, String> map = new HashMap<>();
91 ITmfTimestampTransform ttl = new TmfTimestampTransformLinear(BigDecimal.valueOf(2.0), BigDecimal.valueOf(3));
92 ITmfTimestampTransform ttl2 = new TmfTimestampTransformLinear(BigDecimal.valueOf(2.0), BigDecimal.valueOf(3));
93 ITmfTimestampTransform ttl3 = new TmfTimestampTransformLinear(BigDecimal.valueOf(3), BigDecimal.valueOf(3));
94 assertEquals(ttl, ttl2);
95 assertFalse(ttl.equals(ttl3));
96 assertFalse(ttl2.equals(ttl3));
97
98 map.put(ttl, "a");
99 assertTrue(map.containsKey(ttl2));
100 assertEquals("a", map.get(ttl));
101
102 ITmfTimestampTransform ti = TmfTimestampTransform.IDENTITY;
103 assertEquals(TmfTimestampTransform.IDENTITY, ti);
104 assertFalse(TmfTimestampTransform.IDENTITY.equals(ttl));
105
106 map.put(ti, "b");
107 assertTrue(map.containsKey(TmfTimestampTransform.IDENTITY));
108 assertEquals("b", map.get(ti));
109
110 assertFalse(ti.equals(ttl));
111 assertFalse(ttl.equals(ti));
112 }
113
114 /**
115 * Test the equality of {@link TmfConstantTransform} objects.
116 */
117 @Test
118 public void testEqualityConstantTransform() {
119 ITmfTimestampTransform tt1 = new TmfConstantTransform(50L);
120 ITmfTimestampTransform tt2 = new TmfConstantTransform(50L);
121 ITmfTimestampTransform tt3 = new TmfConstantTransform(-10L);
122
123 assertEquals(tt1, tt2);
124 assertNotEquals(tt1, tt3);
125 assertNotEquals(tt2, tt3);
126 }
127
128 /**
129 * Test the transform composition function
130 */
131 @Test
132 public void testComposition() {
133 long t = 100;
134 ITmfTimestampTransform ti = TmfTimestampTransform.IDENTITY;
135 ITmfTimestampTransform ttl = new TmfTimestampTransformLinear(BigDecimal.valueOf(2.0), BigDecimal.valueOf(3));
136 ITmfTimestampTransform ttl2 = new TmfTimestampTransformLinear(BigDecimal.valueOf(1.5), BigDecimal.valueOf(8));
137
138 ITmfTimestampTransform tc1 = ti.composeWith(ttl);
139 /* Should be ttl */
140 assertEquals(ttl, tc1);
141 assertEquals(203, tc1.transform(t));
142
143 tc1 = ttl.composeWith(ti);
144 /* Should be ttl also */
145 assertEquals(ttl, tc1);
146 assertEquals(203, tc1.transform(t));
147
148 tc1 = ti.composeWith(ti);
149 /* Should be identity */
150 assertEquals(tc1, TmfTimestampTransform.IDENTITY);
151 assertEquals(100, tc1.transform(t));
152
153 tc1 = ttl.composeWith(ttl2);
154 assertEquals(ttl.transform(ttl2.transform(t)), tc1.transform(t));
155 assertEquals(319, tc1.transform(t));
156
157 tc1 = ttl2.composeWith(ttl);
158 assertEquals(ttl2.transform(ttl.transform(t)), tc1.transform(t));
159 assertEquals(312, tc1.transform(t));
160
161 }
162
163 }
This page took 0.034787 seconds and 5 git commands to generate.