comparison src/tests/gov/nasa/jpf/util/AvailableBufferedInputStreamTest.java @ 0:61d41facf527

initial v8 import (history reset)
author Peter Mehlitz <Peter.C.Mehlitz@nasa.gov>
date Fri, 23 Jan 2015 10:14:01 -0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:61d41facf527
1 /*
2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
4 * All rights reserved.
5 *
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0.
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package gov.nasa.jpf.util;
19
20 import gov.nasa.jpf.util.test.TestJPF;
21 import java.io.IOException;
22 import java.io.PipedInputStream;
23 import java.io.PipedOutputStream;
24
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28
29 public class AvailableBufferedInputStreamTest extends TestJPF {
30
31 private static final int PIPE_SIZE = 1024 * 1024;
32
33 private PipedOutputStream m_output;
34 private AvailableBufferedInputStream m_input;
35
36 @Before
37 public void before() throws IOException {
38 m_output = new PipedOutputStream();
39 m_input = new AvailableBufferedInputStream(new PipedInputStream(m_output, PIPE_SIZE));
40 }
41
42 @After
43 public void after() throws IOException {
44 m_output.flush();
45 m_output.close();
46 m_output = null;
47
48 assertEquals(0, m_input.available());
49 assertEquals(-1, m_input.peek());
50 assertEquals(-1, m_input.read());
51
52 m_input.close();
53 m_input = null;
54 }
55
56 @Test(expected = NullPointerException.class)
57 public void passNullPointerToConstructor() throws IOException {
58 new AvailableBufferedInputStream(null).close();
59 }
60
61 @Test
62 public void availableStuck() throws IOException {
63 int i;
64
65 for (i = 10; --i >= 0;) {
66 m_output.write(i);
67 m_output.flush();
68 assertEquals(1, m_input.available());
69 }
70
71 for (i = 10; --i >= 0;) {
72 assertEquals(i, m_input.peek());
73 assertEquals(i, m_input.read());
74 }
75 }
76
77 @Test
78 public void unreadExtra() throws IOException {
79 int i;
80
81 m_output.write(20);
82 m_output.write(21);
83 m_output.flush();
84
85 assertEquals(20, m_input.peek());
86 assertEquals(20, m_input.read()); // Load the buffer
87
88 for (i = 0; i < 10; i++) {
89 m_input.unread(i);
90 }
91
92 for (i = 10; --i >= 0;) {
93 assertEquals(i, m_input.peek());
94 assertEquals(i, m_input.read());
95 assertEquals(i + 1, m_input.available());
96 }
97
98 assertEquals(21, m_input.peek());
99 assertEquals(21, m_input.read());
100 }
101
102 @Test
103 public void unreadMinus1() throws IOException {
104 m_input.unread(-1);
105 assertEquals(0x00FF, m_input.peek());
106 assertEquals(0x00FF, m_input.read());
107 }
108
109 @Test
110 public void readBufferSplit() throws IOException {
111 byte buffer[];
112
113 buffer = new byte[2];
114
115 m_output.write(30);
116 m_output.flush();
117
118 m_input.available();
119
120 m_output.write(40);
121 m_output.flush();
122
123 assertEquals(30, m_input.peek());
124 assertEquals(1, m_input.read(buffer));
125 assertEquals(30, buffer[0]);
126
127 assertEquals(40, m_input.peek());
128 assertEquals(1, m_input.read(buffer));
129 assertEquals(40, buffer[0]);
130 }
131
132 @Test
133 public void readBufferPartialNoBlock() throws IOException {
134 byte buffer[];
135
136 buffer = new byte[2];
137
138 m_output.write(30);
139 m_output.flush();
140
141 assertEquals(30, m_input.peek());
142 assertEquals(1, m_input.read(buffer));
143 assertEquals(30, buffer[0]);
144 }
145
146 @Test
147 public void readBufferLeftOver() throws IOException {
148 byte buffer[];
149
150 buffer = new byte[2];
151
152 m_output.write(30);
153 m_output.write(40);
154 m_output.write(50);
155 m_output.flush();
156
157 assertEquals(30, m_input.peek());
158 assertEquals(2, m_input.read(buffer));
159 assertEquals(30, buffer[0]);
160 assertEquals(40, buffer[1]);
161
162 assertEquals(50, m_input.peek());
163 assertEquals(1, m_input.read(buffer));
164 assertEquals(50, buffer[0]);
165 assertEquals(40, buffer[1]);
166 }
167
168 @Test
169 public void unreadOverflow() throws IOException {
170 int i;
171
172 try {
173 for (i = 0; i < m_input.getBufferSize(); i++) {
174 m_input.unread(i);
175 assertEquals(i & 0x00FF, m_input.peek());
176 }
177 } catch (IOException e) {
178 fail();
179 }
180
181 try {
182 m_input.unread(0);
183 fail();
184 } catch (IOException e) {
185 e = null; // Get rid of IDE warning
186 }
187
188 for (i = m_input.getBufferSize(); --i >= 0;) {
189 assertEquals(i & 0x00FF, m_input.peek());
190 assertEquals(i & 0x00FF, m_input.read());
191 }
192 }
193
194 @Test
195 public void fillWithNoMoreData() throws IOException {
196 assertEquals(0, m_input.available());
197 assertEquals(0, m_input.available());
198 }
199
200 @Test
201 public void fillWithTooMuchData() throws IOException {
202 int i;
203
204 for (i = 0; i < m_input.getBufferSize() + 1; i++) {
205 m_output.write(i);
206 }
207
208 m_output.flush();
209
210 assertEquals(m_input.getBufferSize(), m_input.available());
211
212 for (i = 0; i < m_input.getBufferSize() + 1; i++) {
213 assertEquals(i & 0x00FF, m_input.peek());
214 assertEquals(i & 0x00FF, m_input.read());
215 }
216 }
217
218 @Test
219 public void readAfterClose() throws IOException {
220 m_output.write(10);
221 m_output.flush();
222
223 m_input.available();
224
225 m_output.close();
226
227 assertEquals(10, m_input.peek());
228 assertEquals(10, m_input.read());
229 assertEquals(-1, m_input.peek());
230 assertEquals(-1, m_input.read());
231 }
232
233 @Test
234 public void readBufferAfterClose() throws IOException {
235 byte buffer[];
236
237 m_output.write(10);
238 m_output.flush();
239
240 m_input.available();
241
242 m_output.close();
243
244 buffer = new byte[10];
245
246 assertEquals(1, m_input.read(buffer));
247 assertEquals(-1, m_input.read(buffer));
248 }
249
250 @Test
251 public void testToString() throws IOException {
252 int i;
253
254 assertEquals("", m_input.toString());
255
256 m_output.write(new byte[]{'h', 'e', 'l', 'l', 'o'});
257 m_output.flush();
258
259 m_input.available();
260
261 assertEquals("hello", m_input.toString());
262
263 for (i = 5; --i >= 0;) {
264 m_input.read();
265 }
266 }
267
268 @Test
269 public void readBufferEmptyBuffer() throws IOException {
270 byte buffer[];
271
272 m_output.write(10);
273 m_output.flush();
274
275 buffer = new byte[1];
276
277 assertEquals(1, m_input.read(buffer));
278 assertEquals(10, buffer[0]);
279 }
280 }