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