aboutsummaryrefslogtreecommitdiff
path: root/test/script/basic/arrays.js
blob: 03ad4312e1f37a7e7dbabbcd0a3beb64c142a09e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * Basic Array tests.
 *
 * @test
 * @run
 */

var arr = new Array(3);
print(arr.length);

print("isArray.length = " + Array.isArray.length);
print(Array.isArray(44));
print(Array.isArray([44]));

function even(num) {
    return (num % 2) == 0;
}

print("join.length = " + Array.prototype.join.length);
print(["javascript", "is", "great"].join("<->"));

var arr = [4, 56, 5];
print("every.length = " + Array.prototype.every.length);
print(arr.toString() + " every even? = " + arr.every(even));
arr = [4, 56, 688];
print(arr.toString() + " every even? = " + arr.every(even));

print("some.length = " + Array.prototype.some.length);
arr = [4, 56, 5];
print(arr.toString() + " some even? = " + arr.some(even));
arr = [3, 5, 17];
print(arr.toString() + " some even? = " + arr.some(even));

print("forEach.length = " + Array.prototype.forEach.length);
arr = [ "java", "javascript", "jython", "jruby"];
arr.forEach(function(val, idx, obj) {
    print(obj.toString() + "[" + idx + "] is " + val);
});

print(arr.map(function(val) { return val.toUpperCase(); }));
print("shifted is " + arr.shift() + ", remaining is " + arr.toString() + ", length is " + arr.length);

arr = [ "c++", "java", "javascript", "objective c" ];
print(arr.filter(function(val) { return val.charAt(0) == 'j'; }));

print([3, 66, 2, 44].reduce(function (acc, e) { return acc + e; }));
print([1, 2, 3, 4, 5].reduce(function (acc, e) { return acc * e; }));

print(arr.reduce(
    function(acc, e) { return acc + " " + e; }
));

print(["javascript", "from", "world", "hello"].reduceRight(
    function(acc, x) { return acc + " " + x; }
));

var langs = ["java", "javascript", "jython", "jruby", "c"];
print("indexOf.length = " + Array.prototype.indexOf.length);
print("indexOf('java') = " + langs.indexOf("java"));
print("indexOf('javascript') = " + langs.indexOf("javascript"));
print("indexOf('javascript', 3) = " + langs.indexOf("javascript", 3));
print("indexOf('c++') = " + langs.indexOf("c++"));
print("[].indexOf('any') = " + [].indexOf("any"));

langs = ["java", "javascript", "jython", "jruby", "java", "jython", "c"];
print("lastIndexOf.length = " + Array.prototype.lastIndexOf.length);
print("lastIndexOf('java') = " + langs.lastIndexOf("java"));
print("lastIndexOf('jython') = " + langs.lastIndexOf("jython"));
print("lastIndexOf('c') = " + langs.lastIndexOf("c"));
print("lastIndexOf('c++') = " + langs.lastIndexOf("c++"));
print("[].lastIndexOf('any') = " + [].lastIndexOf("any"));

print("concat.length = " + Array.prototype.concat.length);
print(["foo", "bar"].concat(["x", "y"], 34, "sss", [3, 4, 2]));


// Check various array length arguments to constructor

function expectRangeError(length) {
    try {
        var arr = new Array(length);
        print("range error expected for " + length);
    } catch (e) {
        if (! (e instanceof RangeError)) {
            print("range error expected for " + length);
        }
    }
}

expectRangeError(NaN);
expectRangeError(Infinity);
expectRangeError(-Infinity);
expectRangeError(-10);

var arr = new Array("10");
if (arr.length != 1 && arr[0] != '10') {
    throw new Error("expected length 1 array");
}

arr = new Array(new Number(34));
if (arr.length != 1 && arr[0] != new Number(34)) {
    throw new Error("expected length 1 array");
}

arr = new Array(15);
if (arr.length != 15) {
    throw new Error("expected length 15 array");
}

print("Array.length = " + Array.length);

print([NaN,NaN,NaN]);

// check setting array's length
arr = [3,2,1];
arr.length = 1;
print(arr);
print(arr.length);

// test typeof array
var numberArray = [];
numberArray[0] = 1;
print(typeof numberArray[0]);

print(numberArray.toLocaleString());

// Array functions on non-array objects

print(Array.prototype.join.call(new java.lang.Object()));
print(Array.prototype.concat.call("hello", "world"));
print(Array.prototype.map.call("hello", function() {}));
print(Array.prototype.reduce.call("hello", function() {}));
print(Array.prototype.toString.call(new java.lang.Object()));
print(Array.prototype.toLocaleString.call(new java.lang.Object()));
print(Array.prototype.reduceRight.call(new java.lang.Object(),
      function() {}, 33));