001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.lang;
019:
020: public class MathTest extends junit.framework.TestCase {
021:
022: double HYP = Math.sqrt(2.0);
023:
024: double OPP = 1.0;
025:
026: double ADJ = 1.0;
027:
028: /* Required to make previous preprocessor flags work - do not remove */
029: int unused = 0;
030:
031: /**
032: * @tests java.lang.Math#abs(double)
033: */
034: public void test_absD() {
035: // Test for method double java.lang.Math.abs(double)
036:
037: assertTrue("Incorrect double abs value",
038: (Math.abs(-1908.8976) == 1908.8976));
039: assertTrue("Incorrect double abs value",
040: (Math.abs(1908.8976) == 1908.8976));
041: }
042:
043: /**
044: * @tests java.lang.Math#abs(float)
045: */
046: public void test_absF() {
047: // Test for method float java.lang.Math.abs(float)
048: assertTrue("Incorrect float abs value",
049: (Math.abs(-1908.8976f) == 1908.8976f));
050: assertTrue("Incorrect float abs value",
051: (Math.abs(1908.8976f) == 1908.8976f));
052: }
053:
054: /**
055: * @tests java.lang.Math#abs(int)
056: */
057: public void test_absI() {
058: // Test for method int java.lang.Math.abs(int)
059: assertTrue("Incorrect int abs value",
060: (Math.abs(-1908897) == 1908897));
061: assertTrue("Incorrect int abs value",
062: (Math.abs(1908897) == 1908897));
063: }
064:
065: /**
066: * @tests java.lang.Math#abs(long)
067: */
068: public void test_absJ() {
069: // Test for method long java.lang.Math.abs(long)
070: assertTrue("Incorrect long abs value", (Math
071: .abs(-19088976000089L) == 19088976000089L));
072: assertTrue("Incorrect long abs value", (Math
073: .abs(19088976000089L) == 19088976000089L));
074: }
075:
076: /**
077: * @tests java.lang.Math#acos(double)
078: */
079: public void test_acosD() {
080: // Test for method double java.lang.Math.acos(double)
081: double r = Math.cos(Math.acos(ADJ / HYP));
082: long lr = Double.doubleToLongBits(r);
083: long t = Double.doubleToLongBits(ADJ / HYP);
084: assertTrue("Returned incorrect arc cosine", lr == t
085: || (lr + 1) == t || (lr - 1) == t);
086: }
087:
088: /**
089: * @tests java.lang.Math#asin(double)
090: */
091: public void test_asinD() {
092: // Test for method double java.lang.Math.asin(double)
093: double r = Math.sin(Math.asin(OPP / HYP));
094: long lr = Double.doubleToLongBits(r);
095: long t = Double.doubleToLongBits(OPP / HYP);
096: assertTrue("Returned incorrect arc sine", lr == t
097: || (lr + 1) == t || (lr - 1) == t);
098: }
099:
100: /**
101: * @tests java.lang.Math#atan(double)
102: */
103: public void test_atanD() {
104: // Test for method double java.lang.Math.atan(double)
105: double answer = Math.tan(Math.atan(1.0));
106: assertTrue("Returned incorrect arc tangent: " + answer,
107: answer <= 1.0 && answer >= 9.9999999999999983E-1);
108: }
109:
110: /**
111: * @tests java.lang.Math#atan2(double, double)
112: */
113: public void test_atan2DD() {
114: // Test for method double java.lang.Math.atan2(double, double)
115: double answer = Math.atan(Math.tan(1.0));
116: assertTrue("Returned incorrect arc tangent: " + answer,
117: answer <= 1.0 && answer >= 9.9999999999999983E-1);
118: }
119:
120: /**
121: * @tests java.lang.Math#cbrt(double)
122: */
123: public void test_cbrt_D() {
124: //Test for special situations
125: assertTrue("Should return Double.NaN", Double.isNaN(Math
126: .cbrt(Double.NaN)));
127: assertEquals("Should return Double.POSITIVE_INFINITY",
128: Double.POSITIVE_INFINITY, Math
129: .cbrt(Double.POSITIVE_INFINITY), 0D);
130: assertEquals("Should return Double.NEGATIVE_INFINITY",
131: Double.NEGATIVE_INFINITY, Math
132: .cbrt(Double.NEGATIVE_INFINITY), 0D);
133: assertEquals(Double.doubleToLongBits(0.0), Double
134: .doubleToLongBits(Math.cbrt(0.0)));
135: assertEquals(Double.doubleToLongBits(+0.0), Double
136: .doubleToLongBits(Math.cbrt(+0.0)));
137: assertEquals(Double.doubleToLongBits(-0.0), Double
138: .doubleToLongBits(Math.cbrt(-0.0)));
139:
140: assertEquals("Should return 3.0", 3.0, Math.cbrt(27.0), 0D);
141: assertEquals("Should return 23.111993172558684",
142: 23.111993172558684, Math.cbrt(12345.6), 0D);
143: assertEquals("Should return 5.643803094122362E102",
144: 5.643803094122362E102, Math.cbrt(Double.MAX_VALUE), 0D);
145: assertEquals("Should return 0.01", 0.01, Math.cbrt(0.000001),
146: 0D);
147:
148: assertEquals("Should return -3.0", -3.0, Math.cbrt(-27.0), 0D);
149: assertEquals("Should return -23.111993172558684",
150: -23.111993172558684, Math.cbrt(-12345.6), 0D);
151: assertEquals("Should return 1.7031839360032603E-108",
152: 1.7031839360032603E-108, Math.cbrt(Double.MIN_VALUE),
153: 0D);
154: assertEquals("Should return -0.01", -0.01,
155: Math.cbrt(-0.000001), 0D);
156: }
157:
158: /**
159: * @tests java.lang.Math#ceil(double)
160: */
161: public void test_ceilD() {
162: // Test for method double java.lang.Math.ceil(double)
163: assertEquals("Incorrect ceiling for double", 79, Math
164: .ceil(78.89), 0);
165: assertEquals("Incorrect ceiling for double", -78, Math
166: .ceil(-78.89), 0);
167: }
168:
169: /**
170: * @tests java.lang.Math#cos(double)
171: */
172: public void test_cosD() {
173: // Test for method double java.lang.Math.cos(double)
174: assertEquals("Incorrect answer", 1.0, Math.cos(0), 0D);
175: assertEquals("Incorrect answer", 0.5403023058681398, Math
176: .cos(1), 0D);
177: }
178:
179: /**
180: * @tests java.lang.Math#cosh(double)
181: */
182: public void test_cosh_D() {
183: // Test for special situations
184: assertTrue(Double.isNaN(Math.cosh(Double.NaN)));
185: assertEquals("Should return POSITIVE_INFINITY",
186: Double.POSITIVE_INFINITY, Math
187: .cosh(Double.POSITIVE_INFINITY), 0D);
188: assertEquals("Should return POSITIVE_INFINITY",
189: Double.POSITIVE_INFINITY, Math
190: .cosh(Double.NEGATIVE_INFINITY), 0D);
191: assertEquals("Should return 1.0", 1.0, Math.cosh(+0.0), 0D);
192: assertEquals("Should return 1.0", 1.0, Math.cosh(-0.0), 0D);
193:
194: assertEquals("Should return POSITIVE_INFINITY",
195: Double.POSITIVE_INFINITY, Math.cosh(1234.56), 0D);
196: assertEquals("Should return POSITIVE_INFINITY",
197: Double.POSITIVE_INFINITY, Math.cosh(-1234.56), 0D);
198: assertEquals("Should return 1.0000000000005", 1.0000000000005,
199: Math.cosh(0.000001), 0D);
200: assertEquals("Should return 1.0000000000005", 1.0000000000005,
201: Math.cosh(-0.000001), 0D);
202: assertEquals("Should return 5.212214351945598",
203: 5.212214351945598, Math.cosh(2.33482), 0D);
204:
205: assertEquals("Should return POSITIVE_INFINITY",
206: Double.POSITIVE_INFINITY, Math.cosh(Double.MAX_VALUE),
207: 0D);
208: assertEquals("Should return 1.0", 1.0, Math
209: .cosh(Double.MIN_VALUE), 0D);
210: }
211:
212: /**
213: * @tests java.lang.Math#exp(double)
214: */
215: public void test_expD() {
216: // Test for method double java.lang.Math.exp(double)
217: assertTrue("Incorrect answer returned for simple power",
218: Math.abs(Math.exp(4D) - Math.E * Math.E * Math.E
219: * Math.E) < 0.1D);
220: assertTrue("Incorrect answer returned for larger power", Math
221: .log(Math.abs(Math.exp(5.5D)) - 5.5D) < 10.0D);
222: }
223:
224: /**
225: * @tests java.lang.Math#expm1(double)
226: */
227: public void test_expm1_D() {
228: // Test for special cases
229: assertTrue("Should return NaN", Double.isNaN(Math
230: .expm1(Double.NaN)));
231: assertEquals("Should return POSITIVE_INFINITY",
232: Double.POSITIVE_INFINITY, Math
233: .expm1(Double.POSITIVE_INFINITY), 0D);
234: assertEquals("Should return -1.0", -1.0, Math
235: .expm1(Double.NEGATIVE_INFINITY), 0D);
236: assertEquals(Double.doubleToLongBits(0.0), Double
237: .doubleToLongBits(Math.expm1(0.0)));
238: assertEquals(Double.doubleToLongBits(+0.0), Double
239: .doubleToLongBits(Math.expm1(+0.0)));
240: assertEquals(Double.doubleToLongBits(-0.0), Double
241: .doubleToLongBits(Math.expm1(-0.0)));
242:
243: assertEquals("Should return -9.999950000166666E-6",
244: -9.999950000166666E-6, Math.expm1(-0.00001), 0D);
245: assertEquals("Should return 1.0145103074469635E60",
246: 1.0145103074469635E60, Math.expm1(138.16951162), 0D);
247: assertEquals("Should return POSITIVE_INFINITY",
248: Double.POSITIVE_INFINITY, Math
249: .expm1(123456789123456789123456789.4521584223),
250: 0D);
251: assertEquals("Should return POSITIVE_INFINITY",
252: Double.POSITIVE_INFINITY, Math.expm1(Double.MAX_VALUE),
253: 0D);
254: assertEquals("Should return MIN_VALUE", Double.MIN_VALUE, Math
255: .expm1(Double.MIN_VALUE), 0D);
256: }
257:
258: /**
259: * @tests java.lang.Math#floor(double)
260: */
261: public void test_floorD() {
262: // Test for method double java.lang.Math.floor(double)
263: assertEquals("Incorrect floor for double", 78, Math
264: .floor(78.89), 0);
265: assertEquals("Incorrect floor for double", -79, Math
266: .floor(-78.89), 0);
267: }
268:
269: /**
270: * @tests java.lang.Math#hypot(double, double)
271: */
272: public void test_hypot_DD() {
273: // Test for special cases
274: assertEquals("Should return POSITIVE_INFINITY",
275: Double.POSITIVE_INFINITY, Math.hypot(
276: Double.POSITIVE_INFINITY, 1.0), 0D);
277: assertEquals("Should return POSITIVE_INFINITY",
278: Double.POSITIVE_INFINITY, Math.hypot(
279: Double.NEGATIVE_INFINITY, 123.324), 0D);
280: assertEquals("Should return POSITIVE_INFINITY",
281: Double.POSITIVE_INFINITY, Math.hypot(-758.2587,
282: Double.POSITIVE_INFINITY), 0D);
283: assertEquals("Should return POSITIVE_INFINITY",
284: Double.POSITIVE_INFINITY, Math.hypot(5687.21,
285: Double.NEGATIVE_INFINITY), 0D);
286: assertEquals("Should return POSITIVE_INFINITY",
287: Double.POSITIVE_INFINITY, Math.hypot(
288: Double.POSITIVE_INFINITY,
289: Double.NEGATIVE_INFINITY), 0D);
290: assertEquals("Should return POSITIVE_INFINITY",
291: Double.POSITIVE_INFINITY, Math.hypot(
292: Double.NEGATIVE_INFINITY,
293: Double.POSITIVE_INFINITY), 0D);
294: assertTrue("Should be NaN", Double.isNaN(Math.hypot(Double.NaN,
295: 2342301.89843)));
296: assertTrue("Should be NaN", Double.isNaN(Math.hypot(-345.2680,
297: Double.NaN)));
298:
299: assertEquals("Should return 2396424.905416697",
300: 2396424.905416697, Math.hypot(12322.12, -2396393.2258),
301: 0D);
302: assertEquals("Should return 138.16958070558556",
303: 138.16958070558556, Math.hypot(-138.16951162,
304: 0.13817035864), 0D);
305: assertEquals("Should return 1.7976931348623157E308",
306: 1.7976931348623157E308, Math.hypot(Double.MAX_VALUE,
307: 211370.35), 0D);
308: assertEquals("Should return 5413.7185", 5413.7185, Math.hypot(
309: -5413.7185, Double.MIN_VALUE), 0D);
310: }
311:
312: /**
313: * @tests java.lang.Math#IEEEremainder(double, double)
314: */
315: public void test_IEEEremainderDD() {
316: // Test for method double java.lang.Math.IEEEremainder(double, double)
317: assertEquals("Incorrect remainder returned", 0.0, Math
318: .IEEEremainder(1.0, 1.0), 0D);
319: assertTrue(
320: "Incorrect remainder returned",
321: Math.IEEEremainder(1.32, 89.765) >= 1.4705063220631647E-2
322: || Math.IEEEremainder(1.32, 89.765) >= 1.4705063220631649E-2);
323: }
324:
325: /**
326: * @tests java.lang.Math#log(double)
327: */
328: public void test_logD() {
329: // Test for method double java.lang.Math.log(double)
330: for (double d = 10; d >= -10; d -= 0.5) {
331: double answer = Math.log(Math.exp(d));
332: assertTrue("Answer does not equal expected answer for d = "
333: + d + " answer = " + answer,
334: Math.abs(answer - d) <= Math.abs(d * 0.00000001));
335: }
336: }
337:
338: /**
339: * @tests java.lang.Math#log10(double)
340: */
341: @SuppressWarnings("boxing")
342: public void test_log10_D() {
343: // Test for special cases
344: assertTrue(Double.isNaN(Math.log10(Double.NaN)));
345: assertTrue(Double.isNaN(Math.log10(-2541.05745687234187532)));
346: assertTrue(Double.isNaN(Math.log10(-0.1)));
347: assertEquals(Double.POSITIVE_INFINITY, Math
348: .log10(Double.POSITIVE_INFINITY));
349: assertEquals(Double.NEGATIVE_INFINITY, Math.log10(0.0));
350: assertEquals(Double.NEGATIVE_INFINITY, Math.log10(+0.0));
351: assertEquals(Double.NEGATIVE_INFINITY, Math.log10(-0.0));
352:
353: assertEquals(3.0, Math.log10(1000.0));
354: assertEquals(14.0, Math.log10(Math.pow(10, 14)));
355: assertEquals(3.7389561269540406, Math.log10(5482.2158));
356: assertEquals(14.661551142893833, Math
357: .log10(458723662312872.125782332587));
358: assertEquals(-0.9083828622192334, Math.log10(0.12348583358871));
359: assertEquals(308.25471555991675, Math.log10(Double.MAX_VALUE));
360: assertEquals(-323.3062153431158, Math.log10(Double.MIN_VALUE));
361: }
362:
363: /**
364: * @tests java.lang.Math#log1p(double)
365: */
366: public void test_log1p_D() {
367: // Test for special cases
368: assertTrue("Should return NaN", Double.isNaN(Math
369: .log1p(Double.NaN)));
370: assertTrue("Should return NaN", Double.isNaN(Math
371: .log1p(-32.0482175)));
372: assertEquals("Should return POSITIVE_INFINITY",
373: Double.POSITIVE_INFINITY, Math
374: .log1p(Double.POSITIVE_INFINITY), 0D);
375: assertEquals(Double.doubleToLongBits(0.0), Double
376: .doubleToLongBits(Math.log1p(0.0)));
377: assertEquals(Double.doubleToLongBits(+0.0), Double
378: .doubleToLongBits(Math.log1p(+0.0)));
379: assertEquals(Double.doubleToLongBits(-0.0), Double
380: .doubleToLongBits(Math.log1p(-0.0)));
381:
382: assertEquals("Should return -0.2941782295312541",
383: -0.2941782295312541, Math.log1p(-0.254856327), 0D);
384: assertEquals("Should return 7.368050685564151",
385: 7.368050685564151, Math.log1p(1583.542), 0D);
386: assertEquals("Should return 0.4633708685409921",
387: 0.4633708685409921, Math.log1p(0.5894227), 0D);
388: assertEquals("Should return 709.782712893384",
389: 709.782712893384, Math.log1p(Double.MAX_VALUE), 0D);
390: assertEquals("Should return Double.MIN_VALUE",
391: Double.MIN_VALUE, Math.log1p(Double.MIN_VALUE), 0D);
392: }
393:
394: /**
395: * @tests java.lang.Math#max(double, double)
396: */
397: public void test_maxDD() {
398: // Test for method double java.lang.Math.max(double, double)
399: assertEquals("Incorrect double max value", 1908897.6000089,
400: Math.max(-1908897.6000089, 1908897.6000089), 0D);
401: assertEquals("Incorrect double max value", 1908897.6000089,
402: Math.max(2.0, 1908897.6000089), 0D);
403: assertEquals("Incorrect double max value", -2.0, Math.max(-2.0,
404: -1908897.6000089), 0D);
405:
406: }
407:
408: /**
409: * @tests java.lang.Math#max(float, float)
410: */
411: public void test_maxFF() {
412: // Test for method float java.lang.Math.max(float, float)
413: assertTrue("Incorrect float max value", Math.max(-1908897.600f,
414: 1908897.600f) == 1908897.600f);
415: assertTrue("Incorrect float max value", Math.max(2.0f,
416: 1908897.600f) == 1908897.600f);
417: assertTrue("Incorrect float max value", Math.max(-2.0f,
418: -1908897.600f) == -2.0f);
419: }
420:
421: /**
422: * @tests java.lang.Math#max(int, int)
423: */
424: public void test_maxII() {
425: // Test for method int java.lang.Math.max(int, int)
426: assertEquals("Incorrect int max value", 19088976, Math.max(
427: -19088976, 19088976));
428: assertEquals("Incorrect int max value", 19088976, Math.max(20,
429: 19088976));
430: assertEquals("Incorrect int max value", -20, Math.max(-20,
431: -19088976));
432: }
433:
434: /**
435: * @tests java.lang.Math#max(long, long)
436: */
437: public void test_maxJJ() {
438: // Test for method long java.lang.Math.max(long, long)
439: assertEquals("Incorrect long max value", 19088976000089L, Math
440: .max(-19088976000089L, 19088976000089L));
441: assertEquals("Incorrect long max value", 19088976000089L, Math
442: .max(20, 19088976000089L));
443: assertEquals("Incorrect long max value", -20, Math.max(-20,
444: -19088976000089L));
445: }
446:
447: /**
448: * @tests java.lang.Math#min(double, double)
449: */
450: public void test_minDD() {
451: // Test for method double java.lang.Math.min(double, double)
452: assertEquals("Incorrect double min value", -1908897.6000089,
453: Math.min(-1908897.6000089, 1908897.6000089), 0D);
454: assertEquals("Incorrect double min value", 2.0, Math.min(2.0,
455: 1908897.6000089), 0D);
456: assertEquals("Incorrect double min value", -1908897.6000089,
457: Math.min(-2.0, -1908897.6000089), 0D);
458: }
459:
460: /**
461: * @tests java.lang.Math#min(float, float)
462: */
463: public void test_minFF() {
464: // Test for method float java.lang.Math.min(float, float)
465: assertTrue("Incorrect float min value", Math.min(-1908897.600f,
466: 1908897.600f) == -1908897.600f);
467: assertTrue("Incorrect float min value", Math.min(2.0f,
468: 1908897.600f) == 2.0f);
469: assertTrue("Incorrect float min value", Math.min(-2.0f,
470: -1908897.600f) == -1908897.600f);
471: }
472:
473: /**
474: * @tests java.lang.Math#min(int, int)
475: */
476: public void test_minII() {
477: // Test for method int java.lang.Math.min(int, int)
478: assertEquals("Incorrect int min value", -19088976, Math.min(
479: -19088976, 19088976));
480: assertEquals("Incorrect int min value", 20, Math.min(20,
481: 19088976));
482: assertEquals("Incorrect int min value", -19088976, Math.min(
483: -20, -19088976));
484:
485: }
486:
487: /**
488: * @tests java.lang.Math#min(long, long)
489: */
490: public void test_minJJ() {
491: // Test for method long java.lang.Math.min(long, long)
492: assertEquals("Incorrect long min value", -19088976000089L, Math
493: .min(-19088976000089L, 19088976000089L));
494: assertEquals("Incorrect long min value", 20, Math.min(20,
495: 19088976000089L));
496: assertEquals("Incorrect long min value", -19088976000089L, Math
497: .min(-20, -19088976000089L));
498: }
499:
500: /**
501: * @tests java.lang.Math#pow(double, double)
502: */
503: public void test_powDD() {
504: // Test for method double java.lang.Math.pow(double, double)
505: assertTrue("pow returned incorrect value", (long) Math
506: .pow(2, 8) == 256l);
507: assertTrue("pow returned incorrect value",
508: Math.pow(2, -8) == 0.00390625d);
509: assertEquals("Incorrect root returned1", 2, Math.sqrt(Math.pow(
510: Math.sqrt(2), 4)), 0);
511: }
512:
513: /**
514: * @tests java.lang.Math#rint(double)
515: */
516: public void test_rintD() {
517: // Test for method double java.lang.Math.rint(double)
518: assertEquals("Failed to round properly - up to odd", 3.0, Math
519: .rint(2.9), 0D);
520: assertTrue("Failed to round properly - NaN", Double.isNaN(Math
521: .rint(Double.NaN)));
522: assertEquals("Failed to round properly down to even", 2.0,
523: Math.rint(2.1), 0D);
524: assertTrue("Failed to round properly " + 2.5 + " to even", Math
525: .rint(2.5) == 2.0);
526: assertTrue("Failed to round properly " + (+0.0d), Math
527: .rint(+0.0d) == +0.0d);
528: assertTrue("Failed to round properly " + (-0.0d), Math
529: .rint(-0.0d) == -0.0d);
530: }
531:
532: /**
533: * @tests java.lang.Math#round(double)
534: */
535: public void test_roundD() {
536: // Test for method long java.lang.Math.round(double)
537: assertEquals("Incorrect rounding of a float", -91, Math
538: .round(-90.89d));
539: }
540:
541: /**
542: * @tests java.lang.Math#round(float)
543: */
544: public void test_roundF() {
545: // Test for method int java.lang.Math.round(float)
546: assertEquals("Incorrect rounding of a float", -91, Math
547: .round(-90.89f));
548: }
549:
550: /**
551: * @tests java.lang.Math#signum(double)
552: */
553: public void test_signum_D() {
554: assertTrue(Double.isNaN(Math.signum(Double.NaN)));
555: assertTrue(Double.isNaN(Math.signum(Double.NaN)));
556: assertEquals(Double.doubleToLongBits(0.0), Double
557: .doubleToLongBits(Math.signum(0.0)));
558: assertEquals(Double.doubleToLongBits(+0.0), Double
559: .doubleToLongBits(Math.signum(+0.0)));
560: assertEquals(Double.doubleToLongBits(-0.0), Double
561: .doubleToLongBits(Math.signum(-0.0)));
562:
563: assertEquals(1.0, Math.signum(253681.2187962), 0D);
564: assertEquals(-1.0, Math.signum(-125874693.56), 0D);
565: assertEquals(1.0, Math.signum(1.2587E-308), 0D);
566: assertEquals(-1.0, Math.signum(-1.2587E-308), 0D);
567:
568: assertEquals(1.0, Math.signum(Double.MAX_VALUE), 0D);
569: assertEquals(1.0, Math.signum(Double.MIN_VALUE), 0D);
570: assertEquals(-1.0, Math.signum(-Double.MAX_VALUE), 0D);
571: assertEquals(-1.0, Math.signum(-Double.MIN_VALUE), 0D);
572: assertEquals(1.0, Math.signum(Double.POSITIVE_INFINITY), 0D);
573: assertEquals(-1.0, Math.signum(Double.NEGATIVE_INFINITY), 0D);
574: }
575:
576: /**
577: * @tests java.lang.Math#signum(float)
578: */
579: public void test_signum_F() {
580: assertTrue(Float.isNaN(Math.signum(Float.NaN)));
581: assertEquals(Float.floatToIntBits(0.0f), Float
582: .floatToIntBits(Math.signum(0.0f)));
583: assertEquals(Float.floatToIntBits(+0.0f), Float
584: .floatToIntBits(Math.signum(+0.0f)));
585: assertEquals(Float.floatToIntBits(-0.0f), Float
586: .floatToIntBits(Math.signum(-0.0f)));
587:
588: assertEquals(1.0f, Math.signum(253681.2187962f), 0f);
589: assertEquals(-1.0f, Math.signum(-125874693.56f), 0f);
590: assertEquals(1.0f, Math.signum(1.2587E-11f), 0f);
591: assertEquals(-1.0f, Math.signum(-1.2587E-11f), 0f);
592:
593: assertEquals(1.0f, Math.signum(Float.MAX_VALUE), 0f);
594: assertEquals(1.0f, Math.signum(Float.MIN_VALUE), 0f);
595: assertEquals(-1.0f, Math.signum(-Float.MAX_VALUE), 0f);
596: assertEquals(-1.0f, Math.signum(-Float.MIN_VALUE), 0f);
597: assertEquals(1.0f, Math.signum(Float.POSITIVE_INFINITY), 0f);
598: assertEquals(-1.0f, Math.signum(Float.NEGATIVE_INFINITY), 0f);
599: }
600:
601: /**
602: * @tests java.lang.Math#sin(double)
603: */
604: public void test_sinD() {
605: // Test for method double java.lang.Math.sin(double)
606: assertEquals("Incorrect answer", 0.0, Math.sin(0), 0D);
607: assertEquals("Incorrect answer", 0.8414709848078965, Math
608: .sin(1), 0D);
609: }
610:
611: /**
612: * @tests java.lang.Math#sinh(double)
613: */
614: public void test_sinh_D() {
615: // Test for special situations
616: assertTrue("Should return NaN", Double.isNaN(Math
617: .sinh(Double.NaN)));
618: assertEquals("Should return POSITIVE_INFINITY",
619: Double.POSITIVE_INFINITY, Math
620: .sinh(Double.POSITIVE_INFINITY), 0D);
621: assertEquals("Should return NEGATIVE_INFINITY",
622: Double.NEGATIVE_INFINITY, Math
623: .sinh(Double.NEGATIVE_INFINITY), 0D);
624: assertEquals(Double.doubleToLongBits(0.0), Double
625: .doubleToLongBits(Math.sinh(0.0)));
626: assertEquals(Double.doubleToLongBits(+0.0), Double
627: .doubleToLongBits(Math.sinh(+0.0)));
628: assertEquals(Double.doubleToLongBits(-0.0), Double
629: .doubleToLongBits(Math.sinh(-0.0)));
630:
631: assertEquals("Should return POSITIVE_INFINITY",
632: Double.POSITIVE_INFINITY, Math.sinh(1234.56), 0D);
633: assertEquals("Should return NEGATIVE_INFINITY",
634: Double.NEGATIVE_INFINITY, Math.sinh(-1234.56), 0D);
635: assertEquals("Should return 1.0000000000001666E-6",
636: 1.0000000000001666E-6, Math.sinh(0.000001), 0D);
637: assertEquals("Should return -1.0000000000001666E-6",
638: -1.0000000000001666E-6, Math.sinh(-0.000001), 0D);
639: assertEquals("Should return 5.115386441963859",
640: 5.115386441963859, Math.sinh(2.33482), 0D);
641: assertEquals("Should return POSITIVE_INFINITY",
642: Double.POSITIVE_INFINITY, Math.sinh(Double.MAX_VALUE),
643: 0D);
644: assertEquals("Should return 4.9E-324", 4.9E-324, Math
645: .sinh(Double.MIN_VALUE), 0D);
646: }
647:
648: /**
649: * @tests java.lang.Math#sqrt(double)
650: */
651: public void test_sqrtD() {
652: // Test for method double java.lang.Math.sqrt(double)
653: assertEquals("Incorrect root returned2", 7, Math.sqrt(49), 0);
654: }
655:
656: /**
657: * @tests java.lang.Math#tan(double)
658: */
659: public void test_tanD() {
660: // Test for method double java.lang.Math.tan(double)
661: assertEquals("Incorrect answer", 0.0, Math.tan(0), 0D);
662: assertEquals("Incorrect answer", 1.5574077246549023, Math
663: .tan(1), 0D);
664:
665: }
666:
667: /**
668: * @tests java.lang.Math#tanh(double)
669: */
670: public void test_tanh_D() {
671: // Test for special situations
672: assertTrue("Should return NaN", Double.isNaN(Math
673: .tanh(Double.NaN)));
674: assertEquals("Should return +1.0", +1.0, Math
675: .tanh(Double.POSITIVE_INFINITY), 0D);
676: assertEquals("Should return -1.0", -1.0, Math
677: .tanh(Double.NEGATIVE_INFINITY), 0D);
678: assertEquals(Double.doubleToLongBits(0.0), Double
679: .doubleToLongBits(Math.tanh(0.0)));
680: assertEquals(Double.doubleToLongBits(+0.0), Double
681: .doubleToLongBits(Math.tanh(+0.0)));
682: assertEquals(Double.doubleToLongBits(-0.0), Double
683: .doubleToLongBits(Math.tanh(-0.0)));
684:
685: assertEquals("Should return 1.0", 1.0, Math.tanh(1234.56), 0D);
686: assertEquals("Should return -1.0", -1.0, Math.tanh(-1234.56),
687: 0D);
688: assertEquals("Should return 9.999999999996666E-7",
689: 9.999999999996666E-7, Math.tanh(0.000001), 0D);
690: assertEquals("Should return 0.981422884124941",
691: 0.981422884124941, Math.tanh(2.33482), 0D);
692: assertEquals("Should return 1.0", 1.0, Math
693: .tanh(Double.MAX_VALUE), 0D);
694: assertEquals("Should return 4.9E-324", 4.9E-324, Math
695: .tanh(Double.MIN_VALUE), 0D);
696: }
697:
698: /**
699: * @tests java.lang.Math#random()
700: */
701: public void test_random() {
702: // There isn't a place for these tests so just stick them here
703: assertEquals("Wrong value E", 4613303445314885481L, Double
704: .doubleToLongBits(Math.E));
705: assertEquals("Wrong value PI", 4614256656552045848L, Double
706: .doubleToLongBits(Math.PI));
707:
708: for (int i = 500; i >= 0; i--) {
709: double d = Math.random();
710: assertTrue("Generated number is out of range: " + d,
711: d >= 0.0 && d < 1.0);
712: }
713: }
714:
715: /**
716: * @tests java.lang.Math#toRadians(double)
717: */
718: public void test_toRadiansD() {
719: for (double d = 500; d >= 0; d -= 1.0) {
720: double converted = Math.toDegrees(Math.toRadians(d));
721: assertTrue("Converted number not equal to original. d = "
722: + d, converted >= d * 0.99999999
723: && converted <= d * 1.00000001);
724: }
725: }
726:
727: /**
728: * @tests java.lang.Math#toDegrees(double)
729: */
730: public void test_toDegreesD() {
731: for (double d = 500; d >= 0; d -= 1.0) {
732: double converted = Math.toRadians(Math.toDegrees(d));
733: assertTrue("Converted number not equal to original. d = "
734: + d, converted >= d * 0.99999999
735: && converted <= d * 1.00000001);
736: }
737: }
738:
739: /**
740: * @tests java.lang.Math#ulp(double)
741: */
742: @SuppressWarnings("boxing")
743: public void test_ulp_D() {
744: // Test for special cases
745: assertTrue("Should return NaN", Double.isNaN(Math
746: .ulp(Double.NaN)));
747: assertEquals("Returned incorrect value",
748: Double.POSITIVE_INFINITY, Math
749: .ulp(Double.POSITIVE_INFINITY), 0D);
750: assertEquals("Returned incorrect value",
751: Double.POSITIVE_INFINITY, Math
752: .ulp(Double.NEGATIVE_INFINITY), 0D);
753: assertEquals("Returned incorrect value", Double.MIN_VALUE, Math
754: .ulp(0.0), 0D);
755: assertEquals("Returned incorrect value", Double.MIN_VALUE, Math
756: .ulp(+0.0), 0D);
757: assertEquals("Returned incorrect value", Double.MIN_VALUE, Math
758: .ulp(-0.0), 0D);
759: assertEquals("Returned incorrect value", Math.pow(2, 971), Math
760: .ulp(Double.MAX_VALUE), 0D);
761: assertEquals("Returned incorrect value", Math.pow(2, 971), Math
762: .ulp(-Double.MAX_VALUE), 0D);
763:
764: assertEquals("Returned incorrect value", Double.MIN_VALUE, Math
765: .ulp(Double.MIN_VALUE), 0D);
766: assertEquals("Returned incorrect value", Double.MIN_VALUE, Math
767: .ulp(-Double.MIN_VALUE), 0D);
768:
769: assertEquals("Returned incorrect value", 2.220446049250313E-16,
770: Math.ulp(1.0), 0D);
771: assertEquals("Returned incorrect value", 2.220446049250313E-16,
772: Math.ulp(-1.0), 0D);
773: assertEquals("Returned incorrect value",
774: 2.2737367544323206E-13, Math.ulp(1153.0), 0D);
775: }
776:
777: /**
778: * @tests java.lang.Math#ulp(float)
779: */
780: @SuppressWarnings("boxing")
781: public void test_ulp_f() {
782: // Test for special cases
783: assertTrue("Should return NaN", Float
784: .isNaN(Math.ulp(Float.NaN)));
785: assertEquals("Returned incorrect value",
786: Float.POSITIVE_INFINITY, Math
787: .ulp(Float.POSITIVE_INFINITY), 0f);
788: assertEquals("Returned incorrect value",
789: Float.POSITIVE_INFINITY, Math
790: .ulp(Float.NEGATIVE_INFINITY), 0f);
791: assertEquals("Returned incorrect value", Float.MIN_VALUE, Math
792: .ulp(0.0f), 0f);
793: assertEquals("Returned incorrect value", Float.MIN_VALUE, Math
794: .ulp(+0.0f), 0f);
795: assertEquals("Returned incorrect value", Float.MIN_VALUE, Math
796: .ulp(-0.0f), 0f);
797: assertEquals("Returned incorrect value", 2.028241E31f, Math
798: .ulp(Float.MAX_VALUE), 0f);
799: assertEquals("Returned incorrect value", 2.028241E31f, Math
800: .ulp(-Float.MAX_VALUE), 0f);
801:
802: assertEquals("Returned incorrect value", 1.4E-45f, Math
803: .ulp(Float.MIN_VALUE), 0f);
804: assertEquals("Returned incorrect value", 1.4E-45f, Math
805: .ulp(-Float.MIN_VALUE), 0f);
806:
807: assertEquals("Returned incorrect value", 1.1920929E-7f, Math
808: .ulp(1.0f), 0f);
809: assertEquals("Returned incorrect value", 1.1920929E-7f, Math
810: .ulp(-1.0f), 0f);
811: assertEquals("Returned incorrect value", 1.2207031E-4f, Math
812: .ulp(1153.0f), 0f);
813: assertEquals("Returned incorrect value", 5.6E-45f, Math
814: .ulp(9.403954E-38f), 0f);
815: }
816: }
|