001/*
002 * $Id: PolynomialListTest.java 5931 2018-09-23 17:21:33Z kredel $
003 */
004
005package edu.jas.poly;
006
007import java.util.ArrayList;
008import java.util.List;
009
010import junit.framework.Test;
011import junit.framework.TestCase;
012import junit.framework.TestSuite;
013import edu.jas.arith.BigRational;
014
015
016/**
017 * PolynomialList Test using JUnit.
018 * @author Heinz Kredel
019 */
020
021public class PolynomialListTest extends TestCase {
022
023    /**
024     * main.
025     */
026    public static void main (String[] args) {
027        junit.textui.TestRunner.run( suite() );
028    }
029
030
031    /**
032     * Constructs a <CODE>PolynomialListTest</CODE> object.
033     * @param name String.
034     */
035    public PolynomialListTest(String name) {
036        super(name);
037    }
038
039
040    /**
041     */ 
042    public static Test suite() {
043        TestSuite suite= new TestSuite(PolynomialListTest.class);
044        return suite;
045    }
046
047    GenPolynomialRing<BigRational> fac;
048
049    PolynomialList<BigRational> m;
050    PolynomialList<BigRational> p;
051
052    GenPolynomial<BigRational> a, b, c, d, e;
053
054    int rl = 4; 
055    int kl = 4;
056    int ll = 4;
057    int el = 5;
058    float q = 0.5f;
059
060
061    protected void setUp() {
062        a = b = c = d = e = null;
063        m = null;
064        p = null;
065        BigRational coeff = new BigRational(9);
066        fac = new GenPolynomialRing<BigRational>(coeff,rl);
067    }
068
069
070    protected void tearDown() {
071        a = b = c = d = e = null;
072        m = null;
073        p = null;
074    }
075
076
077    /**
078     * Test constructor and toString.
079     */
080    public void testConstructor() {
081        p = new PolynomialList<BigRational>(fac,(List<GenPolynomial<BigRational>>)null);
082        assertTrue("p = 0", p.list == null);
083
084        m = new PolynomialList<BigRational>(fac,
085                                            new ArrayList<GenPolynomial<BigRational>>());
086        assertTrue("m = 0", m.list != null);
087        assertTrue("m.size() == 0", m.list.size() == 0 );
088    }
089
090
091    /**
092     * Test polynomial list.
093     */
094    public void testPolynomialList() {
095        List<GenPolynomial<BigRational>> l = new ArrayList<GenPolynomial<BigRational>>();
096        for (int i = 0; i < 7; i++) {
097            a = fac.random(ll+i); 
098            assertTrue("length( a"+i+" ) <> 0", a.length() >= 0);
099            assertTrue(" not isZERO( a"+i+" )", !a.isZERO() );
100            assertTrue(" not isONE( a"+i+" )", !a.isONE() );
101            l.add( a );
102        }
103        p = new PolynomialList<BigRational>(fac,l);
104        //System.out.println("p = "+p);
105
106        assertTrue("p == p", p.equals(p) );
107        assertEquals("p.length", 7, p.list.size() );
108    }
109
110
111    /**
112     * Test ordered polynomial list.
113     */
114    public void testOrderedPolynomialList() {
115        List<GenPolynomial<BigRational>> l = new ArrayList<GenPolynomial<BigRational>>();
116        for (int i = 0; i < 7; i++) {
117            a = fac.random(ll+i); 
118            assertTrue("length( a"+i+" ) <> 0", a.length() >= 0);
119            assertTrue(" not isZERO( a"+i+" )", !a.isZERO() );
120            assertTrue(" not isONE( a"+i+" )", !a.isONE() );
121            l.add( a );
122        }
123        p = new PolynomialList<BigRational>(fac,l);
124        //System.out.println("p = "+p);
125
126        m = new OrderedPolynomialList<BigRational>(fac,p.list);
127        //System.out.println("m = "+m);
128 
129        assertTrue("p == m", p.equals(m) );
130        assertTrue("m != p", !m.equals(p) );
131        assertEquals("p.length", 7, p.list.size() );
132        assertEquals("m.length", 7, m.list.size() );
133    }
134
135
136    /**
137     * Test homogeneous polynomial list.
138     */
139    public void testHomogeneousPolynomialList() {
140        List<GenPolynomial<BigRational>> l = new ArrayList<GenPolynomial<BigRational>>();
141        for (int i = 0; i < 7; i++) {
142            a = fac.random(ll+i); 
143            assertTrue("length( a"+i+" ) <> 0", a.length() >= 0);
144            assertTrue(" not isZERO( a"+i+" )", !a.isZERO() );
145            assertTrue(" not isONE( a"+i+" )", !a.isONE() );
146            l.add( a );
147        }
148        p = new PolynomialList<BigRational>(fac,l);
149        //System.out.println("p = "+p);
150
151        PolynomialList<BigRational> h = p.homogenize();
152        //System.out.println("h = "+h);
153        assertTrue("h is homogen", h.isHomogeneous() );
154
155        PolynomialList<BigRational> pp = h.deHomogenize();
156        //System.out.println("pp = "+pp);
157        assertTrue("p == pp", p.equals(pp) );
158    }
159
160}