Ejemplo n.º 1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void shouldSearchAndFindOnRandomData()
        internal virtual void ShouldSearchAndFindOnRandomData()
        {
            // GIVEN a leaf node with random, although sorted (as of course it must be to binary-search), data
            _node.initializeLeaf(_cursor, STABLE_GENERATION, UNSTABLE_GENERATION);
            IList <MutableLong> keys = new List <MutableLong>();
            int         currentKey   = _random.Next(10_000);
            MutableLong key          = _layout.newKey();

            int keyCount = 0;

            while (true)
            {
                MutableLong expectedKey = _layout.newKey();
                key.Value = currentKey;
                if (_node.leafOverflow(_cursor, keyCount, key, _dummyValue) != NO)
                {
                    break;
                }
                _layout.copyKey(key, expectedKey);
                keys.Insert(keyCount, expectedKey);
                _node.insertKeyValueAt(_cursor, key, _dummyValue, keyCount, keyCount);
                currentKey += _random.Next(100) + 10;
                keyCount++;
            }
            TreeNode.SetKeyCount(_cursor, keyCount);

            // WHEN searching for random keys within that general range
            MutableLong searchKey = _layout.newKey();

            for (int i = 0; i < 1_000; i++)
            {
                searchKey.Value = _random.Next(currentKey + 10);
                int searchResult = search(_cursor, _node, LEAF, searchKey, _readKey, keyCount);

                // THEN position should be as expected
                bool exists   = contains(keys, searchKey, _layout);
                int  position = KeySearch.PositionOf(searchResult);
                assertEquals(exists, KeySearch.IsHit(searchResult));
                if (_layout.Compare(searchKey, keys[0]) <= 0)
                {                         // Our search key was lower than any of our keys, expect 0
                    assertEquals(0, position);
                }
                else
                {                         // step backwards through our expected keys and see where it should fit, assert that fact
                    bool found = false;
                    for (int j = keyCount - 1; j >= 0; j--)
                    {
                        if (_layout.Compare(searchKey, keys[j]) > 0)
                        {
                            assertEquals(j + 1, position);
                            found = true;
                            break;
                        }
                    }

                    assertTrue(found);
                }
            }
        }
Ejemplo n.º 2
0
 private void AssertSearchResult(bool hit, int position, int searchResult)
 {
     assertEquals(hit, KeySearch.IsHit(searchResult));
     assertEquals(position, KeySearch.PositionOf(searchResult));
 }