nashorn/test/script/basic/arrays.js
author lagergren
Mon, 04 Feb 2013 16:20:05 +0100
changeset 16210 8ad1381b69d0
parent 16151 97c1e756ae1e
child 24778 2ff5d7041566
permissions -rw-r--r--
8007215: Varargs broken for the case of passing more than the arg limit arguments. Reviewed-by: jlaskey, attila
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
 * Basic Array tests.
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
var arr = new Array(3);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
print(arr.length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
print("isArray.length = " + Array.isArray.length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
print(Array.isArray(44));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
print(Array.isArray([44]));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
function even(num) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
    return (num % 2) == 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
print("join.length = " + Array.prototype.join.length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
print(["javascript", "is", "great"].join("<->"));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    45
var arr = [4, 56, 5];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
print("every.length = " + Array.prototype.every.length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
print(arr.toString() + " every even? = " + arr.every(even));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
arr = [4, 56, 688];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
print(arr.toString() + " every even? = " + arr.every(even));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    51
print("some.length = " + Array.prototype.some.length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
arr = [4, 56, 5];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
print(arr.toString() + " some even? = " + arr.some(even));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
arr = [3, 5, 17];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    55
print(arr.toString() + " some even? = " + arr.some(even));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
print("forEach.length = " + Array.prototype.forEach.length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
arr = [ "java", "javascript", "jython", "jruby"];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
arr.forEach(function(val, idx, obj) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
    print(obj.toString() + "[" + idx + "] is " + val);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
});
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
print(arr.map(function(val) { return val.toUpperCase(); }));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
print("shifted is " + arr.shift() + ", remaining is " + arr.toString() + ", length is " + arr.length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
arr = [ "c++", "java", "javascript", "objective c" ];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
print(arr.filter(function(val) { return val.charAt(0) == 'j'; }));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
print([3, 66, 2, 44].reduce(function (acc, e) { return acc + e; }));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
print([1, 2, 3, 4, 5].reduce(function (acc, e) { return acc * e; }));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
print(arr.reduce(
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
    function(acc, e) { return acc + " " + e; }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
print(["javascript", "from", "world", "hello"].reduceRight(
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
    function(acc, x) { return acc + " " + x; }
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
var langs = ["java", "javascript", "jython", "jruby", "c"];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
print("indexOf.length = " + Array.prototype.indexOf.length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
print("indexOf('java') = " + langs.indexOf("java"));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
print("indexOf('javascript') = " + langs.indexOf("javascript"));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
print("indexOf('javascript', 3) = " + langs.indexOf("javascript", 3));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    85
print("indexOf('c++') = " + langs.indexOf("c++"));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
print("[].indexOf('any') = " + [].indexOf("any"));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    87
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    88
langs = ["java", "javascript", "jython", "jruby", "java", "jython", "c"];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
print("lastIndexOf.length = " + Array.prototype.lastIndexOf.length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
print("lastIndexOf('java') = " + langs.lastIndexOf("java"));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
print("lastIndexOf('jython') = " + langs.lastIndexOf("jython"));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    92
print("lastIndexOf('c') = " + langs.lastIndexOf("c"));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    93
print("lastIndexOf('c++') = " + langs.lastIndexOf("c++"));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
print("[].lastIndexOf('any') = " + [].lastIndexOf("any"));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
print("concat.length = " + Array.prototype.concat.length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
print(["foo", "bar"].concat(["x", "y"], 34, "sss", [3, 4, 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
// Check various array length arguments to constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   101
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   102
function expectRangeError(length) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   103
    try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   104
        var arr = new Array(length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
        print("range error expected for " + length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   106
    } catch (e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   107
        if (! (e instanceof RangeError)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
            print("range error expected for " + length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   109
        }
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
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   113
expectRangeError(NaN);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   114
expectRangeError(Infinity);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
expectRangeError(-Infinity);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   116
expectRangeError(-10);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
var arr = new Array("10");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
if (arr.length != 1 && arr[0] != '10') {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
    throw new Error("expected length 1 array");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   123
arr = new Array(new Number(34));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
if (arr.length != 1 && arr[0] != new Number(34)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   125
    throw new Error("expected length 1 array");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   128
arr = new Array(15);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
if (arr.length != 15) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
    throw new Error("expected length 15 array");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
print("Array.length = " + Array.length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
print([NaN,NaN,NaN]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
// check setting array's length
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   138
arr = [3,2,1];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   139
arr.length = 1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   140
print(arr);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   141
print(arr.length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   142
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   143
// test typeof array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
var numberArray = [];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   145
numberArray[0] = 1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
print(typeof numberArray[0]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
print(numberArray.toLocaleString());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
// Array functions on non-array objects
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
print(Array.prototype.join.call(new java.lang.Object()));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
print(Array.prototype.concat.call("hello", "world"));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   154
print(Array.prototype.map.call("hello", function() {}));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   155
print(Array.prototype.reduce.call("hello", function() {}));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   156
print(Array.prototype.toString.call(new java.lang.Object()));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
print(Array.prototype.toLocaleString.call(new java.lang.Object()));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   158
print(Array.prototype.reduceRight.call(new java.lang.Object(), 
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
      function() {}, 33));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160