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