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
.assertNotEquals
;
18 import static org
.junit
.Assert
.assertTrue
;
20 import java
.math
.BigDecimal
;
21 import java
.util
.HashMap
;
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
;
34 * Tests for {@link TmfTimestampTransform} and its descendants
36 * @author Geneviève Bastien
38 @SuppressWarnings("nls")
39 public class TsTransformTest
{
41 private static final long ts
= 1361657893526374091L;
42 private static final @NonNull ITmfTimestamp oTs
= TmfTimestamp
.fromSeconds(ts
);
45 * Test the linear transform
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());
55 ttl
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(1.0), BigDecimal
.valueOf(3));
56 assertEquals(1361657893526374094L, ttl
.transform(ts
));
57 assertEquals(1361657893526374094L, ttl
.transform(oTs
).getValue());
60 ttl
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(2.0), BigDecimal
.valueOf(0));
61 assertEquals(2723315787052748182L, ttl
.transform(ts
));
62 assertEquals(2723315787052748182L, ttl
.transform(oTs
).getValue());
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());
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());
76 * Test for the identity transform
79 public void testIdentityTransform() {
80 ITmfTimestampTransform tt
= TmfTimestampTransform
.IDENTITY
;
81 assertEquals(ts
, tt
.transform(ts
));
82 assertEquals(oTs
, tt
.transform(oTs
));
86 * Test hash and equals function
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
));
99 assertTrue(map
.containsKey(ttl2
));
100 assertEquals("a", map
.get(ttl
));
102 ITmfTimestampTransform ti
= TmfTimestampTransform
.IDENTITY
;
103 assertEquals(TmfTimestampTransform
.IDENTITY
, ti
);
104 assertFalse(TmfTimestampTransform
.IDENTITY
.equals(ttl
));
107 assertTrue(map
.containsKey(TmfTimestampTransform
.IDENTITY
));
108 assertEquals("b", map
.get(ti
));
110 assertFalse(ti
.equals(ttl
));
111 assertFalse(ttl
.equals(ti
));
115 * Test the equality of {@link TmfConstantTransform} objects.
118 public void testEqualityConstantTransform() {
119 ITmfTimestampTransform tt1
= new TmfConstantTransform(50L);
120 ITmfTimestampTransform tt2
= new TmfConstantTransform(50L);
121 ITmfTimestampTransform tt3
= new TmfConstantTransform(-10L);
123 assertEquals(tt1
, tt2
);
124 assertNotEquals(tt1
, tt3
);
125 assertNotEquals(tt2
, tt3
);
129 * Test the transform composition function
132 public void testComposition() {
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));
138 ITmfTimestampTransform tc1
= ti
.composeWith(ttl
);
140 assertEquals(ttl
, tc1
);
141 assertEquals(203, tc1
.transform(t
));
143 tc1
= ttl
.composeWith(ti
);
144 /* Should be ttl also */
145 assertEquals(ttl
, tc1
);
146 assertEquals(203, tc1
.transform(t
));
148 tc1
= ti
.composeWith(ti
);
149 /* Should be identity */
150 assertEquals(tc1
, TmfTimestampTransform
.IDENTITY
);
151 assertEquals(100, tc1
.transform(t
));
153 tc1
= ttl
.composeWith(ttl2
);
154 assertEquals(ttl
.transform(ttl2
.transform(t
)), tc1
.transform(t
));
155 assertEquals(319, tc1
.transform(t
));
157 tc1
= ttl2
.composeWith(ttl
);
158 assertEquals(ttl2
.transform(ttl
.transform(t
)), tc1
.transform(t
));
159 assertEquals(312, tc1
.transform(t
));