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