1 43 44 package org.jfree.data.statistics.junit; 45 46 import java.util.ArrayList ; 47 import java.util.Collection ; 48 import java.util.Collections ; 49 import java.util.List ; 50 51 import junit.framework.Test; 52 import junit.framework.TestCase; 53 import junit.framework.TestSuite; 54 55 import org.jfree.data.statistics.Statistics; 56 57 60 public class StatisticsTests extends TestCase { 61 62 67 public static Test suite() { 68 return new TestSuite(StatisticsTests.class); 69 } 70 71 76 public StatisticsTests(String name) { 77 super(name); 78 } 79 80 84 public void testCalculateMean_Array() { 85 86 boolean pass = false; 88 try { 89 Statistics.calculateMean((Number []) null); 90 } 91 catch (IllegalArgumentException e) { 92 pass = true; 93 } 94 assertTrue(pass); 95 96 pass = false; 97 try { 98 Statistics.calculateMean((Number []) null, false); 99 } 100 catch (IllegalArgumentException e) { 101 pass = true; 102 } 103 assertTrue(pass); 104 105 assertTrue(Double.isNaN(Statistics.calculateMean(new Number [0]))); 107 assertTrue(Double.isNaN(Statistics.calculateMean(new Number [0], 108 false))); 109 110 Number [] values = new Number [] { new Double (1.0) }; 112 assertEquals(1.0, Statistics.calculateMean(values), EPSILON); 113 assertEquals(1.0, Statistics.calculateMean(values, true), EPSILON); 114 assertEquals(1.0, Statistics.calculateMean(values, false), EPSILON); 115 116 values = new Number [] { new Double (1.0), null }; 118 assertTrue(Double.isNaN(Statistics.calculateMean(values))); 119 assertTrue(Double.isNaN(Statistics.calculateMean(values, true))); 120 assertEquals(1.0, Statistics.calculateMean(values, false), EPSILON); 121 122 values = new Number [] { new Double (1.0), new Double (Double.NaN) }; 124 assertTrue(Double.isNaN(Statistics.calculateMean(values))); 125 assertTrue(Double.isNaN(Statistics.calculateMean(values, true))); 126 assertEquals(1.0, Statistics.calculateMean(values, false), EPSILON); 127 } 128 129 133 public void testCalculateMean_Collection() { 134 135 boolean pass = false; 137 try { 138 Statistics.calculateMean((Collection ) null); 139 } 140 catch (IllegalArgumentException e) { 141 pass = true; 142 } 143 assertTrue(pass); 144 145 pass = false; 146 try { 147 Statistics.calculateMean((Collection ) null, false); 148 } 149 catch (IllegalArgumentException e) { 150 pass = true; 151 } 152 assertTrue(pass); 153 154 List values = new ArrayList (); 156 assertTrue(Double.isNaN(Statistics.calculateMean(values))); 157 assertTrue(Double.isNaN(Statistics.calculateMean(values, true))); 158 assertTrue(Double.isNaN(Statistics.calculateMean(values, false))); 159 160 values.add(new Double (9.0)); 162 assertEquals(9.0, Statistics.calculateMean(values), EPSILON); 163 assertEquals(9.0, Statistics.calculateMean(values, true), EPSILON); 164 assertEquals(9.0, Statistics.calculateMean(values, false), EPSILON); 165 166 values.add(null); 168 assertTrue(Double.isNaN(Statistics.calculateMean(values))); 169 assertTrue(Double.isNaN(Statistics.calculateMean(values, true))); 170 assertEquals(9.0, Statistics.calculateMean(values, false), EPSILON); 171 172 values.clear(); 174 values.add(new Double (9.0)); 175 values.add(new Double (Double.NaN)); 176 assertTrue(Double.isNaN(Statistics.calculateMean(values))); 177 assertTrue(Double.isNaN(Statistics.calculateMean(values, true))); 178 assertEquals(9.0, Statistics.calculateMean(values, false), EPSILON); 179 180 values = new ArrayList (); 182 values.add(new Double (9.0)); 183 values.add(new Double (3.0)); 184 values.add(new Double (2.0)); 185 values.add(new Double (2.0)); 186 double mean = Statistics.calculateMean(values); 187 assertEquals(4.0, mean, EPSILON); 188 189 values.add(new Double (Double.NaN)); 191 assertTrue(Double.isNaN(Statistics.calculateMean(values))); 192 } 193 194 static final double EPSILON = 0.0000000001; 195 196 199 public void testCalculateMedian() { 200 201 assertTrue(Double.isNaN(Statistics.calculateMedian(null, false))); 203 assertTrue(Double.isNaN(Statistics.calculateMedian(null, true))); 204 205 List list = new ArrayList (); 207 assertTrue(Double.isNaN(Statistics.calculateMedian(list, false))); 208 assertTrue(Double.isNaN(Statistics.calculateMedian(list, true))); 209 210 list.add(null); 212 boolean pass = false; 213 try { 214 Statistics.calculateMedian(list, false); 215 } 216 catch (NullPointerException e) { 217 pass = true; 218 } 219 assertTrue(pass); 220 221 pass = false; 222 try { 223 Statistics.calculateMedian(list, true); 224 } 225 catch (NullPointerException e) { 226 pass = true; 227 } 228 assertTrue(pass); 229 230 list.clear(); 232 list.add("Not a number"); 233 pass = false; 234 try { 235 Statistics.calculateMedian(list, false); 236 } 237 catch (ClassCastException e) { 238 pass = true; 239 } 240 assertTrue(pass); 241 242 pass = false; 243 try { 244 Statistics.calculateMedian(list, true); 245 } 246 catch (ClassCastException e) { 247 pass = true; 248 } 249 assertTrue(pass); 250 251 } 252 253 256 public void testCalculateMedian1() { 257 List values = new ArrayList (); 258 values.add(new Double (1.0)); 259 double median = Statistics.calculateMedian(values); 260 assertEquals(1.0, median, 0.0000001); 261 } 262 263 266 public void testCalculateMedian2() { 267 List values = new ArrayList (); 268 values.add(new Double (2.0)); 269 values.add(new Double (1.0)); 270 double median = Statistics.calculateMedian(values); 271 assertEquals(1.5, median, 0.0000001); 272 } 273 274 277 public void testCalculateMedian3() { 278 List values = new ArrayList (); 279 values.add(new Double (1.0)); 280 values.add(new Double (2.0)); 281 values.add(new Double (3.0)); 282 values.add(new Double (6.0)); 283 values.add(new Double (5.0)); 284 values.add(new Double (4.0)); 285 double median = Statistics.calculateMedian(values); 286 assertEquals(3.5, median, 0.0000001); 287 } 288 289 292 public void testCalculateMedian4() { 293 List values = new ArrayList (); 294 values.add(new Double (7.0)); 295 values.add(new Double (2.0)); 296 values.add(new Double (3.0)); 297 values.add(new Double (5.0)); 298 values.add(new Double (4.0)); 299 values.add(new Double (6.0)); 300 values.add(new Double (1.0)); 301 double median = Statistics.calculateMedian(values); 302 assertEquals(4.0, median, 0.0000001); 303 } 304 305 308 public void testCalculateMedian5() { 309 List values = new ArrayList (); 310 values.add(new Double (11.228692993861783)); 311 values.add(new Double (11.30823353859889)); 312 values.add(new Double (11.75312904769314)); 313 values.add(new Double (11.825102897465314)); 314 values.add(new Double (10.184252778401783)); 315 values.add(new Double (12.207951828057766)); 316 values.add(new Double (10.68841994040566)); 317 values.add(new Double (12.099522004479438)); 318 values.add(new Double (11.508874945056881)); 319 values.add(new Double (12.052517729558513)); 320 values.add(new Double (12.401481645578734)); 321 values.add(new Double (12.185377793028543)); 322 values.add(new Double (10.666372951930315)); 323 values.add(new Double (11.680978041499548)); 324 values.add(new Double (11.06528277406718)); 325 values.add(new Double (11.36876492904596)); 326 values.add(new Double (11.927565516175939)); 327 values.add(new Double (11.39307785978655)); 328 values.add(new Double (11.989603679523857)); 329 values.add(new Double (12.009834360354864)); 330 values.add(new Double (10.653351822461559)); 331 values.add(new Double (11.851776254376754)); 332 values.add(new Double (11.045441544755946)); 333 values.add(new Double (11.993674040560624)); 334 values.add(new Double (12.898219965238944)); 335 values.add(new Double (11.97095782819647)); 336 values.add(new Double (11.73234406745488)); 337 values.add(new Double (11.649006017243991)); 338 values.add(new Double (12.20549704915365)); 339 values.add(new Double (11.799723639384919)); 340 values.add(new Double (11.896208658005628)); 341 values.add(new Double (12.164149111823424)); 342 values.add(new Double (12.042795103513766)); 343 values.add(new Double (12.114839532596426)); 344 values.add(new Double (12.166609097075824)); 345 values.add(new Double (12.183017546225935)); 346 values.add(new Double (11.622009125845342)); 347 values.add(new Double (11.289365786738633)); 348 values.add(new Double (12.462984323671568)); 349 values.add(new Double (11.573494921030598)); 350 values.add(new Double (10.862867940485804)); 351 values.add(new Double (12.018186939664872)); 352 values.add(new Double (10.418046849313018)); 353 values.add(new Double (11.326344465881341)); 354 double median = Statistics.calculateMedian(values, true); 355 assertEquals(11.812413268425116, median, 0.000001); 356 Collections.sort(values); 357 double median2 = Statistics.calculateMedian(values, false); 358 assertEquals(11.812413268425116, median2, 0.000001); 359 } 360 361 364 public void testCalculateMedian6() { 365 List values = new ArrayList (); 366 values.add(new Double (7.0)); 367 values.add(new Double (2.0)); 368 values.add(new Double (3.0)); 369 values.add(new Double (5.0)); 370 values.add(new Double (4.0)); 371 values.add(new Double (6.0)); 372 values.add(new Double (1.0)); 373 double median = Statistics.calculateMedian(values, 0, 2); 374 assertEquals(3.0, median, 0.0000001); 375 } 376 377 380 public void testCorrelation1() { 381 Number [] data1 = new Number [3]; 382 data1[0] = new Double (1); 383 data1[1] = new Double (2); 384 data1[2] = new Double (3); 385 Number [] data2 = new Number [3]; 386 data2[0] = new Double (1); 387 data2[1] = new Double (2); 388 data2[2] = new Double (3); 389 double r = Statistics.getCorrelation(data1, data2); 390 assertEquals(1.0, r, 0.00000001); 391 } 392 393 398 public void testCorrelation2() { 399 Number [] data1 = new Number [20]; 400 data1[0] = new Double (68); 401 data1[1] = new Double (71); 402 data1[2] = new Double (62); 403 data1[3] = new Double (75); 404 data1[4] = new Double (58); 405 data1[5] = new Double (60); 406 data1[6] = new Double (67); 407 data1[7] = new Double (68); 408 data1[8] = new Double (71); 409 data1[9] = new Double (69); 410 data1[10] = new Double (68); 411 data1[11] = new Double (67); 412 data1[12] = new Double (63); 413 data1[13] = new Double (62); 414 data1[14] = new Double (60); 415 data1[15] = new Double (63); 416 data1[16] = new Double (65); 417 data1[17] = new Double (67); 418 data1[18] = new Double (63); 419 data1[19] = new Double (61); 420 Number [] data2 = new Number [20]; 421 data2[0] = new Double (4.1); 422 data2[1] = new Double (4.6); 423 data2[2] = new Double (3.8); 424 data2[3] = new Double (4.4); 425 data2[4] = new Double (3.2); 426 data2[5] = new Double (3.1); 427 data2[6] = new Double (3.8); 428 data2[7] = new Double (4.1); 429 data2[8] = new Double (4.3); 430 data2[9] = new Double (3.7); 431 data2[10] = new Double (3.5); 432 data2[11] = new Double (3.2); 433 data2[12] = new Double (3.7); 434 data2[13] = new Double (3.3); 435 data2[14] = new Double (3.4); 436 data2[15] = new Double (4.0); 437 data2[16] = new Double (4.1); 438 data2[17] = new Double (3.8); 439 data2[18] = new Double (3.4); 440 data2[19] = new Double (3.6); 441 double r = Statistics.getCorrelation(data1, data2); 442 assertEquals(0.7306356862792885, r, 0.000000000001); 443 } 444 445 448 public void testGetStdDev() { 449 450 boolean pass = false; 452 try { 453 Statistics.getStdDev(null); 454 } 455 catch (IllegalArgumentException e) { 456 pass = true; 457 } 458 assertTrue(pass); 459 460 pass = false; 462 try { 463 Statistics.getStdDev(new Double [0]); 464 } 465 catch (IllegalArgumentException e) { 466 pass = true; 467 } 468 assertTrue(pass); 469 470 assertTrue(Double.isNaN(Statistics.getStdDev(new Double [] 472 {new Double (1.0)}))); 473 } 474 475 } 476 | Popular Tags |