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