Commit 623c9e71 authored by Ole Streicher's avatar Ole Streicher

Merge tag 'upstream/0.1+2017.02.21'

Upstream version 0.1+2017.02.21
parents 03858373 0e9f5409
......@@ -151,11 +151,11 @@ public abstract class AbstractArrayDataIO
}
public int readUnsignedByte() throws IOException {
return get() | 0xff;
return get() & 0xff;
}
public int readUnsignedShort() throws IOException {
return readShort() | 0xffff;
return readShort() & 0xffff;
}
/*
......
......@@ -7,6 +7,8 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -18,10 +20,19 @@ import uk.ac.starlink.util.TestCase;
public class ArrayDataTest extends TestCase {
private final int cblock = 8 + 4 + 4 + 2 + 1;
private final String txt_ = "A" + "b" + "\u0393";
private final int nc_;
private final int cblock_;
public ArrayDataTest( String name ) {
super( name );
try {
nc_ = txt_.getBytes( "UTF-8" ).length + 2; // includes 2-byte length
}
catch ( UnsupportedEncodingException e ) {
throw new AssertionError();
}
cblock_ = 8 + 4 + 4 + 2 + 2 + 1 + nc_;
Logger.getLogger( "uk.ac.starlink.fits" ).setLevel( Level.WARNING );
}
......@@ -29,14 +40,20 @@ public class ArrayDataTest extends TestCase {
int count = 999;
File file = File.createTempFile( "iotest", ".fits" );
file.deleteOnExit();
DataOutputStream out =
ByteBuffer mbuf = ByteBuffer.allocate( cblock_ * count );
DataOutputStream fout =
new DataOutputStream(
new BufferedOutputStream( new FileOutputStream( file ) ) );
AbstractArrayDataIO mout = new MappedFile( mbuf );
for ( int i = 0; i < count; i++ ) {
writeBlock( i, out );
writeBlock( i, fout );
writeBlock( i, mout );
}
out.close();
assertEquals( cblock * count, file.length() );
fout.close();
assertEquals( 0, mout.remaining() );
mout.close();
mbuf.rewind();
assertEquals( cblock_ * count, file.length() );
ArrayDataInput[] ins = new ArrayDataInput[] {
new BufferedDataInputStream( new FileInputStream( file ) ),
......@@ -45,6 +62,7 @@ public class ArrayDataTest extends TestCase {
new BufferedFile( file.toString(), "r", 29 ),
new MappedFile( file.toString() ),
new MultiMappedFile( file, FileChannel.MapMode.READ_ONLY, 1023 ),
new MappedFile( mbuf ),
};
for ( int ii = 0; ii < ins.length; ii++ ) {
ArrayDataInput in = ins[ ii ];
......@@ -54,10 +72,10 @@ public class ArrayDataTest extends TestCase {
if ( in instanceof RandomAccess ) {
RandomAccess rin = (RandomAccess) in;
for ( int j = count - 1; j >= 0; j-- ) {
rin.seek( j * cblock );
assertEquals( j * cblock, rin.getFilePointer() );
rin.seek( j * cblock_ );
assertEquals( j * cblock_, rin.getFilePointer() );
readBlock( j, rin );
assertEquals( ( j + 1 ) * cblock, rin.getFilePointer() );
assertEquals( ( j + 1 ) * cblock_, rin.getFilePointer() );
}
}
in.close();
......@@ -68,9 +86,11 @@ public class ArrayDataTest extends TestCase {
private void writeBlock( int i, DataOutput out ) throws IOException {
out.writeByte( (byte) i );
out.writeShort( (short) i );
out.writeShort( (short) i );
out.writeInt( i );
out.writeFloat( (float) i );
out.writeDouble( (float) i );
out.writeUTF( txt_ );
}
private void readBlock( int i, ArrayDataInput in ) throws IOException {
......@@ -78,32 +98,39 @@ public class ArrayDataTest extends TestCase {
if ( phase == 0 ) {
assertEquals( (byte) i, in.readByte() );
assertEquals( (short) i, in.readShort() );
assertEquals( (int) i, in.readUnsignedShort() );
assertEquals( (int) i, in.readInt() );
assertEquals( (float) i, in.readFloat() );
assertEquals( (double) i, in.readDouble() );
assertEquals( txt_, in.readUTF() );
}
else if ( phase == 1 ) {
for ( int skip = cblock; skip > 0; skip -= in.skip( skip ) );
for ( int skip = cblock_; skip > 0; skip -= in.skip( skip ) );
}
else if ( phase == 2 ) {
byte[] bbuf = new byte[ 1 ];
short[] sbuf = new short[ 1 ];
int[] usbuf = new int[ 1 ];
int[] ibuf = new int[ 2 ];
float[] fbuf = new float[ 4 ];
double[] dbuf = new double[ 1 ];
byte[] cbuf = new byte[ nc_ ];
in.read( bbuf );
in.read( sbuf, 0, 1 );
usbuf[ 0 ] = in.readUnsignedShort();
in.read( ibuf, 1, 1 );
in.read( fbuf, 2, 1 );
in.read( dbuf );
in.read( cbuf );
assertEquals( (byte) i, bbuf[ 0 ] );
assertEquals( (short) i, sbuf[ 0 ] );
assertEquals( (int) i, usbuf[ 0 ] );
assertEquals( (int) i, ibuf[ 1 ] );
assertEquals( (float) i, fbuf[ 2 ] );
assertEquals( (double) i, dbuf[ 0 ] );
}
else if ( phase == 3 ) {
byte[] buf = new byte[ cblock ];
byte[] buf = new byte[ cblock_ ];
in.read( buf );
assertEquals( (byte) i, buf[ 0 ] );
assertEquals( (short) i,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment