I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit 1d7eedd4 authored by zzz's avatar zzz
Browse files

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
parent 796a231f
No related branches found
No related tags found
No related merge requests found
package net.i2p.router.networkdb.kademlia;
package net.i2p.kademlia;
/*
* free (adj.): unencumbered; not under the control of others
* 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{
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(){
context = I2PAppContext.getGlobalContext();
set = new KBucketSet(context, Hash.FAKE_HASH);
set = new KBucketSet<Hash>(context, Hash.FAKE_HASH, K, B);
}
public void testRandom(){
......@@ -36,6 +38,7 @@ public class KBucketSetTest extends TestCase{
}
public void testSelf() {
assertTrue(set.add(Hash.FAKE_HASH));
// new implementation will never include myself
assertFalse(set.add(Hash.FAKE_HASH));
}
}
\ No newline at end of file
}
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));
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment