/* * (c) Copyright 2008, 2009 Hewlett-Packard Development Company, LP * All rights reserved. * [See end of file] */ package structure.exthash; import static org.openjena.atlas.lib.ListUtils.asList; import static org.openjena.atlas.lib.ListUtils.unique; import static org.openjena.atlas.lib.RandomLib.random; import static org.openjena.atlas.test.Gen.permute; import static org.openjena.atlas.test.Gen.rand; import static org.openjena.atlas.test.Gen.strings; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.util.Iterator; import java.util.List; import org.openjena.atlas.test.ExecGenerator; import org.openjena.atlas.test.RepeatExecution; public class ExtHashMemTestBase { public static int BucketSize = 2 ; public static void randTests(int maxValue, int maxNumKeys, int iterations, boolean showProgess) { ExtHashTest test = new ExtHashTest(maxValue, maxNumKeys) ; RepeatExecution.repeatExecutions(test, iterations, showProgess) ; } static class ExtHashTest implements ExecGenerator { int maxNumKeys ; int maxValue ; ExtHashTest(int maxValue, int maxNumKeys) { if ( maxValue <= maxNumKeys ) throw new IllegalArgumentException("ExtHashTest: Max value less than number of keys") ; this.maxValue = maxValue ; this.maxNumKeys = maxNumKeys ; } @Override public void executeOneTest() { int numKeys = random.nextInt(maxNumKeys)+1 ; randTest(maxValue, numKeys) ; } } public static void randTest(int maxValue, int numKeys) { // if ( numKeys >= 3000 ) // System.err.printf("Warning: too many keys\n") ; int[] r1 = rand(numKeys, 0, maxValue) ; int[] r2 = permute(r1, 4*numKeys) ; runTest(r1, r2) ; } public static void runTest(int[] r1, int[] r2) { try { ExtHashMem table = create(r1) ; check(table, r1) ; delete(table, r2) ; check(table) ; } catch (RuntimeException ex) { System.err.println() ; System.err.printf("int[] r1 = {%s} ;\n", strings(r1)) ; System.err.printf("int[] r2 = {%s}; \n", strings(r2)) ; throw ex ; } } public static ExtHashMem create(int...recs) { ExtHashMem table = new ExtHashMem(BucketSize) ; for ( int i : recs ) { table.put(i, "X"+i) ; if ( false ) table.dump() ; } return table ; } public static ExtHashMem delete(ExtHashMem table, int...recs) { for ( int i : recs ) table.remove(i) ; return table ; } public static void check(ExtHashMem table, int...recs) { table.check(); for ( int i : recs ) assertNotNull(table.get(i)) ; List y = unique(asList(recs)) ; assertEquals(y.size(), table.size()); } public static void check(Iterator iter, int...recs) { for ( int i : recs ) { assertTrue("Iterator shorter than test answers", iter.hasNext()) ; int j = iter.next() ; assertEquals(i,j) ; } assertFalse("Iterator longer than test answers", iter.hasNext()) ; } public static void size(ExtHashMem table, long size) { // long x = avl.size() ; // assertEquals(size, x) ; // if ( size == 0 ) // assertTrue(avl.isEmpty()) ; // else // assertFalse(avl.isEmpty()) ; } } /* * (c) Copyright 2008, 2009 Hewlett-Packard Development Company, LP * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */