7839cdc448c4db625371577c2890de5d067e855b
[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.assertTrue;
18
19 import java.math.BigDecimal;
20 import java.util.HashMap;
21 import java.util.Map;
22
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;
30
31 /**
32 * Tests for {@link TmfTimestampTransform} and its descendants
33 *
34 * @author Geneviève Bastien
35 */
36 @SuppressWarnings("nls")
37 public class TsTransformTest {
38
39 private static final long ts = 1361657893526374091L;
40 private static final @NonNull ITmfTimestamp oTs = TmfTimestamp.fromSeconds(ts);
41
42 /**
43 * Test the linear transform
44 */
45 @Test
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());
51
52 /* Just an offset */
53 ttl = new TmfTimestampTransformLinear(BigDecimal.valueOf(1.0), BigDecimal.valueOf(3));
54 assertEquals(1361657893526374094L, ttl.transform(ts));
55 assertEquals(1361657893526374094L, ttl.transform(oTs).getValue());
56
57 /* Just a slope */
58 ttl = new TmfTimestampTransformLinear(BigDecimal.valueOf(2.0), BigDecimal.valueOf(0));
59 assertEquals(2723315787052748182L, ttl.transform(ts));
60 assertEquals(2723315787052748182L, ttl.transform(oTs).getValue());
61
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());
66
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());
71 }
72
73 /**
74 * Test for the identity transform
75 */
76 @Test
77 public void testIdentityTransform() {
78 ITmfTimestampTransform tt = TmfTimestampTransform.IDENTITY;
79 assertEquals(ts, tt.transform(ts));
80 assertEquals(oTs, tt.transform(oTs));
81 }
82
83 /**
84 * Test hash and equals function
85 */
86 @Test
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));
95
96 map.put(ttl, "a");
97 assertTrue(map.containsKey(ttl2));
98 assertEquals("a", map.get(ttl));
99
100 ITmfTimestampTransform ti = TmfTimestampTransform.IDENTITY;
101 assertEquals(TmfTimestampTransform.IDENTITY, ti);
102 assertFalse(TmfTimestampTransform.IDENTITY.equals(ttl));
103
104 map.put(ti, "b");
105 assertTrue(map.containsKey(TmfTimestampTransform.IDENTITY));
106 assertEquals("b", map.get(ti));
107
108 assertFalse(ti.equals(ttl));
109 assertFalse(ttl.equals(ti));
110
111 }
112
113 /**
114 * Test the transform composition function
115 */
116 @Test
117 public void testComposition() {
118 long t = 100;
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));
122
123 ITmfTimestampTransform tc1 = ti.composeWith(ttl);
124 /* Should be ttl */
125 assertEquals(ttl, tc1);
126 assertEquals(203, tc1.transform(t));
127
128 tc1 = ttl.composeWith(ti);
129 /* Should be ttl also */
130 assertEquals(ttl, tc1);
131 assertEquals(203, tc1.transform(t));
132
133 tc1 = ti.composeWith(ti);
134 /* Should be identity */
135 assertEquals(tc1, TmfTimestampTransform.IDENTITY);
136 assertEquals(100, tc1.transform(t));
137
138 tc1 = ttl.composeWith(ttl2);
139 assertEquals(ttl.transform(ttl2.transform(t)), tc1.transform(t));
140 assertEquals(319, tc1.transform(t));
141
142 tc1 = ttl2.composeWith(ttl);
143 assertEquals(ttl2.transform(ttl.transform(t)), tc1.transform(t));
144 assertEquals(312, tc1.transform(t));
145
146 }
147
148 }
This page took 0.034216 seconds and 4 git commands to generate.