001 /*
002 * $Id: ListUtilTest.java 3443 2010-12-25 16:49:30Z kredel $
003 */
004
005 package edu.jas.util;
006
007
008 import java.util.ArrayList;
009 import java.util.List;
010
011 import junit.framework.Test;
012 import junit.framework.TestCase;
013 import junit.framework.TestSuite;
014
015 import edu.jas.arith.BigInteger;
016 import edu.jas.structure.RingElem;
017 import edu.jas.structure.UnaryFunctor;
018
019
020 /**
021 * ListUtil tests with JUnit.
022 * @author Heinz Kredel.
023 */
024 public class ListUtilTest extends TestCase {
025
026
027 /**
028 * main.
029 */
030 public static void main(String[] args) {
031 junit.textui.TestRunner.run(suite());
032 }
033
034
035 /**
036 * Constructs a <CODE>ListUtilTest</CODE> object.
037 * @param name String.
038 */
039 public ListUtilTest(String name) {
040 super(name);
041 }
042
043
044 /**
045 */
046 public static Test suite() {
047 TestSuite suite = new TestSuite(ListUtilTest.class);
048 return suite;
049 }
050
051
052 BigInteger ai;
053
054
055 BigInteger bi;
056
057
058 BigInteger ci;
059
060
061 BigInteger di;
062
063
064 BigInteger ei;
065
066
067 @Override
068 protected void setUp() {
069 ai = bi = ci = di = ei = null;
070 }
071
072
073 @Override
074 protected void tearDown() {
075 ai = bi = ci = di = ei = null;
076 }
077
078
079 /**
080 * Test list map.
081 *
082 */
083 public void testListMap() {
084 ai = new BigInteger();
085 List<BigInteger> list = new ArrayList<BigInteger>();
086 for (int i = 0; i < 10; i++) {
087 list.add(ai.random(7));
088 }
089 bi = ai.getONE();
090 List<BigInteger> nl;
091 nl = ListUtil.<BigInteger, BigInteger> map(list, new Multiply<BigInteger>(bi));
092 assertEquals("list == nl ", list, nl);
093 }
094
095
096 /**
097 * Test tuple transpose.
098 *
099 */
100 public void testTuple() {
101 ai = new BigInteger();
102 List<List<BigInteger>> tlist = new ArrayList<List<BigInteger>>();
103 int s1 = 4;
104 int s2 = 3;
105 int s = 1;
106 for (int i = 0; i < s1; i++) {
107 s *= s2;
108 }
109 //System.out.println("s = " + s);
110 for (int i = 0; i < s1; i++) {
111 List<BigInteger> list = new ArrayList<BigInteger>();
112 for (int j = 0; j < s2; j++) {
113 list.add(ai.random(7));
114 }
115 tlist.add(list);
116 }
117 //System.out.println("tlist = " + tlist);
118
119 List<List<BigInteger>> ltuple = ListUtil.<BigInteger> tupleFromList(tlist);
120 //System.out.println("ltuple = " + ltuple);
121 assertTrue("#ltuple == " + s + " ", ltuple.size() == s);
122
123 for (List<BigInteger> t : ltuple) {
124 assertTrue("#t == " + s1 + " ", t.size() == s1);
125 }
126 }
127
128 }
129
130
131 /**
132 * Internal scalar multiplication functor.
133 */
134 class Multiply<C extends RingElem<C>> implements UnaryFunctor<C, C> {
135
136
137 C x;
138
139
140 public Multiply(C x) {
141 this.x = x;
142 }
143
144
145 public C eval(C c) {
146 return c.multiply(x);
147 }
148 }