Changed folder structure of test classes and renamed some packages

This commit is contained in:
Ziver Koc 2016-03-04 17:48:59 +01:00
parent ccd50ec104
commit 884c5d64c3
76 changed files with 5305 additions and 5375 deletions

View file

@ -0,0 +1,87 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Ziver Koc
*
* 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
* THE SOFTWARE.
*/
package zutil.struct;
import junit.framework.TestCase;
import java.text.DecimalFormat;
import java.util.HashSet;
import java.util.Random;
/**
* This code may be used, modified, and redistributed provided that the
* author tag below remains intact.
*
* @author Ian Clarke <ian@uprizer.com>
*/
public class BloomFilterTest extends TestCase {
public void testBloomFilter() {
DecimalFormat df = new DecimalFormat("0.00000");
Random r = new Random(124445l);
int bfSize = 400000;
System.out.println("Testing " + bfSize + " bit SimpleBloomFilter");
for (int i = 5; i < 10; i++) {
int addCount = 10000 * (i + 1);
BloomFilter<Integer> bf = new BloomFilter<Integer>(bfSize, addCount);
HashSet<Integer> added = new HashSet<Integer>();
for (int x = 0; x < addCount; x++) {
int num = r.nextInt();
added.add(num);
}
bf.addAll(added);
assertTrue("Assert that there are no false negatives", bf
.containsAll(added));
int falsePositives = 0;
for (int x = 0; x < addCount; x++) {
int num = r.nextInt();
// Ensure that this random number hasn't been added already
if (added.contains(num)) {
continue;
}
// If necessary, record a false positive
if (bf.contains(num)) {
falsePositives++;
}
}
double expectedFP = bf.falsePosetiveProbability();
double actualFP = (double) falsePositives / (double) addCount;
System.out.println("Got " + falsePositives
+ " false positives out of " + addCount + " added items, rate = "
+ df.format(actualFP) + ", expected = "
+ df.format(expectedFP));
double ratio = actualFP/expectedFP;
assertTrue(
"Assert that the actual false positive rate doesn't deviate by more than 10% from what was predicted, ratio: "+ratio,
ratio < 1.1);
}
}
}

View file

@ -0,0 +1,119 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Ziver Koc
*
* 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
* THE SOFTWARE.
*/
package zutil.struct;
import org.junit.Test;
import java.util.Iterator;
import java.util.NoSuchElementException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/**
* Created by Ziver on 2015-09-22.
*/
public class CircularBufferTest {
@Test
public void addToEmpty() {
CircularBuffer<Integer> buff = new CircularBuffer<Integer>(0);
try {
buff.add(10);
fail("IndexOutOfBoundsException was not thrown");
} catch (IndexOutOfBoundsException e) {}
}
@Test
public void addOneElement() {
CircularBuffer<Integer> buff = new CircularBuffer<Integer>(1);
assertEquals(0, buff.size());
buff.add(10);
assertEquals(1, buff.size());
assertEquals((Integer) 10, buff.get(0));
}
@Test
public void addThreeElements() {
CircularBuffer<Integer> buff = new CircularBuffer<Integer>(10);
buff.add(10);
buff.add(11);
buff.add(12);
assertEquals(3, buff.size());
assertEquals((Integer) 12, buff.get(0));
assertEquals((Integer) 11, buff.get(1));
assertEquals((Integer) 10, buff.get(2));
}
@Test
public void addOutOfRange() {
CircularBuffer<Integer> buff = new CircularBuffer<Integer>(2);
buff.add(10);
buff.add(11);
buff.add(12);
assertEquals(2, buff.size());
assertEquals((Integer) 12, buff.get(0));
assertEquals((Integer) 11, buff.get(1));
try {
buff.get(2);
fail("IndexOutOfBoundsException was not thrown");
} catch (IndexOutOfBoundsException e) {}
}
@Test
public void iteratorEmpty() {
CircularBuffer<Integer> buff = new CircularBuffer<Integer>(10);
Iterator<Integer> it = buff.iterator();
assert (!it.hasNext());
try {
it.next();
fail("NoSuchElementException was not thrown");
} catch (NoSuchElementException e) {}
}
@Test
public void iteratorThreeElements() {
CircularBuffer<Integer> buff = new CircularBuffer<Integer>(10);
buff.add(10);
buff.add(11);
buff.add(12);
Iterator<Integer> it = buff.iterator();
assert (it.hasNext());
assertEquals((Integer) 12, it.next());
assert (it.hasNext());
assertEquals((Integer) 11, it.next());
assert (it.hasNext());
assertEquals((Integer) 10, it.next());
assert (!it.hasNext());
try {
it.next();
fail("NoSuchElementException was not thrown");
} catch (NoSuchElementException e) {}
}
}

View file

@ -0,0 +1,63 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Ziver Koc
*
* 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
* THE SOFTWARE.
*/
package zutil.struct;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
/**
* Created by Ziver on 2015-11-20.
*/
public class TimedHashSetTest {
public static final String ENTRY = "key";
@Test
public void zeroTTL() throws InterruptedException {
TimedHashSet set = new TimedHashSet(0);
set.add(ENTRY);
Thread.sleep(1);
assertFalse(set.contains(ENTRY));
}
@Test
public void tenMsTTL() throws InterruptedException {
TimedHashSet set = new TimedHashSet(10);
set.add(ENTRY);
Thread.sleep(1);
assert(set.contains(ENTRY));
Thread.sleep(10);
assertFalse(set.contains(ENTRY));
}
@Test
public void oneSecTTL() throws InterruptedException {
TimedHashSet set = new TimedHashSet(1000);
set.add(ENTRY);
Thread.sleep(1);
assert(set.contains(ENTRY));
}
}