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 (at) netscape.com 20 * Date: 2001-07-15 21 * 22 * SUMMARY: Testing Number.prototype.toPrecision(precision) 23 * See EMCA 262 Edition 3 Section 15.7.4.7 24 * 25 * Also see http://bugzilla.mozilla.org/show_bug.cgi?id=90551 26 * 27 */ 28 //----------------------------------------------------------------------------- 29 var UBound = 0; 30 var bug = '(none)'; 31 var summary = 'Testing Number.prototype.toPrecision(precision)'; 32 var cnIsRangeError = 'instanceof RangeError'; 33 var cnNotRangeError = 'NOT instanceof RangeError'; 34 var cnNoErrorCaught = 'NO ERROR CAUGHT...'; 35 var status = ''; 36 var statusitems = []; 37 var actual = ''; 38 var actualvalues = []; 39 var expect= ''; 40 var expectedvalues = []; 41 var testNum = 5.123456; 42 43 44 status = 'Section A of test: no error intended!'; 45 actual = testNum.toPrecision(4); 46 expect = '5.123'; 47 captureThis(); 48 49 50 /////////////////////////// OOPS.... /////////////////////////////// 51 /************************************************************************* 52 * 15.7.4.7 Number.prototype.toPrecision(precision) 53 * 54 * An implementation is permitted to extend the behaviour of toPrecision 55 * for values of precision less than 1 or greater than 21. In this 56 * case toPrecision would not necessarily throw RangeError for such values. 57 58 status = 'Section B of test: expect RangeError because precision < 1'; 59 actual = catchError('testNum.toPrecision(0)'); 60 expect = cnIsRangeError; 61 captureThis(); 62 63 status = 'Section C of test: expect RangeError because precision < 1'; 64 actual = catchError('testNum.toPrecision(-4)'); 65 expect = cnIsRangeError; 66 captureThis(); 67 68 status = 'Section D of test: expect RangeError because precision > 21 '; 69 actual = catchError('testNum.toPrecision(22)'); 70 expect = cnIsRangeError; 71 captureThis(); 72 *************************************************************************/ 73 74 75 76 //----------------------------------------------------------------------------- 77 test(); 78 //----------------------------------------------------------------------------- 79 80 81 function captureThis() 82 { 83 statusitems[UBound] = status; 84 actualvalues[UBound] = actual; 85 expectedvalues[UBound] = expect; 86 UBound++; 87 } 88 89 90 function test() 91 { 92 enterFunc ('test'); 93 printBugNumber (bug); 94 printStatus (summary); 95 96 for (var i = 0; i < UBound; i++) 97 { 98 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); 99 } 100 101 exitFunc ('test'); 102 } 103 104 105 function catchError(sEval) 106 { 107 try {eval(sEval);} 108 catch(e) {return isRangeError(e);} 109 return cnNoErrorCaught; 110 } 111 112 113 function isRangeError(obj) 114 { 115 if (obj instanceof RangeError) 116 return cnIsRangeError; 117 return cnNotRangeError; 118 } 119