Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma_3 / Number / 15.7.4.7-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.toPrecision(precision)
43  * See EMCA 262 Edition 3 Section 15.7.4.7
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.toPrecision(precision)';
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 = 5.123456;
62
63
64 status = 'Section A of test: no error intended!';
65 actual = testNum.toPrecision(4);
66 expect = '5.123';
67 captureThis();
68
69 status = 'Section B of test: Infinity.toPrecision() with out-of-range fractionDigits';
70 actual = Number.POSITIVE_INFINITY.toPrecision(-3);
71 expect = 'Infinity';
72 captureThis();
73
74 status = 'Section C of test: -Infinity.toPrecision() with out-of-range fractionDigits';
75 actual = Number.NEGATIVE_INFINITY.toPrecision(-3);
76 expect = '-Infinity';
77 captureThis();
78
79 status = 'Section D of test: NaN.toPrecision() with out-of-range fractionDigits';
80 actual = Number.NaN.toPrecision(-3);
81 expect = 'NaN';
82 captureThis();
83
84
85 ///////////////////////////    OOPS....    ///////////////////////////////
86 /*************************************************************************
87  * 15.7.4.7 Number.prototype.toPrecision(precision)
88  *
89  * An implementation is permitted to extend the behaviour of toPrecision
90  * for values of precision less than 1 or greater than 21. In this
91  * case toPrecision would not necessarily throw RangeError for such values.
92
93 status = 'Section B of test: expect RangeError because precision < 1';
94 actual = catchError('testNum.toPrecision(0)');
95 expect = cnIsRangeError;
96 captureThis();
97
98 status = 'Section C of test: expect RangeError because precision < 1';
99 actual = catchError('testNum.toPrecision(-4)');
100 expect = cnIsRangeError;
101 captureThis();
102
103 status = 'Section D of test: expect RangeError because precision > 21 ';
104 actual = catchError('testNum.toPrecision(22)');
105 expect = cnIsRangeError;
106 captureThis();
107 *************************************************************************/
108
109
110
111 //-----------------------------------------------------------------------------
112 test();
113 //-----------------------------------------------------------------------------
114
115
116 function captureThis()
117 {
118   statusitems[UBound] = status;
119   actualvalues[UBound] = actual;
120   expectedvalues[UBound] = expect;
121   UBound++;
122 }
123
124
125 function test()
126 {
127   enterFunc ('test');
128   printBugNumber(BUGNUMBER);
129   printStatus (summary);
130
131   for (var i = 0; i < UBound; i++)
132   {
133     reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
134   }
135
136   exitFunc ('test');
137 }
138
139
140 function catchError(sEval)
141 {
142   try {eval(sEval);}
143   catch(e) {return isRangeError(e);}
144   return cnNoErrorCaught;
145 }
146
147
148 function isRangeError(obj)
149 {
150   if (obj instanceof RangeError)
151     return cnIsRangeError;
152   return cnNotRangeError;
153 }