Search in sources :

Example 16 with Random

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

the class LoggableTests method testCompressedLongByteIterator2.

@Test
public void testCompressedLongByteIterator2() {
    final Random rnd = new Random();
    for (int i = 0; i < 10000; ++i) {
        final long l = Math.abs(rnd.nextLong());
        Assert.assertEquals(l, CompressedUnsignedLongByteIterable.getLong(CompressedUnsignedLongByteIterable.getIterable(l)));
    }
}
Also used : Random(jetbrains.exodus.util.Random) Test(org.junit.Test)

Example 17 with Random

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

the class TreeCursorDuplicatesTest method test_xd_347_like.

@Test
public void test_xd_347_like() {
    tm = createMutableTree(true, 1);
    final int count = 20000;
    long value = 0;
    final LongHashMap<LongHashSet> values = new LongHashMap<>();
    final Random rnd = new Random();
    for (int i = 0; i < count; ++i, ++value) {
        if (i > count / 2) {
            final Pair<Long, LongHashSet>[] pair = new Pair[1];
            values.forEachEntry((ObjectProcedure<Map.Entry<Long, LongHashSet>>) object -> {
                pair[0] = new Pair<>(object.getKey(), object.getValue());
                return false;
            });
            final Pair<Long, LongHashSet> p = pair[0];
            final LongHashSet oldSet = p.getSecond();
            final long oldValue = oldSet.iterator().nextLong();
            final Long oldKey = p.getFirst();
            try (ITreeCursor cursor = tm.openCursor()) {
                if (!cursor.getSearchBoth(key(oldKey), value(oldValue))) {
                    Assert.assertTrue(cursor.getSearchBoth(key(oldKey), value(oldValue)));
                }
                cursor.deleteCurrent();
            }
            Assert.assertTrue(oldSet.remove(oldValue));
            if (oldSet.isEmpty()) {
                Assert.assertEquals(oldSet, values.remove(oldKey));
            }
        }
        final long key = System.currentTimeMillis() + rnd.nextInt(count / 10);
        LongHashSet keyValues = values.get(key);
        if (keyValues == null) {
            keyValues = new LongHashSet();
            values.put(key, keyValues);
        }
        Assert.assertTrue(keyValues.add(value));
        tm.put(key(key), value(value));
    }
}
Also used : java.util(java.util) Pair(jetbrains.exodus.core.dataStructures.Pair) Random(jetbrains.exodus.util.Random) LongHashSet(jetbrains.exodus.core.dataStructures.hash.LongHashSet) Test(org.junit.Test) Cursor(jetbrains.exodus.env.Cursor) Assert(org.junit.Assert) ObjectProcedure(jetbrains.exodus.core.dataStructures.hash.ObjectProcedure) LongHashMap(jetbrains.exodus.core.dataStructures.hash.LongHashMap) Before(org.junit.Before) LongHashSet(jetbrains.exodus.core.dataStructures.hash.LongHashSet) Random(jetbrains.exodus.util.Random) LongHashMap(jetbrains.exodus.core.dataStructures.hash.LongHashMap) Pair(jetbrains.exodus.core.dataStructures.Pair) Test(org.junit.Test)

Example 18 with Random

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

the class TreeCursorNoDuplicatesTest method xd_333.

@Test
public void xd_333() {
    rnd = new Random(0);
    final ByteIterable value = value("value");
    tm = createMutableTree(false, 1);
    final TreeSet<String> keys = new TreeSet<>();
    for (int i = 0; i < 15; ++i) {
        final String key = rndString();
        tm.put(key(key), value);
        keys.add(key);
    }
    /*final long address = saveTree();
        reopen();
        final ITree t = openTree(address, false);*/
    testCursorOrder(keys);
}
Also used : Random(jetbrains.exodus.util.Random) TreeSet(java.util.TreeSet) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) Test(org.junit.Test)

Example 19 with Random

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

the class FindTests method testFindComparableSet.

public void testFindComparableSet() {
    final StoreTransaction txn = getStoreTransactionSafe();
    final Entity issue = txn.newEntity("Issue");
    final ComparableSet<Integer> randomSet = new ComparableSet<>();
    final Random rnd = new Random();
    final int setSize = 20;
    for (int i = 0; i < setSize; ++i) {
        randomSet.addItem(rnd.nextInt());
    }
    for (int i = 0; i < 1000; ++i) {
        Assert.assertEquals(setSize, randomSet.size());
        issue.setProperty("randomSet", randomSet);
        randomSet.forEach((item, index) -> Assert.assertEquals(issue, txn.find("Issue", "randomSet", item).getFirst()));
        Assert.assertTrue(randomSet.removeItem(randomSet.iterator().next()));
        while (true) {
            final int newItem = rnd.nextInt();
            if (randomSet.addItem(newItem)) {
                Assert.assertTrue(txn.find("Issue", "randomSet", newItem).isEmpty());
                break;
            }
        }
        if (i % 20 == 19) {
            txn.flush();
        }
    }
}
Also used : Random(jetbrains.exodus.util.Random) ComparableSet(jetbrains.exodus.bindings.ComparableSet)

Example 20 with Random

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

the class StressTests method test_xd_347_like.

public void test_xd_347_like() {
    final PersistentEntityStoreImpl store = getEntityStore();
    final List<Entity> issues = store.computeInTransaction(new StoreTransactionalComputable<List<Entity>>() {

        @Override
        public List<Entity> compute(@NotNull final StoreTransaction txn) {
            final List<Entity> result = new ArrayList<>();
            for (final Entity issue : txn.getAll("Issue")) {
                result.add(issue);
            }
            while (result.size() < 10000) {
                result.add(txn.newEntity("Issue"));
            }
            return result;
        }
    });
    final Random rnd = new Random();
    final int count = 300;
    for (int i = 0; i < count; ++i) {
        store.executeInTransaction(new StoreTransactionalExecutable() {

            @Override
            public void execute(@NotNull final StoreTransaction txn) {
                for (int j = 0; j < 100; ++j) {
                    issues.get(rnd.nextInt(issues.size())).setProperty("created", System.currentTimeMillis());
                    issues.get(rnd.nextInt(issues.size())).setProperty("updated", System.currentTimeMillis());
                }
            }
        });
        if (i % 10 == 0) {
            System.out.print((i * 100 / count) + "%\r");
        }
    }
}
Also used : Random(jetbrains.exodus.util.Random) List(java.util.List) ArrayList(java.util.ArrayList)

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