blob: bf25668d30174c5c05265b1eb04e80c18042600f [file] [log] [blame]
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001/*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/publicdomain/zero/1.0/
5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
8
9package jsr166;
10
Calin Juravle8f0d92b2013-08-01 17:26:00 +010011import java.util.AbstractQueue;
12import java.util.Arrays;
13import java.util.Iterator;
14import java.util.NoSuchElementException;
15
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010016import junit.framework.Test;
17import junit.framework.TestSuite;
18
Calin Juravle8f0d92b2013-08-01 17:26:00 +010019public class AbstractQueueTest extends JSR166TestCase {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010020 // android-note: Removed because the CTS runner does a bad job of
21 // retrying tests that have suite() declarations.
22 //
23 // public static void main(String[] args) {
24 // main(suite(), args);
25 // }
26 // public static Test suite() {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +000027 // return new TestSuite(AbstractQueueTest.class);
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010028 // }
Calin Juravle8f0d92b2013-08-01 17:26:00 +010029
30 static class Succeed extends AbstractQueue<Integer> {
31 public boolean offer(Integer x) {
32 if (x == null) throw new NullPointerException();
33 return true;
34 }
35 public Integer peek() { return one; }
36 public Integer poll() { return one; }
37 public int size() { return 0; }
38 public Iterator iterator() { return null; } // not needed
39 }
40
41 static class Fail extends AbstractQueue<Integer> {
42 public boolean offer(Integer x) {
43 if (x == null) throw new NullPointerException();
44 return false;
45 }
46 public Integer peek() { return null; }
47 public Integer poll() { return null; }
48 public int size() { return 0; }
49 public Iterator iterator() { return null; } // not needed
50 }
51
52 /**
53 * add returns true if offer succeeds
54 */
55 public void testAddS() {
56 Succeed q = new Succeed();
57 assertTrue(q.add(two));
58 }
59
60 /**
61 * add throws ISE true if offer fails
62 */
63 public void testAddF() {
64 Fail q = new Fail();
65 try {
66 q.add(one);
67 shouldThrow();
68 } catch (IllegalStateException success) {}
69 }
70
71 /**
72 * add throws NPE if offer does
73 */
74 public void testAddNPE() {
75 Succeed q = new Succeed();
76 try {
77 q.add(null);
78 shouldThrow();
79 } catch (NullPointerException success) {}
80 }
81
82 /**
83 * remove returns normally if poll succeeds
84 */
85 public void testRemoveS() {
86 Succeed q = new Succeed();
87 q.remove();
88 }
89
90 /**
91 * remove throws NSEE if poll returns null
92 */
93 public void testRemoveF() {
94 Fail q = new Fail();
95 try {
96 q.remove();
97 shouldThrow();
98 } catch (NoSuchElementException success) {}
99 }
100
101 /**
102 * element returns normally if peek succeeds
103 */
104 public void testElementS() {
105 Succeed q = new Succeed();
106 q.element();
107 }
108
109 /**
110 * element throws NSEE if peek returns null
111 */
112 public void testElementF() {
113 Fail q = new Fail();
114 try {
115 q.element();
116 shouldThrow();
117 } catch (NoSuchElementException success) {}
118 }
119
120 /**
121 * addAll(null) throws NPE
122 */
123 public void testAddAll1() {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100124 Succeed q = new Succeed();
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100125 try {
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100126 q.addAll(null);
127 shouldThrow();
128 } catch (NullPointerException success) {}
129 }
130
131 /**
132 * addAll(this) throws IAE
133 */
134 public void testAddAllSelf() {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100135 Succeed q = new Succeed();
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100136 try {
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100137 q.addAll(q);
138 shouldThrow();
139 } catch (IllegalArgumentException success) {}
140 }
141
142 /**
143 * addAll of a collection with null elements throws NPE
144 */
145 public void testAddAll2() {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100146 Succeed q = new Succeed();
147 Integer[] ints = new Integer[SIZE];
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100148 try {
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100149 q.addAll(Arrays.asList(ints));
150 shouldThrow();
151 } catch (NullPointerException success) {}
152 }
153
154 /**
155 * addAll of a collection with any null elements throws NPE after
156 * possibly adding some elements
157 */
158 public void testAddAll3() {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100159 Succeed q = new Succeed();
160 Integer[] ints = new Integer[SIZE];
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000161 for (int i = 0; i < SIZE - 1; ++i)
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100162 ints[i] = new Integer(i);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100163 try {
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100164 q.addAll(Arrays.asList(ints));
165 shouldThrow();
166 } catch (NullPointerException success) {}
167 }
168
169 /**
170 * addAll throws ISE if an add fails
171 */
172 public void testAddAll4() {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100173 Fail q = new Fail();
174 Integer[] ints = new Integer[SIZE];
175 for (int i = 0; i < SIZE; ++i)
176 ints[i] = new Integer(i);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100177 try {
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100178 q.addAll(Arrays.asList(ints));
179 shouldThrow();
180 } catch (IllegalStateException success) {}
181 }
182
183}