📄 memorystoragetest.java
字号:
if (result.numElements() != 2) { stepDone(FAILURE, "Result had " + result.numElements() + " elements, expected 2."); return; } if (!((result.isMemberId(data[3])) && (result.isMemberId(data[4])))) { stepDone(FAILURE, "Result had incorrect elements " + data[0] + ", " + data[1] + ", expected 3 and 4."); return; } stepDone(SUCCESS); stepStart("Requesting Scan from 8 to 10"); result = storage.scan(FACTORY.buildIdRange(data[8], data[10])); if (result.numElements() != 0) { stepDone(FAILURE, "Result had " + result.numElements() + " elements, expected 0."); return; } stepDone(SUCCESS); stepStart("Requesting Scan from 'Monkey' to 9"); handleBadScan.receiveException(new Exception()); } public void receiveException(Exception e) { stepException(e); } }; Continuation insertString = new Continuation() { public void receiveResult(Object o) { if (o.equals(new Boolean(true))) { sectionStart("Testing Scan"); stepStart("Inserting String as Key"); storage.store(data[11], null, "Monkey", query); } else { stepException(new RuntimeException("Removal did not complete correctly.")); } } public void receiveException(Exception e) { stepException(e); } }; testRemoval(insertString); } /** * A unit test for JUnit * * @param c DESCRIBE THE PARAMETER */ private void testRandomInserts(final Continuation c) { final int START_NUM = 10; final int END_NUM = 98; final int SKIP = 2; final int NUM_ELEMENTS = 1 + ((END_NUM - START_NUM) / SKIP); final Continuation checkRandom = new Continuation() { public void receiveResult(Object o) { stepStart("Checking object deletion"); int NUM_DELETED = ((Integer) o).intValue(); int length = storage.scan(FACTORY.buildIdRange(data[13 + START_NUM], data[13 + END_NUM + SKIP])).numElements(); int desired = NUM_ELEMENTS - NUM_DELETED; if (length == desired) { stepDone(SUCCESS); sectionEnd(); c.receiveResult(new Boolean(true)); } else { stepDone(FAILURE, "Expected " + desired + " objects after deletes, found " + length + "."); return; } } public void receiveException(Exception e) { stepException(e); } }; final Continuation removeRandom = new Continuation() { private int count = START_NUM; private int num_deleted = 0; public void receiveResult(Object o) { if (count == START_NUM) { stepStart("Removing random subset of objects"); } if (o.equals(new Boolean(false))) { stepDone(FAILURE, "Deletion of " + count + " failed."); return; } if (count == END_NUM) { stepDone(SUCCESS); checkRandom.receiveResult(new Integer(num_deleted)); return; } if (environment.getRandomSource().nextBoolean()) { num_deleted++; storage.unstore(data[13 + (count += SKIP)], this); } else { count += SKIP; receiveResult(new Boolean(true)); } } public void receiveException(Exception e) { stepException(e); } }; final Continuation checkScan = new Continuation() { private int count = START_NUM; public void receiveResult(Object o) { stepStart("Checking scans for all ranges"); for (int count = START_NUM; count < END_NUM - SKIP; count += SKIP) { IdSet result = storage.scan(FACTORY.buildIdRange(data[13 + (count += SKIP)], data[13 + END_NUM])); int i = NUM_ELEMENTS - ((count - START_NUM + SKIP) / SKIP); if (result.numElements() != i) { stepDone(FAILURE, "Expected " + i + " found " + result.numElements() + " keys in scan from " + count + " to " + END_NUM + "."); return; } } stepDone(SUCCESS); removeRandom.receiveResult(new Boolean(true)); } public void receiveException(Exception e) { stepException(e); } }; final Continuation checkExists = new Continuation() { public void receiveResult(Object o) { stepStart("Checking exists for all 50 objects"); for (int count = START_NUM; count < END_NUM; count += SKIP) { boolean b = storage.exists(data[13 + count]); if (!b) { stepDone(FAILURE, "Element " + count + " did exist (" + data[13 + count].toStringFull() + ") - should not have (START " + START_NUM + " END " + END_NUM + ")."); return; } } stepDone(SUCCESS); checkScan.receiveResult(new Boolean(true)); } public void receiveException(Exception e) { stepException(e); } }; final Continuation insert = new Continuation() { private int count = START_NUM; public void receiveResult(Object o) { if (o.equals(new Boolean(false))) { stepDone(FAILURE, "Insertion of " + count + " failed."); return; } if (count == START_NUM) { sectionStart("Stress Testing"); stepStart("Inserting 40 objects from 100 to 1000000 bytes"); } if (count > END_NUM) { stepDone(SUCCESS); checkExists.receiveResult(new Boolean(true)); return; } int num = count; count += SKIP; storage.store(data[13 + num], null, new byte[num * num], this); } public void receiveException(Exception e) { stepException(e); } }; testScan(insert); } /** * A unit test for JUnit * * @param c DESCRIBE THE PARAMETER */ private void testErrors(final Continuation c) { final Continuation validateNullValue = new Continuation() { public void receiveResult(Object o) { if (o.equals(new Boolean(true))) { stepDone(FAILURE, "Null value should return false"); return; } stepDone(SUCCESS); sectionEnd(); c.receiveResult(new Boolean(true)); } public void receiveException(Exception e) { stepException(e); } }; final Continuation insertNullValue = new Continuation() { public void receiveResult(Object o) { if (o.equals(new Boolean(true))) { stepDone(FAILURE, "Null key should return false"); return; } stepDone(SUCCESS); stepStart("Inserting null value"); storage.store(data[12], null, null, validateNullValue); } public void receiveException(Exception e) { stepException(e); } }; final Continuation insertNullKey = new Continuation() { public void receiveResult(Object o) { if (o.equals(new Boolean(false))) { stepDone(FAILURE, "Randon insert tests failed."); return; } sectionStart("Testing Error Cases"); stepStart("Inserting null key"); storage.store(null, null, "null key", insertNullValue); } public void receiveException(Exception e) { stepException(e); } }; testRandomInserts(insertNullKey); } /** * A unit test for JUnit */ public void testVariableLength() { final HashSet tmp = new HashSet(); final HashSet all = new HashSet(); testErrors( new Continuation() { public void receiveResult(Object o) { if (o.equals(new Boolean(false))) { stepDone(FAILURE, "Error tests failed"); return; } sectionStart("Testing variable-length Ids"); stepStart("Inserting a whole bunch of Ids"); new Continuation() { int num = 0; public void receiveResult(Object o) { if (o.equals(new Boolean(false))) { stepDone(FAILURE, "Insert of Id #" + num + " failed"); return; } if (num < 1000) { num++; storage.store(new VariableId(num), null, num + " length", this); } else { stepDone(SUCCESS); stepStart("Reinserting same Ids"); new Continuation() { int num = 0; public void receiveResult(Object o) { if (o.equals(new Boolean(false))) { stepDone(FAILURE, "Reinsert of Id #" + num + " failed"); return; } if (num < 1000) { num++; VariableId id = new VariableId(num); all.add(id); tmp.add(id); storage.store(new VariableId(num), null, num + " length", this); } else { stepDone(SUCCESS); stepStart("Deleting all Ids again"); new Continuation() { Id id = null; public void receiveResult(Object o) { if (o.equals(new Boolean(false))) { stepDone(FAILURE, "Delete of Id " + id + " failed"); return; } if (tmp.size() > 0) { id = (Id) tmp.iterator().next(); tmp.remove(id); storage.unstore(id, this); } else { stepDone(SUCCESS); sectionEnd(); System.out.println("All tests completed successfully - exiting."); System.exit(0); } } public void receiveException(Exception e) { stepException(e); } }.receiveResult(Boolean.TRUE); } } public void receiveException(Exception e) { stepException(e); } }.receiveResult(Boolean.TRUE); } } public void receiveException(Exception e) { stepException(e); } }.receiveResult(Boolean.TRUE); } public void receiveException(Exception e) { stepException(e); } }); } /** * DESCRIBE THE METHOD */ public void start() { testVariableLength(); try { Thread.sleep(20000); } catch (InterruptedException ie) { ; } } /** * The main program for the MemoryStorageTest class * * @param args The command line arguments * @exception IOException DESCRIBE THE EXCEPTION */ public static void main(String[] args) throws IOException { MemoryStorageTest test = new MemoryStorageTest(true, new Environment()); test.start(); } /** * DESCRIBE THE CLASS * * @version $Id: pretty.settings 2305 2005-03-11 20:22:33Z jeffh $ * @author jeffh */ public class VariableId implements Id { /** * DESCRIBE THE FIELD */ protected int num; /** * DESCRIBE THE FIELD */ public final static short TYPE = 4893; /** * DESCRIBE THE FIELD */ public final static String STRING = "0123456789ABCDEF"; /** * Constructor for VariableId. * * @param num DESCRIBE THE PARAMETER */ public VariableId(int num) { this.num = num; } /** * Gets the Between attribute of the VariableId object * * @param ccw DESCRIBE THE PARAMETER * @param cw DESCRIBE THE PARAMETER * @return The Between value */ public boolean isBetween(Id ccw, Id cw) { return false; } /** * Gets the ByteArrayLength attribute of the VariableId object * * @return The ByteArrayLength value */ public int getByteArrayLength() { return 0; } /** * Gets the Type attribute of the VariableId object * * @return The Type value */ public short getType() { return TYPE; } /** * DESCRIBE THE METHOD * * @param nid DESCRIBE THE PARAMETER * @return DESCRIBE THE RETURN VALUE */ public boolean clockwise(Id nid) { return false; } /** * Adds a feature to the ToId attribute of the VariableId object * * @param offset The feature to be added to the ToId attribute * @return DESCRIBE THE RETURN VALUE */ public Id addToId(Distance offset) { return null; } /** * DESCRIBE THE METHOD * * @param nid DESCRIBE THE PARAMETER * @return DESCRIBE THE RETURN VALUE */ public Distance distanceFromId(Id nid) { return null; } /** * DESCRIBE THE METHOD * * @param nid DESCRIBE THE PARAMETER * @return DESCRIBE THE RETURN VALUE */ public Distance longDistanceFromId(Id nid) { return null; } /** * DESCRIBE THE METHOD * * @return DESCRIBE THE RETURN VALUE */ public byte[] toByteArray() { return null; } /** * DESCRIBE THE METHOD * * @param array DESCRIBE THE PARAMETER * @param offset DESCRIBE THE PARAMETER */ public void toByteArray(byte[] array, int offset) { } /** * DESCRIBE THE METHOD * * @return DESCRIBE THE RETURN VALUE */ public String toStringFull() { if (num <= STRING.length()) { return STRING.substring(0, num); } else { return STRING + num; } } /** * DESCRIBE THE METHOD * * @param o DESCRIBE THE PARAMETER * @return DESCRIBE THE RETURN VALUE */ public int compareTo(Object o) { return 0; } /** * DESCRIBE THE METHOD * * @param buf DESCRIBE THE PARAMETER * @exception IOException DESCRIBE THE EXCEPTION */ public void serialize(OutputBuffer buf) throws IOException { buf.writeInt(num); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -