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