Commit | Line | Data |
---|---|---|
e73a4ba5 GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 É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.linuxtools.tmf.core.tests.synchronization; | |
14 | ||
15 | import static org.junit.Assert.*; | |
16 | ||
17 | import java.math.BigDecimal; | |
18 | import java.util.HashMap; | |
19 | import java.util.Map; | |
20 | ||
21 | import org.eclipse.linuxtools.tmf.core.synchronization.ITmfTimestampTransform; | |
22 | import org.eclipse.linuxtools.tmf.core.synchronization.TmfTimestampTransform; | |
23 | import org.eclipse.linuxtools.tmf.core.synchronization.TmfTimestampTransformLinear; | |
24 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; | |
25 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; | |
26 | import org.junit.Test; | |
27 | ||
28 | /** | |
29 | * Tests for {@link TmfTimestampTransform} and its descendants | |
30 | * | |
31 | * @author Geneviève Bastien | |
32 | */ | |
33 | @SuppressWarnings("nls") | |
34 | public class TsTransformTest { | |
35 | ||
36 | private long ts = 1361657893526374091L; | |
37 | private ITmfTimestamp oTs = new TmfTimestamp(ts); | |
38 | ||
39 | /** | |
40 | * Test the linear transform | |
41 | */ | |
42 | @Test | |
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()); | |
48 | ||
49 | /* Just an offset */ | |
50 | ttl = new TmfTimestampTransformLinear(BigDecimal.valueOf(1.0), BigDecimal.valueOf(3)); | |
51 | assertEquals(1361657893526374094L, ttl.transform(ts)); | |
52 | assertEquals(1361657893526374094L, ttl.transform(oTs).getValue()); | |
53 | ||
54 | /* Just a slope */ | |
55 | ttl = new TmfTimestampTransformLinear(BigDecimal.valueOf(2.0), BigDecimal.valueOf(0)); | |
56 | assertEquals(2723315787052748182L, ttl.transform(ts)); | |
57 | assertEquals(2723315787052748182L, ttl.transform(oTs).getValue()); | |
58 | ||
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()); | |
63 | ||
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()); | |
68 | } | |
69 | ||
70 | /** | |
71 | * Test for the identity transform | |
72 | */ | |
73 | @Test | |
74 | public void testIdentityTransform() { | |
75 | ITmfTimestampTransform tt = TmfTimestampTransform.IDENTITY; | |
76 | assertEquals(ts, tt.transform(ts)); | |
77 | assertEquals(oTs, tt.transform(oTs)); | |
78 | } | |
79 | ||
80 | /** | |
81 | * Test hash and equals function | |
82 | */ | |
83 | @Test | |
84 | public void testEquality() { | |
ccf2bbb4 | 85 | Map<ITmfTimestampTransform, String> map = new HashMap<>(); |
e73a4ba5 GB |
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)); | |
92 | ||
93 | map.put(ttl, "a"); | |
94 | assertTrue(map.containsKey(ttl2)); | |
95 | assertEquals("a", map.get(ttl)); | |
96 | ||
97 | ITmfTimestampTransform ti = TmfTimestampTransform.IDENTITY; | |
98 | assertEquals(TmfTimestampTransform.IDENTITY, ti); | |
99 | assertFalse(TmfTimestampTransform.IDENTITY.equals(ttl)); | |
100 | ||
101 | map.put(ti, "b"); | |
102 | assertTrue(map.containsKey(TmfTimestampTransform.IDENTITY)); | |
103 | assertEquals("b", map.get(ti)); | |
104 | ||
105 | assertFalse(ti.equals(ttl)); | |
106 | assertFalse(ttl.equals(ti)); | |
107 | ||
108 | } | |
109 | ||
110 | /** | |
111 | * Test the transform composition function | |
112 | */ | |
113 | @Test | |
114 | public void testComposition() { | |
115 | long t = 100; | |
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)); | |
119 | ||
120 | ITmfTimestampTransform tc1 = ti.composeWith(ttl); | |
121 | /* Should be ttl */ | |
122 | assertEquals(ttl, tc1); | |
123 | assertEquals(203, tc1.transform(t)); | |
124 | ||
125 | tc1 = ttl.composeWith(ti); | |
126 | /* Should be ttl also */ | |
127 | assertEquals(ttl, tc1); | |
128 | assertEquals(203, tc1.transform(t)); | |
129 | ||
130 | tc1 = ti.composeWith(ti); | |
131 | /* Should be identity */ | |
132 | assertEquals(tc1, TmfTimestampTransform.IDENTITY); | |
133 | assertEquals(100, tc1.transform(t)); | |
134 | ||
135 | tc1 = ttl.composeWith(ttl2); | |
136 | assertEquals(ttl.transform(ttl2.transform(t)), tc1.transform(t)); | |
137 | assertEquals(319, tc1.transform(t)); | |
138 | ||
139 | tc1 = ttl2.composeWith(ttl); | |
140 | assertEquals(ttl2.transform(ttl.transform(t)), tc1.transform(t)); | |
141 | assertEquals(312, tc1.transform(t)); | |
142 | ||
143 | } | |
144 | } |