Copyright header update, 2015 edition
[deliverable/tracecompass.git] / 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.tracecompass.internal.tmf.core.synchronization.TmfTimestampTransform;
24 import org.eclipse.tracecompass.internal.tmf.core.synchronization.TmfTimestampTransformLinear;
25 import org.eclipse.tracecompass.tmf.core.synchronization.ITmfTimestampTransform;
26 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
27 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
28 import org.junit.Test;
29
30 /**
31 * Tests for {@link TmfTimestampTransform} and its descendants
32 *
33 * @author Geneviève Bastien
34 */
35 @SuppressWarnings("nls")
36 public class TsTransformTest {
37
38 private static final long ts = 1361657893526374091L;
39 private static final ITmfTimestamp oTs = new TmfTimestamp(ts);
40
41 /**
42 * Test the linear transform
43 */
44 @Test
45 public void testLinearTransform() {
46 /* Default constructor */
47 TmfTimestampTransformLinear ttl = new TmfTimestampTransformLinear();
48 assertEquals(1361657893526374091L, ttl.transform(ts));
49 assertEquals(1361657893526374091L, ttl.transform(oTs).getValue());
50
51 /* Just an offset */
52 ttl = new TmfTimestampTransformLinear(BigDecimal.valueOf(1.0), BigDecimal.valueOf(3));
53 assertEquals(1361657893526374094L, ttl.transform(ts));
54 assertEquals(1361657893526374094L, ttl.transform(oTs).getValue());
55
56 /* Just a slope */
57 ttl = new TmfTimestampTransformLinear(BigDecimal.valueOf(2.0), BigDecimal.valueOf(0));
58 assertEquals(2723315787052748182L, ttl.transform(ts));
59 assertEquals(2723315787052748182L, ttl.transform(oTs).getValue());
60
61 /* Offset and slope */
62 ttl = new TmfTimestampTransformLinear(BigDecimal.valueOf(2.0), BigDecimal.valueOf(3));
63 assertEquals(2723315787052748185L, ttl.transform(ts));
64 assertEquals(2723315787052748185L, ttl.transform(oTs).getValue());
65
66 /* Offset and slope */
67 ttl = new TmfTimestampTransformLinear(BigDecimal.valueOf(0.5), BigDecimal.valueOf(0));
68 assertEquals(680828946763187045L, ttl.transform(ts));
69 assertEquals(680828946763187045L, ttl.transform(oTs).getValue());
70 }
71
72 /**
73 * Test for the identity transform
74 */
75 @Test
76 public void testIdentityTransform() {
77 ITmfTimestampTransform tt = TmfTimestampTransform.IDENTITY;
78 assertEquals(ts, tt.transform(ts));
79 assertEquals(oTs, tt.transform(oTs));
80 }
81
82 /**
83 * Test hash and equals function
84 */
85 @Test
86 public void testEquality() {
87 Map<ITmfTimestampTransform, String> map = new HashMap<>();
88 ITmfTimestampTransform ttl = new TmfTimestampTransformLinear(BigDecimal.valueOf(2.0), BigDecimal.valueOf(3));
89 ITmfTimestampTransform ttl2 = new TmfTimestampTransformLinear(BigDecimal.valueOf(2.0), BigDecimal.valueOf(3));
90 ITmfTimestampTransform ttl3 = new TmfTimestampTransformLinear(BigDecimal.valueOf(3), BigDecimal.valueOf(3));
91 assertEquals(ttl, ttl2);
92 assertFalse(ttl.equals(ttl3));
93 assertFalse(ttl2.equals(ttl3));
94
95 map.put(ttl, "a");
96 assertTrue(map.containsKey(ttl2));
97 assertEquals("a", map.get(ttl));
98
99 ITmfTimestampTransform ti = TmfTimestampTransform.IDENTITY;
100 assertEquals(TmfTimestampTransform.IDENTITY, ti);
101 assertFalse(TmfTimestampTransform.IDENTITY.equals(ttl));
102
103 map.put(ti, "b");
104 assertTrue(map.containsKey(TmfTimestampTransform.IDENTITY));
105 assertEquals("b", map.get(ti));
106
107 assertFalse(ti.equals(ttl));
108 assertFalse(ttl.equals(ti));
109
110 }
111
112 /**
113 * Test the transform composition function
114 */
115 @Test
116 public void testComposition() {
117 long t = 100;
118 ITmfTimestampTransform ti = TmfTimestampTransform.IDENTITY;
119 ITmfTimestampTransform ttl = new TmfTimestampTransformLinear(BigDecimal.valueOf(2.0), BigDecimal.valueOf(3));
120 ITmfTimestampTransform ttl2 = new TmfTimestampTransformLinear(BigDecimal.valueOf(1.5), BigDecimal.valueOf(8));
121
122 ITmfTimestampTransform tc1 = ti.composeWith(ttl);
123 /* Should be ttl */
124 assertEquals(ttl, tc1);
125 assertEquals(203, tc1.transform(t));
126
127 tc1 = ttl.composeWith(ti);
128 /* Should be ttl also */
129 assertEquals(ttl, tc1);
130 assertEquals(203, tc1.transform(t));
131
132 tc1 = ti.composeWith(ti);
133 /* Should be identity */
134 assertEquals(tc1, TmfTimestampTransform.IDENTITY);
135 assertEquals(100, tc1.transform(t));
136
137 tc1 = ttl.composeWith(ttl2);
138 assertEquals(ttl.transform(ttl2.transform(t)), tc1.transform(t));
139 assertEquals(319, tc1.transform(t));
140
141 tc1 = ttl2.composeWith(ttl);
142 assertEquals(ttl2.transform(ttl.transform(t)), tc1.transform(t));
143 assertEquals(312, tc1.transform(t));
144
145 }
146
147 }
This page took 0.039005 seconds and 5 git commands to generate.