1/*
2 * Copyright (c) 1999, 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 * @test
26 * @bug 4251519 4251520
27 * @summary indexOf and lastIndex of used to let you look outside the
28 *          valid range in the backing array
29 */
30
31import java.util.*;
32
33public class HeadTailTypeError {
34    public static void main(String argv[]) throws Exception {
35        try {
36            SortedMap m = new TreeMap();
37            m.headMap(new Object());
38            throw new Exception("headMap, natural ordering");
39        } catch (ClassCastException e) {
40        }
41
42        try {
43            SortedMap m = new TreeMap();
44            m.tailMap(new Object());
45            throw new Exception("tailMap, natural ordering");
46        } catch (ClassCastException e) {
47        }
48
49        try {
50            SortedMap m = new TreeMap(String.CASE_INSENSITIVE_ORDER);
51            m.headMap(new Integer(0));
52            throw new Exception("headMap, explicit comparator");
53        } catch (ClassCastException e) {
54        }
55
56        try {
57            SortedMap m = new TreeMap(String.CASE_INSENSITIVE_ORDER);
58            m.tailMap(new Integer(0));
59            throw new Exception("tailMap, explicit comparator");
60        } catch (ClassCastException e) {
61        }
62
63        try {
64            SortedSet m = new TreeSet();
65            m.headSet(new Object());
66            throw new Exception("headSet, natural ordering");
67        } catch (ClassCastException e) {
68        }
69
70        try {
71            SortedSet m = new TreeSet();
72            m.tailSet(new Object());
73            throw new Exception("tailSet, natural ordering");
74        } catch (ClassCastException e) {
75        }
76
77        try {
78            SortedSet m = new TreeSet(String.CASE_INSENSITIVE_ORDER);
79            m.headSet(new Integer(0));
80            throw new Exception("headSet, explicit comparator");
81        } catch (ClassCastException e) {
82        }
83
84        try {
85            SortedSet m = new TreeSet(String.CASE_INSENSITIVE_ORDER);
86            m.tailSet(new Integer(0));
87            throw new Exception("tailSet, explicit comparator");
88        } catch (ClassCastException e) {
89        }
90
91        try {
92            SortedMap m = new TreeMap();
93            m.headMap(null);
94            throw new Exception("(null endpoint)headMap, natural ordering");
95        } catch (NullPointerException e) {
96        }
97
98        try {
99            SortedMap m = new TreeMap();
100            m.tailMap(null);
101            throw new Exception("(null endpoint)tailMap, natural ordering");
102        } catch (NullPointerException e) {
103        }
104
105
106        try {
107            SortedMap m = new TreeMap(String.CASE_INSENSITIVE_ORDER);
108            m.headMap(null);
109            throw new Exception("(null endpoint)headMap, explicit comparator");
110        } catch (NullPointerException e) {
111        }
112
113        try {
114            SortedMap m = new TreeMap(String.CASE_INSENSITIVE_ORDER);
115            m.tailMap(null);
116            throw new Exception("(null endpoint)tailMap, explicit comparator");
117        } catch (NullPointerException e) {
118        }
119
120        try {
121            SortedSet m = new TreeSet();
122            m.headSet(null);
123            throw new Exception("(null endpoint)headSet, natural ordering");
124        } catch (NullPointerException e) {
125        }
126
127        try {
128            SortedSet m = new TreeSet();
129            m.tailSet(null);
130            throw new Exception("(null endpoint)tailSet, natural ordering");
131        } catch (NullPointerException e) {
132        }
133
134        try {
135            SortedSet m = new TreeSet(String.CASE_INSENSITIVE_ORDER);
136            m.headSet(null);
137            throw new Exception("(null endpoint)headSet, explicit comparator");
138        } catch (NullPointerException e) {
139        }
140
141        try {
142            SortedSet m = new TreeSet(String.CASE_INSENSITIVE_ORDER);
143            m.tailSet(null);
144            throw new Exception("(null endpoint)tailSet, explicit comparator");
145        } catch (NullPointerException e) {
146        }
147
148        // These should not fail
149        SortedMap m = new TreeMap();
150        m.headMap(new Integer(0));
151        m.tailMap(new Integer(0));
152        m = new TreeMap(String.CASE_INSENSITIVE_ORDER);
153        m.headMap("llama");
154        m.tailMap("llama");
155
156        SortedSet s = new TreeSet();
157        s.headSet(new Integer(0));
158        s.tailSet(new Integer(0));
159        s = new TreeSet(String.CASE_INSENSITIVE_ORDER);
160        s.headSet("drama");
161        s.tailSet("drama");
162    }
163}
164