Skip to content

Commit eeb7eb2

Browse files
committed
add tests
1 parent 45ff4a5 commit eeb7eb2

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

net.lecousin.core/src/main/java/net/lecousin/framework/io/PositionKnownWrapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ protected AsyncWork<Integer, IOException> readFullyAsync(ByteBuffer buffer, Runn
270270
}
271271

272272
protected long skipSync(long n) throws IOException {
273+
if (n <= 0) return 0;
273274
long skipped = ((IO.Readable)io).skipSync(n);
274275
position.add(skipped);
275276
return skipped;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package net.lecousin.framework.core.tests.io.util;
2+
3+
import java.io.File;
4+
import java.nio.ByteBuffer;
5+
import java.util.Collection;
6+
7+
import org.junit.runner.RunWith;
8+
import org.junit.runners.Parameterized;
9+
import org.junit.runners.Parameterized.Parameters;
10+
11+
import net.lecousin.framework.core.test.io.TestIO;
12+
import net.lecousin.framework.core.test.io.TestReadableSeekable;
13+
import net.lecousin.framework.io.FileIO;
14+
import net.lecousin.framework.io.buffering.ByteArrayIO;
15+
import net.lecousin.framework.io.util.ReadableSeekableToDeterminedSize;
16+
17+
@RunWith(Parameterized.class)
18+
public class TestReadableSeekableToDeterminedSize extends TestReadableSeekable {
19+
20+
@Parameters(name = "nbBuf = {2}")
21+
public static Collection<Object[]> parameters() {
22+
return TestIO.UsingGeneratedTestFiles.generateTestCases(false);
23+
}
24+
25+
public TestReadableSeekableToDeterminedSize(File testFile, byte[] testBuf, int nbBuf) {
26+
super(testFile, testBuf, nbBuf);
27+
}
28+
29+
@Override
30+
protected ReadableSeekableToDeterminedSize createReadableSeekableFromFile(FileIO.ReadOnly file, long fileSize) throws Exception {
31+
byte[] b = new byte[(int)fileSize];
32+
if (file.readFullySync(ByteBuffer.wrap(b)) != fileSize)
33+
throw new Exception("Error loading file into memory");
34+
file.close();
35+
ByteArrayIO io = new ByteArrayIO(b, b.length, "Test");
36+
ReadableSeekableToDeterminedSize io2 = new ReadableSeekableToDeterminedSize(io);
37+
return io2;
38+
}
39+
40+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package net.lecousin.framework.core.tests.io.util;
2+
3+
import java.io.File;
4+
import java.nio.ByteBuffer;
5+
import java.util.Collection;
6+
7+
import org.junit.runner.RunWith;
8+
import org.junit.runners.Parameterized;
9+
import org.junit.runners.Parameterized.Parameters;
10+
11+
import net.lecousin.framework.core.test.io.TestIO;
12+
import net.lecousin.framework.core.test.io.TestReadable;
13+
import net.lecousin.framework.io.FileIO;
14+
import net.lecousin.framework.io.buffering.ByteArrayIO;
15+
import net.lecousin.framework.io.util.ReadableWithProgress;
16+
import net.lecousin.framework.progress.WorkProgress;
17+
import net.lecousin.framework.progress.WorkProgressImpl;
18+
19+
@RunWith(Parameterized.class)
20+
public class TestReadableWithProgress extends TestReadable {
21+
22+
@Parameters(name = "nbBuf = {2}")
23+
public static Collection<Object[]> parameters() {
24+
return TestIO.UsingGeneratedTestFiles.generateTestCases(false);
25+
}
26+
27+
public TestReadableWithProgress(File testFile, byte[] testBuf, int nbBuf) {
28+
super(testFile, testBuf, nbBuf);
29+
}
30+
31+
@Override
32+
protected ReadableWithProgress createReadableFromFile(FileIO.ReadOnly file, long fileSize) throws Exception {
33+
byte[] b = new byte[(int)fileSize];
34+
if (file.readFullySync(ByteBuffer.wrap(b)) != fileSize)
35+
throw new Exception("Error loading file into memory");
36+
file.close();
37+
ByteArrayIO io = new ByteArrayIO(b, b.length, "Test");
38+
WorkProgress progress = new WorkProgressImpl(10000, "Test");
39+
ReadableWithProgress io2 = new ReadableWithProgress(io, fileSize, progress, 10000);
40+
return io2;
41+
}
42+
43+
}

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/util/TestUnprotectedStringBuffer.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@ public UnprotectedStringBuffer provide(UnprotectedStringBuffer value) {
5353
s = new UnprotectedStringBuffer();
5454
s.addFirst(new UnprotectedString("abc"));
5555
Assert.assertEquals("abc", s.asString());
56+
57+
s = new UnprotectedStringBuffer();
58+
Assert.assertEquals(0, s.getNbUsableUnprotectedStrings());
59+
Assert.assertEquals(0, s.charAt(10));
60+
Assert.assertEquals(0, s.fillUsAsciiBytes(new byte[10], 0));
61+
Assert.assertTrue(s == s.substring(1));
62+
Assert.assertTrue(s == s.substring(1, 2));
63+
Assert.assertTrue(s == s.removeEndChars(10));
64+
try {
65+
s.setCharAt(0, ' ');
66+
throw new AssertionError("must throw IllegalArgumentException");
67+
} catch (IllegalArgumentException e) {}
68+
69+
s.append(new UnprotectedString("hello"));
70+
s.append(new UnprotectedString(" "));
71+
s.append(new UnprotectedString("world"));
72+
s.append(new UnprotectedString("!"));
73+
s.removeStartChars(6);
74+
Assert.assertEquals('w', s.charAt(0));
75+
Assert.assertEquals(0, s.charAt(10));
5676
}
5777

5878
}

0 commit comments

Comments
 (0)