1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
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
10 * Geneviève Bastien - Initial implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.tests
.synchronization
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertFalse
;
17 import static org
.junit
.Assert
.assertTrue
;
19 import java
.math
.BigDecimal
;
20 import java
.util
.HashMap
;
23 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.synchronization
.TmfTimestampTransform
;
24 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.synchronization
.TmfTimestampTransformLinear
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.ITmfTimestampTransform
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
28 import org
.junit
.Test
;
31 * Tests for {@link TmfTimestampTransform} and its descendants
33 * @author Geneviève Bastien
35 @SuppressWarnings("nls")
36 public class TsTransformTest
{
38 private static final long ts
= 1361657893526374091L;
39 private static final ITmfTimestamp oTs
= new TmfTimestamp(ts
);
42 * Test the linear transform
45 public void testLinearTransform() {
46 /* Default constructor */
47 TmfTimestampTransformLinear ttl
= new TmfTimestampTransformLinear();
48 assertEquals(1361657893526374091L, ttl
.transform(ts
));
49 assertEquals(1361657893526374091L, ttl
.transform(oTs
).getValue());
52 ttl
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(1.0), BigDecimal
.valueOf(3));
53 assertEquals(1361657893526374094L, ttl
.transform(ts
));
54 assertEquals(1361657893526374094L, ttl
.transform(oTs
).getValue());
57 ttl
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(2.0), BigDecimal
.valueOf(0));
58 assertEquals(2723315787052748182L, ttl
.transform(ts
));
59 assertEquals(2723315787052748182L, ttl
.transform(oTs
).getValue());
61 /* Offset and slope */
62 ttl
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(2.0), BigDecimal
.valueOf(3));
63 assertEquals(2723315787052748185L, ttl
.transform(ts
));
64 assertEquals(2723315787052748185L, ttl
.transform(oTs
).getValue());
66 /* Offset and slope */
67 ttl
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(0.5), BigDecimal
.valueOf(0));
68 assertEquals(680828946763187045L, ttl
.transform(ts
));
69 assertEquals(680828946763187045L, ttl
.transform(oTs
).getValue());
73 * Test for the identity transform
76 public void testIdentityTransform() {
77 ITmfTimestampTransform tt
= TmfTimestampTransform
.IDENTITY
;
78 assertEquals(ts
, tt
.transform(ts
));
79 assertEquals(oTs
, tt
.transform(oTs
));
83 * Test hash and equals function
86 public void testEquality() {
87 Map
<ITmfTimestampTransform
, String
> map
= new HashMap
<>();
88 ITmfTimestampTransform ttl
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(2.0), BigDecimal
.valueOf(3));
89 ITmfTimestampTransform ttl2
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(2.0), BigDecimal
.valueOf(3));
90 ITmfTimestampTransform ttl3
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(3), BigDecimal
.valueOf(3));
91 assertEquals(ttl
, ttl2
);
92 assertFalse(ttl
.equals(ttl3
));
93 assertFalse(ttl2
.equals(ttl3
));
96 assertTrue(map
.containsKey(ttl2
));
97 assertEquals("a", map
.get(ttl
));
99 ITmfTimestampTransform ti
= TmfTimestampTransform
.IDENTITY
;
100 assertEquals(TmfTimestampTransform
.IDENTITY
, ti
);
101 assertFalse(TmfTimestampTransform
.IDENTITY
.equals(ttl
));
104 assertTrue(map
.containsKey(TmfTimestampTransform
.IDENTITY
));
105 assertEquals("b", map
.get(ti
));
107 assertFalse(ti
.equals(ttl
));
108 assertFalse(ttl
.equals(ti
));
113 * Test the transform composition function
116 public void testComposition() {
118 ITmfTimestampTransform ti
= TmfTimestampTransform
.IDENTITY
;
119 ITmfTimestampTransform ttl
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(2.0), BigDecimal
.valueOf(3));
120 ITmfTimestampTransform ttl2
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(1.5), BigDecimal
.valueOf(8));
122 ITmfTimestampTransform tc1
= ti
.composeWith(ttl
);
124 assertEquals(ttl
, tc1
);
125 assertEquals(203, tc1
.transform(t
));
127 tc1
= ttl
.composeWith(ti
);
128 /* Should be ttl also */
129 assertEquals(ttl
, tc1
);
130 assertEquals(203, tc1
.transform(t
));
132 tc1
= ti
.composeWith(ti
);
133 /* Should be identity */
134 assertEquals(tc1
, TmfTimestampTransform
.IDENTITY
);
135 assertEquals(100, tc1
.transform(t
));
137 tc1
= ttl
.composeWith(ttl2
);
138 assertEquals(ttl
.transform(ttl2
.transform(t
)), tc1
.transform(t
));
139 assertEquals(319, tc1
.transform(t
));
141 tc1
= ttl2
.composeWith(ttl
);
142 assertEquals(ttl2
.transform(ttl
.transform(t
)), tc1
.transform(t
));
143 assertEquals(312, tc1
.transform(t
));