• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* The contents of this file are subject to the Netscape Public
2 * License Version 1.1 (the "License"); you may not use this file
3 * except in compliance with the License. You may obtain a copy of
4 * the License at http://www.mozilla.org/NPL/
5 *
6 * Software distributed under the License is distributed on an "AS
7 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
8 * implied. See the License for the specific language governing
9 * rights and limitations under the License.
10 *
11 * The Original Code is Mozilla Communicator client code, released March
12 * 31, 1998.
13 *
14 * The Initial Developer of the Original Code is Netscape Communications
15 * Corporation. Portions created by Netscape are
16 * Copyright (C) 1998 Netscape Communications Corporation. All
17 * Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 */
22/**
23    File Name:          15.8.2.15.js
24    ECMA Section:       15.8.2.15  Math.round(x)
25    Description:        return the greatest number value that is closest to the
26                        argument and is an integer.  if two integers are equally
27                        close to the argument. then the result is the number value
28                        that is closer to Infinity.  if the argument is an integer,
29                        return the argument.
30                        special cases:
31                            - if x is NaN       return NaN
32                            - if x = +0         return +0
33                            - if x = -0          return -0
34                            - if x = Infinity   return Infinity
35                            - if x = -Infinity  return -Infinity
36                            - if 0 < x < 0.5    return 0
37                            - if -0.5 <= x < 0  return -0
38                        example:
39                            Math.round( 3.5 ) == 4
40                            Math.round( -3.5 ) == 3
41                        also:
42                            - Math.round(x) == Math.floor( x + 0.5 )
43                              except if x = -0.  in that case, Math.round(x) = -0
44
45                              and Math.floor( x+0.5 ) = +0
46
47
48    Author:             christine@netscape.com
49    Date:               7 july 1997
50*/
51
52    var SECTION = "15.8.2.15";
53    var VERSION = "ECMA_1";
54    startTest();
55    var TITLE   = "Math.round(x)";
56    var BUGNUMBER="331411";
57
58    var EXCLUDE = "true";
59
60    writeHeaderToLog( SECTION + " "+ TITLE);
61
62    var testcases = getTestCases();
63    test();
64
65function getTestCases() {
66    var array = new Array();
67    var item = 0;
68
69    array[item++] = new TestCase( SECTION,  "Math.round.length",   1,               Math.round.length );
70
71    array[item++] = new TestCase( SECTION,  "Math.round()",         Number.NaN,     Math.round() );
72    array[item++] = new TestCase( SECTION,  "Math.round(null)",     0,              Math.round(0) );
73    array[item++] = new TestCase( SECTION,  "Math.round(void 0)",   Number.NaN,     Math.round(void 0) );
74    array[item++] = new TestCase( SECTION,  "Math.round(true)",     1,              Math.round(true) );
75    array[item++] = new TestCase( SECTION,  "Math.round(false)",    0,              Math.round(false) );
76    array[item++] = new TestCase( SECTION,  "Math.round('.99999')",  1,              Math.round('.99999') );
77    array[item++] = new TestCase( SECTION,  "Math.round('12345e-2')",  123,          Math.round('12345e-2') );
78
79    array[item++] = new TestCase( SECTION,  "Math.round(NaN)",      Number.NaN,     Math.round(Number.NaN) );
80    array[item++] = new TestCase( SECTION,  "Math.round(0)",        0,              Math.round(0) );
81    array[item++] = new TestCase( SECTION,  "Math.round(-0)",        -0,            Math.round(-0));
82    array[item++] = new TestCase( SECTION,  "Infinity/Math.round(-0)",  -Infinity,  Infinity/Math.round(-0) );
83
84    array[item++] = new TestCase( SECTION,  "Math.round(Infinity)", Number.POSITIVE_INFINITY,   Math.round(Number.POSITIVE_INFINITY));
85    array[item++] = new TestCase( SECTION,  "Math.round(-Infinity)",Number.NEGATIVE_INFINITY,       Math.round(Number.NEGATIVE_INFINITY));
86    array[item++] = new TestCase( SECTION,  "Math.round(0.49)",     0,              Math.round(0.49));
87    array[item++] = new TestCase( SECTION,  "Math.round(0.5)",      1,              Math.round(0.5));
88    array[item++] = new TestCase( SECTION,  "Math.round(0.51)",     1,              Math.round(0.51));
89
90    array[item++] = new TestCase( SECTION,  "Math.round(-0.49)",    -0,             Math.round(-0.49));
91    array[item++] = new TestCase( SECTION,  "Math.round(-0.5)",     -0,             Math.round(-0.5));
92    array[item++] = new TestCase( SECTION,  "Infinity/Math.round(-0.49)",    -Infinity,             Infinity/Math.round(-0.49));
93    array[item++] = new TestCase( SECTION,  "Infinity/Math.round(-0.5)",     -Infinity,             Infinity/Math.round(-0.5));
94
95    array[item++] = new TestCase( SECTION,  "Math.round(-0.51)",    -1,             Math.round(-0.51));
96    array[item++] = new TestCase( SECTION,  "Math.round(3.5)",      4,              Math.round(3.5));
97    array[item++] = new TestCase( SECTION,  "Math.round(-3.5)",     -3,             Math.round(-3));
98
99    return ( array );
100}
101function test() {
102    for ( tc=0; tc < testcases.length; tc++ ) {
103        testcases[tc].passed = writeTestCaseResult(
104                            testcases[tc].expect,
105                            testcases[tc].actual,
106                            testcases[tc].description +" = "+
107                            testcases[tc].actual );
108
109        testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
110    }
111    stopTest();
112    return ( testcases );
113}