Skip to content

Commit

Permalink
Merge branch 'fred-test-refinement-00' of github.com:venfernand/fred …
Browse files Browse the repository at this point in the history
…into next
  • Loading branch information
ArneBab committed Sep 4, 2023
2 parents 1846202 + 9904a91 commit 5fd4a65
Show file tree
Hide file tree
Showing 24 changed files with 2,227 additions and 2,474 deletions.
262 changes: 133 additions & 129 deletions test/freenet/client/CodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,133 +15,137 @@

public class CodeTest {

public static FECMath fecMath = new FECMath(8);

public static final int KK = 192;
public static final int PACKET_SIZE = 4096;

/**
* Creates k packets of size sz of random data, encodes them, and tries to decode. Index
* contains the permutation entry.
*/
private static final void encodeDecode(FECCode encode, FECCode decode, int index[]) {
byte[] src = new byte[KK * PACKET_SIZE];
Util.rand.nextBytes(src);
Buffer[] srcBufs = new Buffer[KK];
for (int i = 0; i < srcBufs.length; i++)
srcBufs[i] = new Buffer(src, i * PACKET_SIZE, PACKET_SIZE);

byte[] repair = new byte[KK * PACKET_SIZE];
Buffer[] repairBufs = new Buffer[KK];
for (int i = 0; i < repairBufs.length; i++) {
repairBufs[i] = new Buffer(repair, i * PACKET_SIZE, PACKET_SIZE);
}

encode.encode(srcBufs, repairBufs, index);
decode.decode(repairBufs, index);

for (int i = 0; i < src.length; i++)
assertEquals(src[i], repair[i]);
}

@Test
public void testBenchmark() {
if(!TestProperty.BENCHMARK) return;

int lim = fecMath.gfSize + 1;
FECCode maybeNative = FECCodeFactory.getDefault().createFECCode(KK, lim);
FECCode pureCode = new PureCode(KK, lim);
int[] index = new int[KK];

for (int i = 0; i < KK; i++)
index[i] = lim - i - 1;

byte[] src = new byte[KK * PACKET_SIZE];
Util.rand.nextBytes(src);
Buffer[] srcBufs = new Buffer[KK];
for (int i = 0; i < srcBufs.length; i++)
srcBufs[i] = new Buffer(src, i * PACKET_SIZE, PACKET_SIZE);

byte[] repair = new byte[KK * PACKET_SIZE];
Buffer[] repairBufs = new Buffer[KK];
for (int i = 0; i < repairBufs.length; i++) {
repairBufs[i] = new Buffer(repair, i * PACKET_SIZE, PACKET_SIZE);
}

int[] indexBackup = new int[index.length];
System.arraycopy(index,0,indexBackup,0,index.length);

System.out.println("Getting ready for benchmarking encode()");
long t1 = System.currentTimeMillis();
maybeNative.encode(srcBufs, repairBufs, index);
long t2 = System.currentTimeMillis();
pureCode.encode(srcBufs, repairBufs, indexBackup);
long t3 = System.currentTimeMillis();

float dNativeEncode = t2 - t1;
float dPureEncode = t3 - t2;

Buffer[] repairBufs2 = repairBufs.clone();
System.arraycopy(repairBufs, 0, repairBufs2, 0, repairBufs.length);
System.out.println("Getting ready for benchmarking decode()");
t1 = System.currentTimeMillis();
maybeNative.decode(repairBufs, index);
t2 = System.currentTimeMillis();
pureCode.decode(repairBufs2, indexBackup);
t3 = System.currentTimeMillis();

float dNativeDecode = t2 - t1;
float dPureDecode = t3 - t2;

System.out.println(maybeNative);
System.out.println(pureCode);
System.out.println("Native code took "+dNativeEncode+"ms whereas java's code took "+dPureEncode+"ms to encode()");
System.out.println("Native code took "+dNativeDecode+"ms whereas java's code took "+dPureDecode+"ms to decode()");
}

@Test
public void testSimpleRev() {
int lim = fecMath.gfSize + 1;
FECCode code = FECCodeFactory.getDefault().createFECCode(KK, lim);
FECCode code2 = new PureCode(KK, lim);
int[] index = new int[KK];

for (int i = 0; i < KK; i++)
index[i] = lim - i - 1;

encodeDecode(code, code2, index);
encodeDecode(code2, code, index);
}

@Test
public void testSimple() {
int lim = fecMath.gfSize + 1;
FECCode code = FECCodeFactory.getDefault().createFECCode(KK, lim);
FECCode code2 = new PureCode(KK, lim);
int[] index = new int[KK];

for (int i = 0; i < KK; i++)
index[i] = KK - i;
encodeDecode(code, code2, index);
encodeDecode(code2, code, index);
}

@Test
public void testShifted() {
int lim = fecMath.gfSize + 1;
FECCode code = FECCodeFactory.getDefault().createFECCode(KK, lim);
FECCode code2 = new PureCode(KK, lim);
int[] index = new int[KK];

int max_i0 = KK / 2;
if (max_i0 + KK > lim)
max_i0 = lim - KK;

for (int s = max_i0 - 2; s <= max_i0; s++) {
for (int i = 0; i < KK; i++)
index[i] = i + s;
encodeDecode(code, code2, index);
encodeDecode(code2, code, index);
}
}
public static FECMath fecMath = new FECMath(8);

public static final int KK = 192;
public static final int PACKET_SIZE = 4096;

/**
* Creates k packets of size sz of random data, encodes them, and tries to decode. Index
* contains the permutation entry.
*/
private static void encodeDecode(FECCode encode, FECCode decode, int[] index) {
byte[] src = new byte[KK * PACKET_SIZE];
Util.rand.nextBytes(src);
Buffer[] srcBufs = createBuffers(src);

byte[] repair = new byte[KK * PACKET_SIZE];
Buffer[] repairBufs = createBuffers(repair);

encode.encode(srcBufs, repairBufs, index);
decode.decode(repairBufs, index);

assertArrayEquals(src, repair);
}

@Test
public void testBenchmark() {
if (!TestProperty.BENCHMARK) {
return;
}

int lim = fecMath.gfSize + 1;
FECCode maybeNative = FECCodeFactory.getDefault().createFECCode(KK, lim);
FECCode pureCode = new PureCode(KK, lim);
int[] index = new int[KK];

for (int i = 0; i < KK; i++) {
index[i] = lim - i - 1;
}

byte[] src = new byte[KK * PACKET_SIZE];
Util.rand.nextBytes(src);
Buffer[] srcBufs = createBuffers(src);

byte[] repair = new byte[KK * PACKET_SIZE];
Buffer[] repairBufs = createBuffers(repair);

int[] indexBackup = new int[index.length];
System.arraycopy(index, 0, indexBackup, 0, index.length);

System.out.println("Getting ready for benchmarking encode()");
long t1 = System.currentTimeMillis();
maybeNative.encode(srcBufs, repairBufs, index);
long t2 = System.currentTimeMillis();
pureCode.encode(srcBufs, repairBufs, indexBackup);
long t3 = System.currentTimeMillis();

float dNativeEncode = t2 - t1;
float dPureEncode = t3 - t2;

Buffer[] repairBufs2 = repairBufs.clone();
System.arraycopy(repairBufs, 0, repairBufs2, 0, repairBufs.length);
System.out.println("Getting ready for benchmarking decode()");
t1 = System.currentTimeMillis();
maybeNative.decode(repairBufs, index);
t2 = System.currentTimeMillis();
pureCode.decode(repairBufs2, indexBackup);
t3 = System.currentTimeMillis();

float dNativeDecode = t2 - t1;
float dPureDecode = t3 - t2;

System.out.println(maybeNative);
System.out.println(pureCode);
System.out.println("Native code took " + dNativeEncode + "ms whereas java's code took " + dPureEncode + "ms to encode()");
System.out.println("Native code took " + dNativeDecode + "ms whereas java's code took " + dPureDecode + "ms to decode()");
}

@Test
public void testSimpleRev() {
int lim = fecMath.gfSize + 1;
FECCode code = FECCodeFactory.getDefault().createFECCode(KK, lim);
FECCode code2 = new PureCode(KK, lim);
int[] index = new int[KK];

for (int i = 0; i < KK; i++) {
index[i] = lim - i - 1;
}

encodeDecode(code, code2, index);
encodeDecode(code2, code, index);
}

@Test
public void testSimple() {
int lim = fecMath.gfSize + 1;
FECCode code = FECCodeFactory.getDefault().createFECCode(KK, lim);
FECCode code2 = new PureCode(KK, lim);
int[] index = new int[KK];

for (int i = 0; i < KK; i++) {
index[i] = KK - i;
}
encodeDecode(code, code2, index);
encodeDecode(code2, code, index);
}

@Test
public void testShifted() {
int lim = fecMath.gfSize + 1;
FECCode code = FECCodeFactory.getDefault().createFECCode(KK, lim);
FECCode code2 = new PureCode(KK, lim);
int[] index = new int[KK];

int max_i0 = KK / 2;
if (max_i0 + KK > lim) {
max_i0 = lim - KK;
}

for (int s = max_i0 - 2; s <= max_i0; s++) {
for (int i = 0; i < KK; i++) {
index[i] = i + s;
}
encodeDecode(code, code2, index);
encodeDecode(code2, code, index);
}
}

private static Buffer[] createBuffers(byte[] src) {
Buffer[] srcBufs = new Buffer[KK];
for (int i = 0; i < srcBufs.length; i++) {
srcBufs[i] = new Buffer(src, i * PACKET_SIZE, PACKET_SIZE);
}
return srcBufs;
}
}
26 changes: 13 additions & 13 deletions test/freenet/client/DefaultMIMETypesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
import org.junit.Test;

