Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma_3 / Number / 15.7.4.6-1.js
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is mozilla.org code.
16  *
17  * The Initial Developer of the Original Code is
18  * Netscape Communications Corporation.
19  * Portions created by the Initial Developer are Copyright (C) 1998
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *   pschwartau@netscape.com
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39 /*
40  * Date: 2001-07-15
41  *
42  * SUMMARY: Testing Number.prototype.toExponential(fractionDigits)
43  * See EMCA 262 Edition 3 Section 15.7.4.6
44  *
45  * Also see http://bugzilla.mozilla.org/show_bug.cgi?id=90551
46  *
47  */
48 //-----------------------------------------------------------------------------
49 var UBound = 0;
50 var BUGNUMBER = '(none)';
51 var summary = 'Testing Number.prototype.toExponential(fractionDigits)';
52 var cnIsRangeError = 'instanceof RangeError';
53 var cnNotRangeError = 'NOT instanceof RangeError';
54 var cnNoErrorCaught = 'NO ERROR CAUGHT...';
55 var status = '';
56 var statusitems = [];
57 var actual = '';
58 var actualvalues = [];
59 var expect= '';
60 var expectedvalues = [];
61 var testNum = 77.1234;
62
63
64 status = 'Section A of test: no error intended!';
65 actual = testNum.toExponential(4);
66 expect = '7.7123e+1';
67 captureThis();
68
69 status = 'Section B of test: Infinity.toExponential() with out-of-range fractionDigits';
70 actual = Number.POSITIVE_INFINITY.toExponential(-3);
71 expect = 'Infinity';
72 captureThis();
73
74 status = 'Section C of test: -Infinity.toExponential() with out-of-range fractionDigits';
75 actual = Number.NEGATIVE_INFINITY.toExponential(-3);
76 expect = '-Infinity';
77 captureThis();
78
79 status = 'Section D of test: NaN.toExponential() with out-of-range fractionDigits';
80 actual = Number.NaN.toExponential(-3);
81 expect = 'NaN';
82 captureThis();
83
84
85 ///////////////////////////    OOPS....    ///////////////////////////////
86 /*************************************************************************
87  * 15.7.4.6 Number.prototype.toExponential(fractionDigits)
88  *
89  * An implementation is permitted to extend the behaviour of toExponential
90  * for values of fractionDigits less than 0 or greater than 20. In this
91  * case toExponential would not necessarily throw RangeError for such values.
92
93 status = 'Section B of test: expect RangeError because fractionDigits < 0';
94 actual = catchError('testNum.toExponential(-4)');
95 expect = cnIsRangeError;
96 captureThis();
97
98 status = 'Section C of test: expect RangeError because fractionDigits > 20 ';
99 actual = catchError('testNum.toExponential(21)');
100 expect = cnIsRangeError;
101 captureThis();
102 *************************************************************************/
103
104
105
106 //-----------------------------------------------------------------------------
107 test();
108 //-----------------------------------------------------------------------------
109
110
111 function captureThis()
112 {
113   statusitems[UBound] = status;
114   actualvalues[UBound] = actual;
115   expectedvalues[UBound] = expect;
116   UBound++;
117 }
118
119
120 function test()
121 {
122   enterFunc ('test');
123   printBugNumber(BUGNUMBER);
124   printStatus (summary);
125
126   for (var i = 0; i < UBound; i++)
127   {
128     reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
129   }
130
131   exitFunc ('test');
132 }
133
134
135 function catchError(sEval)
136 {
137   try {eval(sEval);}
138   catch(e) {return isRangeError(e);}
139   return cnNoErrorCaught;
140 }
141
142
143 function isRangeError(obj)
144 {
145   if (obj instanceof RangeError)
146     return cnIsRangeError;
147   return cnNotRangeError;
148 }