test/script/basic/JDK-8023026.js

Wed, 18 Jun 2014 12:35:42 -0700

author
katleman
date
Wed, 18 Jun 2014 12:35:42 -0700
changeset 863
6e9c4e34bc61
parent 590
3470bc26128f
child 952
6d5471a497fb
child 962
ac62e33a99b0
permissions
-rw-r--r--

Added tag jdk8u20-b19 for changeset b047df215de4

sundar@502 1 /*
sundar@502 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
sundar@502 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
sundar@502 4 *
sundar@502 5 * This code is free software; you can redistribute it and/or modify it
sundar@502 6 * under the terms of the GNU General Public License version 2 only, as
sundar@502 7 * published by the Free Software Foundation.
sundar@502 8 *
sundar@502 9 * This code is distributed in the hope that it will be useful, but WITHOUT
sundar@502 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
sundar@502 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
sundar@502 12 * version 2 for more details (a copy is included in the LICENSE file that
sundar@502 13 * accompanied this code).
sundar@502 14 *
sundar@502 15 * You should have received a copy of the GNU General Public License version
sundar@502 16 * 2 along with this work; if not, write to the Free Software Foundation,
sundar@502 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
sundar@502 18 *
sundar@502 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
sundar@502 20 * or visit www.oracle.com if you need additional information or have any
sundar@502 21 * questions.
sundar@502 22 */
sundar@502 23
sundar@502 24 /**
sundar@502 25 * JDK-8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar@502 26 *
sundar@502 27 * @test
sundar@502 28 * @run
sundar@502 29 */
sundar@502 30
sundar@502 31 function checkIterations(obj) {
sundar@502 32 if (typeof obj.getClass == 'function') {
sundar@502 33 print("iterating on an object of " + obj.getClass());
sundar@502 34 } else {
sundar@502 35 print("iterating on " + String(obj));
sundar@502 36 }
sundar@502 37
sundar@502 38 Array.prototype.forEach.call(obj,
sundar@502 39 function(x) { print("forEach " + x); });
sundar@502 40
sundar@502 41 print("left sum " + Array.prototype.reduce.call(obj,
sundar@502 42 function(x, y) { print("reduce", x, y); return x + y; }));
sundar@502 43
sundar@502 44 print("right sum " + Array.prototype.reduceRight.call(obj,
sundar@502 45 function(x, y) { print("reduceRight", x, y); return x + y; }));
sundar@502 46
sundar@502 47 print("squared " + Array.prototype.map.call(obj,
sundar@502 48 function(x) x*x));
sundar@502 49 }
sundar@502 50
sundar@590 51 var array = new (Java.type("int[]"))(4);
sundar@502 52 for (var i in array) {
sundar@502 53 array[i] = i;
sundar@502 54 }
sundar@502 55
sundar@502 56 checkIterations(array);
sundar@502 57
sundar@502 58 var list = new java.util.ArrayList();
sundar@502 59 list.add(1);
sundar@502 60 list.add(3);
sundar@502 61 list.add(5);
sundar@502 62 list.add(7);
sundar@502 63
sundar@502 64 checkIterations(list);
sundar@502 65
sundar@502 66 var mirror = loadWithNewGlobal({
sundar@502 67 name: "test",
sundar@502 68 script: "[2, 4, 6, 8]"
sundar@502 69 });
sundar@502 70
sundar@502 71 checkIterations(mirror);

mercurial