1 /* 2 * Copyright (C) 2014 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 package com.android.compatibility.common.util; 18 19 import com.android.compatibility.common.util.Stat; 20 import junit.framework.TestCase; 21 22 /** 23 * Unit tests for the {@link Stat} class. 24 */ 25 public class StatTest extends TestCase { 26 27 /** 28 * Test {@link Stat#get95PercentileValue(double[])}. 29 */ testGet95PercentileValue()30 public void testGet95PercentileValue() { 31 double[] values = new double[100]; 32 for (int i = 0; i < 100; i++) { 33 values[i] = i; 34 } 35 assertEquals(95, (int) Stat.get95PercentileValue(values)); 36 37 values = new double[1000]; 38 for (int i = 0; i < 1000; i++) { 39 values[i] = i; 40 } 41 assertEquals(950, (int) Stat.get95PercentileValue(values)); 42 43 values = new double[100]; 44 for (int i = 0; i < 100; i++) { 45 values[i] = i * i; 46 } 47 assertEquals(95 * 95, (int) Stat.get95PercentileValue(values)); 48 } 49 50 /** 51 * Test {@link Stat#getAverage(double[])}. 52 */ testGetAverage()53 public void testGetAverage() { 54 double[] values = new double[]{0, 1, 2, 3, 4}; 55 double average = Stat.getAverage(values); 56 assertEquals(2.0, average, 0.00001); 57 58 values = new double[]{1, 2, 3, 4, 5}; 59 average = Stat.getAverage(values); 60 assertEquals(3.0, average, 0.00001); 61 62 values = new double[]{0, 1, 4, 9, 16}; 63 average = Stat.getAverage(values); 64 assertEquals(6.0, average, 0.00001); 65 } 66 67 /** 68 * Test standard deviation. 69 */ testGetStandardDeviation()70 public void testGetStandardDeviation() { 71 double[] values = new double[]{0, 1, 2, 3, 4}; 72 double stddev = Stat.getStat(values).mStddev; 73 assertEquals(Math.sqrt(2.5), stddev, 0.00001); 74 75 values = new double[]{1, 2, 3, 4, 5}; 76 stddev = Stat.getStat(values).mStddev; 77 assertEquals(Math.sqrt(2.5), stddev, 0.00001); 78 79 values = new double[]{0, 2, 4, 6, 8}; 80 stddev = Stat.getStat(values).mStddev; 81 assertEquals(Math.sqrt(10.0), stddev, 0.00001); 82 } 83 84 85 } 86