001 /* 002 * $Id: LISTTest.java 1899 2008-07-12 14:04:08Z kredel $ 003 */ 004 005 package edu.mas.kern; 006 007 008 import edu.jas.arith.BigRational; 009 010 import static edu.mas.kern.LIST.*; 011 012 import java.util.List; 013 import java.util.ArrayList; 014 015 import junit.framework.Test; 016 import junit.framework.TestCase; 017 import junit.framework.TestSuite; 018 019 020 /** 021 * Basic list processing tests with JUnit. 022 * @author Heinz Kredel. 023 */ 024 025 public class LISTTest extends TestCase { 026 027 /** 028 * main. 029 */ 030 public static void main (String[] args) { 031 junit.textui.TestRunner.run( suite() ); 032 } 033 034 /** 035 * Constructs a <CODE>LISTTest</CODE> object. 036 * @param name String. 037 */ 038 public LISTTest(String name) { 039 super(name); 040 } 041 042 /** 043 * suite. 044 */ 045 public static Test suite() { 046 TestSuite suite= new TestSuite(LISTTest.class); 047 return suite; 048 } 049 050 051 boolean timing =false; 052 053 protected void setUp() { 054 //a = b = c = d = e = null; 055 } 056 057 protected void tearDown() { 058 //a = b = c = d = e = null; 059 } 060 061 062 /** 063 * Test static initialization LIST. 064 */ 065 public void testLISTinit() { 066 LIST<Object> a = null; 067 assertTrue("a == () ", isEmpty(a) ); 068 assertEquals("len(a) == 0 ", LENGTH(a), 0 ); 069 a = new LIST<Object>(); 070 assertTrue("a == () ", isEmpty(a) ); 071 assertEquals("len(a) == 0 ", LENGTH(a), 0 ); 072 //System.out.println("a = " + a); 073 a = new LIST<Object>( a.list ); 074 assertTrue("a == () ", isEmpty(a) ); 075 assertEquals("len(a) == 0 ", LENGTH(a), 0 ); 076 } 077 078 079 /** 080 * Test static LIST creation. 081 */ 082 public void testLISTcreate() { 083 Object five = 5; 084 LIST<Object> a = null; 085 assertTrue("a == () ", isEmpty(a) ); 086 assertEquals("len(a) == 0 ", LENGTH(a), 0 ); 087 //System.out.println("a = " + a); 088 a = LIST1( five ); 089 assertFalse("a != () ", isEmpty(a) ); 090 assertEquals("len(a) == 1 ", LENGTH(a), 1 ); 091 //System.out.println("a = " + a); 092 a = COMP( five, a ); 093 assertFalse("a != () ", isEmpty(a) ); 094 assertEquals("len(a) == 2 ", LENGTH(a), 2 ); 095 //System.out.println("a = " + a); 096 097 LIST<Object> b = LIST2(five,five); 098 assertFalse("b != () ", isEmpty(b) ); 099 assertEquals("len(a) == 2 ", LENGTH(a), 2 ); 100 assertTrue("a == b ", EQUAL(a,b) ); 101 } 102 103 104 /** 105 * Test static LIST operations. 106 */ 107 public void testLISToper() { 108 Object five = 5; 109 LIST<Object> a = LIST1( five ); 110 assertFalse("a != () ", isEmpty(a) ); 111 assertEquals("len(a) == 1 ", LENGTH(a), 1 ); 112 a = COMP( five, a ); 113 assertFalse("a != () ", isEmpty(a) ); 114 assertEquals("len(a) == 2 ", LENGTH(a), 2 ); 115 116 LIST<Object> b = CINV(a); 117 assertFalse("b != () ", isEmpty(b) ); 118 assertEquals("len(a) == 2 ", LENGTH(a), 2 ); 119 assertTrue("a == b ", EQUAL(a,b) ); 120 121 LIST<Object> c = INV(a); 122 assertFalse("c != () ", isEmpty(c) ); 123 assertEquals("len(c) == 2 ", LENGTH(c), 2 ); 124 assertTrue("a == c ", EQUAL(a,c) ); 125 //System.out.println("a = " + a); 126 //System.out.println("c = " + c); 127 } 128 129 130 /** 131 * Test static LIST many elements. 132 */ 133 public void testLISTelems() { 134 int max = 100; 135 Object n; 136 LIST<Object> a = null; 137 for ( int i = 0; i < max; i++ ) { 138 n = i; 139 a = COMP( n, a ); 140 } 141 assertFalse("a != () ", isEmpty(a) ); 142 assertEquals("len(a) == "+max+" ", LENGTH(a), max ); 143 //System.out.println("a = " + a); 144 145 LIST<Object> b = CINV(a); 146 assertFalse("b != () ", isEmpty(b) ); 147 assertEquals("len(b) == "+max+" ", LENGTH(b), max ); 148 //System.out.println("b = " + b); 149 150 b = INV( b ); 151 assertFalse("b != () ", isEmpty(b) ); 152 assertEquals("len(b) == "+max+" ", LENGTH(b), max ); 153 //System.out.println("b = " + b); 154 assertTrue("a == INV(CINV(a)) ", EQUAL(a,b) ); 155 } 156 157 158 /** 159 * Test static LIST destructive operations. 160 */ 161 public void testLISTdestruct() { 162 Object n = 5; 163 LIST<Object> a = LIST1( n ); 164 LIST<Object> b = LIST1( n ); 165 assertEquals("len(a) == 1 ", LENGTH(a), 1 ); 166 assertEquals("len(b) == 1 ", LENGTH(b), 1 ); 167 SRED(a,b); 168 assertFalse("a != () ", isEmpty(a) ); 169 assertFalse("b != () ", isEmpty(b) ); 170 assertEquals("len(b) == 1 ", LENGTH(b), 1 ); 171 assertEquals("len(a) == 2 ", LENGTH(a), 2 ); 172 //System.out.println("a = " + a); 173 174 n = 7; 175 SFIRST(a,n); 176 //System.out.println("a = " + a); 177 assertEquals("len(a) == 2 ", LENGTH(a), 2 ); 178 LIST<Object> c = COMP( n, b ); 179 assertEquals("len(c) == 2 ", LENGTH(c), 2 ); 180 assertTrue("a == c ", EQUAL(a,c) ); 181 } 182 183 184 /** 185 * Test static LIST recursive structures. 186 */ 187 public void testLISTrecursive() { 188 Object n = 5; 189 LIST<Object> a = LIST1( n ); 190 LIST<LIST<Object>> b = LIST2( a, a ); 191 LIST<LIST<LIST<Object>>> c = LIST3( b, b, b ); 192 //System.out.println("a = " + a); 193 //System.out.println("b = " + b); 194 //System.out.println("c = " + c); 195 assertEquals("len(a) == 1 ", LENGTH(a), 1 ); 196 assertEquals("len(b) == 2 ", LENGTH(b), 2 ); 197 assertEquals("len(c) == 3 ", LENGTH(c), 3 ); 198 199 //System.out.println("EXTENT(a) = " + EXTENT(a)); 200 //System.out.println("EXTENT(b) = " + EXTENT(b)); 201 //System.out.println("EXTENT(c) = " + EXTENT(c)); 202 assertEquals("EXTENT(a) == 1 ", EXTENT(a), 1 ); 203 assertEquals("EXTENT(b) == 2 ", EXTENT(b), 2 ); 204 assertEquals("EXTENT(c) == 6 ", EXTENT(c), 6 ); 205 206 //System.out.println("ORDER(a) = " + ORDER(a)); 207 //System.out.println("ORDER(b) = " + ORDER(b)); 208 //System.out.println("ORDER(c) = " + ORDER(c)); 209 assertEquals("ORDER(a) == 1 ", ORDER(a), 1 ); 210 assertEquals("ORDER(b) == 2 ", ORDER(b), 2 ); 211 assertEquals("ORDER(c) == 3 ", ORDER(c), 3 ); 212 } 213 214 215 /** 216 * Test static LIST rational content. 217 */ 218 public void testLISTcontent() { 219 int max = 5000; 220 long t0, t1; 221 BigRational cf = new BigRational(2,3); 222 BigRational n; 223 LIST<BigRational> a = null; 224 t0 = System.currentTimeMillis(); 225 for ( int i = 0; i < max; i++ ) { 226 n = cf.random(5); 227 a = COMP( n, a ); 228 } 229 t1 = System.currentTimeMillis(); 230 if ( timing ) System.out.println("t.comp = " + (t1-t0)); 231 //System.out.println("a = " + LENGTH(a)); 232 assertEquals("len(a) == "+max+" ", LENGTH(a), max ); 233 234 List<BigRational> b = new ArrayList<BigRational>(); 235 /* is/was inefficient */ 236 LIST<BigRational> ap = a; 237 t0 = System.currentTimeMillis(); 238 while ( ! isEmpty( ap ) ) { 239 n = FIRST(ap); ap = RED(ap); 240 b.add( n ); 241 //System.out.println("n = " + n); 242 } 243 t1 = System.currentTimeMillis(); 244 if ( timing ) System.out.println("t.red = " + (t1-t0)); 245 //System.out.println("b = " + b.size()); 246 assertEquals("size(b) == "+max+" ", b.size(), max ); 247 248 b = new ArrayList<BigRational>(); 249 int len = LENGTH( a ); 250 t0 = System.currentTimeMillis(); 251 for ( int i = 0; i < len; i++ ) { 252 n = LELT(a,i); 253 b.add( n ); 254 } 255 t1 = System.currentTimeMillis(); 256 if ( timing ) System.out.println("t.lelt = " + (t1-t0)); 257 //System.out.println("b = " + b.size()); 258 259 LIST<BigRational> c = new LIST<BigRational>( b ); 260 //System.out.println("c = " + LENGTH(c)); 261 assertEquals("len(c) == "+max+" ", LENGTH(c), max ); 262 assertTrue("a == c ", EQUAL(a,c) ); 263 } 264 265 }