Esempio n. 1
0
        private Action RandomAction(bool allowCheckPoint)
        {
            float randomized = _random.nextFloat();

            if (randomized <= 0.7)
            {
                // put
                long[] data = ModificationData(_minInsertCountPerBatch, _maxInsertCountPerBatch);
                return(new InsertAction(this, data));
            }
            else if (randomized <= 0.95 || !allowCheckPoint)
            {
                // remove
                long[] data = ModificationData(_minRemoveCountPerBatch, _maxRemoveCountPerBatch);
                return(new RemoveAction(this, data));
            }
            else
            {
                return(_checkpoint);
            }
        }
Esempio n. 2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void shouldInsertAndRemoveRandomKeysAndValues()
        internal virtual void ShouldInsertAndRemoveRandomKeysAndValues()
        {
            // This test doesn't care about sorting, that's an aspect that lies outside of TreeNode, really

            // GIVEN
            _node.initializeLeaf(Cursor, STABLE_GENERATION, UNSTABLE_GENERATION);
            // add +1 to these to simplify some array logic in the test itself
            IList <KEY>   expectedKeys     = new List <KEY>();
            IList <VALUE> expectedValues   = new List <VALUE>();
            int           expectedKeyCount = 0;
            KEY           readKey          = _layout.newKey();
            VALUE         readValue        = _layout.newValue();

            // WHEN/THEN
            for (int i = 0; i < 1000; i++)
            {
                if (_random.nextFloat() < 0.7)
                {                         // 70% insert
                    KEY newKey;
                    do
                    {
                        newKey = Key(_random.nextLong());
                    } while (contains(expectedKeys, newKey, _layout));
                    VALUE newValue = Value(_random.nextLong());

                    Overflow overflow = _node.leafOverflow(Cursor, expectedKeyCount, newKey, newValue);
                    if (overflow == NO_NEED_DEFRAG)
                    {
                        _node.defragmentLeaf(Cursor);
                    }
                    if (overflow != YES)
                    {                              // there's room
                        int position = expectedKeyCount == 0 ? 0 : _random.Next(expectedKeyCount);
                        // ensure unique
                        _node.insertKeyValueAt(Cursor, newKey, newValue, position, expectedKeyCount);
                        expectedKeys.Insert(position, newKey);
                        expectedValues.Insert(position, newValue);

                        TreeNode.SetKeyCount(Cursor, ++expectedKeyCount);
                    }
                }
                else
                {                         // 30% remove
                    if (expectedKeyCount > 0)
                    {                     // there are things to remove
                        int position = _random.Next(expectedKeyCount);
                        _node.keyAt(Cursor, readKey, position, LEAF);
                        _node.valueAt(Cursor, readValue, position);
                        _node.removeKeyValueAt(Cursor, position, expectedKeyCount);
                        KEY   expectedKey   = expectedKeys.RemoveAt(position);
                        VALUE expectedValue = expectedValues.RemoveAt(position);
//JAVA TO C# CONVERTER TODO TASK: The following line has a Java format specifier which cannot be directly translated to .NET:
//ORIGINAL LINE: assertEquals(0, layout.compare(expectedKey, readKey), String.format("Key differ with expected%n    readKey=%s %nexpectedKey=%s%n", readKey, expectedKey));
                        assertEquals(0, _layout.Compare(expectedKey, readKey), string.Format("Key differ with expected%n    readKey=%s %nexpectedKey=%s%n", readKey, expectedKey));
                        assertEquals(0, _layout.compareValue(expectedValue, readValue), "Value differ with expected, value=" + readValue + ", expectedValue=" + expectedValue);

                        TreeNode.SetKeyCount(Cursor, --expectedKeyCount);
                    }
                }
            }

            // THEN
            AssertContent(expectedKeys, expectedValues, expectedKeyCount);
        }