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 * TermOrderByName tests with JUnit. Tests different names for TermOrders.
015 * @author Heinz Kredel
016 */
017
018public class TermOrderByNameTest 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>TermOrderByNameTest</CODE> object.
031     * @param name String.
032     */
033    public TermOrderByNameTest(String name) {
034        super(name);
035    }
036
037
038    /**
039     * suite.
040     */
041    public static Test suite() {
042        TestSuite suite = new TestSuite(TermOrderByNameTest.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 = TermOrderByName.DEFAULT;
072        t = TermOrderByName.DEFAULT;
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 = TermOrderByName.Lexicographic;
081        x = "REVILEX";
082        y = t.toString();
083        boolean z = y.startsWith(x);
084        assertTrue("REVILEX(.): " + y, z);
085
086        s = TermOrderByName.DegreeLexicographic;
087        t = TermOrderByName.DegreeLexicographic;
088        assertEquals("t = s", t, s);
089    }
090
091
092    /*
093     * Test constructor, split TO.
094     */
095    public void testConstructorSplit() {
096        int r = 10;
097        int sp = 5;
098
099        ExpVector ev = ExpVectorLong.create(r);
100        s = TermOrderByName.blockOrder(TermOrderByName.DegreeLexicographic,
101                        TermOrderByName.DegreeLexicographic, ev, sp);
102        t = TermOrderByName.blockOrder(TermOrderByName.DegreeLexicographic,
103                        TermOrderByName.DegreeLexicographic, ev, sp);
104        assertEquals("t = s", t, s);
105
106        String x = t.toString();
107        String y = s.toString();
108        assertEquals("x = y", x, y);
109        //System.out.println("s = " + s);
110
111        s = TermOrderByName.blockOrder(TermOrderByName.DegreeLexicographic, TermOrderByName.Lexicographic, ev,
112                        sp);
113        t = TermOrderByName.blockOrder(TermOrderByName.DegreeLexicographic, TermOrderByName.Lexicographic, ev,
114                        sp);
115        assertEquals("t = s", t, s);
116        //System.out.println("s = " + s);
117    }
118
119
120    /**
121     * Test constructor weight and toString.
122     */
123    public void testConstructorWeight() {
124        long[][] w = new long[][] { new long[] { 1l, 1l, 1l, 1l, 1l } };
125        s = TermOrderByName.weightOrder(w);
126        t = TermOrderByName.weightOrder(w);
127        //System.out.println("s = " + s);
128        //System.out.println("t = " + t);
129        assertEquals("t = s", t, s);
130
131        String x = t.toString();
132        String y = s.toString();
133
134        //System.out.println("x = " + x);
135        //System.out.println("y = " + y);
136        assertEquals("x = y", x, y);
137
138        //int r = 5;
139        //int sp = 3;
140        //w = new long [][] { new long[] { 5l, 4l, 3l, 2l, 1l } };
141
142        //s = new TermOrder(w,sp);
143        //t = new TermOrder(w,sp);
144        //assertEquals("t = s",t,s);
145
146        //x = t.toString();
147        //y = s.toString();
148
149        //assertEquals("x = y",x,y);
150        //System.out.println("s = " + s);
151
152        x = "W(";
153        boolean z = t.toString().startsWith(x);
154        assertTrue("W(.)", z);
155    }
156
157
158    /**
159     * Test compare weight.
160     */
161    public void testCompareWeight() {
162        float q = (float) 0.9;
163
164        a = ExpVector.random(5, 10, q);
165        b = ExpVector.random(5, 10, q);
166        c = a.sum(b);
167
168        long[][] w = new long[][] { new long[] { 1l, 1l, 1l, 1l, 1l } };
169        t = TermOrderByName.weightOrder(w);
170
171        int x = ExpVector.EVIWLC(w, c, a);
172        int y = ExpVector.EVIWLC(w, c, b);
173
174        assertEquals("x = 1", 1, x);
175        assertEquals("y = 1", 1, y);
176
177        x = ExpVector.EVIWLC(w, a, c);
178        y = ExpVector.EVIWLC(w, b, c);
179
180        assertEquals("x = -1", -1, x);
181        assertEquals("y = -1", -1, y);
182
183        x = ExpVector.EVIWLC(w, a, a);
184        y = ExpVector.EVIWLC(w, b, b);
185
186        assertEquals("x = 0", 0, x);
187        assertEquals("y = 0", 0, y);
188    }
189
190
191    /**
192     * Test compare weight 2 rows.
193     */
194    public void testCompareWeight2() {
195        float q = (float) 0.9;
196
197        a = ExpVector.random(5, 10, q);
198        b = ExpVector.random(5, 10, q);
199        c = a.sum(b);
200
201        long[][] w = new long[][] { new long[] { 1l, 1l, 1l, 1l, 1l }, new long[] { 1l, 1l, 1l, 1l, 1l } };
202        t = TermOrderByName.weightOrder(w);
203
204        int x = ExpVector.EVIWLC(w, c, a);
205        int y = ExpVector.EVIWLC(w, c, b);
206
207        assertEquals("x = 1", 1, x);
208        assertEquals("y = 1", 1, y);
209
210        x = ExpVector.EVIWLC(w, a, c);
211        y = ExpVector.EVIWLC(w, b, c);
212
213        assertEquals("x = -1", -1, x);
214        assertEquals("y = -1", -1, y);
215
216        x = ExpVector.EVIWLC(w, a, a);
217        y = ExpVector.EVIWLC(w, b, b);
218
219        assertEquals("x = 0", 0, x);
220        assertEquals("y = 0", 0, y);
221    }
222
223
224    /**
225     * Test ascend comparators.
226     */
227    public void testAscendComparator() {
228        float q = (float) 0.9;
229
230        a = ExpVector.random(5, 10, q);
231        b = ExpVector.random(5, 10, q);
232        c = a.sum(b);
233
234        t = TermOrderByName.DegreeLexicographic;
235
236        int x = t.getAscendComparator().compare(c, a);
237        int y = t.getAscendComparator().compare(c, b);
238
239        assertEquals("x = 1", 1, x);
240        assertEquals("y = 1", 1, y);
241
242        x = t.getAscendComparator().compare(a, c);
243        y = t.getAscendComparator().compare(b, c);
244
245        assertEquals("x = -1", -1, x);
246        assertEquals("y = -1", -1, y);
247
248        x = t.getAscendComparator().compare(a, a);
249        y = t.getAscendComparator().compare(b, b);
250
251        assertEquals("x = 0", 0, x);
252        assertEquals("y = 0", 0, y);
253    }
254
255
256    /**
257     * Test ascend comparators split.
258     */
259    public void testAscendComparatorSplit() {
260        float q = (float) 0.9;
261
262        int r = 10;
263        int sp = 5;
264
265        a = ExpVector.random(r, 10, q);
266        b = ExpVector.random(r, 10, q);
267        c = a.sum(b);
268
269        t = TermOrderByName.blockOrder(TermOrderByName.DegreeLexicographic, TermOrderByName.Lexicographic, c,
270                        sp);
271
272        int x = t.getAscendComparator().compare(c, a);
273        int y = t.getAscendComparator().compare(c, b);
274        assertEquals("x = 1", 1, x);
275        assertEquals("y = 1", 1, y);
276
277        x = t.getAscendComparator().compare(a, c);
278        y = t.getAscendComparator().compare(b, c);
279        assertEquals("x = -1", -1, x);
280        assertEquals("y = -1", -1, y);
281
282        x = t.getAscendComparator().compare(a, a);
283        y = t.getAscendComparator().compare(b, b);
284        assertEquals("x = 0", 0, x);
285        assertEquals("y = 0", 0, y);
286    }
287
288
289    /**
290     * Test ascend comparators weight and split.
291     */
292    public void testAscendComparatorWeightSplit() {
293        float q = (float) 0.9;
294        int r = 8;
295        //int sp = 5;
296
297        a = ExpVector.random(r, 10, q);
298        b = ExpVector.random(r, 10, q);
299        c = a.sum(b);
300
301        //long [][] w  = new long [][] { new long[] { 1l, 2l, 3l, 4l, 5l, 1l, 2l, 3l } };
302        long[][] w2 = new long[][] { new long[] { 1l, 2l, 3l, 4l, 5l, 0l, 0l, 0l },
303                new long[] { 0l, 0l, 0l, 0l, 0l, 1l, 2l, 3l } };
304        // t = new TermOrder(w,sp);
305        t = TermOrderByName.weightOrder(w2);
306        TermOrder t2 = TermOrderByName.weightOrder(w2);
307        assertEquals("t = t2", t, t2);
308
309        int x = t.getAscendComparator().compare(c, a);
310        int y = t.getAscendComparator().compare(c, b);
311        assertEquals("x = 1", 1, x);
312        assertEquals("y = 1", 1, y);
313
314        int x2 = t2.getAscendComparator().compare(c, a);
315        int y2 = t2.getAscendComparator().compare(c, b);
316        assertEquals("x2 = 1", 1, x2);
317        assertEquals("y2 = 1", 1, y2);
318
319        assertEquals("x = x2", x, x2);
320        assertEquals("y = y2", y, y2);
321
322
323        x = t.getAscendComparator().compare(a, c);
324        y = t.getAscendComparator().compare(b, c);
325        assertEquals("x = -1", -1, x);
326        assertEquals("y = -1", -1, y);
327
328        x2 = t2.getAscendComparator().compare(a, c);
329        y2 = t2.getAscendComparator().compare(b, c);
330        assertEquals("x2 = -1", -1, x2);
331        assertEquals("y2 = -1", -1, y2);
332
333        assertEquals("x = x2", x, x2);
334        assertEquals("y = y2", y, y2);
335
336
337        x = t.getAscendComparator().compare(a, a);
338        y = t.getAscendComparator().compare(b, b);
339        assertEquals("x = 0", 0, x);
340        assertEquals("y = 0", 0, y);
341
342        x2 = t2.getAscendComparator().compare(a, a);
343        y2 = t2.getAscendComparator().compare(b, b);
344        assertEquals("x2 = 0", 0, x2);
345        assertEquals("y2 = 0", 0, y2);
346
347        assertEquals("x = x2", x, x2);
348        assertEquals("y = y2", y, y2);
349    }
350
351
352    /**
353     * Test descend comparators.
354     */
355    public void testDescendComparator() {
356        float q = (float) 0.9;
357
358        a = ExpVector.random(5, 10, q);
359        b = ExpVector.random(5, 10, q);
360        c = a.sum(b);
361
362        t = TermOrderByName.DegreeLexicographic;
363
364        int x = t.getDescendComparator().compare(c, a);
365        int y = t.getDescendComparator().compare(c, b);
366
367        assertEquals("x = -1", -1, x);
368        assertEquals("y = -1", -1, y);
369
370        x = t.getDescendComparator().compare(a, c);
371        y = t.getDescendComparator().compare(b, c);
372
373        assertEquals("x = 1", 1, x);
374        assertEquals("y = 1", 1, y);
375
376        x = t.getDescendComparator().compare(a, a);
377        y = t.getDescendComparator().compare(b, b);
378
379        assertEquals("x = 0", 0, x);
380        assertEquals("y = 0", 0, y);
381    }
382
383
384    /**
385     * Test descend comparators split.
386     */
387    public void testDescendComparatorSplit() {
388        float q = (float) 0.9;
389        int r = 10;
390        int sp = 5;
391
392        a = ExpVector.random(r, 10, q);
393        b = ExpVector.random(r, 10, q);
394        c = a.sum(b);
395
396        t = TermOrderByName.blockOrder(TermOrderByName.DegreeLexicographic, TermOrderByName.Lexicographic, c,
397                        sp);
398
399        int x = t.getDescendComparator().compare(c, a);
400        int y = t.getDescendComparator().compare(c, b);
401        assertEquals("x = -1", -1, x);
402        assertEquals("y = -1", -1, y);
403
404        x = t.getDescendComparator().compare(a, c);
405        y = t.getDescendComparator().compare(b, c);
406        assertEquals("x = 1", 1, x);
407        assertEquals("y = 1", 1, y);
408
409        x = t.getDescendComparator().compare(a, a);
410        y = t.getDescendComparator().compare(b, b);
411        assertEquals("x = 0", 0, x);
412        assertEquals("y = 0", 0, y);
413    }
414
415
416    /**
417     * Test descend comparators weight and split.
418     */
419    public void testDescendComparatorWeightSplit() {
420        float q = (float) 0.9;
421        int r = 8;
422        //int sp = 5;
423
424        a = ExpVector.random(r, 10, q);
425        b = ExpVector.random(r, 10, q);
426        c = a.sum(b);
427
428        //long [][] w  = new long [][] { new long[] { 1l, 2l, 3l, 4l, 5l, 1l, 2l, 3l } };
429        long[][] w2 = new long[][] { new long[] { 1l, 2l, 3l, 4l, 5l, 0l, 0l, 0l },
430                new long[] { 0l, 0l, 0l, 0l, 0l, 1l, 2l, 3l } };
431        //t = new TermOrder(w,sp);
432        t = TermOrderByName.weightOrder(w2);
433        TermOrder t2 = TermOrderByName.weightOrder(w2);
434        assertEquals("t = t2", t, t2);
435
436        int x = t.getDescendComparator().compare(c, a);
437        int y = t.getDescendComparator().compare(c, b);
438        assertEquals("x = -1", -1, x);
439        assertEquals("y = -1", -1, y);
440
441        int x2 = t2.getDescendComparator().compare(c, a);
442        int y2 = t2.getDescendComparator().compare(c, b);
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.getDescendComparator().compare(a, c);
451        y = t.getDescendComparator().compare(b, c);
452        assertEquals("x = 1", 1, x);
453        assertEquals("y = 1", 1, y);
454
455        x2 = t2.getDescendComparator().compare(a, c);
456        y2 = t2.getDescendComparator().compare(b, c);
457        assertEquals("x2 = 1", 1, x2);
458        assertEquals("y2 = 1", 1, y2);
459
460        assertEquals("x = x2", x, x2);
461        assertEquals("y = y2", y, y2);
462
463
464        x = t.getDescendComparator().compare(a, a);
465        y = t.getDescendComparator().compare(b, b);
466        assertEquals("x = 0", 0, x);
467        assertEquals("y = 0", 0, y);
468
469        x2 = t2.getDescendComparator().compare(a, a);
470        y2 = t2.getDescendComparator().compare(b, b);
471        assertEquals("x2 = 0", 0, x2);
472        assertEquals("y2 = 0", 0, y2);
473
474        assertEquals("x = x2", x, x2);
475        assertEquals("y = y2", y, y2);
476    }
477
478
479    /**
480     * Test compare exception split.
481     */
482    public void testCompareExceptionSplit() {
483        float q = (float) 0.9;
484        int r = 10;
485        int sp = 5;
486
487        a = ExpVector.random(r, 10, q);
488        b = ExpVector.random(r, 10, q);
489        c = ExpVector.random(2, 10, q);
490
491        TermOrder t2 = TermOrderByName.REVITDG;
492        int x = 0;
493
494        try {
495            t = TermOrderByName.blockOrder(t2, t2, c, sp);
496            x = t.getDescendComparator().compare(a, b);
497            fail("IllegalArgumentException " + x);
498        } catch (IllegalArgumentException e) {
499            //return;
500        } catch (NullPointerException e) {
501            //return;
502        }
503    }
504
505
506    /**
507     * Test compare exception weight.
508     */
509    public void testCompareExceptionWeigth() {
510        float q = (float) 0.9;
511        int r = 10;
512
513        a = ExpVector.random(r, 10, q);
514        b = ExpVector.random(2, 10, q);
515
516        int x = 0;
517
518        try {
519            t = TermOrderByName.weightOrder((long[][]) null);
520            x = t.getDescendComparator().compare(a, b);
521            fail("IllegalArgumentException " + x);
522        } catch (IllegalArgumentException e) {
523            //return;
524        } catch (NullPointerException e) {
525            //return;
526        } catch (ArrayIndexOutOfBoundsException e) {
527            //return;
528        }
529    }
530
531
532    /**
533     * Test weight for order.
534     */
535    public void testWeigthForOrder() {
536        int r = 10;
537        long[][] w;
538
539        w = TermOrderByName.weightForOrder(TermOrderByName.INVLEX, r);
540        //System.out.println("w = " + Arrays.toString(w[0]));
541        assertEquals("w[0][0] = 1", w[0][0], 1L);
542        assertEquals("w[0][r-1] = 1", w[0][r - 1], 0L);
543
544        w = TermOrderByName.weightForOrder(TermOrderByName.IGRLEX, r);
545        //System.out.println("w = " + Arrays.toString(w[0]));
546        assertEquals("w[0][0] = 1", w[0][0], 1L);
547        assertEquals("w[0][r-1] = 1", w[0][r - 1], 1L);
548
549        w = TermOrderByName.weightForOrder(TermOrderByName.REVILEX, r);
550        //System.out.println("w = " + Arrays.toString(w[0]));
551        assertEquals("w[0][r-1] = 1", w[0][r - 1], 1L);
552        assertEquals("w[0][0] = 0", w[0][0], 0L);
553    }
554
555}