001/* 002 * $Id: TermOrderTest.java 4071 2012-07-27 19:59:38Z kredel $ 003 */ 004 005package edu.jas.poly; 006 007 008// import edu.jas.poly.TermOrder; 009 010import junit.framework.Test; 011import junit.framework.TestCase; 012import junit.framework.TestSuite; 013 014import org.apache.log4j.BasicConfigurator; 015 016 017/** 018 * TermOrder tests with JUnit. Tests also ExpVector comparisons. 019 * @author Heinz Kredel 020 */ 021 022public class TermOrderTest extends TestCase { 023 024 025 /** 026 * main. 027 */ 028 public static void main(String[] args) { 029 BasicConfigurator.configure(); 030 junit.textui.TestRunner.run(suite()); 031 } 032 033 034 /** 035 * Constructs a <CODE>TermOrderTest</CODE> object. 036 * @param name String. 037 */ 038 public TermOrderTest(String name) { 039 super(name); 040 } 041 042 043 /** 044 * suite. 045 */ 046 public static Test suite() { 047 TestSuite suite = new TestSuite(TermOrderTest.class); 048 return suite; 049 } 050 051 052 //private final static int bitlen = 100; 053 054 ExpVector a; 055 056 057 ExpVector b; 058 059 060 ExpVector c; 061 062 063 ExpVector d; 064 065 066 TermOrder t; 067 068 069 TermOrder s; 070 071 072 @Override 073 protected void setUp() { 074 a = b = c = d = null; 075 t = s = null; 076 } 077 078 079 @Override 080 protected void tearDown() { 081 a = b = c = d = null; 082 t = s = null; 083 } 084 085 086 /** 087 * Test constructor and toString. 088 * 089 */ 090 public void testConstructor() { 091 092 s = new TermOrder(); 093 t = new TermOrder(); 094 assertEquals("t = s", t, s); 095 096 String x = t.toString(); 097 String y = s.toString(); 098 099 assertEquals("x = y", x, y); 100 101 t = new TermOrder(TermOrder.INVLEX); 102 x = "INVLEX"; 103 boolean z = t.toString().startsWith(x); 104 assertTrue("INVLEX(.)", z); 105 106 s = new TermOrder(TermOrder.IGRLEX); 107 t = new TermOrder(TermOrder.IGRLEX); 108 assertEquals("t = s", t, s); 109 } 110 111 112 /** 113 * Test constructor, split TO. 114 * 115 */ 116 public void testConstructorSplit() { 117 118 int r = 10; 119 int sp = 5; 120 s = new TermOrder(r, sp); 121 t = new TermOrder(r, sp); 122 assertEquals("t = s", t, s); 123 124 String x = t.toString(); 125 String y = s.toString(); 126 assertEquals("x = y", x, y); 127 //System.out.println("s = " + s); 128 129 s = new TermOrder(TermOrder.IGRLEX, TermOrder.INVLEX, r, sp); 130 t = new TermOrder(TermOrder.IGRLEX, TermOrder.INVLEX, r, sp); 131 assertEquals("t = s", t, s); 132 //System.out.println("s = " + s); 133 } 134 135 136 /** 137 * Test constructor weight and toString. 138 * 139 */ 140 public void testConstructorWeight() { 141 long[][] w = new long[][] { new long[] { 1l, 1l, 1l, 1l, 1l } }; 142 143 s = new TermOrder(w); 144 t = new TermOrder(w); 145 assertEquals("t = s", t, s); 146 147 String x = t.toString(); 148 String y = s.toString(); 149 150 assertEquals("x = y", x, y); 151 //System.out.println("s = " + s); 152 153 //int r = 5; 154 //int sp = 3; 155 //w = new long [][] { new long[] { 5l, 4l, 3l, 2l, 1l } }; 156 157 //s = new TermOrder(w,sp); 158 //t = new TermOrder(w,sp); 159 //assertEquals("t = s",t,s); 160 161 //x = t.toString(); 162 //y = s.toString(); 163 164 //assertEquals("x = y",x,y); 165 //System.out.println("s = " + s); 166 167 x = "W("; 168 boolean z = t.toString().startsWith(x); 169 assertTrue("W(.)", z); 170 } 171 172 173 /** 174 * Test compare weight. 175 * 176 */ 177 public void testCompareWeight() { 178 float q = (float) 0.9; 179 180 a = ExpVector.EVRAND(5, 10, q); 181 b = ExpVector.EVRAND(5, 10, q); 182 183 c = a.sum(b); 184 185 long[][] w = new long[][] { new long[] { 1l, 1l, 1l, 1l, 1l } }; 186 187 t = new TermOrder(w); 188 189 int x = ExpVector.EVIWLC(w, c, a); 190 int y = ExpVector.EVIWLC(w, c, b); 191 192 assertEquals("x = 1", 1, x); 193 assertEquals("y = 1", 1, y); 194 195 x = ExpVector.EVIWLC(w, a, c); 196 y = ExpVector.EVIWLC(w, b, c); 197 198 assertEquals("x = -1", -1, x); 199 assertEquals("y = -1", -1, y); 200 201 x = ExpVector.EVIWLC(w, a, a); 202 y = ExpVector.EVIWLC(w, b, b); 203 204 assertEquals("x = 0", 0, x); 205 assertEquals("y = 0", 0, y); 206 } 207 208 209 /** 210 * Test compare weight 2 rows. 211 * 212 */ 213 public void testCompareWeight2() { 214 float q = (float) 0.9; 215 216 a = ExpVector.EVRAND(5, 10, q); 217 b = ExpVector.EVRAND(5, 10, q); 218 219 c = a.sum(b); 220 221 long[][] w = new long[][] { new long[] { 1l, 1l, 1l, 1l, 1l }, new long[] { 1l, 1l, 1l, 1l, 1l } }; 222 223 t = new TermOrder(w); 224 225 int x = ExpVector.EVIWLC(w, c, a); 226 int y = ExpVector.EVIWLC(w, c, b); 227 228 assertEquals("x = 1", 1, x); 229 assertEquals("y = 1", 1, y); 230 231 x = ExpVector.EVIWLC(w, a, c); 232 y = ExpVector.EVIWLC(w, b, c); 233 234 assertEquals("x = -1", -1, x); 235 assertEquals("y = -1", -1, y); 236 237 x = ExpVector.EVIWLC(w, a, a); 238 y = ExpVector.EVIWLC(w, b, b); 239 240 assertEquals("x = 0", 0, x); 241 assertEquals("y = 0", 0, y); 242 } 243 244 245 /** 246 * Test compare weight split. 247 * 248 */ 249 public void testCompareWeightSplit() { 250 float q = (float) 0.9; 251 int r = 8; 252 int sp = 4; 253 254 a = ExpVector.EVRAND(r, 10, q); 255 b = ExpVector.EVRAND(r, 10, q); 256 c = a.sum(b); 257 258 long[][] w = new long[][] { new long[] { 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l } }; 259 260 //t = new TermOrder(w,sp); 261 262 int x; 263 int y; 264 x = ExpVector.EVIWLC(w, c, a); 265 y = ExpVector.EVIWLC(w, c, b); 266 assertEquals("x = 1", 1, x); 267 assertEquals("y = 1", 1, y); 268 269 x = ExpVector.EVIWLC(w, c, a, 0, sp); 270 y = ExpVector.EVIWLC(w, c, b, 0, sp); 271 assertEquals("x = 1", 1, x); 272 assertEquals("y = 1", 1, y); 273 274 x = ExpVector.EVIWLC(w, c, a, sp, r); 275 y = ExpVector.EVIWLC(w, c, b, sp, r); 276 assertEquals("x = 1", 1, x); 277 assertEquals("y = 1", 1, y); 278 279 280 x = ExpVector.EVIWLC(w, a, c); 281 y = ExpVector.EVIWLC(w, b, c); 282 assertEquals("x = -1", -1, x); 283 assertEquals("y = -1", -1, y); 284 285 x = ExpVector.EVIWLC(w, a, c, 0, sp); 286 y = ExpVector.EVIWLC(w, b, c, 0, sp); 287 assertEquals("x = -1", -1, x); 288 assertEquals("y = -1", -1, y); 289 290 x = ExpVector.EVIWLC(w, a, c, sp, r); 291 y = ExpVector.EVIWLC(w, b, c, sp, r); 292 assertEquals("x = -1", -1, x); 293 assertEquals("y = -1", -1, y); 294 295 296 x = ExpVector.EVIWLC(w, a, a); 297 y = ExpVector.EVIWLC(w, b, b); 298 assertEquals("x = 0", 0, x); 299 assertEquals("y = 0", 0, y); 300 301 x = ExpVector.EVIWLC(w, a, a, 0, sp); 302 y = ExpVector.EVIWLC(w, b, b, 0, sp); 303 assertEquals("x = 0", 0, x); 304 assertEquals("y = 0", 0, y); 305 306 x = ExpVector.EVIWLC(w, a, a, sp, r); 307 y = ExpVector.EVIWLC(w, b, b, sp, r); 308 assertEquals("x = 0", 0, x); 309 assertEquals("y = 0", 0, y); 310 311 312 long[][] w2 = new long[][] { new long[] { 1l, 1l, 1l, 1l, 0l, 0l, 0l, 0l }, 313 new long[] { 0l, 0l, 0l, 0l, 1l, 1l, 1l, 1l } }; 314 315 //t = new TermOrder(w2); 316 317 x = ExpVector.EVIWLC(w2, c, a); 318 y = ExpVector.EVIWLC(w2, c, b); 319 assertEquals("x = 1", 1, x); 320 assertEquals("y = 1", 1, y); 321 322 x = ExpVector.EVIWLC(w2, c, a, 0, sp); 323 y = ExpVector.EVIWLC(w2, c, b, 0, sp); 324 assertEquals("x = 1", 1, x); 325 assertEquals("y = 1", 1, y); 326 327 x = ExpVector.EVIWLC(w2, c, a, sp, r); 328 y = ExpVector.EVIWLC(w2, c, b, sp, r); 329 assertEquals("x = 1", 1, x); 330 assertEquals("y = 1", 1, y); 331 332 333 x = ExpVector.EVIWLC(w2, a, c); 334 y = ExpVector.EVIWLC(w2, b, c); 335 assertEquals("x = -1", -1, x); 336 assertEquals("y = -1", -1, y); 337 338 x = ExpVector.EVIWLC(w2, a, c, 0, sp); 339 y = ExpVector.EVIWLC(w2, b, c, 0, sp); 340 assertEquals("x = -1", -1, x); 341 assertEquals("y = -1", -1, y); 342 343 x = ExpVector.EVIWLC(w2, a, c, sp, r); 344 y = ExpVector.EVIWLC(w2, b, c, sp, r); 345 assertEquals("x = -1", -1, x); 346 assertEquals("y = -1", -1, y); 347 348 349 x = ExpVector.EVIWLC(w2, a, a); 350 y = ExpVector.EVIWLC(w2, b, b); 351 assertEquals("x = 0", 0, x); 352 assertEquals("y = 0", 0, y); 353 354 x = ExpVector.EVIWLC(w2, a, a, 0, sp); 355 y = ExpVector.EVIWLC(w2, b, b, 0, sp); 356 assertEquals("x = 0", 0, x); 357 assertEquals("y = 0", 0, y); 358 359 x = ExpVector.EVIWLC(w2, a, a, sp, r); 360 y = ExpVector.EVIWLC(w2, b, b, sp, r); 361 assertEquals("x = 0", 0, x); 362 assertEquals("y = 0", 0, y); 363 } 364 365 366 /** 367 * Test ascend comparators. 368 * 369 */ 370 public void testAscendComparator() { 371 float q = (float) 0.9; 372 373 a = ExpVector.EVRAND(5, 10, q); 374 b = ExpVector.EVRAND(5, 10, q); 375 376 c = a.sum(b); 377 378 t = new TermOrder(); 379 380 int x = t.getAscendComparator().compare(c, a); 381 int y = t.getAscendComparator().compare(c, b); 382 383 assertEquals("x = 1", 1, x); 384 assertEquals("y = 1", 1, y); 385 386 x = t.getAscendComparator().compare(a, c); 387 y = t.getAscendComparator().compare(b, c); 388 389 assertEquals("x = -1", -1, x); 390 assertEquals("y = -1", -1, y); 391 392 x = t.getAscendComparator().compare(a, a); 393 y = t.getAscendComparator().compare(b, b); 394 395 assertEquals("x = 0", 0, x); 396 assertEquals("y = 0", 0, y); 397 } 398 399 400 /** 401 * Test ascend comparators split. 402 * 403 */ 404 public void testAscendComparatorSplit() { 405 float q = (float) 0.9; 406 407 int r = 10; 408 int sp = 5; 409 410 a = ExpVector.EVRAND(r, 10, q); 411 b = ExpVector.EVRAND(r, 10, q); 412 c = a.sum(b); 413 414 t = new TermOrder(r, sp); 415 416 int x = t.getAscendComparator().compare(c, a); 417 int y = t.getAscendComparator().compare(c, b); 418 assertEquals("x = 1", 1, x); 419 assertEquals("y = 1", 1, y); 420 421 x = t.getAscendComparator().compare(a, c); 422 y = t.getAscendComparator().compare(b, c); 423 assertEquals("x = -1", -1, x); 424 assertEquals("y = -1", -1, y); 425 426 x = t.getAscendComparator().compare(a, a); 427 y = t.getAscendComparator().compare(b, b); 428 assertEquals("x = 0", 0, x); 429 assertEquals("y = 0", 0, y); 430 } 431 432 433 /** 434 * Test ascend comparators weight and split. 435 * 436 */ 437 public void testAscendComparatorWeightSplit() { 438 float q = (float) 0.9; 439 440 int r = 8; 441 //int sp = 5; 442 //long [][] w = new long [][] { new long[] { 1l, 2l, 3l, 4l, 5l, 1l, 2l, 3l } }; 443 long[][] w2 = new long[][] { new long[] { 1l, 2l, 3l, 4l, 5l, 0l, 0l, 0l }, 444 new long[] { 0l, 0l, 0l, 0l, 0l, 1l, 2l, 3l } }; 445 446 a = ExpVector.EVRAND(r, 10, q); 447 b = ExpVector.EVRAND(r, 10, q); 448 c = a.sum(b); 449 450 // t = new TermOrder(w,sp); 451 t = new TermOrder(w2); 452 TermOrder t2 = new TermOrder(w2); 453 // now t equals t2 454 455 int x = t.getAscendComparator().compare(c, a); 456 int y = t.getAscendComparator().compare(c, b); 457 assertEquals("x = 1", 1, x); 458 assertEquals("y = 1", 1, y); 459 460 int x2 = t2.getAscendComparator().compare(c, a); 461 int y2 = t2.getAscendComparator().compare(c, b); 462 assertEquals("x2 = 1", 1, x2); 463 assertEquals("y2 = 1", 1, y2); 464 465 assertEquals("x = x2", x, x2); 466 assertEquals("y = y2", y, y2); 467 468 469 x = t.getAscendComparator().compare(a, c); 470 y = t.getAscendComparator().compare(b, c); 471 assertEquals("x = -1", -1, x); 472 assertEquals("y = -1", -1, y); 473 474 x2 = t2.getAscendComparator().compare(a, c); 475 y2 = t2.getAscendComparator().compare(b, c); 476 assertEquals("x2 = -1", -1, x2); 477 assertEquals("y2 = -1", -1, y2); 478 479 assertEquals("x = x2", x, x2); 480 assertEquals("y = y2", y, y2); 481 482 483 x = t.getAscendComparator().compare(a, a); 484 y = t.getAscendComparator().compare(b, b); 485 assertEquals("x = 0", 0, x); 486 assertEquals("y = 0", 0, y); 487 488 x2 = t2.getAscendComparator().compare(a, a); 489 y2 = t2.getAscendComparator().compare(b, b); 490 assertEquals("x2 = 0", 0, x2); 491 assertEquals("y2 = 0", 0, y2); 492 493 assertEquals("x = x2", x, x2); 494 assertEquals("y = y2", y, y2); 495 } 496 497 498 /** 499 * Test descend comparators. 500 * 501 */ 502 public void testDescendComparator() { 503 float q = (float) 0.9; 504 505 a = ExpVector.EVRAND(5, 10, q); 506 b = ExpVector.EVRAND(5, 10, q); 507 508 c = a.sum(b); 509 510 t = new TermOrder(); 511 512 int x = t.getDescendComparator().compare(c, a); 513 int y = t.getDescendComparator().compare(c, b); 514 515 assertEquals("x = -1", -1, x); 516 assertEquals("y = -1", -1, y); 517 518 x = t.getDescendComparator().compare(a, c); 519 y = t.getDescendComparator().compare(b, c); 520 521 assertEquals("x = 1", 1, x); 522 assertEquals("y = 1", 1, y); 523 524 x = t.getDescendComparator().compare(a, a); 525 y = t.getDescendComparator().compare(b, b); 526 527 assertEquals("x = 0", 0, x); 528 assertEquals("y = 0", 0, y); 529 } 530 531 532 /** 533 * Test descend comparators split. 534 * 535 */ 536 public void testDescendComparatorSplit() { 537 float q = (float) 0.9; 538 539 int r = 10; 540 int sp = 5; 541 542 a = ExpVector.EVRAND(r, 10, q); 543 b = ExpVector.EVRAND(r, 10, q); 544 c = a.sum(b); 545 546 t = new TermOrder(r, sp); 547 548 int x = t.getDescendComparator().compare(c, a); 549 int y = t.getDescendComparator().compare(c, b); 550 assertEquals("x = -1", -1, x); 551 assertEquals("y = -1", -1, y); 552 553 x = t.getDescendComparator().compare(a, c); 554 y = t.getDescendComparator().compare(b, c); 555 assertEquals("x = 1", 1, x); 556 assertEquals("y = 1", 1, y); 557 558 x = t.getDescendComparator().compare(a, a); 559 y = t.getDescendComparator().compare(b, b); 560 assertEquals("x = 0", 0, x); 561 assertEquals("y = 0", 0, y); 562 } 563 564 565 /** 566 * Test descend comparators weight and split. 567 * 568 */ 569 public void testDescendComparatorWeightSplit() { 570 float q = (float) 0.9; 571 572 int r = 8; 573 //int sp = 5; 574 //long [][] w = new long [][] { new long[] { 1l, 2l, 3l, 4l, 5l, 1l, 2l, 3l } }; 575 long[][] w2 = new long[][] { new long[] { 1l, 2l, 3l, 4l, 5l, 0l, 0l, 0l }, 576 new long[] { 0l, 0l, 0l, 0l, 0l, 1l, 2l, 3l } }; 577 578 a = ExpVector.EVRAND(r, 10, q); 579 b = ExpVector.EVRAND(r, 10, q); 580 c = a.sum(b); 581 582 //t = new TermOrder(w,sp); 583 t = new TermOrder(w2); 584 TermOrder t2 = new TermOrder(w2); 585 // now t equals t2 586 587 int x = t.getDescendComparator().compare(c, a); 588 int y = t.getDescendComparator().compare(c, b); 589 assertEquals("x = -1", -1, x); 590 assertEquals("y = -1", -1, y); 591 592 int x2 = t2.getDescendComparator().compare(c, a); 593 int y2 = t2.getDescendComparator().compare(c, b); 594 assertEquals("x2 = -1", -1, x2); 595 assertEquals("y2 = -1", -1, y2); 596 597 assertEquals("x = x2", x, x2); 598 assertEquals("y = y2", y, y2); 599 600 601 x = t.getDescendComparator().compare(a, c); 602 y = t.getDescendComparator().compare(b, c); 603 assertEquals("x = 1", 1, x); 604 assertEquals("y = 1", 1, y); 605 606 x2 = t2.getDescendComparator().compare(a, c); 607 y2 = t2.getDescendComparator().compare(b, c); 608 assertEquals("x2 = 1", 1, x2); 609 assertEquals("y2 = 1", 1, y2); 610 611 assertEquals("x = x2", x, x2); 612 assertEquals("y = y2", y, y2); 613 614 615 x = t.getDescendComparator().compare(a, a); 616 y = t.getDescendComparator().compare(b, b); 617 assertEquals("x = 0", 0, x); 618 assertEquals("y = 0", 0, y); 619 620 x2 = t2.getDescendComparator().compare(a, a); 621 y2 = t2.getDescendComparator().compare(b, b); 622 assertEquals("x2 = 0", 0, x2); 623 assertEquals("y2 = 0", 0, y2); 624 625 assertEquals("x = x2", x, x2); 626 assertEquals("y = y2", y, y2); 627 } 628 629 630 /** 631 * Test exception. 632 * 633 */ 634 public void testException() { 635 float q = (float) 0.9; 636 637 a = ExpVector.EVRAND(5, 10, q); 638 b = ExpVector.EVRAND(5, 10, q); 639 640 int wrong = 99; 641 //int x = 0; 642 643 try { 644 t = new TermOrder(wrong); 645 } catch (IllegalArgumentException e) { 646 return; 647 } 648 fail("IllegalArgumentException"); 649 } 650 651 652 /** 653 * Test exception split. 654 * 655 */ 656 public void testExceptionSplit() { 657 float q = (float) 0.9; 658 659 int r = 10; 660 int sp = 5; 661 662 a = ExpVector.EVRAND(r, 10, q); 663 b = ExpVector.EVRAND(r, 10, q); 664 665 int wrong = 99; 666 //int x = 0; 667 668 try { 669 t = new TermOrder(wrong, wrong, r, sp); 670 } catch (IllegalArgumentException e) { 671 return; 672 } 673 fail("IllegalArgumentException"); 674 } 675 676 677 /** 678 * Test compare exception. 679 * 680 */ 681 public void testCompareException() { 682 float q = (float) 0.9; 683 684 a = ExpVector.EVRAND(5, 10, q); 685 b = ExpVector.EVRAND(5, 10, q); 686 687 int notimpl = TermOrder.REVITDG + 2; 688 int x = 0; 689 690 try { 691 t = new TermOrder(notimpl); 692 x = t.getDescendComparator().compare(a, b); 693 fail("IllegalArgumentException " + x); 694 } catch (IllegalArgumentException e) { 695 //return; 696 } catch (NullPointerException e) { 697 //return; 698 } 699 } 700 701 702 /** 703 * Test compare exception split. 704 * 705 */ 706 public void testCompareExceptionSplit() { 707 float q = (float) 0.9; 708 709 int r = 10; 710 int sp = 5; 711 712 a = ExpVector.EVRAND(r, 10, q); 713 b = ExpVector.EVRAND(r, 10, q); 714 715 int notimpl = TermOrder.REVITDG + 2; 716 int x = 0; 717 718 try { 719 t = new TermOrder(notimpl, notimpl, r, sp); 720 x = t.getDescendComparator().compare(a, b); 721 fail("IllegalArgumentException " + x); 722 } catch (IllegalArgumentException e) { 723 //return; 724 } catch (NullPointerException e) { 725 //return; 726 } 727 } 728 729 730 /** 731 * Test compare exception weight. 732 * 733 */ 734 public void testCompareExceptionWeigth() { 735 float q = (float) 0.9; 736 737 int r = 10; 738 //int sp = 5; 739 740 a = ExpVector.EVRAND(r, 10, q); 741 b = ExpVector.EVRAND(r, 10, q); 742 743 int x = 0; 744 745 try { 746 t = new TermOrder((long[][]) null); 747 x = t.getDescendComparator().compare(a, b); 748 fail("IllegalArgumentException " + x); 749 } catch (IllegalArgumentException e) { 750 //return; 751 } catch (NullPointerException e) { 752 //return; 753 } 754 } 755 756}