1   /*
2    * @(#) TallyTest.java Sep 2, 2003
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.jstats.statistics;
11  
12  import junit.framework.Assert;
13  import junit.framework.TestCase;
14  import nl.tudelft.simulation.event.Event;
15  
16  /***
17   * The TallyTest test the tally.
18   * <p>
19   * (c) copyright 2003-2004 <a href="http://www.simulation.tudelft.nl">Delft
20   * University of Technology </a>, the Netherlands. <br>
21   * See for project information <a
22   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
23   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
24   * License (GPL) </a>, no warranty <br>
25   * 
26   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
27   *         Jacobs </a>
28   * @version 1.6, 2004-03-18
29   * @since 1.2
30   */
31  public class TallyTest extends TestCase
32  {
33  	/*** TEST_METHOD reflects the method which is invoked */
34  	public static final String TEST_METHOD = "test";
35  
36  	/***
37  	 * constructs a new TallyTest
38  	 */
39  	public TallyTest()
40  	{
41  		this(TEST_METHOD);
42  	}
43  
44  	/***
45  	 * constructs a new TallyTest
46  	 * 
47  	 * @param arg0 the name of the method to be tested
48  	 */
49  	public TallyTest(final String arg0)
50  	{
51  		super(arg0);
52  	}
53  
54  	/***
55  	 * tests the tally
56  	 */
57  	public void test()
58  	{
59  		String description = "THIS TALLY IS TESTED";
60  		Tally tally = new Tally(description);
61  
62  		//check the description
63  		Assert.assertTrue(tally.toString().equals(description));
64  
65  		//now we check the initial values
66  		Assert.assertTrue(new Double(tally.getMin()).isNaN());
67  		Assert.assertTrue(new Double(tally.getMax()).isNaN());
68  		Assert.assertTrue(new Double(tally.getSampleMean()).isNaN());
69  		Assert.assertTrue(new Double(tally.getSampleVariance()).isNaN());
70  		Assert.assertTrue(new Double(tally.getStdDev()).isNaN());
71  		Assert.assertTrue(new Double(tally.getSum()).isNaN());
72  		Assert.assertTrue(tally.getN() == Long.MIN_VALUE);
73  		Assert.assertTrue(tally.getConfidenceInterval(0.95) == null);
74  		Assert.assertTrue(tally.getConfidenceInterval(0.95,
75  				Tally.LEFT_SIDE_CONFIDENCE) == null);
76  
77  		//now we initialize the tally
78  		tally.initialize();
79  
80  		//now we check wether all the properties are correct
81  		Assert.assertTrue(tally.getMin() == Double.MAX_VALUE);
82  		Assert.assertTrue(tally.getMax() == -Double.MAX_VALUE);
83  		Assert.assertTrue(new Double(tally.getSampleMean()).isNaN());
84  		Assert.assertTrue(new Double(tally.getSampleVariance()).isNaN());
85  		Assert.assertTrue(new Double(tally.getStdDev()).isNaN());
86  		Assert.assertTrue(tally.getSum() == 0);
87  		Assert.assertTrue(tally.getN() == 0);
88  		Assert.assertTrue(tally.getConfidenceInterval(0.95) == null);
89  		Assert.assertTrue(tally.getConfidenceInterval(0.95,
90  				Tally.LEFT_SIDE_CONFIDENCE) == null);
91  
92  		//We first fire a wrong event
93  		try
94  		{
95  			tally.notify(new Event(null, "ERROR", "ERROR"));
96  			Assert
97  					.fail("tally should react on events.value !instanceOf Double");
98  		} catch (Exception exception)
99  		{
100 			Assert.assertNotNull(exception);
101 		}
102 
103 		//Now we fire some events
104 		try
105 		{
106 			tally.notify(new Event(null, this, new Double(1.0)));
107 			tally.notify(new Event(null, this, new Double(1.1)));
108 			tally.notify(new Event(null, this, new Double(1.2)));
109 			tally.notify(new Event(null, this, new Double(1.3)));
110 			tally.notify(new Event(null, this, new Double(1.4)));
111 			tally.notify(new Event(null, this, new Double(1.5)));
112 			tally.notify(new Event(null, this, new Double(1.6)));
113 			tally.notify(new Event(null, this, new Double(1.7)));
114 			tally.notify(new Event(null, this, new Double(1.8)));
115 			tally.notify(new Event(null, this, new Double(1.9)));
116 			tally.notify(new Event(null, this, new Double(2.0)));
117 		} catch (Exception exception)
118 		{
119 			Assert.fail(exception.getMessage());
120 		}
121 
122 		//Now we check the tally
123 		Assert.assertTrue(tally.getMax() == 2.0);
124 		Assert.assertTrue(tally.getMin() == 1.0);
125 		Assert.assertTrue(tally.getN() == 11);
126 		Assert.assertTrue(tally.getSum() == 16.5);
127 		double mean = Math.round(1000 * tally.getSampleMean()) / 1000.0;
128 		Assert.assertTrue(mean == 1.5);
129 		double variance = Math.round(1000 * tally.getSampleVariance()) / 1000.0;
130 		Assert.assertTrue(variance == 0.11);
131 		double stdv = Math.round(1000 * tally.getStdDev()) / 1000.0;
132 		Assert.assertTrue(stdv == 0.332);
133 		double confidence = Math
134 				.round(1000 * tally.getConfidenceInterval(0.05)[0]) / 1000.0;
135 		Assert.assertTrue(confidence == 1.304);
136 
137 		//we check the input of the confidence interval
138 		try
139 		{
140 			tally.getConfidenceInterval(0.95, (short) 14);
141 			Assert.fail("14 is not defined as side of confidence level");
142 		} catch (Exception exception)
143 		{
144 			Assert.assertTrue(exception.getClass().equals(
145 					IllegalArgumentException.class));
146 		}
147 		try
148 		{
149 			Assert.assertTrue(tally.getConfidenceInterval(-0.95) == null);
150 			Assert.assertTrue(tally.getConfidenceInterval(1.14) == null);
151 			Assert.fail("should have reacted on wrong confidence levels");
152 		} catch (Exception exception)
153 		{
154 			Assert.assertTrue(exception.getClass().equals(
155 					IllegalArgumentException.class));
156 		}
157 
158 		Assert.assertTrue(Math.abs(tally.getSampleMean() - 1.5) < 10E-6);
159 
160 		//Let's compute the standard deviation
161 		variance = 0;
162 		for (int i = 0; i < 11; i++)
163 		{
164 			variance = Math.pow(1.5 - (1.0 + i / 10.0), 2) + variance;
165 		}
166 		variance = variance / 10.0;
167 		double stDev = Math.sqrt(variance);
168 
169 		Assert
170 				.assertTrue(Math.abs(tally.getSampleVariance() - variance) < 10E-6);
171 		Assert.assertTrue(Math.abs(tally.getStdDev() - stDev) < 10E-6);
172 	}
173 }