aboutsummaryrefslogtreecommitdiff
path: root/android/WALT/app/src/test/java/org/chromium/latency/walt/UtilsTest.java
blob: 33d84543b65dd4f3a22171b163f606965f843d63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.chromium.latency.walt;

import com.github.mikephil.charting.data.Entry;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static java.lang.Double.NaN;
import static org.junit.Assert.assertEquals;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class UtilsTest {

    @Test
    public void testMedian_singleNumber() {
        ArrayList<Double> arr = new ArrayList<>();
        arr.add(0d);
        assertThat(Utils.median(arr), is(0d));
    }

    @Test
    public void testMedian_evenSize() {
        ArrayList<Double> arr = new ArrayList<>();
        arr.add(1d); arr.add(2d); arr.add(3d); arr.add(4d);
        assertThat(Utils.median(arr), is(2.5d));
    }

    @Test
    public void testMedian_oddSize() {
        ArrayList<Double> arr = new ArrayList<>();
        arr.add(1d); arr.add(2d); arr.add(3d); arr.add(4d); arr.add(5d);
        assertThat(Utils.median(arr), is(3d));
    }

    @Test
    public void testMean() {
        assertThat(Utils.mean(new double[]{-1,1,2,3}), is(1.25d));
    }

    @Test
    public void testMean_singleNumber() {
        assertThat(Utils.mean(new double[]{0}), is(0d));
    }

    @Test
    public void testMean_empty() {
        assertThat(Utils.mean(new double[]{}), is(NaN));
    }

    @Test
    public void testMean_repeatedNumbers() {
        assertThat(Utils.mean(new double[]{5,5,5,5}), is(5d));
    }

    @Test
    public void testInterp() {
        assertThat(Utils.interp(new double[]{5,6,16,17}, new double[]{0, 10, 12, 18},
                new double[]{35, 50, 75, 93}), is(new double[]{42.5, 44, 87, 90}));
    }

    @Test
    public void testInterp_singleNumber() {
        assertThat(Utils.interp(new double[]{5}, new double[]{0, 10},
                new double[]{35, 50}), is(new double[]{42.5}));
    }

    @Test
    public void testInterp_twoNumbers() {
        assertThat(Utils.interp(new double[]{0}, new double[]{0, 10},
                new double[]{35, 50}), is(new double[]{35}));
    }

    @Test
    public void testInterp_numberContained() {
        assertThat(Utils.interp(new double[]{5, 10}, new double[]{0, 5, 10},
                new double[]{35, 19, 50}), is(new double[]{19, 50}));
    }

    @Test
    public void testStdev() {
        assertThat(Utils.stdev(new double[]{10,12,14,18}), is(Math.sqrt(8.75)));
    }

    @Test
    public void testStdev_empty() {
        assertThat(Utils.stdev(new double[]{}), is(NaN));
    }

    @Test
    public void testStdev_singleNumber() {
        assertThat(Utils.stdev(new double[]{42}), is(0d));
    }

    @Test
    public void testStdev_manyNumbers() {
        assertThat(Utils.stdev(new double[]{-1,0,1}), is(Math.sqrt(2d/3d)));
    }

    @Test
    public void testExtract() {
        assertThat(Utils.extract(new int[]{1, 2, 2, 1, 2, 2, 1, 2, 2}, 1,
                new double[]{1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}),
                is(new double[]{1.5, 4.5, 7.5}));
    }

    @Test
    public void testExtract_empty() {
        assertThat(Utils.extract(new int[]{}, 1, new double[]{}), is(new double[]{}));
    }

    @Test
    public void testArgmin() {
        assertThat(Utils.argmin(new double[]{5, 2, 1, -10, -20, 5, 19, 100}), is(4));
    }

    @Test
    public void testArgmin_empty() {
        assertThat(Utils.argmin(new double[]{}), is(0));
    }

    @Test
    public void testFindBestShift() {
        Random rand = new Random(42);
        double latency = 12.34;
        double[] touchTimes = new double[4000];
        for (int i = 0; i < touchTimes.length; i++) {
            // touch events every millisecond with some jitter
            touchTimes[i] = i + rand.nextDouble()*0.2 - 0.1;
        }
        double[] touchY = new double[touchTimes.length];
        for (int i = 0; i < touchY.length; i++) {
            // sine wave will oscillate 1 time
            touchY[i] = 1000*Math.cos((touchTimes[i] - latency) * Math.PI/500) + rand.nextDouble()*0.02 - 0.01;
        }
        double[] laserTimes = new double[4];
        int i = 0;
        for (int root = 0; root < 1000; root+=1000) {
            laserTimes[i++] = root + 250 - 10;
            laserTimes[i++] = root + 250 + 10;
            laserTimes[i++] = root + 750 - 10;
            laserTimes[i++] = root + 750 + 10;
        }
        assertEquals(latency, Utils.findBestShift(laserTimes, touchTimes, touchY), 1e-6);
    }

    @Test
    public void testMeanEntries() {
        List<Entry> entries = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            entries.add(new Entry(i, i));
        }
        assertEquals(5.5, Utils.mean(entries), 1e-12);
    }

    @Test
    public void testMinEntries() {
        List<Entry> entries = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            entries.add(new Entry(i, i));
        }
        assertEquals(1, Utils.min(entries), 1e-12);
    }

    @Test
    public void testMaxEntries() {
        List<Entry> entries = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            entries.add(new Entry(i, i));
        }
        assertEquals(10, Utils.max(entries), 1e-12);
    }
}