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