001 /* 002 * $Id: BigComplexTest.java 1244 2007-07-29 09:54:27Z kredel $ 003 */ 004 005 package edu.jas.arith; 006 007 //import edu.jas.arith.BigRational; 008 009 import junit.framework.Test; 010 import junit.framework.TestCase; 011 import junit.framework.TestSuite; 012 013 /** 014 * BigComplex tests with JUnit. 015 * @author Heinz Kredel. 016 */ 017 018 public class BigComplexTest extends TestCase { 019 020 /** 021 * main 022 */ 023 public static void main (String[] args) { 024 junit.textui.TestRunner.run( suite() ); 025 } 026 027 /** 028 * Constructs a <CODE>BigComplexTest</CODE> object. 029 * @param name String. 030 */ 031 public BigComplexTest(String name) { 032 super(name); 033 } 034 035 /** 036 * suite. 037 */ 038 public static Test suite() { 039 TestSuite suite= new TestSuite(BigComplexTest.class); 040 return suite; 041 } 042 043 BigComplex a; 044 BigComplex b; 045 BigComplex c; 046 BigComplex d; 047 BigComplex e; 048 049 protected void setUp() { 050 a = b = c = d = e = null; 051 } 052 053 protected void tearDown() { 054 a = b = c = d = e = null; 055 } 056 057 058 /** 059 * Test static initialization and constants. 060 * 061 */ 062 public void testConstants() { 063 a = BigComplex.ZERO; 064 b = BigComplex.ONE; 065 c = BigComplex.CDIF(b,b); 066 067 assertEquals("1-1 = 0",c,a); 068 assertTrue("1-1 = 0",c.isZERO()); 069 assertTrue("1 = 1", b.isONE() ); 070 071 a = BigComplex.ZERO; 072 b = BigComplex.ONE; 073 c = BigComplex.CDIF(b,b); 074 075 assertEquals("1-1 = 0",c,a); 076 } 077 078 079 /** 080 * Test constructor and toString. 081 * 082 */ 083 public void testConstructor() { 084 a = new BigComplex( "6/8" ); 085 b = new BigComplex( "3/4" ); 086 087 assertEquals("6/8 = 3/4",a,b); 088 089 a = new BigComplex( "3/4 i 4/5" ); 090 b = new BigComplex( "-3/4 i -4/5" ); 091 092 assertEquals("3/4 + i 4/5 ",a,b.negate()); 093 094 String s = "6/1111111111111111111111111111111111111111111"; 095 a = new BigComplex( s ); 096 String t = a.toString(); 097 098 assertEquals("stringConstr = toString",s,t); 099 100 a = new BigComplex( 1 ); 101 b = new BigComplex( -1 ); 102 c = BigComplex.CSUM(b,a); 103 104 assertTrue("1 = 1", a.isONE() ); 105 assertEquals("1+(-1) = 0",c,BigComplex.ZERO); 106 } 107 108 109 /** 110 * Test random rationals. 111 * 112 */ 113 public void testRandom() { 114 a = BigComplex.CRAND( 500 ); 115 b = new BigComplex( a.getRe(), a.getIm() ); 116 c = BigComplex.CDIF(b,a); 117 118 assertEquals("a-b = 0",c,BigComplex.ZERO); 119 120 d = new BigComplex( b.getRe(), b.getIm() ); 121 assertEquals("sign(a-a) = 0", 0, b.compareTo(d) ); 122 } 123 124 125 /** 126 * Test addition. 127 * 128 */ 129 public void testAddition() { 130 a = BigComplex.CRAND( 100 ); 131 b = BigComplex.CSUM( a, a ); 132 c = BigComplex.CDIF( b, a ); 133 134 assertEquals("a+a-a = a",c,a); 135 assertEquals("a+a-a = a",0,c.compareTo(a)); 136 137 d = BigComplex.CSUM( a, BigComplex.ZERO ); 138 assertEquals("a+0 = a",d,a); 139 d = BigComplex.CDIF( a, BigComplex.ZERO ); 140 assertEquals("a-0 = a",d,a); 141 d = BigComplex.CDIF( a, a ); 142 assertEquals("a-a = 0",d,BigComplex.ZERO); 143 144 } 145 146 147 /** 148 * Test multiplication. 149 * 150 */ 151 public void testMultiplication() { 152 a = BigComplex.CRAND( 100 ); 153 b = BigComplex.CPROD( a, a ); 154 c = BigComplex.CQ( b, a ); 155 156 assertEquals("a*a/a = a",c,a); 157 assertEquals("a*a/a = a",0,c.compareTo(a)); 158 159 d = BigComplex.CPROD( a, BigComplex.ONE ); 160 assertEquals("a*1 = a",d,a); 161 d = BigComplex.CQ( a, BigComplex.ONE ); 162 assertEquals("a/1 = a",d,a); 163 164 a = BigComplex.CRAND( 100 ); 165 b = BigComplex.CINV( a ); 166 c = BigComplex.CPROD( a, b ); 167 168 assertTrue("a*1/a = 1", c.isONE() ); 169 } 170 171 172 /** 173 * Test distributive law. 174 * 175 */ 176 public void testDistributive() { 177 BigComplex fac = new BigComplex(); 178 179 a = fac.random( 500 ); 180 b = fac.random( 500 ); 181 c = fac.random( 500 ); 182 183 d = a.multiply( b.sum(c) ); 184 e = a.multiply( b ).sum( a.multiply(c) ); 185 186 assertEquals("a(b+c) = ab+ac",d,e); 187 } 188 189 }