Commit | Line | Data |
---|---|---|
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 | 13 | package org.eclipse.tracecompass.tmf.core.tests.synchronization; |
e73a4ba5 | 14 | |
5745c0a3 FG |
15 | import static org.junit.Assert.assertEquals; |
16 | import static org.junit.Assert.assertFalse; | |
4a1d13c4 | 17 | import static org.junit.Assert.assertNotEquals; |
5745c0a3 | 18 | import static org.junit.Assert.assertTrue; |
e73a4ba5 GB |
19 | |
20 | import java.math.BigDecimal; | |
21 | import java.util.HashMap; | |
22 | import java.util.Map; | |
23 | ||
5dca27ae | 24 | import org.eclipse.jdt.annotation.NonNull; |
4a1d13c4 | 25 | import org.eclipse.tracecompass.internal.tmf.core.synchronization.TmfConstantTransform; |
2bdf0193 AM |
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; | |
e73a4ba5 GB |
31 | import org.junit.Test; |
32 | ||
33 | /** | |
34 | * Tests for {@link TmfTimestampTransform} and its descendants | |
35 | * | |
36 | * @author Geneviève Bastien | |
37 | */ | |
38 | @SuppressWarnings("nls") | |
39 | public class TsTransformTest { | |
40 | ||
5745c0a3 | 41 | private static final long ts = 1361657893526374091L; |
b2c971ec | 42 | private static final @NonNull ITmfTimestamp oTs = TmfTimestamp.fromSeconds(ts); |
e73a4ba5 GB |
43 | |
44 | /** | |
45 | * Test the linear transform | |
46 | */ | |
47 | @Test | |
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()); | |
53 | ||
54 | /* Just an offset */ | |
55 | ttl = new TmfTimestampTransformLinear(BigDecimal.valueOf(1.0), BigDecimal.valueOf(3)); | |
56 | assertEquals(1361657893526374094L, ttl.transform(ts)); | |
57 | assertEquals(1361657893526374094L, ttl.transform(oTs).getValue()); | |
58 | ||
59 | /* Just a slope */ | |
60 | ttl = new TmfTimestampTransformLinear(BigDecimal.valueOf(2.0), BigDecimal.valueOf(0)); | |
61 | assertEquals(2723315787052748182L, ttl.transform(ts)); | |
62 | assertEquals(2723315787052748182L, ttl.transform(oTs).getValue()); | |
63 | ||
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()); | |
68 | ||
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()); | |
73 | } | |
74 | ||
75 | /** | |
76 | * Test for the identity transform | |
77 | */ | |
78 | @Test | |
79 | public void testIdentityTransform() { | |
80 | ITmfTimestampTransform tt = TmfTimestampTransform.IDENTITY; | |
81 | assertEquals(ts, tt.transform(ts)); | |
82 | assertEquals(oTs, tt.transform(oTs)); | |
83 | } | |
84 | ||
85 | /** | |
86 | * Test hash and equals function | |
87 | */ | |
88 | @Test | |
89 | public void testEquality() { | |
ccf2bbb4 | 90 | Map<ITmfTimestampTransform, String> map = new HashMap<>(); |
e73a4ba5 GB |
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)); | |
97 | ||
98 | map.put(ttl, "a"); | |
99 | assertTrue(map.containsKey(ttl2)); | |
100 | assertEquals("a", map.get(ttl)); | |
101 | ||
102 | ITmfTimestampTransform ti = TmfTimestampTransform.IDENTITY; | |
103 | assertEquals(TmfTimestampTransform.IDENTITY, ti); | |
104 | assertFalse(TmfTimestampTransform.IDENTITY.equals(ttl)); | |
105 | ||
106 | map.put(ti, "b"); | |
107 | assertTrue(map.containsKey(TmfTimestampTransform.IDENTITY)); | |
108 | assertEquals("b", map.get(ti)); | |
109 | ||
110 | assertFalse(ti.equals(ttl)); | |
111 | assertFalse(ttl.equals(ti)); | |
4a1d13c4 | 112 | } |
e73a4ba5 | 113 | |
4a1d13c4 AM |
114 | /** |
115 | * Test the equality of {@link TmfConstantTransform} objects. | |
116 | */ | |
117 | @Test | |
118 | public void testEqualityConstantTransform() { | |
119 | ITmfTimestampTransform tt1 = new TmfConstantTransform(50L); | |
120 | ITmfTimestampTransform tt2 = new TmfConstantTransform(50L); | |
121 | ITmfTimestampTransform tt3 = new TmfConstantTransform(-10L); | |
122 | ||
123 | assertEquals(tt1, tt2); | |
124 | assertNotEquals(tt1, tt3); | |
125 | assertNotEquals(tt2, tt3); | |
e73a4ba5 GB |
126 | } |
127 | ||
128 | /** | |
129 | * Test the transform composition function | |
130 | */ | |
131 | @Test | |
132 | public void testComposition() { | |
133 | long t = 100; | |
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)); | |
137 | ||
138 | ITmfTimestampTransform tc1 = ti.composeWith(ttl); | |
139 | /* Should be ttl */ | |
140 | assertEquals(ttl, tc1); | |
141 | assertEquals(203, tc1.transform(t)); | |
142 | ||
143 | tc1 = ttl.composeWith(ti); | |
144 | /* Should be ttl also */ | |
145 | assertEquals(ttl, tc1); | |
146 | assertEquals(203, tc1.transform(t)); | |
147 | ||
148 | tc1 = ti.composeWith(ti); | |
149 | /* Should be identity */ | |
150 | assertEquals(tc1, TmfTimestampTransform.IDENTITY); | |
151 | assertEquals(100, tc1.transform(t)); | |
152 | ||
153 | tc1 = ttl.composeWith(ttl2); | |
154 | assertEquals(ttl.transform(ttl2.transform(t)), tc1.transform(t)); | |
155 | assertEquals(319, tc1.transform(t)); | |
156 | ||
157 | tc1 = ttl2.composeWith(ttl); | |
158 | assertEquals(ttl2.transform(ttl.transform(t)), tc1.transform(t)); | |
159 | assertEquals(312, tc1.transform(t)); | |
160 | ||
161 | } | |
5745c0a3 | 162 | |
e73a4ba5 | 163 | } |