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.cts.util; 18 19 import junit.framework.TestCase; 20 21 import java.util.Arrays; 22 import java.util.Collection; 23 import java.util.HashSet; 24 import java.util.List; 25 import java.util.concurrent.TimeUnit; 26 27 /** 28 * Unit tests for the {@link StatisticsUtils} class. 29 */ 30 public class StatisticsUtilsTest extends TestCase { 31 32 /** 33 * Test {@link StatisticsUtils#get95PercentileValue(Collection)}. 34 */ testGet95PercentileValue()35 public void testGet95PercentileValue() { 36 Collection<Integer> values = new HashSet<Integer>(); 37 for (int i = 0; i < 100; i++) { 38 values.add(i); 39 } 40 assertEquals(95, (int) StatisticsUtils.get95PercentileValue(values)); 41 42 values = new HashSet<Integer>(); 43 for (int i = 0; i < 1000; i++) { 44 values.add(i); 45 } 46 assertEquals(950, (int) StatisticsUtils.get95PercentileValue(values)); 47 48 values = new HashSet<Integer>(); 49 for (int i = 0; i < 100; i++) { 50 values.add(i * i); 51 } 52 assertEquals(95 * 95, (int) StatisticsUtils.get95PercentileValue(values)); 53 } 54 55 /** 56 * Test {@link StatisticsUtils#getMean(Collection)}. 57 */ testGetMean()58 public void testGetMean() { 59 List<Integer> values = Arrays.asList(0, 1, 2, 3, 4); 60 double mean = StatisticsUtils.getMean(values); 61 assertEquals(2.0, mean, 0.00001); 62 63 values = Arrays.asList(1, 2, 3, 4, 5); 64 mean = StatisticsUtils.getMean(values); 65 assertEquals(3.0, mean, 0.00001); 66 67 values = Arrays.asList(0, 1, 4, 9, 16); 68 mean = StatisticsUtils.getMean(values); 69 assertEquals(6.0, mean, 0.00001); 70 } 71 72 /** 73 * Test {@link StatisticsUtils#getVariance(Collection)}. 74 */ testGetVariance()75 public void testGetVariance() { 76 List<Integer> values = Arrays.asList(0, 1, 2, 3, 4); 77 double variance = StatisticsUtils.getVariance(values); 78 assertEquals(2.5, variance, 0.00001); 79 80 values = Arrays.asList(1, 2, 3, 4, 5); 81 variance = StatisticsUtils.getVariance(values); 82 assertEquals(2.5, variance, 0.00001); 83 84 values = Arrays.asList(0, 2, 4, 6, 8); 85 variance = StatisticsUtils.getVariance(values); 86 assertEquals(10.0, variance, 0.00001); 87 } 88 89 /** 90 * Test {@link StatisticsUtils#getStandardDeviation(Collection)}. 91 */ testGetStandardDeviation()92 public void testGetStandardDeviation() { 93 List<Integer> values = Arrays.asList(0, 1, 2, 3, 4); 94 double stddev = StatisticsUtils.getStandardDeviation(values); 95 assertEquals(Math.sqrt(2.5), stddev, 0.00001); 96 97 values = Arrays.asList(1, 2, 3, 4, 5); 98 stddev = StatisticsUtils.getStandardDeviation(values); 99 assertEquals(Math.sqrt(2.5), stddev, 0.00001); 100 101 values = Arrays.asList(0, 2, 4, 6, 8); 102 stddev = StatisticsUtils.getStandardDeviation(values); 103 assertEquals(Math.sqrt(10.0), stddev, 0.00001); 104 } 105 106 107 } 108