public void FillUpTableTest() { const string tableName = "FillUpHashTable"; const int tableSize = 25; const int keySize = 4; const int valueSize = 4; const int userHeaderSize = 6; PersistentHashTable hashTable = InitTable(tableName, tableSize, keySize, valueSize, userHeaderSize); try { // fill the hash table for (int i = 1; i < tableSize + 1; i++) { hashTable.Put(i.ToBytes(), i.ToBytes()); } //add one more than allowed try { hashTable.Put(new byte[] { byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue }, new byte[] { byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue }); Assert.Fail("Should throw exception"); } catch (IndexOutOfRangeException) {} } finally { hashTable.Close(); } }
private static byte[] FillTableWithCollisions(int targetHashToCollideOn, PersistentHashTable hashTable) { byte[] collisionKey = new byte[] { 0, 0, 0, 0 }; for (int run = 0; run < hashTable.GetTableSize(); run++) { collisionKey = GetNextCollisionKey(targetHashToCollideOn, hashTable.GetTableSize(), collisionKey); hashTable.Put(collisionKey, new byte[] { 0, 0, 0, (byte)run }); } //assert the table is full try { hashTable.Put(new byte[] { 255, 255, 255, 255 }, new byte[hashTable.GetValueSize()]); Assert.Fail("Should throw exception"); } catch (IndexOutOfRangeException) {} return(collisionKey); }
private static void RemoveTestAssert(PersistentHashTable hashTable, int valueSize, byte[] key) { hashTable.Put(key, new byte[valueSize]); hashTable.Remove(key); try { hashTable.Get(key); } catch (KeyNotFoundException) {} }
public void RemoveCollisionTest() { PersistentHashTable hashTable = InitTable("HashTableRemove", 20, 4, 4, 6); try { //fill the table up with collisions byte[] lastCollisionKey = FillTableWithCollisions(5, hashTable); hashTable.Remove(lastCollisionKey); hashTable.Put(lastCollisionKey, new byte[hashTable.GetValueSize()]); } finally { hashTable.Close(); } }
public void ReopenTest() { string hastTableName = "ReopenHashTableTest"; PersistentHashTable hashTable = InitTable(hastTableName, 20, 4, 4, 6); try { byte[] key = new byte[] { 4, 4, 4, 4 }; byte[] value = new byte[] { 5, 5, 5, 5 }; hashTable.Put(key, value); hashTable.Close(); hashTable = new PersistentHashTable(hastTableName); TestHelper.AssertByteArraysAreSame(value, hashTable.Get(key)); } finally { hashTable.Close(); } }
public void PutGetTest() { const int keySize = 5; const int valueSize = 10; PersistentHashTable hashTable = InitTable("PutGetHashTable", 15, keySize, valueSize, 7); try { PutGetTestAssert(hashTable, new byte[keySize] { 1, 2, 3, 4, 5 }, new byte[valueSize] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }); PutGetTestAssert(hashTable, new byte[keySize] { byte.MinValue, byte.MaxValue, 0, 0, 0 }, new byte[valueSize] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }); PutGetTestAssert(hashTable, new byte[keySize] { 128, 64, 32, 16, 8 }, new byte[valueSize] { byte.MinValue, byte.MinValue, 0, 0, 0, 0, 0, 0, 0, 0 }); try { hashTable.Put(new byte[0], new byte[valueSize]); Assert.Fail("Should throw exception"); } catch (InvalidKeySizeException) {} try { hashTable.Put(new byte[1], new byte[valueSize]); Assert.Fail("Should throw exception"); }catch (InvalidKeySizeException) { } try { hashTable.Put(new byte[keySize] { 1, 2, 3, 4, 5 }, new byte[0]); Assert.Fail("Should throw exception"); } catch (InvalidValueSizeException) { } try { hashTable.Put(new byte[keySize] { 1, 2, 3, 4, 5 }, new byte[1]); Assert.Fail("Should throw exception"); } catch (InvalidValueSizeException) { } try { hashTable.Put(new byte[keySize], new byte[valueSize]); Assert.Fail("Should throw exception"); }catch (InvalidKeyException) { } } finally { hashTable.Close(); } }
private static void PutGetTestAssert(PersistentHashTable hashTable, byte[] key, byte[] value) { hashTable.Put(key, value); byte[] actual = hashTable.Get(key); TestHelper.AssertByteArraysAreSame(value, actual); }