• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.assertEquals;
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) min = value;
56             test.updateStats(value);
57         }
58         assertEquals(n, test.getCount(), threshold);
59         assertEquals(min, test.getMin(), threshold);
60     }
61 
62     /** Test computation of maximum. */
63     @Test
testMax()64     public void testMax() {
65         double max = Double.MIN_VALUE;
66         int n = 1000;
67         Random rand = new Random();
68         for (int i = 0; i < n; i++) {
69             double value = rand.nextInt(1000);
70             if (value > max) max = value;
71             test.updateStats(value);
72         }
73         assertEquals(max, test.getMax(), threshold);
74     }
75 
76     /** Test computation of standard deviation. */
77     @Test
testStd()78     public void testStd() {
79         int n = 1000;
80         double[] values = new double[n];
81         Random rand = new Random();
82         double sum = 0.0;
83         for (int i = 0; i < n; i++) {
84             values[i] = rand.nextInt(1000);
85             sum += values[i];
86             test.updateStats(values[i]);
87         }
88         double mean = sum / n;
89         double sumSq = 0;
90         for (int i = 0; i < n; i++) {
91             sumSq += (values[i] - mean) * (values[i] - mean);
92         }
93         double std = Math.sqrt(sumSq / (n - 1));
94         assertEquals(std, test.getStd(), threshold);
95     }
96 
97     /** Test computation of standard deviation. */
98     @Test
testMerge()99     public void testMerge() {
100         StatSummary test2 =
101                 new StatSummary("label", VtsProfilingRegressionMode.VTS_REGRESSION_MODE_DECREASING);
102         StatSummary all =
103                 new StatSummary("label", VtsProfilingRegressionMode.VTS_REGRESSION_MODE_DECREASING);
104         int n1 = 1000;
105         int n2 = 2000;
106         Random rand = new Random();
107         for (int i = 0; i < n1; i++) {
108             int value = rand.nextInt(1000);
109             test.updateStats(value);
110             all.updateStats(value);
111         }
112         for (int i = 0; i < n2; i++) {
113             int value = rand.nextInt(1000);
114             test2.updateStats(value);
115             all.updateStats(value);
116         }
117 
118         test.merge(test2);
119         assertEquals(all.getCount(), test.getCount());
120         assertEquals(all.getStd(), test.getStd(), threshold);
121         assertEquals(all.getMean(), test.getMean(), threshold);
122     }
123 }
124