001/* 002 * $Id: BigComplexTest.java 6014 2020-04-29 21:18:51Z kredel $ 003 */ 004 005package edu.jas.arith; 006 007 008import junit.framework.Test; 009import junit.framework.TestCase; 010import junit.framework.TestSuite; 011 012 013/** 014 * BigComplex tests with JUnit. 015 * @author Heinz Kredel 016 */ 017 018public class BigComplexTest extends TestCase { 019 020 021 /** 022 * main 023 */ 024 public static void main(String[] args) { 025 junit.textui.TestRunner.run(suite()); 026 } 027 028 029 /** 030 * Constructs a <CODE>BigComplexTest</CODE> object. 031 * @param name String. 032 */ 033 public BigComplexTest(String name) { 034 super(name); 035 } 036 037 038 /** 039 * suite. 040 */ 041 public static Test suite() { 042 TestSuite suite = new TestSuite(BigComplexTest.class); 043 return suite; 044 } 045 046 047 BigComplex a, b, c, d, e; 048 049 050 BigComplex fac; 051 052 053 @Override 054 protected void setUp() { 055 a = b = c = d = e = null; 056 fac = new BigComplex(); 057 } 058 059 060 @Override 061 protected void tearDown() { 062 a = b = c = d = e = null; 063 fac = null; 064 } 065 066 067 /** 068 * Test static initialization and constants. 069 */ 070 public void testConstants() { 071 a = BigComplex.ZERO; 072 b = BigComplex.ONE; 073 c = BigComplex.CDIF(b, b); 074 d = BigComplex.I; 075 e = BigComplex.CDIF(d, d); 076 077 assertEquals("1-1 = 0", c, a); 078 assertTrue("1-1 = 0", c.isZERO()); 079 assertTrue("1 = 1", b.isONE()); 080 assertEquals("1-1 = 0", c, a); 081 assertTrue("i = i", d.isIMAG()); 082 assertTrue("i-i = 0", e.isZERO()); 083 } 084 085 086 /** 087 * Test bitLength. 088 */ 089 public void testBitLength() { 090 a = BigComplex.ZERO; 091 b = BigComplex.ONE; 092 c = BigComplex.CDIF(b, b); 093 d = BigComplex.I; 094 e = BigComplex.CDIF(d, d); 095 096 assertEquals("len(0) = 6", 6, a.bitLength()); 097 assertEquals("len(1) = 7", 7, b.bitLength()); 098 assertEquals("len(-1) = 7", 7, b.negate().bitLength()); 099 assertEquals("len(i) = 7", 7, d.bitLength()); 100 assertEquals("len(-i) = 7", 7, d.negate().bitLength()); 101 102 e = BigComplex.CDIF(b, d); 103 assertEquals("len(1-i) = 8", 8, e.bitLength()); 104 } 105 106 107 /** 108 * Test constructor and toString. 109 */ 110 public void testConstructor() { 111 a = new BigComplex("6/8"); 112 b = new BigComplex("3/4"); 113 114 assertEquals("6/8 = 3/4", a, b); 115 116 a = new BigComplex("3/4 i 4/5"); 117 b = new BigComplex("-3/4 i -4/5"); 118 119 assertEquals("3/4 + i 4/5 ", a, b.negate()); 120 121 String s = "6/1111111111111111111111111111111111111111111"; 122 a = new BigComplex(s); 123 String t = a.toString(); 124 125 assertEquals("stringConstr = toString", s, t); 126 127 a = new BigComplex(1); 128 b = new BigComplex(-1); 129 c = BigComplex.CSUM(b, a); 130 131 assertTrue("1 = 1", a.isONE()); 132 assertEquals("1+(-1) = 0", c, BigComplex.ZERO); 133 } 134 135 136 /** 137 * Test random rationals. 138 */ 139 public void testRandom() { 140 a = BigComplex.CRAND(500); 141 b = new BigComplex(a.getRe(), a.getIm()); 142 c = BigComplex.CDIF(b, a); 143 144 assertEquals("a-b = 0", c, BigComplex.ZERO); 145 146 d = new BigComplex(b.getRe(), b.getIm()); 147 assertEquals("sign(a-a) = 0", 0, b.compareTo(d)); 148 } 149 150 151 /** 152 * Test addition. 153 */ 154 public void testAddition() { 155 a = BigComplex.CRAND(100); 156 b = BigComplex.CSUM(a, a); 157 c = BigComplex.CDIF(b, a); 158 159 assertEquals("a+a-a = a", c, a); 160 assertEquals("a+a-a = a", 0, c.compareTo(a)); 161 162 b = fac.random(5); 163 c = a.sum(b); 164 d = b.sum(a); 165 assertEquals("a+b == b+a: " + c.subtract(d), c, d); 166 167 d = BigComplex.CSUM(a, BigComplex.ZERO); 168 assertEquals("a+0 = a", d, a); 169 d = BigComplex.CDIF(a, BigComplex.ZERO); 170 assertEquals("a-0 = a", d, a); 171 d = BigComplex.CDIF(a, a); 172 assertEquals("a-a = 0", d, BigComplex.ZERO); 173 } 174 175 176 /** 177 * Test multiplication. 178 */ 179 public void testMultiplication() { 180 a = BigComplex.CRAND(100); 181 b = BigComplex.CPROD(a, a); 182 c = BigComplex.CQ(b, a); 183 184 assertEquals("a*a/a = a", c, a); 185 assertEquals("a*a/a = a", 0, c.compareTo(a)); 186 187 d = BigComplex.CPROD(a, BigComplex.ONE); 188 assertEquals("a*1 = a", d, a); 189 d = BigComplex.CQ(a, BigComplex.ONE); 190 assertEquals("a/1 = a", d, a); 191 192 b = fac.random(5); 193 c = a.multiply(b); 194 d = b.multiply(a); 195 assertEquals("a*b == b*a: " + c.subtract(d), c, d); 196 197 a = BigComplex.CRAND(100); 198 b = BigComplex.CINV(a); 199 c = BigComplex.CPROD(a, b); 200 201 assertTrue("a*1/a = 1", c.isONE()); 202 } 203 204 205 /** 206 * Test distributive law. 207 */ 208 public void testDistributive() { 209 BigComplex fac = new BigComplex(); 210 a = fac.random(500); 211 b = fac.random(500); 212 c = fac.random(500); 213 214 d = a.multiply(b.sum(c)); 215 e = a.multiply(b).sum(a.multiply(c)); 216 217 assertEquals("a(b+c) = ab+ac", d, e); 218 } 219 220 221 /** 222 * Test power and abs. 223 */ 224 public void testOther() { 225 BigComplex fac = new BigComplex(); 226 a = fac.random(100); 227 b = a.multiply(a); 228 c = a.power(2); 229 230 assertEquals("a*a == a**2", b, c); 231 232 d = b.abs(); 233 c = b.norm(); 234 //System.out.println("a = " + a); 235 //System.out.println("b = " + b); 236 //System.out.println("c = " + c); 237 //System.out.println("d = " + d); 238 BigDecimal dd = new BigDecimal(d.re); 239 BigDecimal cd = new BigDecimal(c.re); 240 int t = dd.power(2).compareToRelative(cd); 241 assertTrue("abs(a)**2 ~= norm(a): ", t == 0); 242 } 243 244}