001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.common.statistic; 017 018import static org.hamcrest.core.Is.is; 019import static org.junit.Assert.assertEquals; 020import static org.junit.Assert.assertThat; 021import static org.junit.Assert.assertTrue; 022import org.junit.Test; 023import org.modeshape.common.math.FloatOperations; 024import org.modeshape.common.math.IntegerOperations; 025 026public class SimpleStatisticsTest { 027 028 private SimpleStatistics<Integer> intStats = new SimpleStatistics<Integer>(new IntegerOperations()); 029 private SimpleStatistics<Float> floatStats = new SimpleStatistics<Float>(new FloatOperations()); 030 031 // private Logger logger = Logger.getLogger(SimpleStatisticsTest.class); 032 033 @Test 034 public void shouldHaveValidValuesWhenUnused() { 035 assertThat(this.intStats.getCount(), is(0)); 036 assertThat(this.intStats.getMinimum(), is(0)); 037 assertThat(this.intStats.getMaximum(), is(0)); 038 assertThat(this.intStats.getMean(), is(0)); 039 } 040 041 @Test 042 public void shouldCorrectStatisitcValuesWhenUnusedOnce() { 043 this.intStats.add(10); 044 assertThat(this.intStats.getCount(), is(1)); 045 assertThat(this.intStats.getMinimum(), is(10)); 046 assertThat(this.intStats.getMaximum(), is(10)); 047 assertThat(this.intStats.getMeanValue(), is(10.0d)); 048 } 049 050 @Test 051 public void shouldCorrectStatisitcValuesWhenUsedAnOddNumberOfTimesButMoreThanOnce() { 052 this.intStats.add(1); 053 this.intStats.add(2); 054 this.intStats.add(3); 055 assertThat(this.intStats.getCount(), is(3)); 056 assertThat(this.intStats.getMinimum(), is(1)); 057 assertThat(this.intStats.getMaximum(), is(3)); 058 assertThat(this.intStats.getMeanValue(), is(2.0d)); 059 } 060 061 @Test 062 public void shouldCorrectStatisitcValuesWhenUsedAnEvenNumberOfTimes() { 063 this.intStats.add(2); 064 this.intStats.add(4); 065 this.intStats.add(1); 066 this.intStats.add(3); 067 assertThat(this.intStats.getCount(), is(4)); 068 assertThat(this.intStats.getMinimum(), is(1)); 069 assertThat(this.intStats.getMaximum(), is(4)); 070 assertThat(this.intStats.getMeanValue(), is(2.5d)); 071 } 072 073 @Test 074 public void shouldCorrectStatisitcValuesWhenAllValuesAreTheSame() { 075 this.intStats.add(2); 076 this.intStats.add(2); 077 this.intStats.add(2); 078 this.intStats.add(2); 079 assertThat(this.intStats.getCount(), is(4)); 080 assertThat(this.intStats.getMinimum(), is(2)); 081 assertThat(this.intStats.getMaximum(), is(2)); 082 assertThat(this.intStats.getMeanValue(), is(2.0d)); 083 } 084 085 @Test 086 public void shouldCorrectStatisitcValuesForComplexIntegerData() { 087 this.intStats.add(19); 088 this.intStats.add(10); 089 this.intStats.add(20); 090 this.intStats.add(7); 091 this.intStats.add(73); 092 this.intStats.add(72); 093 this.intStats.add(42); 094 this.intStats.add(9); 095 this.intStats.add(47); 096 this.intStats.add(24); 097 System.out.println(this.intStats); 098 assertThat(this.intStats.getCount(), is(10)); 099 assertThat(this.intStats.getMinimum(), is(7)); 100 assertThat(this.intStats.getMaximum(), is(73)); 101 assertEquals(32.3d, this.intStats.getMeanValue(), 0.0001d); 102 } 103 104 @Test 105 public void shouldCorrectStatisitcValuesForComplexFloatData() { 106 this.floatStats.add(1.9f); 107 this.floatStats.add(1.0f); 108 this.floatStats.add(2.0f); 109 this.floatStats.add(0.7f); 110 this.floatStats.add(7.3f); 111 this.floatStats.add(7.2f); 112 this.floatStats.add(4.2f); 113 this.floatStats.add(0.9f); 114 this.floatStats.add(4.7f); 115 this.floatStats.add(2.4f); 116 System.out.println(this.floatStats); 117 assertThat(this.floatStats.getCount(), is(10)); 118 assertThat(this.floatStats.getMinimum(), is(0.7f)); 119 assertThat(this.floatStats.getMaximum(), is(7.3f)); 120 assertEquals(3.23f, this.floatStats.getMeanValue(), 0.0001f); 121 } 122 123 @Test 124 public void shouldHaveNoStatisticValuesAfterUnusedAndReset() { 125 this.intStats.add(19); 126 this.intStats.add(10); 127 this.intStats.add(20); 128 assertThat(this.intStats.getCount(), is(3)); 129 this.intStats.reset(); 130 assertThat(this.intStats.getCount(), is(0)); 131 assertThat(this.intStats.getMinimum(), is(0)); 132 assertThat(this.intStats.getMaximum(), is(0)); 133 assertThat(this.intStats.getMean(), is(0)); 134 } 135 136 @Test 137 public void shouldHaveStringRepresentationWithoutStatisticsForSingleSample() { 138 this.intStats.add(19); 139 String str = this.intStats.toString(); 140 System.out.println(str); 141 assertTrue(str.matches("1 sample.*")); 142 assertTrue(str.matches(".*min=\\d{1,5}.*")); 143 assertTrue(str.matches(".*max=\\d{1,5}.*")); 144 assertTrue(str.matches(".*avg=\\d{1,5}.*")); 145 } 146 147 @Test 148 public void shouldHaveStringRepresentationWithStatisticsForMultipleSample() { 149 this.intStats.add(19); 150 this.intStats.add(10); 151 this.intStats.add(20); 152 String str = this.intStats.toString(); 153 System.out.println(str); 154 assertTrue(str.matches("^\\d{1,5}.*")); 155 assertTrue(str.matches(".*3 samples.*")); 156 assertTrue(str.matches(".*min=\\d{1,5}.*")); 157 assertTrue(str.matches(".*max=\\d{1,5}.*")); 158 assertTrue(str.matches(".*avg=\\d{1,5}.*")); 159 } 160 161}