Search in sources :

Example 21 with Random

use of jetbrains.exodus.util.Random in project xodus by JetBrains.

the class Persistent23TreeMapTest method testSize.

@Test
public void testSize() {
    Random random = new Random(249578);
    int[] p = genPermutation(random, 10000);
    final Persistent23TreeMap<Integer, String> source = new Persistent23TreeMap<>();
    Persistent23TreeMap.MutableMap<Integer, String> tree = null;
    for (int i = 0; i < p.length; i++) {
        if ((i & 15) == 0) {
            if (i > 0) {
                tree.endWrite();
                Assert.assertEquals(i, source.beginRead().size());
            }
            tree = source.beginWrite();
        }
        Assert.assertEquals(i, tree.size());
        tree.put(p[i], String.valueOf(p[i]));
        Assert.assertEquals(i + 1, tree.size());
        for (int j = 0; j < 3; j++) {
            tree.put(p[random.nextInt(i + 1)], p[random.nextInt(i + 1)] + " " + i + " " + j);
            Assert.assertEquals(i + 1, tree.size());
        }
    }
    tree.endWrite();
    Assert.assertEquals(p.length, source.beginRead().size());
    p = genPermutation(random, p.length);
    tree = null;
    for (int i = 0; i < p.length; i++) {
        if ((i & 15) == 0) {
            if (i > 0) {
                tree.endWrite();
                Assert.assertEquals(p.length - i, source.beginRead().size());
            }
            tree = source.beginWrite();
        }
        Assert.assertEquals(p.length - i, tree.size());
        tree.remove(p[i]);
        Assert.assertEquals(p.length - i - 1, tree.size());
        for (int j = 0; j < 3; j++) {
            tree.remove(p[random.nextInt(i + 1)]);
            Assert.assertEquals(p.length - i - 1, tree.size());
        }
    }
    tree.endWrite();
    Assert.assertEquals(0, source.beginRead().size());
}
Also used : Random(jetbrains.exodus.util.Random) Test(org.junit.Test)

Example 22 with Random

use of jetbrains.exodus.util.Random in project xodus by JetBrains.

the class Persistent23TreeMapTest method reverseIterationTest.

@SuppressWarnings({ "OverlyLongMethod" })
@Test
public void reverseIterationTest() {
    Random random = new Random(5743);
    Persistent23TreeMap.MutableMap<Integer, String> tree = new Persistent23TreeMap<Integer, String>().beginWrite();
    int[] p = genPermutation(random);
    TreeSet<Integer> added = new TreeSet<>();
    for (int i = 0; i < ENTRIES_TO_ADD; i++) {
        int size = tree.size();
        Assert.assertEquals(i, size);
        if ((size & 1023) == 0 || size < 100) {
            Iterator<Integer> iterator = added.descendingIterator();
            for (Iterator<Persistent23TreeMap.Entry<Integer, String>> treeItr = tree.reverseIterator(); treeItr.hasNext(); ) {
                Assert.assertTrue(iterator.hasNext());
                Persistent23TreeMap.Entry<Integer, String> key = treeItr.next();
                Integer next = iterator.next();
                Assert.assertEquals(next, key.getKey());
                Assert.assertEquals(String.valueOf(next), key.getValue());
            }
            Assert.assertFalse(iterator.hasNext());
            iterator = added.descendingIterator();
            Iterator<Persistent23TreeMap.Entry<Integer, String>> treeItr = tree.reverseIterator();
            for (int j = 0; j < size; j++) {
                Persistent23TreeMap.Entry<Integer, String> key = treeItr.next();
                Assert.assertTrue(iterator.hasNext());
                Integer next = iterator.next();
                Assert.assertEquals(next, key.getKey());
                Assert.assertEquals(String.valueOf(next), key.getValue());
            }
            Assert.assertFalse(iterator.hasNext());
            try {
                treeItr.next();
                Assert.fail();
            } catch (NoSuchElementException e) {
            }
            Assert.assertFalse(treeItr.hasNext());
        }
        tree.put(p[i], String.valueOf(p[i]));
        added.add(p[i]);
    }
}
Also used : Random(jetbrains.exodus.util.Random) Test(org.junit.Test)

Example 23 with Random

use of jetbrains.exodus.util.Random in project xodus by JetBrains.

the class Persistent23TreeMapTest method tailIterationTest.

