1 /*******************************************************************************
2 * Copyright (c) 2013 É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
.linuxtools
.tmf
.core
.tests
.synchronization
;
15 import static org
.junit
.Assert
.*;
17 import java
.math
.BigDecimal
;
18 import java
.util
.HashMap
;
21 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.synchronization
.TmfTimestampTransform
;
22 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.synchronization
.TmfTimestampTransformLinear
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.synchronization
.ITmfTimestampTransform
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
26 import org
.junit
.Test
;
29 * Tests for {@link TmfTimestampTransform} and its descendants
31 * @author Geneviève Bastien
33 @SuppressWarnings("nls")
34 public class TsTransformTest
{
36 private long ts
= 1361657893526374091L;
37 private ITmfTimestamp oTs
= new TmfTimestamp(ts
);
40 * Test the linear transform
43 public void testLinearTransform() {
44 /* Default constructor */
45 TmfTimestampTransformLinear ttl
= new TmfTimestampTransformLinear();
46 assertEquals(1361657893526374091L, ttl
.transform(ts
));
47 assertEquals(1361657893526374091L, ttl
.transform(oTs
).getValue());
50 ttl
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(1.0), BigDecimal
.valueOf(3));
51 assertEquals(1361657893526374094L, ttl
.transform(ts
));
52 assertEquals(1361657893526374094L, ttl
.transform(oTs
).getValue());
55 ttl
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(2.0), BigDecimal
.valueOf(0));
56 assertEquals(2723315787052748182L, ttl
.transform(ts
));
57 assertEquals(2723315787052748182L, ttl
.transform(oTs
).getValue());
59 /* Offset and slope */
60 ttl
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(2.0), BigDecimal
.valueOf(3));
61 assertEquals(2723315787052748185L, ttl
.transform(ts
));
62 assertEquals(2723315787052748185L, ttl
.transform(oTs
).getValue());
64 /* Offset and slope */
65 ttl
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(0.5), BigDecimal
.valueOf(0));
66 assertEquals(680828946763187045L, ttl
.transform(ts
));
67 assertEquals(680828946763187045L, ttl
.transform(oTs
).getValue());
71 * Test for the identity transform
74 public void testIdentityTransform() {
75 ITmfTimestampTransform tt
= TmfTimestampTransform
.IDENTITY
;
76 assertEquals(ts
, tt
.transform(ts
));
77 assertEquals(oTs
, tt
.transform(oTs
));
81 * Test hash and equals function
84 public void testEquality() {
85 Map
<ITmfTimestampTransform
, String
> map
= new HashMap
<>();
86 ITmfTimestampTransform ttl
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(2.0), BigDecimal
.valueOf(3));
87 ITmfTimestampTransform ttl2
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(2.0), BigDecimal
.valueOf(3));
88 ITmfTimestampTransform ttl3
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(3), BigDecimal
.valueOf(3));
89 assertEquals(ttl
, ttl2
);
90 assertFalse(ttl
.equals(ttl3
));
91 assertFalse(ttl2
.equals(ttl3
));
94 assertTrue(map
.containsKey(ttl2
));
95 assertEquals("a", map
.get(ttl
));
97 ITmfTimestampTransform ti
= TmfTimestampTransform
.IDENTITY
;
98 assertEquals(TmfTimestampTransform
.IDENTITY
, ti
);
99 assertFalse(TmfTimestampTransform
.IDENTITY
.equals(ttl
));
102 assertTrue(map
.containsKey(TmfTimestampTransform
.IDENTITY
));
103 assertEquals("b", map
.get(ti
));
105 assertFalse(ti
.equals(ttl
));
106 assertFalse(ttl
.equals(ti
));
111 * Test the transform composition function
114 public void testComposition() {
116 ITmfTimestampTransform ti
= TmfTimestampTransform
.IDENTITY
;
117 ITmfTimestampTransform ttl
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(2.0), BigDecimal
.valueOf(3));
118 ITmfTimestampTransform ttl2
= new TmfTimestampTransformLinear(BigDecimal
.valueOf(1.5), BigDecimal
.valueOf(8));
120 ITmfTimestampTransform tc1
= ti
.composeWith(ttl
);
122 assertEquals(ttl
, tc1
);
123 assertEquals(203, tc1
.transform(t
));
125 tc1
= ttl
.composeWith(ti
);
126 /* Should be ttl also */
127 assertEquals(ttl
, tc1
);
128 assertEquals(203, tc1
.transform(t
));
130 tc1
= ti
.composeWith(ti
);
131 /* Should be identity */
132 assertEquals(tc1
, TmfTimestampTransform
.IDENTITY
);
133 assertEquals(100, tc1
.transform(t
));
135 tc1
= ttl
.composeWith(ttl2
);
136 assertEquals(ttl
.transform(ttl2
.transform(t
)), tc1
.transform(t
));
137 assertEquals(319, tc1
.transform(t
));
139 tc1
= ttl2
.composeWith(ttl
);
140 assertEquals(ttl2
.transform(ttl
.transform(t
)), tc1
.transform(t
));
141 assertEquals(312, tc1
.transform(t
));