1 /* 2 * Copyright (c) 2016 Google Inc. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you 5 * may not use this file except in compliance with the License. You may 6 * 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 13 * implied. See the License for the specific language governing 14 * permissions and limitations under the License. 15 */ 16 17 package com.android.vts.util; 18 19 import static org.junit.Assert.*; 20 21 import com.android.vts.proto.VtsReportMessage.VtsProfilingRegressionMode; 22 import java.util.Random; 23 import org.junit.Before; 24 import org.junit.Test; 25 26 public class StatSummaryTest { 27 private static double threshold = 0.0000000001; 28 private StatSummary test; 29 30 @Before setUp()31 public void setUp() { 32 test = new StatSummary("label", VtsProfilingRegressionMode.VTS_REGRESSION_MODE_DECREASING); 33 } 34 35 /** Test computation of average. */ 36 @Test testAverage()37 public void testAverage() { 38 int n = 1000; 39 double mean = (n - 1) / 2.0; 40 for (int i = 0; i < n; i++) { 41 test.updateStats(i); 42 } 43 assertEquals(n, test.getCount(), threshold); 44 assertEquals(mean, test.getMean(), threshold); 45 } 46 47 /** Test computation of minimum. */ 48 @Test testMin()49 public void testMin() { 50 double min = Double.MAX_VALUE; 51 int n = 1000; 52 Random rand = new Random(); 53 for (int i = 0; i < n; i++) { 54 double value = rand.nextInt(1000); 55 if (value < min) 56 min = value; 57 test.updateStats(value); 58 } 59 assertEquals(n, test.getCount(), threshold); 60 assertEquals(min, test.getMin(), threshold); 61 } 62 63 /** Test computation of maximum. */ 64 @Test testMax()65 public void testMax() { 66 double max = Double.MIN_VALUE; 67 int n = 1000; 68 Random rand = new Random(); 69 for (int i = 0; i < n; i++) { 70 double value = rand.nextInt(1000); 71 if (value > max) 72 max = value; 73 test.updateStats(value); 74 } 75 assertEquals(max, test.getMax(), threshold); 76 } 77 78 /** Test computation of standard deviation. */ 79 @Test testStd()80 public void testStd() { 81 int n = 1000; 82 double[] values = new double[n]; 83 Random rand = new Random(); 84 double sum = 0.0; 85 for (int i = 0; i < n; i++) { 86 values[i] = rand.nextInt(1000); 87 sum += values[i]; 88 test.updateStats(values[i]); 89 } 90 double mean = sum / n; 91 double sumSq = 0; 92 for (int i = 0; i < n; i++) { 93 sumSq += (values[i] - mean) * (values[i] - mean); 94 } 95 double std = Math.sqrt(sumSq / (n - 1)); 96 assertEquals(std, test.getStd(), threshold); 97 } 98 } 99