1 /*******************************************************************************
2 * Copyright (c) 2015 École Polytechnique de Montréal
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
10 * Francis Giraldeau - Initial implementation and API
11 * Geneviève Bastien - Fixes and improvements
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.core
.tests
.synchronization
;
16 import static org
.junit
.Assert
.assertEquals
;
17 import static org
.junit
.Assert
.assertNotNull
;
18 import static org
.junit
.Assert
.assertTrue
;
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
;
26 * Tests for {@link TmfTimestampTransformLinearFast}
28 * @author Geneviève Bastien
30 public class TsTransformFastTest
{
32 private static final long ts
= 1361657893526374091L;
35 * Test whether the fast linear transform always yields the same value for
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;
49 // Get the transformed value to a timestamp without cache miss
50 long tsTNoMiss
= fast
.transform(tsNoMiss
);
51 assertEquals(1, fast
.getCacheMisses());
54 fast
.transform(tsMiss
);
55 assertEquals(2, fast
.getCacheMisses());
58 * Get the transformed value of the same previous timestamp after the
61 long tsTAfterMiss
= fast
.transform(tsNoMiss
);
62 assertEquals(tsTNoMiss
, tsTAfterMiss
);
66 * Test that 2 equal fast transform always give the same results for the
70 public void testFLTEquivalence() {
71 TmfTimestampTransformLinearFast fast
= new TmfTimestampTransformLinearFast(Math
.PI
, 0);
72 TmfTimestampTransformLinearFast fast2
= new TmfTimestampTransformLinearFast(Math
.PI
, 0);
74 long deltaMax
= fast
.getDeltaMax();
76 long start
= (ts
- (ts
% deltaMax
) - 10);
77 checkTime(fast
, fast2
, 20, start
, 1);
81 * Test the precision of the fast timestamp transform compared to the
85 public void testFastTransformPrecision() {
86 TmfTimestampTransformLinear precise
= new TmfTimestampTransformLinear(Math
.PI
, 0);
87 TmfTimestampTransformLinearFast fast
= new TmfTimestampTransformLinearFast(Math
.PI
, 0);
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());
95 // check that rescale is done only when required
96 // assumes tsBitWidth == 30
97 // test forward and backward timestamps
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());
113 * Test that fast transform produces the same result for small and large slopes.
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
]);
130 * Check that the proper exception are raised for illegal slopes
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;
138 new TmfTimestampTransformLinearFast(slope
, 0.0);
139 } catch (IllegalArgumentException e
) {
142 assertNotNull(exception
);
146 private static void checkTime(ITmfTimestampTransform precise
, ITmfTimestampTransform fast
,
147 int samples
, long start
, long step
) {
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);
158 assertTrue("monotonic error" + act
+ " " + prev
, act
>= prev
);
159 } else if (step
< 0) {
160 assertTrue("monotonic ", act
<= prev
);