1/* 2* The contents of this file are subject to the Netscape Public 3* License Version 1.1 (the "License"); you may not use this file 4* except in compliance with the License. You may obtain a copy of 5* the License at http://www.mozilla.org/NPL/ 6* 7* Software distributed under the License is distributed on an 8* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either expressed 9* or implied. See the License for the specific language governing 10* rights and limitations under the License. 11* 12* The Original Code is mozilla.org code. 13* 14* The Initial Developer of the Original Code is Netscape 15* Communications Corporation. Portions created by Netscape are 16* Copyright (C) 1998 Netscape Communications Corporation. 17* All Rights Reserved. 18* 19* Contributor(s): pschwartau@netscape.com 20* Date: 2001-07-15 21* 22* SUMMARY: Testing Number.prototype.toFixed(fractionDigits) 23* See EMCA 262 Edition 3 Section 15.7.4.5 24* 25* Also see http://bugzilla.mozilla.org/show_bug.cgi?id=90551 26* 27*/ 28//----------------------------------------------------------------------------- 29var UBound = 0; 30var bug = '(none)'; 31var summary = 'Testing Number.prototype.toFixed(fractionDigits)'; 32var cnIsRangeError = 'instanceof RangeError'; 33var cnNotRangeError = 'NOT instanceof RangeError'; 34var cnNoErrorCaught = 'NO ERROR CAUGHT...'; 35var status = ''; 36var statusitems = []; 37var actual = ''; 38var actualvalues = []; 39var expect= ''; 40var expectedvalues = []; 41var testNum = 234.2040506; 42 43 44status = 'Section A of test: no error intended!'; 45actual = testNum.toFixed(4); 46expect = '234.2041'; 47captureThis(); 48 49 50/////////////////////////// OOPS.... /////////////////////////////// 51/************************************************************************* 52 * 15.7.4.5 Number.prototype.toFixed(fractionDigits) 53 * 54 * An implementation is permitted to extend the behaviour of toFixed 55 * for values of fractionDigits less than 0 or greater than 20. In this 56 * case toFixed would not necessarily throw RangeError for such values. 57 58status = 'Section B of test: expect RangeError because fractionDigits < 0'; 59actual = catchError('testNum.toFixed(-4)'); 60expect = cnIsRangeError; 61captureThis(); 62 63status = 'Section C of test: expect RangeError because fractionDigits > 20 '; 64actual = catchError('testNum.toFixed(21)'); 65expect = cnIsRangeError; 66captureThis(); 67*************************************************************************/ 68 69 70status = 'Section D of test: no error intended!'; 71actual = 0.00001.toFixed(2); 72expect = '0.00'; 73captureThis(); 74 75status = 'Section E of test: no error intended!'; 76actual = 0.000000000000000000001.toFixed(20); 77expect = '0.00000000000000000000'; 78captureThis(); 79 80 81 82//----------------------------------------------------------------------------- 83test(); 84//----------------------------------------------------------------------------- 85 86 87function captureThis() 88{ 89 statusitems[UBound] = status; 90 actualvalues[UBound] = actual; 91 expectedvalues[UBound] = expect; 92 UBound++; 93} 94 95 96function test() 97{ 98 enterFunc ('test'); 99 printBugNumber (bug); 100 printStatus (summary); 101 102 for (var i = 0; i < UBound; i++) 103 { 104 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); 105 } 106 107 exitFunc ('test'); 108} 109 110 111function catchError(sEval) 112{ 113 try {eval(sEval);} 114 catch(e) {return isRangeError(e);} 115 return cnNoErrorCaught; 116} 117 118 119function isRangeError(obj) 120{ 121 if (obj instanceof RangeError) 122 return cnIsRangeError; 123 return cnNotRangeError; 124} 125