1 /* 2 * Copyright (C) 2011 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 package com.android.tradefed.util; 17 18 import junit.framework.TestCase; 19 20 /** 21 * Unit tests for {@link SimpleStats} 22 */ 23 public class SimpleStatsTest extends TestCase { 24 private SimpleStats mStats = null; 25 26 /** 27 * {@inheritDoc} 28 */ 29 @Override 30 public void setUp() throws Exception { 31 mStats = new SimpleStats(); 32 } 33 34 /** 35 * Make sure that the class behaves as expected when the dataset is empty 36 */ 37 public void testStats_empty() { 38 assertTrue(mStats.isEmpty()); 39 assertEquals(0, mStats.size()); 40 assertNull(mStats.mean()); 41 assertNull(mStats.median()); 42 assertNull(mStats.min()); 43 assertNull(mStats.max()); 44 assertNull(mStats.stdev()); 45 } 46 47 /** 48 * Make sure that the class behaves as expected with an even count of uniformly-distributed 49 * measurements. 50 * <p /> 51 * The even/odd distinction applies mainly to the median (which uses a different calculation 52 * depending on whether the "center" element is unique or not). 53 */ 54 public void testStats_evenCount() { 55 // [1, 10] 56 for (int i = 1; i <= 10; ++i) { 57 mStats.add(i); 58 } 59 assertEquals(10, mStats.size()); 60 assertEquals(1, mStats.min(), 0.1); 61 assertEquals(10, mStats.max(), 0.1); 62 assertEquals(5.5, mStats.mean(), 0.1); 63 assertEquals(5.5, mStats.median(), 0.1); 64 assertEquals(2.872281, mStats.stdev(), 0.000001); 65 } 66 67 /** 68 * Make sure that the class behaves as expected with an even count of uniformly-distributed 69 * measurements. 70 * <p /> 71 * The even/odd distinction applies mainly to the median (which uses a different calculation 72 * depending on whether the "center" element is unique or not). 73 */ 74 public void testStats_oddCount() { 75 // [0, 10] 76 for (int i = 0; i <= 10; ++i) { 77 mStats.add(i); 78 } 79 assertEquals(11, mStats.size()); 80 assertEquals(0, mStats.min(), 0.1); 81 assertEquals(10, mStats.max(), 0.1); 82 assertEquals(5.0, mStats.mean(), 0.1); 83 assertEquals(5.0, mStats.median(), 0.1); 84 assertEquals(3.162278, mStats.stdev(), 0.000001); 85 } 86 87 /** 88 * The skewed distribution will cause mean and median to diverge 89 */ 90 public void testStats_skewedMedian() { 91 for (int i = 1; i <= 5; ++i) { 92 for (int j = 1; j <= i; ++j) { 93 mStats.add(i); 94 } 95 } 96 97 // 1 + 2 + 3 + 4 + 5 = 15 elements 98 assertEquals(15, mStats.size()); 99 assertEquals(1, mStats.min(), 0.1); 100 assertEquals(5, mStats.max(), 0.1); 101 // sum = 55, count = 15 102 assertEquals(55.0 / 15.0, mStats.mean(), 0.1); 103 assertEquals(4, mStats.median(), 0.1); 104 assertEquals(1.247219, mStats.stdev(), 0.000001); 105 } 106 } 107 108