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
.jdt
.annotation
.NonNull
;
24 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.synchronization
.TmfTimestampTransform
;
25 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.synchronization
.TmfTimestampTransformLinear
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.ITmfTimestampTransform
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
29 import org
.junit
.Test
;
32 * Tests for {@link TmfTimestampTransform} and its descendants
34 * @author Geneviève Bastien
36 @SuppressWarnings("nls")
37 public class TsTransformTest
{
39 private static final long ts
= 1361657893526374091L;
40 private static final @NonNull ITmfTimestamp oTs
= TmfTimestamp
.fromSeconds(ts
);
43 * Test the linear transform
46 public void testLinearTransform() {
47 /* Default constructor */
48 TmfTimestampTransformLinear ttl
= new TmfTimestampTransformLinear();
49 assertEquals(1361657893526374091L, ttl
.transform(ts
));
50 assertEquals(1361657893526374091L, ttl
.transform(oTs
).getValue());
53 ttl
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(1.0), BigDecimal
.valueOf(3));
54 assertEquals(1361657893526374094L, ttl
.transform(ts
));
55 assertEquals(1361657893526374094L, ttl
.transform(oTs
).getValue());
58 ttl
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(2.0), BigDecimal
.valueOf(0));
59 assertEquals(2723315787052748182L, ttl
.transform(ts
));
60 assertEquals(2723315787052748182L, ttl
.transform(oTs
).getValue());
62 /* Offset and slope */
63 ttl
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(2.0), BigDecimal
.valueOf(3));
64 assertEquals(2723315787052748185L, ttl
.transform(ts
));
65 assertEquals(2723315787052748185L, ttl
.transform(oTs
).getValue());
67 /* Offset and slope */
68 ttl
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(0.5), BigDecimal
.valueOf(0));
69 assertEquals(680828946763187045L, ttl
.transform(ts
));
70 assertEquals(680828946763187045L, ttl
.transform(oTs
).getValue());
74 * Test for the identity transform
77 public void testIdentityTransform() {
78 ITmfTimestampTransform tt
= TmfTimestampTransform
.IDENTITY
;
79 assertEquals(ts
, tt
.transform(ts
));
80 assertEquals(oTs
, tt
.transform(oTs
));
84 * Test hash and equals function
87 public void testEquality() {
88 Map
<ITmfTimestampTransform
, String
> map
= new HashMap
<>();
89 ITmfTimestampTransform ttl
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(2.0), BigDecimal
.valueOf(3));
90 ITmfTimestampTransform ttl2
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(2.0), BigDecimal
.valueOf(3));
91 ITmfTimestampTransform ttl3
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(3), BigDecimal
.valueOf(3));
92 assertEquals(ttl
, ttl2
);
93 assertFalse(ttl
.equals(ttl3
));
94 assertFalse(ttl2
.equals(ttl3
));
97 assertTrue(map
.containsKey(ttl2
));
98 assertEquals("a", map
.get(ttl
));
100 ITmfTimestampTransform ti
= TmfTimestampTransform
.IDENTITY
;
101 assertEquals(TmfTimestampTransform
.IDENTITY
, ti
);
102 assertFalse(TmfTimestampTransform
.IDENTITY
.equals(ttl
));
105 assertTrue(map
.containsKey(TmfTimestampTransform
.IDENTITY
));
106 assertEquals("b", map
.get(ti
));
108 assertFalse(ti
.equals(ttl
));
109 assertFalse(ttl
.equals(ti
));
114 * Test the transform composition function
117 public void testComposition() {
119 ITmfTimestampTransform ti
= TmfTimestampTransform
.IDENTITY
;
120 ITmfTimestampTransform ttl
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(2.0), BigDecimal
.valueOf(3));
121 ITmfTimestampTransform ttl2
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(1.5), BigDecimal
.valueOf(8));
123 ITmfTimestampTransform tc1
= ti
.composeWith(ttl
);
125 assertEquals(ttl
, tc1
);
126 assertEquals(203, tc1
.transform(t
));
128 tc1
= ttl
.composeWith(ti
);
129 /* Should be ttl also */
130 assertEquals(ttl
, tc1
);
131 assertEquals(203, tc1
.transform(t
));
133 tc1
= ti
.composeWith(ti
);
134 /* Should be identity */
135 assertEquals(tc1
, TmfTimestampTransform
.IDENTITY
);
136 assertEquals(100, tc1
.transform(t
));
138 tc1
= ttl
.composeWith(ttl2
);
139 assertEquals(ttl
.transform(ttl2
.transform(t
)), tc1
.transform(t
));
140 assertEquals(319, tc1
.transform(t
));
142 tc1
= ttl2
.composeWith(ttl
);
143 assertEquals(ttl2
.transform(ttl
.transform(t
)), tc1
.transform(t
));
144 assertEquals(312, tc1
.transform(t
));