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