summaryrefslogtreecommitdiff
path: root/libs/input/tests/LatencyStatistics_test.cpp
blob: eb12d4ef6fc4c8be4b1c72574e9d011fd0bbd1d8 (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
/*
 * Copyright (C) 2019 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.
 */

#include <gtest/gtest.h>
#include <input/LatencyStatistics.h>
#include <cmath>
#include <limits>
#include <thread>

namespace android {
namespace test {

TEST(LatencyStatisticsTest, ResetStats) {
    LatencyStatistics stats{5min};
    stats.addValue(5.0);
    stats.addValue(19.3);
    stats.addValue(20);
    stats.reset();

    ASSERT_EQ(stats.getCount(), 0u);
    ASSERT_EQ(std::isnan(stats.getStDev()), true);
    ASSERT_EQ(std::isnan(stats.getMean()), true);
}

TEST(LatencyStatisticsTest, AddStatsValue) {
    LatencyStatistics stats{5min};
    stats.addValue(5.0);

    ASSERT_EQ(stats.getMin(), 5.0);
    ASSERT_EQ(stats.getMax(), 5.0);
    ASSERT_EQ(stats.getCount(), 1u);
    ASSERT_EQ(stats.getMean(), 5.0);
    ASSERT_EQ(stats.getStDev(), 0.0);
}

TEST(LatencyStatisticsTest, AddMultipleStatsValue) {
    LatencyStatistics stats{5min};
    stats.addValue(4.0);
    stats.addValue(6.0);
    stats.addValue(8.0);
    stats.addValue(10.0);

    float stdev = stats.getStDev();

    ASSERT_EQ(stats.getMin(), 4.0);
    ASSERT_EQ(stats.getMax(), 10.0);
    ASSERT_EQ(stats.getCount(), 4u);
    ASSERT_EQ(stats.getMean(), 7.0);
    ASSERT_EQ(stdev * stdev, 5.0);
}

TEST(LatencyStatisticsTest, ShouldReportStats) {
    LatencyStatistics stats{0min};
    stats.addValue(5.0);

    std::this_thread::sleep_for(1us);

    ASSERT_EQ(stats.shouldReport(), true);
}

} // namespace test
} // namespace android