public class DefaultMIMETypesTest {
@Test
public void testFullList() {
for(String mimeType : DefaultMIMETypes.getMIMETypes()) {
assertTrue("Failed: \""+mimeType+"\"", DefaultMIMETypes.isPlausibleMIMEType(mimeType));
}
}
@Test
public void testParams() {
assertTrue(DefaultMIMETypes.isPlausibleMIMEType("text/xhtml+xml; charset=ISO-8859-1; blah=blah"));
assertTrue(DefaultMIMETypes.isPlausibleMIMEType("multipart/mixed; boundary=\"---this is a silly boundary---\""));
}

@Test
public void testFullList() {
for (String mimeType : DefaultMIMETypes.getMIMETypes()) {
assertTrue("Failed: \"" + mimeType + "\"", DefaultMIMETypes.isPlausibleMIMEType(mimeType));
}
}

@Test
public void testParams() {
assertTrue(DefaultMIMETypes.isPlausibleMIMEType("text/xhtml+xml; charset=ISO-8859-1; blah=blah"));
assertTrue(DefaultMIMETypes.isPlausibleMIMEType("multipart/mixed; boundary=\"---this is a silly boundary---\""));
}

}
12 changes: 7 additions & 5 deletions test/freenet/client/FailureCodeTrackerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@

