forked from I2P_Developers/i2p.i2p
Kad unit tests:
- Move KBucketSetTest to new directory - Fix testSelf() as new implementation will never include myself - Delete KBucketImplTest, not applicable/useful now Next todo: port KBSTest from i2p.zzz.kademlia branch
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
package net.i2p.router.networkdb.kademlia;
|
package net.i2p.kademlia;
|
||||||
/*
|
/*
|
||||||
* free (adj.): unencumbered; not under the control of others
|
* free (adj.): unencumbered; not under the control of others
|
||||||
* Written by jrandom in 2003 and released into the public domain
|
* Written by jrandom in 2003 and released into the public domain
|
||||||
@ -20,11 +20,13 @@ import net.i2p.data.Hash;
|
|||||||
|
|
||||||
public class KBucketSetTest extends TestCase{
|
public class KBucketSetTest extends TestCase{
|
||||||
private I2PAppContext context;
|
private I2PAppContext context;
|
||||||
private KBucketSet set;
|
private KBucketSet<Hash> set;
|
||||||
|
private static final int K = 8;
|
||||||
|
private static final int B = 1;
|
||||||
|
|
||||||
public void setUp(){
|
public void setUp(){
|
||||||
context = I2PAppContext.getGlobalContext();
|
context = I2PAppContext.getGlobalContext();
|
||||||
set = new KBucketSet(context, Hash.FAKE_HASH);
|
set = new KBucketSet<Hash>(context, Hash.FAKE_HASH, K, B);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testRandom(){
|
public void testRandom(){
|
||||||
@ -36,6 +38,7 @@ public class KBucketSetTest extends TestCase{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testSelf() {
|
public void testSelf() {
|
||||||
assertTrue(set.add(Hash.FAKE_HASH));
|
// new implementation will never include myself
|
||||||
|
assertFalse(set.add(Hash.FAKE_HASH));
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,77 +0,0 @@
|
|||||||
package net.i2p.router.networkdb.kademlia;
|
|
||||||
/*
|
|
||||||
* free (adj.): unencumbered; not under the control of others
|
|
||||||
* Written by jrandom in 2003 and released into the public domain
|
|
||||||
* with no warranty of any kind, either expressed or implied.
|
|
||||||
* It probably won't make your computer catch on fire, or eat
|
|
||||||
* your children, but it might. Use at your own risk.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
|
||||||
import net.i2p.I2PAppContext;
|
|
||||||
import net.i2p.data.Hash;
|
|
||||||
import net.i2p.util.RandomSource;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test KBucketImpl
|
|
||||||
*
|
|
||||||
* @author comwiz
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class KBucketImplTest extends TestCase{
|
|
||||||
private I2PAppContext context;
|
|
||||||
|
|
||||||
public void setUp(){
|
|
||||||
context = I2PAppContext.getGlobalContext();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testLimits() {
|
|
||||||
int low = 0;
|
|
||||||
int high = 4;
|
|
||||||
|
|
||||||
KBucketImpl bucket = new KBucketImpl(I2PAppContext.getGlobalContext(), Hash.FAKE_HASH);
|
|
||||||
bucket.setRange(low, high);
|
|
||||||
Hash lowerBoundKey = bucket.getRangeBeginKey();
|
|
||||||
Hash upperBoundKey = bucket.getRangeEndKey();
|
|
||||||
assertTrue(bucket.shouldContain(lowerBoundKey));//
|
|
||||||
assertTrue(bucket.shouldContain(upperBoundKey));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testRand() {
|
|
||||||
int low = 1;
|
|
||||||
int high = 2000;
|
|
||||||
|
|
||||||
LocalHash local = new LocalHash(Hash.FAKE_HASH);
|
|
||||||
local.prepareCache();
|
|
||||||
KBucketImpl bucket = new KBucketImpl(I2PAppContext.getGlobalContext(), local);
|
|
||||||
bucket.setRange(low, high);
|
|
||||||
Hash lowerBoundKey = bucket.getRangeBeginKey();
|
|
||||||
Hash upperBoundKey = bucket.getRangeEndKey();
|
|
||||||
|
|
||||||
for (int i = 0; i < 1000; i++) {
|
|
||||||
Hash rnd = bucket.generateRandomKey();
|
|
||||||
assertTrue(bucket.shouldContain(rnd));//
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testRand2() {
|
|
||||||
int low = 1;
|
|
||||||
int high = 2000;
|
|
||||||
|
|
||||||
byte hash[] = new byte[Hash.HASH_LENGTH];
|
|
||||||
RandomSource.getInstance().nextBytes(hash);
|
|
||||||
LocalHash local = new LocalHash(hash);
|
|
||||||
local.prepareCache();
|
|
||||||
KBucketImpl bucket = new KBucketImpl(I2PAppContext.getGlobalContext(), local);
|
|
||||||
bucket.setRange(low, high);
|
|
||||||
Hash lowerBoundKey = bucket.getRangeBeginKey();
|
|
||||||
Hash upperBoundKey = bucket.getRangeEndKey();
|
|
||||||
|
|
||||||
for (int i = 0; i < 1000; i++) {
|
|
||||||
Hash rnd = bucket.generateRandomKey();
|
|
||||||
assertTrue(bucket.shouldContain(rnd));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user