test/script/basic/JDK-8015267.js

Thu, 23 May 2013 12:01:35 +0200

author
attila
date
Thu, 23 May 2013 12:01:35 +0200
changeset 286
1c1453863ea8
parent 0
b1a7da25b547
child 962
ac62e33a99b0
permissions
-rw-r--r--

8015267: Allow conversion of JS arrays to Java List/Deque
Reviewed-by: lagergren, sundar

     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  */
    24 /**
    25  * JDK-8015267: have a List/Deque adapter for JS array-like objects
    26  *
    27  * @test
    28  * @run
    29  */
    31 var a = ['a', 'b', 'c', 'd']
    33 var l = Java.to(a, java.util.List)
    34 print(l instanceof java.util.List)
    35 print(l instanceof java.util.Deque)
    37 print(l[0])
    38 print(l[1])
    39 print(l[2])
    40 print(l[3])
    42 print(l.size())
    44 l.push('x')
    45 print(a)
    47 l.addLast('y')
    48 print(a)
    50 print(l.pop())
    51 print(l.removeLast())
    52 print(a)
    54 l.add('e')
    55 l.add(5, 'f')
    56 print(a)
    58 l.add(0, 'z')
    59 print(a)
    61 l.add(2, 'x')
    62 print(a)
    64 l[7] = 'g'
    65 print(a)
    67 try { l.add(15, '') } catch(e) { print(e.class) } 
    68 try { l.remove(15) } catch(e) { print(e.class) } 
    69 try { l.add(-1, '') } catch(e) { print(e.class) } 
    70 try { l.remove(-1) } catch(e) { print(e.class) } 
    72 l.remove(7)
    73 l.remove(2)
    74 l.remove(0)
    75 print(a)
    77 print(l.peek())
    78 print(l.peekFirst())
    79 print(l.peekLast())
    81 print(l.element())
    82 print(l.getFirst())
    83 print(l.getLast())
    85 l.offer('1')
    86 l.offerFirst('2')
    87 l.offerLast('3')
    88 print(a)
    90 a = ['1', '2', 'x', '3', '4', 'x', '5', '6', 'x', '7', '8']
    91 print(a)
    92 var l = Java.to(a, java.util.List)
    93 l.removeFirstOccurrence('x')
    94 print(a)
    95 l.removeLastOccurrence('x')
    96 print(a)
    98 var empty = Java.to([], java.util.List)
    99 try { empty.pop() } catch(e) { print(e.class) }
   100 try { empty.removeFirst() } catch(e) { print(e.class) }
   101 try { empty.removeLast() } catch(e) { print(e.class) }
   103 try { empty.element() } catch(e) { print(e.class) }
   104 try { empty.getFirst() } catch(e) { print(e.class) }
   105 try { empty.getLast() } catch(e) { print(e.class) }
   107 print(empty.peek())
   108 print(empty.peekFirst())
   109 print(empty.peekLast())

mercurial