public class FailureCodeTrackerTest {

/** Test that the fixed size representation really is fixed size */
/**
* Test that the fixed size representation really is fixed size
*/
@Test
public void testSize() throws IOException {
testSize(false);
testSize(true);
}

public void testSize(boolean insert) throws IOException {
FailureCodeTracker f = new FailureCodeTracker(insert);
int fixedLength = FailureCodeTracker.getFixedLength(insert);
Expand All @@ -31,9 +33,9 @@ public void testSize(boolean insert) throws IOException {

private int getStoredLength(FailureCodeTracker f) throws IOException {
CountedOutputStream os = new CountedOutputStream(new NullOutputStream());
DataOutputStream dos = new DataOutputStream(os);
f.writeFixedLengthTo(dos);
dos.close();
try (DataOutputStream dos = new DataOutputStream(os)) {
f.writeFixedLengthTo(dos);
}
return (int) os.written();
}

Expand Down
34 changes: 20 additions & 14 deletions test/freenet/client/FetchContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,28 @@
import freenet.support.io.StorageFormatException;

public class FetchContextTest {

@Test
public void testPersistence() throws IOException, StorageFormatException {
FetchContext context =
HighLevelSimpleClientImpl.makeDefaultFetchContext(Long.MAX_VALUE, Long.MAX_VALUE,
new ArrayBucketFactory(), new SimpleEventProducer());
FetchContext context = HighLevelSimpleClientImpl.makeDefaultFetchContext(
Long.MAX_VALUE,
Long.MAX_VALUE,
new ArrayBucketFactory(),
new SimpleEventProducer()
);
ArrayBucket bucket = new ArrayBucket();
DataOutputStream dos = new DataOutputStream(bucket.getOutputStream());
context.writeTo(dos);
dos.close();
assert(bucket.size() != 0);
DataInputStream dis = new DataInputStream(bucket.getInputStream());
FetchContext ctx = new FetchContext(dis);
dis.close();
assertTrue(ctx.equals(context));
bucket.free();
try {
try (DataOutputStream dos = new DataOutputStream(bucket.getOutputStream())) {
context.writeTo(dos);
}
assertNotEquals(0, bucket.size());
FetchContext ctx;
try (DataInputStream dis = new DataInputStream(bucket.getInputStream())) {
ctx = new FetchContext(dis);
}
assertEquals(ctx, context);
} finally {
bucket.free();
}
}

}
Loading

0 comments on commit 5fd4a65

Please sign in to comment.