1/* 2 * Copyright (C) 2016 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 17package libcore.java.util; 18 19import java.util.IntSummaryStatistics; 20 21public class IntSummaryStatisticsTest extends junit.framework.TestCase { 22 23 private static final int data1[] = {2, -5, 7, -1, 1, 0, 100}; 24 private static final int data2[] = {1, 3, 2, 1, 7}; 25 26 public void test_empty() { 27 IntSummaryStatistics iss = new IntSummaryStatistics(); 28 assertEquals(0, iss.getCount()); 29 assertEquals(0, iss.getSum()); 30 assertEquals(0.0d, iss.getAverage()); 31 assertEquals(Integer.MAX_VALUE, iss.getMin()); 32 assertEquals(Integer.MIN_VALUE, iss.getMax()); 33 } 34 35 public void test_accept() { 36 IntSummaryStatistics iss = new IntSummaryStatistics(); 37 iss.accept(5); 38 assertEquals(1, iss.getCount()); 39 assertEquals(5, iss.getSum()); 40 iss.accept(10); 41 assertEquals(2, iss.getCount()); 42 assertEquals(15, iss.getSum()); 43 } 44 45 public void test_combine() { 46 IntSummaryStatistics iss1 = getIntSummaryStatisticsData2(); 47 IntSummaryStatistics issCombined = getIntSummaryStatisticsData1(); 48 issCombined.combine(iss1); 49 50 assertEquals(12, issCombined.getCount()); 51 assertEquals(118, issCombined.getSum()); 52 assertEquals(100, issCombined.getMax()); 53 assertEquals(-5, issCombined.getMin()); 54 assertEquals(9.833333d, issCombined.getAverage(), 1E-6); 55 } 56 57 public void test_getCount() { 58 IntSummaryStatistics iss1 = getIntSummaryStatisticsData1(); 59 assertEquals(data1.length, iss1.getCount()); 60 } 61 62 public void test_getSum() { 63 IntSummaryStatistics iss1 = getIntSummaryStatisticsData1(); 64 assertEquals(104, iss1.getSum()); 65 } 66 67 public void test_getMin() { 68 IntSummaryStatistics iss1 = getIntSummaryStatisticsData1(); 69 assertEquals(-5, iss1.getMin()); 70 } 71 72 public void test_getMax() { 73 IntSummaryStatistics iss1 = getIntSummaryStatisticsData1(); 74 assertEquals(100, iss1.getMax()); 75 } 76 77 public void test_getAverage() { 78 IntSummaryStatistics iss1 = getIntSummaryStatisticsData1(); 79 assertEquals(14.857142, iss1.getAverage(), 1E-6); 80 } 81 82 private static IntSummaryStatistics getIntSummaryStatisticsData1() { 83 IntSummaryStatistics iss = new IntSummaryStatistics(); 84 for (int value : data1) { 85 iss.accept(value); 86 } 87 return iss; 88 } 89 90 private static IntSummaryStatistics getIntSummaryStatisticsData2() { 91 IntSummaryStatistics iss = new IntSummaryStatistics(); 92 for (int value : data2) { 93 iss.accept(value); 94 } 95 return iss; 96 } 97} 98