8e894e2ac53b507a17dbce25db3178b504fc3382
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / synchronization / TsTransformFastTest.java
1 /*******************************************************************************
2 * Copyright (c) 2015 É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 * Francis Giraldeau - Initial implementation and API
11 * Geneviève Bastien - Fixes and improvements
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.core.tests.synchronization;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertNotNull;
18 import static org.junit.Assert.assertTrue;
19
20 import org.eclipse.tracecompass.internal.tmf.core.synchronization.TmfTimestampTransformLinear;
21 import org.eclipse.tracecompass.internal.tmf.core.synchronization.TmfTimestampTransformLinearFast;
22 import org.eclipse.tracecompass.tmf.core.synchronization.ITmfTimestampTransform;
23 import org.junit.Test;
24
25 /**
26 * Tests for {@link TmfTimestampTransformLinearFast}
27 *
28 * @author Geneviève Bastien
29 */
30 public class TsTransformFastTest {
31
32 private static final long ts = 1361657893526374091L;
33
34 /**
35 * Test whether the fast linear transform always yields the same value for
36 * the same timestamp
37 */
38 @Test
39 public void testFLTRepeatability() {
40 TmfTimestampTransformLinearFast fast = new TmfTimestampTransformLinearFast(Math.PI, 0);
41 // Access fDeltaMax to compute the cache range boundaries
42 long deltaMax = fast.getDeltaMax();
43 // Initialize the transform
44 long timestamp = ts - (ts % deltaMax);
45 fast.transform(timestamp);
46 long tsMiss = timestamp + deltaMax;
47 long tsNoMiss = timestamp + deltaMax - 1;
48
49 // Get the transformed value to a timestamp without cache miss
50 long tsTNoMiss = fast.transform(tsNoMiss);
51 assertEquals(1, fast.getCacheMisses());
52
53 // Cause a cache miss
54 fast.transform(tsMiss);
55 assertEquals(2, fast.getCacheMisses());
56
57 /*
58 * Get the transformed value of the same previous timestamp after the
59 * miss
60 */
61 long tsTAfterMiss = fast.transform(tsNoMiss);
62 assertEquals(tsTNoMiss, tsTAfterMiss);
63 }
64
65 /**
66 * Test that 2 equal fast transform always give the same results for the
67 * same values
68 */
69 @Test
70 public void testFLTEquivalence() {
71 TmfTimestampTransformLinearFast fast = new TmfTimestampTransformLinearFast(Math.PI, 0);
72 TmfTimestampTransformLinearFast fast2 = new TmfTimestampTransformLinearFast(Math.PI, 0);
73
74 long deltaMax = fast.getDeltaMax();
75
76 long start = (ts - (ts % deltaMax) - 10);
77 checkTime(fast, fast2, 20, start, 1);
78 }
79
80 /**
81 * Test the precision of the fast timestamp transform compared to the
82 * original transform.
83 */
84 @Test
85 public void testFastTransformPrecision() {
86 TmfTimestampTransformLinear precise = new TmfTimestampTransformLinear(Math.PI, 0);
87 TmfTimestampTransformLinearFast fast = new TmfTimestampTransformLinearFast(Math.PI, 0);
88 int samples = 100;
89 long start = (long) Math.pow(10, 18);
90 long end = Long.MAX_VALUE;
91 int step = (int) ((end - start) / (samples * Math.PI));
92 checkTime(precise, fast, samples, start, step);
93 assertEquals(samples, fast.getCacheMisses());
94
95 // check that rescale is done only when required
96 // assumes tsBitWidth == 30
97 // test forward and backward timestamps
98 samples = 1000;
99 int[] directions = new int[] { 1, -1 };
100 for (Integer direction : directions) {
101 for (int i = 0; i <= 30; i++) {
102 fast.resetScaleStats();
103 step = (1 << i) * direction;
104 checkTime(precise, fast, samples, start, step);
105 assertTrue(String.format("samples: %d scale misses: %d",
106 samples, fast.getCacheMisses()), samples >= fast.getCacheMisses());
107 }
108 }
109
110 }
111
112 /**
113 * Test that fast transform produces the same result for small and large slopes.
114 */
115 @Test
116 public void testFastTransformSlope() {
117 int[] dir = new int[] { 1, -1 };
118 long start = (1 << 30);
119 for (int ex = -9; ex <= 9; ex++) {
120 for (int d = 0; d < dir.length; d++) {
121 double slope = Math.pow(10.0, ex);
122 TmfTimestampTransformLinear precise = new TmfTimestampTransformLinear(slope, 0);
123 TmfTimestampTransformLinearFast fast = new TmfTimestampTransformLinearFast(slope, 0);
124 checkTime(precise, fast, 1000, start, dir[d]);
125 }
126 }
127 }
128
129 /**
130 * Check that the proper exception are raised for illegal slopes
131 */
132 @Test
133 public void testFastTransformArguments() {
134 double[] slopes = new double[] { -1.0, ((double)Integer.MAX_VALUE) + 1, 1e-10 };
135 for (double slope: slopes) {
136 Exception exception = null;
137 try {
138 new TmfTimestampTransformLinearFast(slope, 0.0);
139 } catch (IllegalArgumentException e) {
140 exception = e;
141 }
142 assertNotNull(exception);
143 }
144 }
145
146 private static void checkTime(ITmfTimestampTransform precise, ITmfTimestampTransform fast,
147 int samples, long start, long step) {
148 long prev = 0;
149 for (int i = 0; i < samples; i++) {
150 long time = start + i * step;
151 long exp = precise.transform(time);
152 long act = fast.transform(time);
153 long err = act - exp;
154 // allow only two ns of error
155 assertTrue("[" + err + "]", Math.abs(err) < 3);
156 if (i > 0) {
157 if (step > 0) {
158 assertTrue("monotonic error" + act + " " + prev, act >= prev);
159 } else if (step < 0) {
160 assertTrue("monotonic ", act <= prev);
161 }
162 }
163 prev = act;
164 }
165 }
166
167 }
This page took 0.035072 seconds and 4 git commands to generate.