@Test
public void tailIterationTest() {
    Random random = new Random(239786);
    Persistent23TreeMap<Integer, String> map = new Persistent23TreeMap<>();
    Persistent23TreeMap.MutableMap<Integer, String> write = map.beginWrite();
    int[] p = genPermutation(random);
    TreeSet<Integer> added = new TreeSet<>();
    for (int i = 0; i < ENTRIES_TO_ADD; i++) {
        int size = write.size();
        Assert.assertEquals(i, size);
        if ((size & 1023) == 0 || size < 100) {
            if (i > 0) {
                checkTailIteration(write, added, map.createEntry(added.first()));
                checkTailIteration(write, added, map.createEntry(added.first() - 1));
                checkTailIteration(write, added, map.createEntry(added.last()));
                checkTailIteration(write, added, map.createEntry(added.last() + 1));
            }
            checkTailIteration(write, added, map.createEntry(Integer.MAX_VALUE));
            checkTailIteration(write, added, map.createEntry(Integer.MIN_VALUE));
            for (int j = 0; j < 10; j++) {
                checkTailIteration(write, added, map.createEntry(p[i * j / 10]));
            }
        }
        write.put(p[i], String.valueOf(p[i]));
        added.add(p[i]);
    }
}
Also used : Random(jetbrains.exodus.util.Random) Test(org.junit.Test)

Example 24 with Random

use of jetbrains.exodus.util.Random in project xodus by JetBrains.

the class Persistent23TreeMapTest method tailReverseIterationTest.

@Test
public void tailReverseIterationTest() {
    Random random = new Random(239786);
    Persistent23TreeMap<Integer, String> map = new Persistent23TreeMap<>();
    Persistent23TreeMap.MutableMap<Integer, String> write = map.beginWrite();
    int[] p = genPermutation(random);
    TreeSet<Integer> added = new TreeSet<>();
    for (int i = 0; i < ENTRIES_TO_ADD; i++) {
        int size = write.size();
        Assert.assertEquals(i, size);
        if ((size & 1023) == 0 || size < 100) {
            if (i > 0) {
                checkTailReverseIteration(write, added, map.createEntry(added.first()));
                checkTailReverseIteration(write, added, map.createEntry(added.first() - 1));
                checkTailReverseIteration(write, added, map.createEntry(added.last()));
                checkTailReverseIteration(write, added, map.createEntry(added.last() + 1));
            }
            checkTailReverseIteration(write, added, map.createEntry(Integer.MAX_VALUE));
            checkTailReverseIteration(write, added, map.createEntry(Integer.MIN_VALUE));
            for (int j = 0; j < 10; j++) {
                checkTailReverseIteration(write, added, map.createEntry(p[i * j / 10]));
            }
        }
        write.put(p[i], String.valueOf(p[i]));
        added.add(p[i]);
    }
}
Also used : Random(jetbrains.exodus.util.Random) Test(org.junit.Test)

Example 25 with Random

use of jetbrains.exodus.util.Random in project xodus by JetBrains.

the class PersistentHashSetTest method iterationTest.

@Test
public void iterationTest() {
    Random random = new Random(8234890);
    PersistentHashSet.MutablePersistentHashSet<Integer> tree = new PersistentHashSet<Integer>().beginWrite();
    int[] p = genPermutation(random);
    HashSet<Integer> added = new HashSet<>();
    for (int i = 0; i < ENTRIES_TO_ADD; i++) {
        int size = tree.size();
        Assert.assertEquals(i, size);
        if ((size & 1023) == 0 || size < 100) {
            Collection<Integer> actual = new HashSet<>(size);
            for (Integer key : tree) {
                Assert.assertFalse(actual.contains(key));
                actual.add(key);
            }
            Assert.assertEquals(size, actual.size());
            for (Integer key : added) {
                Assert.assertTrue(actual.contains(key));
            }
            Iterator<Integer> treeItr = tree.iterator();
            actual.clear();
            for (int j = 0; j < size; j++) {
                Integer key = treeItr.next();
                Assert.assertFalse(actual.contains(key));
                actual.add(key);
            }
            Assert.assertEquals(size, actual.size());
            for (Integer key : added) {
                Assert.assertTrue(actual.contains(key));
            }
        }
        tree.add(p[i]);
        added.add(p[i]);
    }
}
Also used : Random(jetbrains.exodus.util.Random) Test(org.junit.Test)

Aggregations

Random (jetbrains.exodus.util.Random)31 Test (org.junit.Test)28 java.util (java.util)2 ArrayList (java.util.ArrayList)2 ObjectProcedure (jetbrains.exodus.core.dataStructures.hash.ObjectProcedure)2 Assert (org.junit.Assert)2 List (java.util.List)1 TreeSet (java.util.TreeSet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ArrayByteIterable (jetbrains.exodus.ArrayByteIterable)1 ByteIterable (jetbrains.exodus.ByteIterable)1 ComparableSet (jetbrains.exodus.bindings.ComparableSet)1 Pair (jetbrains.exodus.core.dataStructures.Pair)1 LongHashMap (jetbrains.exodus.core.dataStructures.hash.LongHashMap)1 LongHashSet (jetbrains.exodus.core.dataStructures.hash.LongHashSet)1 Cursor (jetbrains.exodus.env.Cursor)1 Before (org.junit.Before)1