]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma/Expressions/11.5.3.js
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/
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.
11 * The Original Code is Mozilla Communicator client code, released March
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
24 ECMA Section: 11.5.3 Applying the % operator
27 The binary % operator is said to yield the remainder of its operands from
28 an implied division; the left operand is the dividend and the right operand
29 is the divisor. In C and C++, the remainder operator accepts only integral
30 operands, but in ECMAScript, it also accepts floating-point operands.
32 The result of a floating-point remainder operation as computed by the %
33 operator is not the same as the "remainder" operation defined by IEEE 754.
34 The IEEE 754 "remainder" operation computes the remainder from a rounding
35 division, not a truncating division, and so its behavior is not analogous
36 to that of the usual integer remainder operator. Instead the ECMAScript
37 language defines % on floating-point operations to behave in a manner
38 analogous to that of the Java integer remainder operator; this may be
39 compared with the C library function fmod.
41 The result of a ECMAScript floating-point remainder operation is determined by the rules of IEEE arithmetic:
43 If either operand is NaN, the result is NaN.
44 The sign of the result equals the sign of the dividend.
45 If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN.
46 If the dividend is finite and the divisor is an infinity, the result equals the dividend.
47 If the dividend is a zero and the divisor is finite, the result is the same as the dividend.
48 In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, the floating-point remainder r
49 from a dividend n and a divisor d is defined by the mathematical relation r = n (d * q) where q is an integer that
50 is negative only if n/d is negative and positive only if n/d is positive, and whose magnitude is as large as
51 possible without exceeding the magnitude of the true mathematical quotient of n and d.
53 Author: christine@netscape.com
54 Date: 12 november 1997
56 var SECTION
= "11.5.3";
57 var VERSION
= "ECMA_1";
59 var testcases
= getTestCases();
60 var BUGNUMBER
="111202";
62 writeHeaderToLog( SECTION
+ " Applying the % operator");
66 for ( tc
=0; tc
< testcases
.length
; tc
++ ) {
67 testcases
[tc
].passed
= writeTestCaseResult(
70 testcases
[tc
].description
+" = "+
71 testcases
[tc
].actual
);
73 testcases
[tc
].reason
+= ( testcases
[tc
].passed
) ? "" : "wrong value ";
78 function getTestCases() {
79 var array
= new Array();
82 // if either operand is NaN, the result is NaN.
84 array
[item
++] = new TestCase( SECTION
, "Number.NaN % Number.NaN", Number
.NaN
, Number
.NaN
% Number
.NaN
);
85 array
[item
++] = new TestCase( SECTION
, "Number.NaN % 1", Number
.NaN
, Number
.NaN
% 1 );
86 array
[item
++] = new TestCase( SECTION
, "1 % Number.NaN", Number
.NaN
, 1 % Number
.NaN
);
88 array
[item
++] = new TestCase( SECTION
, "Number.POSITIVE_INFINITY % Number.NaN", Number
.NaN
, Number
.POSITIVE_INFINITY
% Number
.NaN
);
89 array
[item
++] = new TestCase( SECTION
, "Number.NEGATIVE_INFINITY % Number.NaN", Number
.NaN
, Number
.NEGATIVE_INFINITY
% Number
.NaN
);
91 // If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN.
92 // dividend is an infinity
94 array
[item
++] = new TestCase( SECTION
, "Number.NEGATIVE_INFINITY % Number.NEGATIVE_INFINITY", Number
.NaN
, Number
.NEGATIVE_INFINITY
% Number
.NEGATIVE_INFINITY
);
95 array
[item
++] = new TestCase( SECTION
, "Number.POSITIVE_INFINITY % Number.NEGATIVE_INFINITY", Number
.NaN
, Number
.POSITIVE_INFINITY
% Number
.NEGATIVE_INFINITY
);
96 array
[item
++] = new TestCase( SECTION
, "Number.NEGATIVE_INFINITY % Number.POSITIVE_INFINITY", Number
.NaN
, Number
.NEGATIVE_INFINITY
% Number
.POSITIVE_INFINITY
);
97 array
[item
++] = new TestCase( SECTION
, "Number.POSITIVE_INFINITY % Number.POSITIVE_INFINITY", Number
.NaN
, Number
.POSITIVE_INFINITY
% Number
.POSITIVE_INFINITY
);
99 array
[item
++] = new TestCase( SECTION
, "Number.POSITIVE_INFINITY % 0", Number
.NaN
, Number
.POSITIVE_INFINITY
% 0 );
100 array
[item
++] = new TestCase( SECTION
, "Number.NEGATIVE_INFINITY % 0", Number
.NaN
, Number
.NEGATIVE_INFINITY
% 0 );
101 array
[item
++] = new TestCase( SECTION
, "Number.POSITIVE_INFINITY % -0", Number
.NaN
, Number
.POSITIVE_INFINITY
% -0 );
102 array
[item
++] = new TestCase( SECTION
, "Number.NEGATIVE_INFINITY % -0", Number
.NaN
, Number
.NEGATIVE_INFINITY
% -0 );
104 array
[item
++] = new TestCase( SECTION
, "Number.NEGATIVE_INFINITY % 1 ", Number
.NaN
, Number
.NEGATIVE_INFINITY
% 1 );
105 array
[item
++] = new TestCase( SECTION
, "Number.NEGATIVE_INFINITY % -1 ", Number
.NaN
, Number
.NEGATIVE_INFINITY
% -1 );
106 array
[item
++] = new TestCase( SECTION
, "Number.POSITIVE_INFINITY % 1 ", Number
.NaN
, Number
.POSITIVE_INFINITY
% 1 );
107 array
[item
++] = new TestCase( SECTION
, "Number.POSITIVE_INFINITY % -1 ", Number
.NaN
, Number
.POSITIVE_INFINITY
% -1 );
109 array
[item
++] = new TestCase( SECTION
, "Number.NEGATIVE_INFINITY % Number.MAX_VALUE ", Number
.NaN
, Number
.NEGATIVE_INFINITY
% Number
.MAX_VALUE
);
110 array
[item
++] = new TestCase( SECTION
, "Number.NEGATIVE_INFINITY % -Number.MAX_VALUE ", Number
.NaN
, Number
.NEGATIVE_INFINITY
% -Number
.MAX_VALUE
);
111 array
[item
++] = new TestCase( SECTION
, "Number.POSITIVE_INFINITY % Number.MAX_VALUE ", Number
.NaN
, Number
.POSITIVE_INFINITY
% Number
.MAX_VALUE
);
112 array
[item
++] = new TestCase( SECTION
, "Number.POSITIVE_INFINITY % -Number.MAX_VALUE ", Number
.NaN
, Number
.POSITIVE_INFINITY
% -Number
.MAX_VALUE
);
115 array
[item
++] = new TestCase( SECTION
, "0 % -0", Number
.NaN
, 0 % -0 );
116 array
[item
++] = new TestCase( SECTION
, "-0 % 0", Number
.NaN
, -0 % 0 );
117 array
[item
++] = new TestCase( SECTION
, "-0 % -0", Number
.NaN
, -0 % -0 );
118 array
[item
++] = new TestCase( SECTION
, "0 % 0", Number
.NaN
, 0 % 0 );
120 array
[item
++] = new TestCase( SECTION
, "1 % 0", Number
.NaN
, 1%0 );
121 array
[item
++] = new TestCase( SECTION
, "1 % -0", Number
.NaN
, 1%-0 );
122 array
[item
++] = new TestCase( SECTION
, "-1 % 0", Number
.NaN
, -1%0 );
123 array
[item
++] = new TestCase( SECTION
, "-1 % -0", Number
.NaN
, -1%-0 );
125 array
[item
++] = new TestCase( SECTION
, "Number.MAX_VALUE % 0", Number
.NaN
, Number
.MAX_VALUE
%0 );
126 array
[item
++] = new TestCase( SECTION
, "Number.MAX_VALUE % -0", Number
.NaN
, Number
.MAX_VALUE
%-0 );
127 array
[item
++] = new TestCase( SECTION
, "-Number.MAX_VALUE % 0", Number
.NaN
, -Number
.MAX_VALUE
%0 );
128 array
[item
++] = new TestCase( SECTION
, "-Number.MAX_VALUE % -0", Number
.NaN
, -Number
.MAX_VALUE
%-0 );
130 // If the dividend is finite and the divisor is an infinity, the result equals the dividend.
132 array
[item
++] = new TestCase( SECTION
, "1 % Number.NEGATIVE_INFINITY", 1, 1 % Number
.NEGATIVE_INFINITY
);
133 array
[item
++] = new TestCase( SECTION
, "1 % Number.POSITIVE_INFINITY", 1, 1 % Number
.POSITIVE_INFINITY
);
134 array
[item
++] = new TestCase( SECTION
, "-1 % Number.POSITIVE_INFINITY", -1, -1 % Number
.POSITIVE_INFINITY
);
135 array
[item
++] = new TestCase( SECTION
, "-1 % Number.NEGATIVE_INFINITY", -1, -1 % Number
.NEGATIVE_INFINITY
);
137 array
[item
++] = new TestCase( SECTION
, "Number.MAX_VALUE % Number.NEGATIVE_INFINITY", Number
.MAX_VALUE
, Number
.MAX_VALUE
% Number
.NEGATIVE_INFINITY
);
138 array
[item
++] = new TestCase( SECTION
, "Number.MAX_VALUE % Number.POSITIVE_INFINITY", Number
.MAX_VALUE
, Number
.MAX_VALUE
% Number
.POSITIVE_INFINITY
);
139 array
[item
++] = new TestCase( SECTION
, "-Number.MAX_VALUE % Number.POSITIVE_INFINITY", -Number
.MAX_VALUE
, -Number
.MAX_VALUE
% Number
.POSITIVE_INFINITY
);
140 array
[item
++] = new TestCase( SECTION
, "-Number.MAX_VALUE % Number.NEGATIVE_INFINITY", -Number
.MAX_VALUE
, -Number
.MAX_VALUE
% Number
.NEGATIVE_INFINITY
);
142 array
[item
++] = new TestCase( SECTION
, "0 % Number.POSITIVE_INFINITY", 0, 0 % Number
.POSITIVE_INFINITY
);
143 array
[item
++] = new TestCase( SECTION
, "0 % Number.NEGATIVE_INFINITY", 0, 0 % Number
.NEGATIVE_INFINITY
);
144 array
[item
++] = new TestCase( SECTION
, "-0 % Number.POSITIVE_INFINITY", -0, -0 % Number
.POSITIVE_INFINITY
);
145 array
[item
++] = new TestCase( SECTION
, "-0 % Number.NEGATIVE_INFINITY", -0, -0 % Number
.NEGATIVE_INFINITY
);
147 // If the dividend is a zero and the divisor is finite, the result is the same as the dividend.
149 array
[item
++] = new TestCase( SECTION
, "0 % 1", 0, 0 % 1 );
150 array
[item
++] = new TestCase( SECTION
, "0 % -1", -0, 0 % -1 );
151 array
[item
++] = new TestCase( SECTION
, "-0 % 1", -0, -0 % 1 );
152 array
[item
++] = new TestCase( SECTION
, "-0 % -1", 0, -0 % -1 );
154 // In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, the floating-point remainder r
155 // from a dividend n and a divisor d is defined by the mathematical relation r = n (d * q) where q is an integer that
156 // is negative only if n/d is negative and positive only if n/d is positive, and whose magnitude is as large as
157 // possible without exceeding the magnitude of the true mathematical quotient of n and d.