| Atif Niyaz | 8384682 | 2019-07-18 15:17:40 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2019 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #include <gtest/gtest.h> | 
|  | 18 | #include <input/LatencyStatistics.h> | 
|  | 19 | #include <cmath> | 
|  | 20 | #include <limits> | 
| Atif Niyaz | 6740e66 | 2019-08-07 10:54:29 -0700 | [diff] [blame] | 21 | #include <thread> | 
| Atif Niyaz | 8384682 | 2019-07-18 15:17:40 -0700 | [diff] [blame] | 22 |  | 
|  | 23 | namespace android { | 
|  | 24 | namespace test { | 
|  | 25 |  | 
|  | 26 | TEST(LatencyStatisticsTest, ResetStats) { | 
|  | 27 | LatencyStatistics stats{5min}; | 
|  | 28 | stats.addValue(5.0); | 
|  | 29 | stats.addValue(19.3); | 
|  | 30 | stats.addValue(20); | 
|  | 31 | stats.reset(); | 
|  | 32 |  | 
|  | 33 | ASSERT_EQ(stats.getCount(), 0u); | 
|  | 34 | ASSERT_EQ(std::isnan(stats.getStDev()), true); | 
|  | 35 | ASSERT_EQ(std::isnan(stats.getMean()), true); | 
|  | 36 | } | 
|  | 37 |  | 
|  | 38 | TEST(LatencyStatisticsTest, AddStatsValue) { | 
|  | 39 | LatencyStatistics stats{5min}; | 
|  | 40 | stats.addValue(5.0); | 
|  | 41 |  | 
|  | 42 | ASSERT_EQ(stats.getMin(), 5.0); | 
|  | 43 | ASSERT_EQ(stats.getMax(), 5.0); | 
|  | 44 | ASSERT_EQ(stats.getCount(), 1u); | 
|  | 45 | ASSERT_EQ(stats.getMean(), 5.0); | 
|  | 46 | ASSERT_EQ(stats.getStDev(), 0.0); | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | TEST(LatencyStatisticsTest, AddMultipleStatsValue) { | 
|  | 50 | LatencyStatistics stats{5min}; | 
|  | 51 | stats.addValue(4.0); | 
|  | 52 | stats.addValue(6.0); | 
|  | 53 | stats.addValue(8.0); | 
|  | 54 | stats.addValue(10.0); | 
|  | 55 |  | 
|  | 56 | float stdev = stats.getStDev(); | 
|  | 57 |  | 
|  | 58 | ASSERT_EQ(stats.getMin(), 4.0); | 
|  | 59 | ASSERT_EQ(stats.getMax(), 10.0); | 
|  | 60 | ASSERT_EQ(stats.getCount(), 4u); | 
|  | 61 | ASSERT_EQ(stats.getMean(), 7.0); | 
|  | 62 | ASSERT_EQ(stdev * stdev, 5.0); | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | TEST(LatencyStatisticsTest, ShouldReportStats) { | 
|  | 66 | LatencyStatistics stats{0min}; | 
|  | 67 | stats.addValue(5.0); | 
|  | 68 |  | 
| Atif Niyaz | 6740e66 | 2019-08-07 10:54:29 -0700 | [diff] [blame] | 69 | std::this_thread::sleep_for(1us); | 
|  | 70 |  | 
| Atif Niyaz | 8384682 | 2019-07-18 15:17:40 -0700 | [diff] [blame] | 71 | ASSERT_EQ(stats.shouldReport(), true); | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | } // namespace test | 
|  | 75 | } // namespace android |