1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * NASHORN-12 :  Number does not have methods toFixed, toPrecision, and toExponential.
  26  *
  27  * @test
  28  * @run
  29  */
  30 
  31 // checks for Number.prototype.toFixed
  32 
  33 if (NaN.toFixed() !== "NaN") {
  34     fail("#1 NaN.toFixed() is not NaN");
  35 }
  36 
  37 if (new Number(1e21).toFixed(12) !== String(1e21)) {
  38     fail("#2 new Number(1e21).toFixed(12) is not String(1e21)");
  39 }
  40 
  41 if (new Number(1.2).toFixed(3) !== "1.200") {
  42     fail("#3 new Number(1.2).toFixed(3) is not '1.200'");
  43 }
  44 
  45 if (new Number(1.2542).toFixed(3) !== "1.254") {
  46     fail("#4 Number(1.2542).toFixed(3) is not '1.254'");
  47 }
  48 
  49 try {
  50     453.334.toFixed(31);
  51     fail("#5 toFixed(31) should have thrown RangeError");
  52 } catch (e) {
  53     if (! (e instanceof RangeError)) {
  54         fail("#6 toFixed(31) should throw RangeError, got " + e);
  55     }
  56 }
  57 
  58 try {
  59     3.14.toFixed(-1);
  60     fail("#7 toFixed(-1) should have thrown RangeError");
  61 } catch (e) {
  62     if (! (e instanceof RangeError)) {
  63         fail("#8 toFixed(-1) should throw RangeError, got " + e);
  64     }
  65 }
  66 
  67 
  68 // checks for Number.prototype.toPrecision
  69 
  70 var num = new Number(0.0);
  71 
  72 try {
  73     num.toPrecision(0);
  74     fail("#9: num.toPrecision(0) should have been thrown RangeError");
  75 } catch (e) {
  76     if (! (e instanceof RangeError)) {
  77         fail("#10: RangeError expected, got " + e);
  78     }
  79 }
  80 
  81 try {
  82     num.toPrecision(22);
  83     fail("#11: num.toPrecision(22) should have been thrown RangeError");
  84 } catch (e) {
  85     if (! (e instanceof RangeError)) {
  86         fail("#12: RangeError expected, got " + e);
  87     }
  88 }
  89 
  90 num = new Number(23.4718);
  91 
  92 if (num.toPrecision(1) != "2e+1") {
  93     fail("#13: toPrecision(1) gives " + num.toPrecision(1));
  94 }
  95 
  96 if (num.toPrecision(2) != "23") {
  97     fail("#14: toPrecision(2) gives " + num.toPrecision(2));
  98 }
  99 
 100 if (num.toPrecision(3) != "23.5") {
 101     fail("#15: toPrecision(3) gives " + num.toPrecision(3));
 102 }
 103 
 104 if (num.toPrecision(11) != "23.471800000") {
 105     fail("#16: toPrecision(11) gives " + num.toPrecision(11));
 106 }
 107 
 108 if (Infinity.toPrecision(1) != "Infinity") {
 109     fail("#17: Infinity.toPrecision(1) gives " + Infinity.toPrecision(1));
 110 }
 111 
 112 if (-Infinity.toPrecision(1) != "-Infinity") {
 113     fail("#18: -Infinity.toPrecision(1) gives " + -Infinity.toPrecision(1));
 114 }
 115 
 116 // checks for Number.prototype.toExponential
 117 
 118 if (num.toExponential(1) != "2.3e+1") {
 119     fail("#20: toExponential(1) gives " + num.toExponential(1));
 120 }
 121 
 122 if (num.toExponential(2) != "2.35e+1") {
 123     fail("#21: toExponential(2) gives " + num.toExponential(2));
 124 }
 125 
 126 if (num.toExponential(3) != "2.347e+1") {
 127     fail("#22: toExponential(3) gives " + num.toExponential(3));
 128 }
 129 
 130 if (num.toExponential(11) != "2.34718000000e+1") {
 131     fail("#23: toExponential(11) gives " + num.toExponential(11));
 132 }
 133 
 134 if (Infinity.toExponential(1) != "Infinity") {
 135     fail("#24: Infinity.toExponential(1) gives " + Infinity.toExponential(1));
 136 }
 137 
 138 if (-Infinity.toExponential(1) != "-Infinity") {
 139     fail("#25: -Infinity.toExponential(1) gives " + -Infinity.toExponential(1));
 140 }
 141 
 142 if (NaN.toExponential(1) != "NaN") {
 143     fail("#26: NaN.toExponential(1) gives " + NaN.toExponential(1));
 144 }
 145