KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > util > TestHashcode


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.util;
11
12 import java.math.*;
13 import java.util.*;
14
15 /**
16  * Class used to help in the testing of the hashCode() method.
17  *
18  * @author John Serri
19  * @since 2.1
20  */

21 public class TestHashcode {
22   /** String containing the CVS revision. Read out via reflection! */
23   private static final String JavaDoc CVS_REVISION = "$Revision: 1.7 $";
24
25   /**
26    * Determines if object should output debug information.
27    */

28   private boolean m_verbose;
29
30   /**
31    * Minimum average value of hashCodes needed to pass the testDispersion()
32    * test
33    *
34    * @see TestHashcode#testDispersion, m_AverageMax
35    */

36   private double m_AverageMin = 0.0d;
37
38   /**
39    * Maximum average value of hashCodes needed to pass the testDispersion()
40    * test
41    *
42    * @see TestHashcode#testDispersion, m_AverageMin
43    */

44   private double m_AverageMax = 1.0d;
45
46   /**
47    * Minimum Standard Deviation value of hashCodes needed to pass the
48    * testDispersion() test
49    *
50    * @see testDispersion(), m_StdDevMax
51    */

52   private double m_StdDevMin = 1.0d;
53
54   /**
55    * Maximum Standard Deviation value of hashCodes needed to pass the
56    * testDispersion() test
57    *
58    * @see testDispersion(), m_StdDevMin
59    */

60   private double m_StdDevMax = 2.0d;
61
62   /**
63    * fraction of hashCodes that must be unique for the testHashCodeUniqueness
64    * method to succed.
65    *
66    * @see testHashCodeUniqueness
67    */

68   private double m_fractionUnique = 0.9d;
69
70   /**
71    * Contains the real fraction of hashCodes that where found to be unique in
72    * testHashCodeUniqueness.
73    */

74   private double m_actualFractionUnique = 0.0d;
75
76   /**
77    * Set whether the object will output debug information.
78    *
79    * @param a_verbose true if you want debug information, else set to false
80    *
81    * @since 2.1
82    */

83   public void setVerbose(boolean a_verbose) {
84     m_verbose = a_verbose;
85   }
86
87   /**
88    * Set's the maximum average value of hashCodes needed to pass the
89    * testDispersion() test. Also see TestHascode#testDispersion.
90    *
91    * @param a_averageMax New Maximum average
92    *
93    */

94   public void setAverageMax(double a_averageMax) {
95     m_AverageMax = a_averageMax;
96   }
97
98   /**
99    * Set's the minimum average value of hashCodes needed to pass the
100    * testDispersion() test
101    *
102    * @param a_averageMin new Minimum average
103    * @see TestHashcode#testDispersion
104    */

105   public void setAverageMin(double a_averageMin) {
106     m_AverageMin = a_averageMin;
107   }
108
109   /**
110    * Set's the Maximum standard deviationvalue of hashCodes needed to pass
111    * the testDispersion() test
112    *
113    * @param a_stdDevMax new Maximum standard deviation
114    * @see TestHashcode#testDispersion
115    *
116    * @since 2.1
117    */

118   public void setStdDevMax(double a_stdDevMax) {
119     m_StdDevMax = a_stdDevMax;
120   }
121
122   /**
123    * Set's the Minimum standard deviationvalue of hashCodes needed to pass
124    * the testDispersion() test
125    *
126    * @param a_stdDevMin new Minimum standard deviation
127    * @see TestHashcode#testDispersion
128    *
129    * @since 2.1
130    */

131   public void setStdDevMin(double a_stdDevMin) {
132     m_StdDevMin = a_stdDevMin;
133   }
134
135   /**
136    * Set's the fraction of hashCodes that must be unique for the
137    * testHashCodeUniqueness method to succed.
138    *
139    * @param a_fractionUnique new value, must be between 0.0 and 1.0
140    * @throws IllegalArgumentException
141    * @see TestHashcode#testHashCodeUniqueness
142    *
143    * @since 2.1
144    */

145   public void setFractionUnique(double a_fractionUnique) {
146     if ( (a_fractionUnique < 0.0) || (a_fractionUnique > 1.0)) {
147       throw new IllegalArgumentException JavaDoc(
148           "fractionUnique must be between 0.0 and 1.0");
149     }
150     m_fractionUnique = a_fractionUnique;
151   }
152
153   public double getActualFractionUnique() {
154     return m_actualFractionUnique;
155   }
156
157   /**
158    * Test that verifies that a the fraction of unique hashCodes is greater
159    * than the one specified.
160    *
161    * @param a_ObjectList list of objects to test
162    * @return true if the fraction of unique hashCodes is greater than the one
163    * specified. Else false
164    * @see TestHashcode#setFractionUnique
165    *
166    * @author John Serri
167    * @since 2.1
168    */

169   public boolean testHashCodeUniqueness(List a_ObjectList) {
170     boolean result = false;
171     int index;
172     int newvalue;
173     int numObjects = a_ObjectList.size();
174     Hashtable hashCodes = new Hashtable();
175     Integer JavaDoc key;
176
177     for (index = 0; index < numObjects; index++) {
178       int hashcode = a_ObjectList.get(index).hashCode();
179       key = new Integer JavaDoc(hashcode);
180       if (hashCodes.containsKey(key)) {
181         newvalue = ( (Integer JavaDoc) hashCodes.get(key)).intValue() + 1;
182         hashCodes.put(key, new Integer JavaDoc(newvalue));
183       }
184       else {
185         hashCodes.put(key, new Integer JavaDoc(1));
186       }
187     }
188     m_actualFractionUnique = ( (double) hashCodes.size() / (double) numObjects);
189     if (m_actualFractionUnique < m_fractionUnique) {
190       result = false;
191     }
192     else {
193       result = true;
194     }
195     return result;
196   }
197
198   /**
199    * Test to make sure that equal objects all have the same hashCode.
200    *
201    * @param a_ObjectList list of objects to test
202    * @return true if all hashCodes in List are the same. Else false
203    *
204    * @author John Serri
205    * @since 2.1
206    */

207   public boolean testHashCodeEquality(List a_ObjectList) {
208     int index;
209     int hashCode;
210     long numObjects = a_ObjectList.size();
211
212     if (numObjects < 2) {
213       return false;
214     }
215     hashCode = a_ObjectList.get(0).hashCode();
216     for (index = 1; index < numObjects; index++) {
217       if (hashCode != a_ObjectList.get(index).hashCode()) {
218         return false;
219       }
220     }
221     return true;
222   }
223
224   /**
225    * Test that verifies if average and Standard deviation of hashCodes in
226    * list match criteria.
227    *
228    * @param a_ObjectList list of objects to test
229    * @return true if average and Standard deviation of hashCodes in List
230    * match criterias. Else false
231    * @see TestHashcode#setAverageMax
232    * @see TestHashcode#setAverageMin
233    * @see TestHashcode#setStdDevMax
234    * @see TestHashcode#setStdDevMin
235    *
236    * @author John Serri
237    * @since 2.1
238    */

239   public boolean testDispersion(List a_ObjectList) {
240     int index;
241     boolean result = false;
242     int[] hashCodes = new int[a_ObjectList.size()];
243     long numObjects = a_ObjectList.size();
244     double average = 0;
245     double stdDev;
246     double sumOfSquare;
247     double squareOfSum;
248
249     for (index = 0; index < numObjects; index++) {
250       hashCodes[index] = a_ObjectList.get(index).hashCode();
251     }
252
253     //Average
254
for (index = 0; index < numObjects; index++) {
255       average += hashCodes[index];
256     }
257     average /= numObjects;
258
259     //STD Deviation
260
sumOfSquare = 0;
261     squareOfSum = 0;
262     for (index = 0; index < numObjects; index++) {
263       sumOfSquare += (double) hashCodes[index]
264           * (double) hashCodes[index];
265       squareOfSum += hashCodes[index];
266     }
267     squareOfSum *= squareOfSum;
268     stdDev = (sumOfSquare * numObjects) - squareOfSum;
269     stdDev /= numObjects * (numObjects - 1);
270     stdDev = Math.sqrt(stdDev);
271
272     if (m_verbose) {
273       System.out.println("Average =" + average + " StdDev =" + stdDev);
274       System.out.println("Average - StdDev =" + (average - stdDev));
275       System.out.println("Average + StdDev =" + (average + stdDev));
276     }
277
278     if ( (m_AverageMin < average) && (average < m_AverageMax)) {
279       result = true;
280     }
281     else {
282       result = false;
283     }
284
285     if ( (m_StdDevMin < stdDev) && (stdDev < m_StdDevMax)) {
286       result &= true;
287     }
288     else {
289       result = false;
290     }
291     return result;
292   }
293
294   /**
295    * Simple main method to test the class.
296    *
297    * @param args ignored
298    *
299    * @author John Serri
300    * @since 2.1
301    */

302   public static void main(String JavaDoc[] args) {
303     int com;
304     TestHashcode th = new TestHashcode();
305     List tl = new ArrayList();
306
307     for (com = 600000; com < 600100; com++) {
308       tl.add(new BigDecimal(com));
309     }
310     th.testDispersion(tl);
311     th.setFractionUnique(0.8);
312     th.testHashCodeUniqueness(tl);
313   }
314 }
315
Popular Tags