Skip to content

Commit

Permalink
Typekpb patch 1 (replacing samtools#725) (samtools#883)
Browse files Browse the repository at this point in the history
* seek() checks for negative position argument value
* tests
  • Loading branch information
Yossi Farjoun authored Jun 10, 2017
1 parent 9f1f191 commit cc538e2
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License
*
* Copyright (c) 2016 The Broad Institute
* Copyright (c) 2015 The Broad Institute
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,13 +24,11 @@

package htsjdk.samtools.seekablestream;

import htsjdk.samtools.seekablestream.SeekableStream;

import java.io.IOException;

/**
* Created by vadim on 23/03/2015.
*/
* Created by vadim on 23/03/2015.
*/
public class ByteArraySeekableStream extends SeekableStream {
private byte[] bytes;
private long position = 0;
Expand All @@ -51,21 +49,27 @@ public long position() throws IOException {

@Override
public void seek(long position) throws IOException {
this.position = position;
if (position < 0) {
throw new IllegalArgumentException("Cannot seek to a negative position, position=" + position + ".");
} else {
this.position = position;
}
}

@Override
public int read() throws IOException {
if (position < bytes.length)
if (position < bytes.length) {
return 0xFF & bytes[((int) position++)];
else return -1;
} else {
return -1;
}
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
} else if (off < 0 || len < 0 || len + off > b.length) {
throw new IndexOutOfBoundsException();
}
if (position >= bytes.length) {
Expand All @@ -85,6 +89,7 @@ public int read(byte[] b, int off, int len) throws IOException {
@Override
public void close() throws IOException {
bytes = null;
position = -1;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* The MIT License
*
* Copyright (c) 2017 The Broad Institute
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*/

package htsjdk.samtools.seekablestream;

import htsjdk.HtsjdkTest;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.IOException;

/**
* Created by farjoun on 5/27/17.
*/
public class ByteArraySeekableStreamTest extends HtsjdkTest {
private final byte[] bytes = "ABCDE12345".getBytes();

@Test
public void testNormalBehavior() throws IOException {
ByteArraySeekableStream byteArraySeekableStream = new ByteArraySeekableStream(bytes);

Assert.assertEquals(byteArraySeekableStream.length(), 10);
for (int i = 0; i < 10; i++) {
Assert.assertFalse(byteArraySeekableStream.eof());
Assert.assertEquals(byteArraySeekableStream.position(), i);
Assert.assertEquals(byteArraySeekableStream.read(), bytes[i]);
}

Assert.assertTrue(byteArraySeekableStream.eof());
Assert.assertEquals(byteArraySeekableStream.position(), 10);
Assert.assertEquals(byteArraySeekableStream.read(), -1);

final long i = 0;
byteArraySeekableStream.seek(i);

Assert.assertEquals(byteArraySeekableStream.position(), i);
Assert.assertEquals(byteArraySeekableStream.read(), bytes[(int) i]);

byte[] copy = new byte[10];

Assert.assertEquals(byteArraySeekableStream.read(copy), 9);
Assert.assertEquals(byteArraySeekableStream.position(), 10);

byteArraySeekableStream.seek(0L);

Assert.assertEquals(byteArraySeekableStream.read(copy), 10);
Assert.assertEquals(byteArraySeekableStream.position(), 10);

Assert.assertEquals(copy, bytes);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testCantSeekNegative() throws IOException {

ByteArraySeekableStream byteArraySeekableStream = new ByteArraySeekableStream(bytes);

byteArraySeekableStream.seek(-1L);

// if allowed to seek, this will throw OutOfBounds
final int f = byteArraySeekableStream.read();
}

@Test
public void testCantReadPostEof() throws IOException {

ByteArraySeekableStream byteArraySeekableStream = new ByteArraySeekableStream(bytes);
byte[] copy = new byte[10];

byteArraySeekableStream.seek(10);
Assert.assertEquals(byteArraySeekableStream.read(copy), -1);
Assert.assertEquals(byteArraySeekableStream.read(), -1);
}

@DataProvider(name = "abnormalReadRequests")
public Object[][] abnormalReadRequestsProvider() {
return new Object[][]{
{new byte[10], -1, 0},
{new byte[10], -1, -1},
{new byte[10], 0, -1},
{new byte[10], 0, -1},
{new byte[10], 0, 11},
{new byte[10], 6, 6},
{new byte[10], 11, 0},
};
}

@Test(dataProvider = "abnormalReadRequests", expectedExceptions = IndexOutOfBoundsException.class)
public void testAbnormalReadRequest(final byte[] b, final int off, final int length) throws IOException {

ByteArraySeekableStream byteArraySeekableStream = new ByteArraySeekableStream(bytes);
int i = byteArraySeekableStream.read(b, off, length);

Assert.assertEquals(i, -2); ///impossible
}
}
4 changes: 2 additions & 2 deletions src/test/java/htsjdk/samtools/sra/AbstractSRATest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public final void checkIfCanResolve() {
canResolveNetworkAccession = SRAAccession.isValid(checkAccession);
}

@BeforeMethod
@BeforeMethod(groups = "sra")
public final void assertSRAIsSupported() {
if(SRAAccession.checkIfInitialized() != null){
throw new SkipException("Skipping SRA Test because SRA native code is unavailable.");
}
}

@BeforeMethod
@BeforeMethod(groups = "sra")
public final void skipIfCantResolve(Method method, Object[] params) {
String accession = null;

Expand Down

0 comments on commit cc538e2

Please sign in to comment.