tmf.core: Introduce TmfTimestamp factory methods
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / synchronization / TsTransformTest.java
CommitLineData
e73a4ba5 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
e73a4ba5
GB
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
2bdf0193 13package org.eclipse.tracecompass.tmf.core.tests.synchronization;
e73a4ba5 14
5745c0a3
FG
15import static org.junit.Assert.assertEquals;
16import static org.junit.Assert.assertFalse;
17import static org.junit.Assert.assertTrue;
e73a4ba5
GB
18
19import java.math.BigDecimal;
20import java.util.HashMap;
21import java.util.Map;
22
5dca27ae 23import org.eclipse.jdt.annotation.NonNull;
2bdf0193
AM
24import org.eclipse.tracecompass.internal.tmf.core.synchronization.TmfTimestampTransform;
25import org.eclipse.tracecompass.internal.tmf.core.synchronization.TmfTimestampTransformLinear;
26import org.eclipse.tracecompass.tmf.core.synchronization.ITmfTimestampTransform;
27import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
28import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
e73a4ba5
GB
29import org.junit.Test;
30
31/**
32 * Tests for {@link TmfTimestampTransform} and its descendants
33 *
34 * @author Geneviève Bastien
35 */
36@SuppressWarnings("nls")
37public class TsTransformTest {
38
5745c0a3 39 private static final long ts = 1361657893526374091L;
b2c971ec 40 private static final @NonNull ITmfTimestamp oTs = TmfTimestamp.fromSeconds(ts);
e73a4ba5
GB
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() {
ccf2bbb4 88 Map<ITmfTimestampTransform, String> map = new HashMap<>();
e73a4ba5
GB
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 }
5745c0a3 147
e73a4ba5 148}
This page took 0.078564 seconds and 5 git commands to generate.