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