Search in sources :

Example 1 with TrieImpl

use of org.aion.mcf.trie.TrieImpl in project aion by aionnetwork.

the class TrieTest method testInsertUpdateDeleteGet.

/**
 * Tests correct retrieval using {@link TrieImpl#get(String)} after an
 * initial insert with {@link TrieImpl#update(String, String)} and after a
 * sequential update using {@link TrieImpl#update(String, String)}. Uses
 * diverse combinations of keys and values from
 * {@link #keyValue1Value2Parameters()}.
 */
@Test
@Parameters(method = "keyValue1Value2Parameters")
public void testInsertUpdateDeleteGet(String key, String value1, String value2) {
    // create a new trie object without database
    TrieImpl trie = new TrieImpl(null);
    // -------------------------------------------------------------------------------------------------------------
    // insert (key,value1) pair into the trie
    trie.update(key, value1);
    // ensure the correct value is retrieved
    assertThat(value1).isEqualTo(new String(trie.get(key)));
    // -------------------------------------------------------------------------------------------------------------
    // update to (key,value2)
    trie.update(key, value2);
    // ensure correct value retrieval after update
    assertThat(value2).isEqualTo(new String(trie.get(key)));
    // -------------------------------------------------------------------------------------------------------------
    // delete (key,value2)
    trie.update(key, "");
    // ensure correct value retrieval after update
    assertThat(value1).isNotEqualTo(new String(trie.get(key)));
    assertThat(value2).isNotEqualTo(new String(trie.get(key)));
}
Also used : TrieImpl(org.aion.mcf.trie.TrieImpl) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 2 with TrieImpl

use of org.aion.mcf.trie.TrieImpl in project aion by aionnetwork.

the class TrieTest method TestTrieReset.

@Test
public void TestTrieReset() {
    TrieImpl trie = new TrieImpl(new MockDB("TestTrieReset"));
    trie.update(cat, LONG_STRING);
    assertNotEquals("Expected cached nodes", 0, trie.getCache().getNodes().size());
    trie.getCache().undo();
    assertEquals("Expected no nodes after undo", 0, trie.getCache().getNodes().size());
}
Also used : TrieImpl(org.aion.mcf.trie.TrieImpl) MockDB(org.aion.db.impl.mockdb.MockDB) Test(org.junit.Test)

Example 3 with TrieImpl

use of org.aion.mcf.trie.TrieImpl in project aion by aionnetwork.

the class TrieTest method testDeleteAll.

/**
 * @param pairs
 * @implNote By design the keys are distinct due to the use of HashMap.
 */
@Test
@Parameters(method = "keyValuePairsParameters")
public void testDeleteAll(HashMap<String, String> pairs) {
    boolean print = false;
    if (print) {
        System.out.println("Number of pairs = " + pairs.size());
    }
    TrieImpl trie = new TrieImpl(new MockDB("TestDeleteAll"));
    // empty at start
    assertThat(Hex.toHexString(trie.getRootHash())).isEqualTo(ROOT_HASH_EMPTY);
    String key, value;
    for (Map.Entry<String, String> entry : pairs.entrySet()) {
        key = entry.getKey();
        value = entry.getValue();
        if (print) {
            System.out.println("(" + key + "," + value + ")");
        }
        // insert (key,value)
        trie.update(key, value);
    }
    // not empty after inserts
    assertThat(Hex.toHexString(trie.getRootHash())).isNotEqualTo(ROOT_HASH_EMPTY);
    // ensure that everything is still there
    for (String deleteKey : pairs.keySet()) {
        trie.delete(deleteKey);
    }
    // empty at end
    assertThat(Hex.toHexString(trie.getRootHash())).isEqualTo(ROOT_HASH_EMPTY);
}
Also used : TrieImpl(org.aion.mcf.trie.TrieImpl) MockDB(org.aion.db.impl.mockdb.MockDB) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 4 with TrieImpl

use of org.aion.mcf.trie.TrieImpl in project aion by aionnetwork.

the class TrieTest method testMasiveUpdate.

@Test
public void testMasiveUpdate() {
    boolean massiveUpdateTestEnabled = false;
    if (massiveUpdateTestEnabled) {
        List<String> randomWords = Arrays.asList(randomValues);
        HashMap<String, String> testerMap = new HashMap<>();
        TrieImpl trie = new TrieImpl(null);
        Random generator = new Random();
        // Random insertion
        for (int i = 0; i < 100000; ++i) {
            int randomIndex1 = generator.nextInt(randomWords.size());
            int randomIndex2 = generator.nextInt(randomWords.size());
            String word1 = randomWords.get(randomIndex1).trim();
            String word2 = randomWords.get(randomIndex2).trim();
            trie.update(word1, word2);
            testerMap.put(word1, word2);
        }
        int half = testerMap.size() / 2;
        for (int r = 0; r < half; ++r) {
            int randomIndex = generator.nextInt(randomWords.size());
            String word1 = randomWords.get(randomIndex).trim();
            testerMap.remove(word1);
            trie.delete(word1);
        }
        // Assert the result now
        Iterator<String> keys = testerMap.keySet().iterator();
        while (keys.hasNext()) {
            String mapWord1 = keys.next();
            String mapWord2 = testerMap.get(mapWord1);
            String treeWord2 = new String(trie.get(mapWord1));
            Assert.assertEquals(mapWord2, treeWord2);
        }
    }
}
Also used : TrieImpl(org.aion.mcf.trie.TrieImpl) Test(org.junit.Test)

Example 5 with TrieImpl

use of org.aion.mcf.trie.TrieImpl in project aion by aionnetwork.

the class TrieTest method testMassiveDelete.

@Test
public void testMassiveDelete() {
    TrieImpl trie = new TrieImpl(null);
    byte[] rootHash1 = null;
    for (int i = 0; i < 11000; i++) {
        trie.update(HashUtil.h256(intToBytes(i)), HashUtil.h256(intToBytes(i + 1000000)));
        if (i == 10000) {
            rootHash1 = trie.getRootHash();
        }
    }
    for (int i = 10001; i < 11000; i++) {
        trie.delete(HashUtil.h256(intToBytes(i)));
    }
    byte[] rootHash2 = trie.getRootHash();
    assertArrayEquals(rootHash1, rootHash2);
}
Also used : TrieImpl(org.aion.mcf.trie.TrieImpl) Test(org.junit.Test)

Aggregations

TrieImpl (org.aion.mcf.trie.TrieImpl)32 Test (org.junit.Test)31 Parameters (junitparams.Parameters)5 MockDB (org.aion.db.impl.mockdb.MockDB)5 ArrayList (java.util.ArrayList)3 IByteArrayKeyValueDatabase (org.aion.base.db.IByteArrayKeyValueDatabase)3 ImportResult (org.aion.mcf.core.ImportResult)3 AionBlock (org.aion.zero.impl.types.AionBlock)3 RLPElement (org.aion.rlp.RLPElement)1 AionTransaction (org.aion.zero.types.AionTransaction)1