1 16 17 package org.apache.commons.math.complex; 18 19 import java.text.NumberFormat ; 20 import java.text.ParseException ; 21 import java.util.Locale ; 22 23 import junit.framework.TestCase; 24 25 public abstract class ComplexFormatAbstractTest extends TestCase { 26 27 ComplexFormat complexFormat = null; 28 ComplexFormat complexFormatJ = null; 29 30 protected abstract Locale getLocale(); 31 32 protected abstract char getDecimalCharacter(); 33 34 protected void setUp() throws Exception { 35 complexFormat = ComplexFormat.getInstance(getLocale()); 36 complexFormatJ = ComplexFormat.getInstance(getLocale()); 37 complexFormatJ.setImaginaryCharacter("j"); 38 } 39 40 public void testSimpleNoDecimals() { 41 Complex c = new Complex(1, 1); 42 String expected = "1 + 1i"; 43 String actual = complexFormat.format(c); 44 assertEquals(expected, actual); 45 } 46 47 public void testSimpleWithDecimals() { 48 Complex c = new Complex(1.23, 1.43); 49 String expected = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i"; 50 String actual = complexFormat.format(c); 51 assertEquals(expected, actual); 52 } 53 54 public void testSimpleWithDecimalsTrunc() { 55 Complex c = new Complex(1.2323, 1.4343); 56 String expected = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i"; 57 String actual = complexFormat.format(c); 58 assertEquals(expected, actual); 59 } 60 61 public void testNegativeReal() { 62 Complex c = new Complex(-1.2323, 1.4343); 63 String expected = "-1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i"; 64 String actual = complexFormat.format(c); 65 assertEquals(expected, actual); 66 } 67 68 public void testNegativeImaginary() { 69 Complex c = new Complex(1.2323, -1.4343); 70 String expected = "1" + getDecimalCharacter() + "23 - 1" + getDecimalCharacter() + "43i"; 71 String actual = complexFormat.format(c); 72 assertEquals(expected, actual); 73 } 74 75 public void testNegativeBoth() { 76 Complex c = new Complex(-1.2323, -1.4343); 77 String expected = "-1" + getDecimalCharacter() + "23 - 1" + getDecimalCharacter() + "43i"; 78 String actual = complexFormat.format(c); 79 assertEquals(expected, actual); 80 } 81 82 public void testZeroReal() { 83 Complex c = new Complex(0.0, -1.4343); 84 String expected = "0 - 1" + getDecimalCharacter() + "43i"; 85 String actual = complexFormat.format(c); 86 assertEquals(expected, actual); 87 } 88 89 public void testZeroImaginary() { 90 Complex c = new Complex(30.233, 0); 91 String expected = "30" + getDecimalCharacter() + "23"; 92 String actual = complexFormat.format(c); 93 assertEquals(expected, actual); 94 } 95 96 public void testDifferentImaginaryChar() { 97 Complex c = new Complex(1, 1); 98 String expected = "1 + 1j"; 99 String actual = complexFormatJ.format(c); 100 assertEquals(expected, actual); 101 } 102 103 public void testStaticFormatComplex() { 104 Locale defaultLocal = Locale.getDefault(); 105 Locale.setDefault(getLocale()); 106 107 Complex c = new Complex(232.222, -342.33); 108 String expected = "232" + getDecimalCharacter() + "22 - 342" + getDecimalCharacter() + "33i"; 109 String actual = ComplexFormat.formatComplex(c); 110 assertEquals(expected, actual); 111 112 Locale.setDefault(defaultLocal); 113 } 114 115 public void testNan() { 116 Complex c = new Complex(Double.NaN, Double.NaN); 117 String expected = "(NaN) + (NaN)i"; 118 String actual = complexFormat.format(c); 119 assertEquals(expected, actual); 120 } 121 122 public void testPositiveInfinity() { 123 Complex c = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); 124 String expected = "(Infinity) + (Infinity)i"; 125 String actual = complexFormat.format(c); 126 assertEquals(expected, actual); 127 } 128 129 public void testNegativeInfinity() { 130 Complex c = new Complex(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY); 131 String expected = "(-Infinity) - (Infinity)i"; 132 String actual = complexFormat.format(c); 133 assertEquals(expected, actual); 134 } 135 136 public void testParseSimpleNoDecimals() { 137 String source = "1 + 1i"; 138 Complex expected = new Complex(1, 1); 139 try { 140 Complex actual = (Complex)complexFormat.parseObject(source); 141 assertEquals(expected, actual); 142 } catch (ParseException ex) { 143 fail(ex.getMessage()); 144 } 145 } 146 147 public void testParseSimpleWithDecimals() { 148 String source = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i"; 149 Complex expected = new Complex(1.23, 1.43); 150 try { 151 Complex actual = (Complex)complexFormat.parseObject(source); 152 assertEquals(expected, actual); 153 } catch (ParseException ex) { 154 fail(ex.getMessage()); 155 } 156 } 157 158 public void testParseSimpleWithDecimalsTrunc() { 159 String source = "1" + getDecimalCharacter() + "2323 + 1" + getDecimalCharacter() + "4343i"; 160 Complex expected = new Complex(1.2323, 1.4343); 161 try { 162 Complex actual = (Complex)complexFormat.parseObject(source); 163 assertEquals(expected, actual); 164 } catch (ParseException ex) { 165 fail(ex.getMessage()); 166 } 167 } 168 169 public void testParseNegativeReal() { 170 String source = "-1" + getDecimalCharacter() + "2323 + 1" + getDecimalCharacter() + "4343i"; 171 Complex expected = new Complex(-1.2323, 1.4343); 172 try { 173 Complex actual = (Complex)complexFormat.parseObject(source); 174 assertEquals(expected, actual); 175 } catch (ParseException ex) { 176 fail(ex.getMessage()); 177 } 178 } 179 180 public void testParseNegativeImaginary() { 181 String source = "1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "4343i"; 182 Complex expected = new Complex(1.2323, -1.4343); 183 try { 184 Complex actual = (Complex)complexFormat.parseObject(source); 185 assertEquals(expected, actual); 186 } catch (ParseException ex) { 187 fail(ex.getMessage()); 188 } 189 } 190 191 public void testParseNegativeBoth() { 192 String source = "-1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "4343i"; 193 Complex expected = new Complex(-1.2323, -1.4343); 194 try { 195 Complex actual = (Complex)complexFormat.parseObject(source); 196 assertEquals(expected, actual); 197 } catch (ParseException ex) { 198 fail(ex.getMessage()); 199 } 200 } 201 202 public void testParseZeroReal() { 203 String source = "0" + getDecimalCharacter() + "0 - 1" + getDecimalCharacter() + "4343i"; 204 Complex expected = new Complex(0.0, -1.4343); 205 try { 206 Complex actual = (Complex)complexFormat.parseObject(source); 207 assertEquals(expected, actual); 208 } catch (ParseException ex) { 209 fail(ex.getMessage()); 210 } 211 } 212 213 public void testParseZeroImaginary() { 214 String source = "-1" + getDecimalCharacter() + "2323"; 215 Complex expected = new Complex(-1.2323, 0); 216 try { 217 Complex actual = (Complex)complexFormat.parseObject(source); 218 assertEquals(expected, actual); 219 } catch (ParseException ex) { 220 fail(ex.getMessage()); 221 } 222 } 223 224 public void testParseDifferentImaginaryChar() { 225 String source = "-1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "4343j"; 226 Complex expected = new Complex(-1.2323, -1.4343); 227 try { 228 Complex actual = (Complex)complexFormatJ.parseObject(source); 229 assertEquals(expected, actual); 230 } catch (ParseException ex) { 231 fail(ex.getMessage()); 232 } 233 } 234 235 public void testParseNan() { 236 String source = "(NaN) + (NaN)i"; 237 Complex expected = new Complex(Double.NaN, Double.NaN); 238 try { 239 Complex actual = (Complex)complexFormat.parseObject(source); 240 assertEquals(expected, actual); 241 } catch (ParseException ex) { 242 fail(ex.getMessage()); 243 } 244 } 245 246 public void testParsePositiveInfinity() { 247 String source = "(Infinity) + (Infinity)i"; 248 Complex expected = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); 249 try { 250 Complex actual = (Complex)complexFormat.parseObject(source); 251 assertEquals(expected, actual); 252 } catch (ParseException ex) { 253 fail(ex.getMessage()); 254 } 255 } 256 257 public void testPaseNegativeInfinity() { 258 String source = "(-Infinity) - (Infinity)i"; 259 Complex expected = new Complex(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY); 260 try { 261 Complex actual = (Complex)complexFormat.parseObject(source); 262 assertEquals(expected, actual); 263 } catch (ParseException ex) { 264 fail(ex.getMessage()); 265 } 266 } 267 268 public void testConstructorSingleFormat() { 269 NumberFormat nf = NumberFormat.getInstance(); 270 ComplexFormat cf = new ComplexFormat(nf); 271 assertNotNull(cf); 272 assertEquals(nf, cf.getRealFormat()); 273 } 274 275 public void testGetImaginaryFormat() { 276 NumberFormat nf = NumberFormat.getInstance(); 277 ComplexFormat cf = new ComplexFormat(); 278 279 assertNotSame(nf, cf.getImaginaryFormat()); 280 cf.setImaginaryFormat(nf); 281 assertSame(nf, cf.getImaginaryFormat()); 282 } 283 284 public void testSetImaginaryFormatNull() { 285 try { 286 ComplexFormat cf = new ComplexFormat(); 287 cf.setImaginaryFormat(null); 288 fail(); 289 } catch (IllegalArgumentException ex) { 290 } 292 } 293 294 public void testSetRealFormatNull() { 295 try { 296 ComplexFormat cf = new ComplexFormat(); 297 cf.setRealFormat(null); 298 fail(); 299 } catch (IllegalArgumentException ex) { 300 } 302 } 303 304 public void testGetRealFormat() { 305 NumberFormat nf = NumberFormat.getInstance(); 306 ComplexFormat cf = new ComplexFormat(); 307 308 assertNotSame(nf, cf.getRealFormat()); 309 cf.setRealFormat(nf); 310 assertSame(nf, cf.getRealFormat()); 311 } 312 313 public void testSetImaginaryCharacterNull() { 314 try { 315 ComplexFormat cf = new ComplexFormat(); 316 cf.setImaginaryCharacter(null); 317 fail(); 318 } catch (IllegalArgumentException ex) { 319 } 321 } 322 323 public void testSetImaginaryCharacterEmpty() { 324 try { 325 ComplexFormat cf = new ComplexFormat(); 326 cf.setImaginaryCharacter(""); 327 fail(); 328 } catch (IllegalArgumentException ex) { 329 } 331 } 332 333 public void testFormatNumber() { 334 ComplexFormat cf = ComplexFormat.getInstance(getLocale()); 335 Double pi = new Double (Math.PI); 336 String text = cf.format(pi); 337 assertEquals("3" + getDecimalCharacter() + "14", text); 338 } 339 340 public void testFormatObject() { 341 try { 342 ComplexFormat cf = new ComplexFormat(); 343 Object object = new Object (); 344 cf.format(object); 345 fail(); 346 } catch (IllegalArgumentException ex) { 347 } 349 } 350 } 351 | Popular Tags |