VirtualBox

Changeset 93693 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Feb 11, 2022 11:06:35 AM (3 years ago)
Author:
vboxsync
Message:

IPRT/hardavl: Added a getHeight() method for tstRTAvl and extended the testcase to check the tree height. bugref:10093

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/testcase/tstRTAvl.cpp

    r93692 r93693  
    12171217    Allocator.initSlabAllocator(cItems, (TESTNODE *)pvItems, (uint64_t *)pbmBitmap);
    12181218
     1219    uint32_t cInserted = 0;
     1220
    12191221    /*
    12201222     * Simple linear insert, get and remove.
    12211223     */
    12221224    /* insert */
    1223     for (unsigned i = 0; i < cItems * 4; i += 4)
     1225    for (unsigned i = 0; i < cItems * 4; i += 4, cInserted++)
    12241226    {
    12251227        MYTESTNODE *pNode = Allocator.allocateNode();
     
    12691271                return RTTestIFailed("freeNode(pNode2=%p) failed: %Rrc (i=%#x)", pNode2, rc, i);
    12701272        }
     1273
     1274        /* check the height */
     1275        uint8_t  const cHeight = Tree.getHeight(&Allocator);
     1276        uint32_t const cMax    = RT_BIT_32(cHeight);
     1277        if (cInserted > cMax || cInserted < (cMax >> 4))
     1278            RTTestIFailed("wrong tree height after linear insert i=%#x: cMax=%#x, cInserted=%#x, cHeight=%u\n",
     1279                          i, cMax, cInserted, cHeight);
    12711280    }
    12721281
     
    13081317
    13091318    /* remove */
    1310     for (unsigned i = 0, j = 0; i < cItems * 4; i += 4, j += 3)
     1319    for (unsigned i = 0, j = 0; i < cItems * 4; i += 4, j += 3, cInserted--)
    13111320    {
    13121321        MYTESTNODE *pNode;
     
    13281337                return RTTestIFailed("linear negative remove(%#x): %Rrc pNode=%p", k, rc, pNode);
    13291338        }
     1339
     1340        /* check the height */
     1341        uint8_t  const cHeight = Tree.getHeight(&Allocator);
     1342        uint32_t const cMax    = RT_BIT_32(cHeight);
     1343        if (cInserted > cMax || cInserted < (cMax >> 4))
     1344            RTTestIFailed("wrong tree height after linear remove i=%#x: cMax=%#x, cInserted=%#x cHeight=%d\n",
     1345                          i, cMax, cInserted, cHeight);
    13301346    }
    13311347
     
    13421358
    13431359    /* insert all in random order */
     1360    cInserted = 0;
    13441361    for (unsigned i = 0; i < cItems; i++)
    13451362    {
     
    13521369        pNode->KeyLast = pNode->Key + cbStep - 1;
    13531370        int rc = Tree.insert(&Allocator, pNode);
    1354         if (rc != VINF_SUCCESS)
     1371        if (rc == VINF_SUCCESS)
     1372            cInserted++;
     1373        else
    13551374            RTTestIFailed("random insert failed: %Rrc, i=%#x, idx=%#x (%RGp ... %RGp)", rc, i, idx, pNode->Key, pNode->KeyLast);
    13561375
     
    13641383        if (rc != VINF_SUCCESS)
    13651384            RTTestIFailed("enum after random insert %#x: %Rrc idx=%#x", i, rc, idx);
    1366         else if (cCount != i + 1)
    1367             RTTestIFailed("wrong count after random removal %#x: %#x, expected %#x", i, cCount, i + 1);
     1385        else if (cCount != cInserted)
     1386            RTTestIFailed("wrong count after random removal %#x: %#x, expected %#x", i, cCount, cInserted);
     1387
     1388        /* check the height */
     1389        uint8_t  const cHeight = Tree.getHeight(&Allocator);
     1390        uint32_t const cMax    = RT_BIT_32(cHeight);
     1391        if (cInserted > cMax || cInserted < (cMax >> 4))
     1392            RTTestIFailed("wrong tree height after random insert %#x: cMax=%#x, cInserted=%#x, cHeight=%u\n",
     1393                          i, cMax, cInserted, cHeight);
    13681394    }
    13691395
     
    13801406        else
    13811407        {
    1382             if (   pNode->Key     != idx * cbStep
     1408            cInserted--;
     1409            if (    pNode->Key     != idx * cbStep
    13831410                 || pNode->KeyLast != idx * cbStep + cbStep - 1)
    13841411                RTTestIFailed("random remove returned wrong node: %RGp ... %RGp, expected %RGp ... %RGp (i=%#x, idx=%#x)",
     
    13951422                if (rc != VINF_SUCCESS)
    13961423                    RTTestIFailed("enum after random removal %#x: %Rrc idx=%#x", i, rc, idx);
    1397                 else if (cCount != cItems - i - 1)
    1398                     RTTestIFailed("wrong count after random removal %#x: %#x, expected %#x", i, cCount, cItems - i - 1);
     1424                else if (cCount != cInserted)
     1425                    RTTestIFailed("wrong count after random removal %#x: %#x, expected %#x", i, cCount, cInserted);
    13991426            }
    14001427
     
    14031430                RTTestIFailed("free after random removal %#x failed: %Rrc pNode=%p idx=%#x", i, rc, pNode, idx);
    14041431        }
     1432
     1433        /* check the height */
     1434        uint8_t  const cHeight = Tree.getHeight(&Allocator);
     1435        uint32_t const cMax    = RT_BIT_32(cHeight);
     1436        if (cInserted > cMax || cInserted < (cMax >> 4))
     1437            RTTestIFailed("wrong tree height after random removal %#x: cMax=%#x, cInserted=%#x, cHeight=%u\n",
     1438                          i, cMax, cInserted, cHeight);
    14051439    }
    14061440
     
    14111445    RTRandAdvSeed(g_hRand, uSeed);
    14121446    RTTestIPrintf(RTTESTLVL_ALWAYS, "Random seed #2: %#RX64\n", uSeed);
    1413     uint32_t       cInserted    = 0;
    14141447    uint64_t       cItemsEnumed = 0;
    14151448    bool           fAdding      = true;
    14161449    uint64_t const nsStart      = RTTimeNanoTS();
    14171450    unsigned       i;
    1418     for (i = 0; i < _64M; i++)
     1451    for (i = 0, cInserted = 0; i < _64M; i++)
    14191452    {
    14201453        /* The operation. */
     
    14831516        else if (cCount != cInserted)
    14841517            RTTestIFailed("wrong count after random %s: %#x, expected %#x - i=%#x",
    1485                           cCount, cInserted, fDelete ? "removal" : "insert", i);
     1518                          fDelete ? "removal" : "insert", cCount, cInserted, i);
    14861519        cItemsEnumed += cCount;
     1520
     1521        /* check the height */
     1522        uint8_t  const cHeight = Tree.getHeight(&Allocator);
     1523        uint32_t const cMax    = RT_BIT_32(cHeight);
     1524        if (cInserted > cMax || cInserted < (cMax >> 4))
     1525            RTTestIFailed("wrong tree height after random %s: cMax=%#x, cInserted=%#x, cHeight=%u - i=%#x\n",
     1526                          fDelete ? "removal" : "insert", cMax, cInserted, cHeight, i);
    14871527
    14881528        /* Check for timeout. */
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette