File: InspectableFileCachedInputStreamTest.java

package info (click to toggle)
libfastutil-java 7.0.13-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,760 kB
  • ctags: 1,211
  • sloc: java: 12,408; sh: 638; makefile: 385; xml: 212
file content (216 lines) | stat: -rw-r--r-- 6,518 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package it.unimi.dsi.fastutil.io;

/*		 
 * fastutil: Fast & compact type-specific collections for Java
 *
 * Copyright (C) 2013 Sebastiano Vigna 
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import it.unimi.dsi.fastutil.io.InspectableFileCachedInputStream;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import org.junit.Test;

public class InspectableFileCachedInputStreamTest {

	private static final Random r = new Random( 0 );
	
	public static List<byte[]> byteArrays;
	static {
		byteArrays = new ArrayList<byte[]>();
		byte[] b;
		// Now generates byte buffers from 1 byte up to 64KiB; we shuffle them so that they are not increasing in size... 
		for ( int k = 0; k < 10; k++ ) {
			b = new byte[ 1 << k ];
			r.nextBytes( b );
			byteArrays.add( b );
		}
		for ( int k = 16; k >= 10; k-- ) {
			b = new byte[ 1 << k ];
			r.nextBytes( b );
			byteArrays.add( b );
		}
		byteArrays.add( new byte[] {} );
		byteArrays.add( "This is a short\nnon empty and purely ASCII\nbyte sequence".getBytes() );
	}
	

	@Test
	public void testSmall() throws IOException {
		InspectableFileCachedInputStream icis = new InspectableFileCachedInputStream( 4 );
		assertTrue( icis.isOpen() );
		byte[] data = new byte[] { 1, 2 };
		icis.write( ByteBuffer.wrap( data ) );
		
		assertEquals( 2, icis.length() );
		assertEquals( 1, icis.read() );
		assertEquals( 2, icis.read() );
		assertEquals( -1, icis.read() );
		
		icis.position( 0 );
		byte b[] = new byte[ 2 ];
		assertEquals( 2, icis.read( b ) );
		assertArrayEquals( data, b );
		assertEquals( -1, icis.read() );
		assertEquals( -1, icis.read( b, 0, b.length ) );
		assertEquals( 0, icis.read( b, 0, 0 ) );

		icis.clear();
		assertTrue( icis.isOpen() );
		data = new byte[] { 1, 2, 3, 4, 5 };
		icis.write( ByteBuffer.wrap( data ) );
		
		assertEquals( 5, icis.length() );
		assertEquals( 1, icis.read() );
		assertEquals( 2, icis.read() );
		assertEquals( 3, icis.read() );
		assertEquals( 4, icis.read() );
		assertEquals( 5, icis.read() );
		assertEquals( -1, icis.read() );
		
		icis.position( 0 );
		assertEquals( 0, icis.position() );
		b = new byte[ 5 ];
		assertEquals( 5, icis.read( b ) );
		assertArrayEquals( data, b );

		icis.position( 2 );
		b = new byte[ 4 ];
		assertEquals( 3, icis.read( b ) );
		assertArrayEquals( Arrays.copyOfRange( data, 2, 5 ), Arrays.copyOfRange( b, 0, 3 ) );


		icis.position( 0 );
		assertEquals( 1, icis.read() );

		icis.position( 4 );
		assertEquals( 1, icis.available() );
		assertEquals( 5, icis.read() );
		assertEquals( 5, icis.position() );

		icis.position( 0 );
		assertEquals( 2, icis.skip( 2 ) );
		assertEquals( 2, icis.skip( 2 ) );
		assertEquals( 5, icis.read() );
		assertEquals( 5, icis.position() );

		icis.position( 5 );
		assertEquals( -1, icis.read() );
		assertEquals( -1, icis.read( b, 0, b.length ) );

		icis.close();
		icis.dispose();
	}

	@Test
	public void test() throws IOException {
		for( int bufferSize: new int[] { 1, 2, 1024, 16384, 1024 * 1024 } ) {
			InspectableFileCachedInputStream icis = new InspectableFileCachedInputStream( bufferSize );
			for( byte[] a: byteArrays ) icis.write( ByteBuffer.wrap( a ) );
			for( byte[] a: byteArrays ) {
				final byte[] buffer = new byte[ a.length ];
				icis.read( buffer );
				assertArrayEquals( a, buffer );
			}

			icis.position( 0 );
			icis.truncate( 0 );
			
			for( byte[] a: byteArrays )
				for( byte b: a ) assertEquals( b, (byte)icis.read() );

			icis.close();
			icis.dispose();
		}
	}

	@Test
	public void testWithSpecifiedFile() throws IOException {
		final InspectableFileCachedInputStream icis = new InspectableFileCachedInputStream( 4, File.createTempFile( getClass().getSimpleName(), "overflow" ) );
		final byte[] data = new byte[] { 1, 2 };
		icis.write( ByteBuffer.wrap( data ) );
		
		assertEquals( 2, icis.length() );
		assertEquals( 1, icis.read() );
		assertEquals( 2, icis.read() );
		assertEquals( -1, icis.read() );
		
		icis.close();
		icis.dispose();
	}
	
	@Test(expected=IOException.class)
	public void testClosed() throws IOException {
		final InspectableFileCachedInputStream icis = new InspectableFileCachedInputStream( 4 );
		final byte[] data = new byte[] { 1, 2 };
		icis.write( ByteBuffer.wrap( data ) );
		icis.close();
		assertFalse( icis.isOpen() );
		icis.read();
	}

	@Test(expected=IOException.class)
	public void testDisposed() throws IOException {
		@SuppressWarnings("resource")
		final InspectableFileCachedInputStream icis = new InspectableFileCachedInputStream( 4 );
		final byte[] data = new byte[] { 1, 2 };
		icis.write( ByteBuffer.wrap( data ) );
		icis.dispose();
		assertFalse( icis.isOpen() );
		icis.read();
	}

	@Test(expected=IOException.class)
	public void testClearDisposed() throws IOException {
		@SuppressWarnings("resource")
		final InspectableFileCachedInputStream icis = new InspectableFileCachedInputStream();
		final byte[] data = new byte[] { 1, 2 };
		icis.write( ByteBuffer.wrap( data ) );
		icis.dispose();
		icis.clear();
	}
	
	@Test(expected=IOException.class)
	public void testResetDisposed() throws IOException {
		@SuppressWarnings("resource")
		final InspectableFileCachedInputStream icis = new InspectableFileCachedInputStream();
		final byte[] data = new byte[] { 1, 2 };
		icis.write( ByteBuffer.wrap( data ) );
		icis.dispose();
		icis.reset();
	}
	
	@SuppressWarnings("resource")
	@Test(expected=IllegalArgumentException.class)
	public void testNegativeBuffer() throws IOException {
		new InspectableFileCachedInputStream( -1 );
	}

}