001: /*
002: * Created on Mar 18, 2004
003: *
004: * To change the template for this generated file go to
005: * Window - Preferences - Java - Code Generation - Code and Comments
006: */
007: package org.hammurapi.inspectors.metrics.statistics.tests;
008:
009: import junit.framework.Test;
010: import junit.framework.TestCase;
011: import junit.framework.TestSuite;
012:
013: import org.hammurapi.inspectors.metrics.statistics.DescriptiveStatistic;
014: import org.hammurapi.inspectors.metrics.statistics.IntVector;
015:
016: /**
017: * @author mucbj0
018: *
019: * To change the template for this generated type comment go to Window -
020: * Preferences - Java - Code Generation - Code and Comments
021: */
022: public class DescriptiveStatTest extends TestCase {
023: /**
024: * The suite() method.
025: */
026: public static Test suite() {
027: //
028: return new TestSuite(DescriptiveStatTest.class);
029: }// End o
030:
031: /**
032: * This method is the starting point of the test programe
033: *
034: * @param strArgsArr
035: * The command line parameters passed.
036: * @exception Exception
037: * A Exception object.
038: */
039: public static void main(String[] strArgsArr) throws Exception {
040: junit.textui.TestRunner.run(suite());
041: }// End of main
042:
043: public void testFrequency1() {
044: IntVector sortedData = new IntVector();
045: sortedData.addElement(1);
046: sortedData.addElement(1);
047: sortedData.addElement(1);
048: sortedData.addElement(3);
049: sortedData.addElement(3);
050: sortedData.addElement(5);
051: sortedData.addElement(5);
052: sortedData.addElement(7);
053:
054: IntVector distinctValues = new IntVector();
055: IntVector frequencies = new IntVector();
056: new DescriptiveStatistic().frequencies(sortedData,
057: distinctValues, frequencies);
058: System.out.println(frequencies);
059: assertTrue("", frequencies.elementAt(0) == 3);
060: assertTrue("", frequencies.elementAt(1) == 2);
061: assertTrue("", frequencies.elementAt(2) == 2);
062: assertTrue("", frequencies.elementAt(3) == 1);
063: }
064:
065: public void testFrequency2() {
066: IntVector sortedData = new IntVector();
067: sortedData.addElement(1);
068: sortedData.addElement(1);
069: sortedData.addElement(1);
070: sortedData.addElement(1);
071: sortedData.addElement(1);
072: sortedData.addElement(1);
073: sortedData.addElement(1);
074: sortedData.addElement(1);
075:
076: IntVector distinctValues = new IntVector();
077: IntVector frequencies = new IntVector();
078: new DescriptiveStatistic().frequencies(sortedData,
079: distinctValues, frequencies);
080: System.out.println(frequencies);
081: assertTrue("", frequencies.size() == 1);
082: assertTrue("", frequencies.elementAt(0) == 8);
083:
084: }
085:
086: public void testFrequencyEmptyList() {
087: IntVector sortedData = new IntVector();
088:
089: IntVector distinctValues = new IntVector();
090: IntVector frequencies = new IntVector();
091: new DescriptiveStatistic().frequencies(sortedData,
092: distinctValues, frequencies);
093: System.out.println(frequencies);
094: assertTrue("", frequencies.isEmpty());
095:
096: }
097:
098: public void testAggregateData() {
099: // int[] numbers = { 26, 26, 27, 27, 27, 32, 42, 42, 42, 42, 43, 43, 43, 43, 43, 44, 44, 45, 45, 46, 46, 48, 49, 51, 52, 53, 54, 54, 55, 61, 65, 66, 67, 68, 69, 71, 72, 74, 74, 74, 82, 89, 103, 106, 108, 112, 113, 127, 139, 140, 145, 150, 161, 175, 179, 183, 202, 211, 256, 269, 276, 286, 302, 309, 324, 346, 387, 684, 720, 1228, 1229, 2569 };
100: int[] numbers = { 26, 26, 26, 26, 26, 42, 65, 175, 387, 684,
101: 720, 1228, 1229, 2569 };
102: IntVector distribution = new IntVector();
103: IntVector distinctValues = new IntVector();
104: IntVector frequencies = new IntVector();
105: for (int i = 0; i < numbers.length; i++) {
106: int index = numbers[i];
107: // System.out.println( index + " -> "+index % 100);
108: int piv = (index / 50) * 50;
109: System.out.println(index + " -> " + piv);
110: distribution.addElement(piv);
111: }
112: System.out
113: .println("-------------------------------------------");
114:
115: new DescriptiveStatistic().frequencies(distribution,
116: distinctValues, frequencies);
117: assertTrue("frequencies.size() is " + frequencies.size(),
118: frequencies.size() == 8);
119: assertTrue("", frequencies.elementAt(0) == 6);
120: assertTrue("", frequencies.elementAt(7) == 1);
121: assertTrue("distinctValues.size is " + distinctValues.size(),
122: distinctValues.size() == 8);
123: assertTrue("", distinctValues.elementAt(0) == 0);
124: assertTrue("", distinctValues.elementAt(7) == 2550);
125:
126: //System.out.println( distribution );
127: //System.out.println( distinctValues );
128: //System.out.println( frequencies );
129:
130: }
131:
132: public void testSortIntVector() {
133: // int[] numbers = { 26, 26, 27, 27, 27, 32, 42, 42, 42, 42, 43, 43, 43, 43, 43, 44, 44, 45, 45, 46, 46, 48, 49, 51, 52, 53, 54, 54, 55, 61, 65, 66, 67, 68, 69, 71, 72, 74, 74, 74, 82, 89, 103, 106, 108, 112, 113, 127, 139, 140, 145, 150, 161, 175, 179, 183, 202, 211, 256, 269, 276, 286, 302, 309, 324, 346, 387, 684, 720, 1228, 1229, 2569 };
134: int[] numbers = { 684, 26, 26, 720, 1228, 26, 65, 175, 387,
135: 1229, 2569, 26, 26, 42 };
136: IntVector distribution = new IntVector(numbers);
137: // System.out.println( distribution );
138: distribution.sort();
139: // System.out.println( distribution );
140: assertTrue("distribution.size is " + distribution.size(),
141: distribution.size() == 14);
142: assertTrue("", distribution.elementAt(0) == 26);
143: assertTrue("", distribution.elementAt(13) == 2569);
144: }
145:
146: public void testSumIntVector1() {
147: IntVector sortedData = new IntVector();
148: sortedData.addElement(1);
149: sortedData.addElement(1);
150: sortedData.addElement(1);
151: sortedData.addElement(3);
152: sortedData.addElement(3);
153: sortedData.addElement(5);
154: sortedData.addElement(5);
155: sortedData.addElement(7);
156: int sum = new DescriptiveStatistic().sum(sortedData);
157: assertTrue("", sum == 26);
158:
159: }
160:
161: public void testSumIntVector2() {
162: IntVector sortedData = new IntVector();
163: sortedData.addElement(1);
164: sortedData.addElement(1);
165: sortedData.addElement(1);
166: sortedData.addElement(3);
167: sortedData.addElement(3);
168: sortedData.addElement(5);
169: sortedData.addElement(5);
170: sortedData.addElement(7);
171: sortedData.addElement(1000);
172: int sum = new DescriptiveStatistic().sum(sortedData);
173: assertTrue("", sum == 1026);
174:
175: }
176:
177: public void testMeanIntVector1() {
178: IntVector sortedData = new IntVector();
179: sortedData.addElement(1);
180: sortedData.addElement(1);
181: sortedData.addElement(1);
182: sortedData.addElement(3);
183: sortedData.addElement(3);
184: sortedData.addElement(5);
185: sortedData.addElement(5);
186: sortedData.addElement(7);
187: double mean = new DescriptiveStatistic().mean(sortedData);
188: assertTrue("Mean is not xx but " + mean, mean == 3.0);
189: }
190:
191: public void testMeanEmpytyIntVector() {
192: IntVector sortedData = new IntVector();
193: double mean = new DescriptiveStatistic().mean(sortedData);
194: assertTrue("Mean is not xx but " + mean, mean == 0.0);
195: }
196:
197: public void testMeanIntVector2() {
198: IntVector sortedData = new IntVector();
199: sortedData.addElement(1);
200: sortedData.addElement(1);
201: sortedData.addElement(1);
202: sortedData.addElement(3);
203: sortedData.addElement(3);
204: sortedData.addElement(5);
205: sortedData.addElement(5);
206: sortedData.addElement(7);
207: sortedData.addElement(1000);
208: double mean = new DescriptiveStatistic().mean(sortedData);
209: assertTrue("Mean is not xx but " + mean, mean == 114.0);
210:
211: }
212: }
|