- Timestamp:
- Sep 11, 2018 11:30:05 AM (6 years ago)
- Location:
- trunk/src/VBox/Runtime
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/rest/RTCRestArrayBase.cpp
r74025 r74193 287 287 if (a_idx < m_cElements) 288 288 memmove(&m_papElements[a_idx], &m_papElements[a_idx + 1], (m_cElements - a_idx) * sizeof(m_papElements[0])); 289 290 m_cElements--;291 289 } 292 290 return false; -
trunk/src/VBox/Runtime/testcase/tstRTCRest-1.cpp
r74188 r74193 1466 1466 } 1467 1467 1468 1469 /** Wraps RTCRestInt16 to check for leaks. */ 1470 class MyRestInt16 : public RTCRestInt16 1471 { 1472 public: 1473 static size_t s_cInstances; 1474 MyRestInt16() : RTCRestInt16() { s_cInstances++; } 1475 MyRestInt16(MyRestInt16 const &a_rThat) : RTCRestInt16(a_rThat) { s_cInstances++; } 1476 MyRestInt16(int16_t a_iValue) : RTCRestInt16(a_iValue) { s_cInstances++; } 1477 ~MyRestInt16() { s_cInstances--; } 1478 }; 1479 1480 size_t MyRestInt16::s_cInstances = 0; 1481 1482 1483 static void verifyArray(RTCRestArray<MyRestInt16> const &rArray, int iLine, unsigned cElements, ...) 1484 { 1485 if (rArray.size() != cElements) 1486 RTTestIFailed("line %u: size() -> %zu, expected %u", iLine, rArray.size(), cElements); 1487 va_list va; 1488 va_start(va, cElements); 1489 for (unsigned i = 0; i < cElements; i++) 1490 { 1491 int iExpected = va_arg(va, int); 1492 if (rArray.at(i)->m_iValue != iExpected) 1493 RTTestIFailed("line %u: element #%u: %d, expected %d", iLine, i, rArray.at(i)->m_iValue, iExpected); 1494 } 1495 va_end(va); 1496 } 1497 1498 1468 1499 static void testArray() 1469 1500 { … … 1473 1504 RTCRestArray<RTCRestBool> obj1; 1474 1505 RTTESTI_CHECK(obj1.size() == 0); 1506 RTTESTI_CHECK(obj1.isEmpty() == true); 1475 1507 RTTESTI_CHECK(obj1.isNull() == false); 1476 1508 RTTESTI_CHECK(strcmp(obj1.typeName(), "RTCRestArray<ElementType>") == 0); … … 1478 1510 } 1479 1511 1480 { 1512 /* Some random order insertion and manipulations: */ 1513 { 1514 RTCRestArray<MyRestInt16> Arr2; 1515 RTCRestArray<MyRestInt16> const *pConstArr2 = &Arr2; 1516 1517 RTTESTI_CHECK_RC(Arr2.insert(0, new MyRestInt16(3)), VINF_SUCCESS); 1518 verifyArray(Arr2, __LINE__, 1, 3); 1519 RTTESTI_CHECK_RC(Arr2.append( new MyRestInt16(7)), VINF_SUCCESS); 1520 verifyArray(Arr2, __LINE__, 2, 3, 7); 1521 RTTESTI_CHECK_RC(Arr2.insert(1, new MyRestInt16(5)), VINF_SUCCESS); 1522 verifyArray(Arr2, __LINE__, 3, 3, 5, 7); 1523 RTTESTI_CHECK_RC(Arr2.insert(2, new MyRestInt16(6)), VINF_SUCCESS); 1524 verifyArray(Arr2, __LINE__, 4, 3, 5, 6, 7); 1525 RTTESTI_CHECK_RC(Arr2.prepend( new MyRestInt16(0)), VINF_SUCCESS); 1526 verifyArray(Arr2, __LINE__, 5, 0, 3, 5, 6, 7); 1527 RTTESTI_CHECK_RC(Arr2.append( new MyRestInt16(9)), VINF_SUCCESS); 1528 verifyArray(Arr2, __LINE__, 6, 0, 3, 5, 6, 7, 9); 1529 RTTESTI_CHECK_RC(Arr2.insert(5, new MyRestInt16(8)), VINF_SUCCESS); 1530 verifyArray(Arr2, __LINE__, 7, 0, 3, 5, 6, 7, 8, 9); 1531 RTTESTI_CHECK_RC(Arr2.insert(1, new MyRestInt16(1)), VINF_SUCCESS); 1532 verifyArray(Arr2, __LINE__, 8, 0, 1, 3, 5, 6, 7, 8, 9); 1533 RTTESTI_CHECK_RC(Arr2.insert(3, new MyRestInt16(4)), VINF_SUCCESS); 1534 verifyArray(Arr2, __LINE__, 9, 0, 1, 3, 4, 5, 6, 7, 8, 9); 1535 RTTESTI_CHECK_RC(Arr2.insert(2, new MyRestInt16(2)), VINF_SUCCESS); 1536 verifyArray(Arr2, __LINE__, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9); 1537 1538 RTTESTI_CHECK(Arr2.size() == 10); 1539 1540 for (size_t i = 0; i < Arr2.size(); i++) 1541 { 1542 MyRestInt16 *pCur = Arr2.at(i); 1543 RTTESTI_CHECK(pCur->m_iValue == i); 1544 1545 MyRestInt16 const *pCur2 = pConstArr2->at(i); 1546 RTTESTI_CHECK(pCur2->m_iValue == i); 1547 } 1548 1549 RTTESTI_CHECK_RC(Arr2.replace(2, new MyRestInt16(22)), VWRN_ALREADY_EXISTS); 1550 verifyArray(Arr2, __LINE__, 10, 0, 1, 22, 3, 4, 5, 6, 7, 8, 9); 1551 1552 RTTESTI_CHECK_RC(Arr2.replace(7, new MyRestInt16(77)), VWRN_ALREADY_EXISTS); 1553 verifyArray(Arr2, __LINE__, 10, 0, 1, 22, 3, 4, 5, 6, 77, 8, 9); 1554 1555 RTTESTI_CHECK_RC(Arr2.replace(10, new MyRestInt16(10)), VINF_SUCCESS); 1556 verifyArray(Arr2, __LINE__, 11, 0, 1, 22, 3, 4, 5, 6, 77, 8, 9, 10); 1557 1558 RTTESTI_CHECK_RC(Arr2.replaceCopy(2, MyRestInt16(2)), VWRN_ALREADY_EXISTS); 1559 verifyArray(Arr2, __LINE__, 11, 0, 1, 2, 3, 4, 5, 6, 77, 8, 9, 10); 1560 1561 /* copy constructor: */ 1562 { 1563 RTCRestArray<MyRestInt16> const Arr2Copy(Arr2); 1564 verifyArray(Arr2Copy, __LINE__, 11, 0, 1, 2, 3, 4, 5, 6, 77, 8, 9, 10); 1565 } 1566 verifyArray(Arr2, __LINE__, 11, 0, 1, 2, 3, 4, 5, 6, 77, 8, 9, 10); 1567 1568 { 1569 RTCRestArray<MyRestInt16> Arr2Copy2(Arr2); 1570 verifyArray(Arr2Copy2, __LINE__, 11, 0, 1, 2, 3, 4, 5, 6, 77, 8, 9, 10); 1571 RTTESTI_CHECK_RC(Arr2Copy2.removeAt(7), VINF_SUCCESS); 1572 verifyArray(Arr2Copy2, __LINE__, 10, 0, 1, 2, 3, 4, 5, 6, 8, 9, 10); 1573 } 1574 verifyArray(Arr2, __LINE__, 11, 0, 1, 2, 3, 4, 5, 6, 77, 8, 9, 10); 1575 1576 /* copy method: */ 1577 { 1578 RTCRestArray<MyRestInt16> Arr2Copy3; 1579 RTTESTI_CHECK_RC(Arr2Copy3.assignCopy(Arr2), VINF_SUCCESS); 1580 verifyArray(Arr2Copy3, __LINE__, 11, 0, 1, 2, 3, 4, 5, 6, 77, 8, 9, 10); 1581 Arr2Copy3.at(3)->m_iValue = 33; 1582 verifyArray(Arr2Copy3, __LINE__, 11, 0, 1, 2, 33, 4, 5, 6, 77, 8, 9, 10); 1583 Arr2Copy3.clear(); 1584 verifyArray(Arr2Copy3, __LINE__, 0); 1585 RTTESTI_CHECK_MSG(MyRestInt16::s_cInstances == Arr2.size(), ("%zu vs %zu\n", MyRestInt16::s_cInstances, Arr2.size())); 1586 } 1587 verifyArray(Arr2, __LINE__, 11, 0, 1, 2, 3, 4, 5, 6, 77, 8, 9, 10); 1588 RTTESTI_CHECK_MSG(MyRestInt16::s_cInstances == Arr2.size(), ("%zu vs %zu\n", MyRestInt16::s_cInstances, Arr2.size())); 1589 } 1590 RTTESTI_CHECK_MSG(MyRestInt16::s_cInstances == 0, ("%zu\n", MyRestInt16::s_cInstances)); 1591 1592 { 1593 RTCRestArray<RTCRestInt64> Arr3; 1594 RTCRestArray<RTCRestInt64> const *pConstArr3 = &Arr3; 1595 1481 1596 /* Insert a range of numbers into a int64 array. */ 1482 RTCRestArray<RTCRestInt64> Arr2;1483 1597 for (int64_t i = 0; i < _64K; i++) 1484 1598 { … … 1487 1601 RTCRestInt64 toCopy(i); 1488 1602 if (i & 2) 1489 RTTESTI_CHECK_RC(Arr 2.insertCopy(i, toCopy), VINF_SUCCESS);1603 RTTESTI_CHECK_RC(Arr3.insertCopy(i, toCopy), VINF_SUCCESS); 1490 1604 else 1491 RTTESTI_CHECK_RC(Arr 2.appendCopy(toCopy), VINF_SUCCESS);1605 RTTESTI_CHECK_RC(Arr3.appendCopy(toCopy), VINF_SUCCESS); 1492 1606 } 1493 1607 else … … 1495 1609 RTCRestInt64 *pDirect = new RTCRestInt64(i); 1496 1610 if (i & 2) 1497 RTTESTI_CHECK_RC(Arr 2.insert(i, pDirect), VINF_SUCCESS);1611 RTTESTI_CHECK_RC(Arr3.insert(i, pDirect), VINF_SUCCESS); 1498 1612 else 1499 RTTESTI_CHECK_RC(Arr 2.append(pDirect), VINF_SUCCESS);1613 RTTESTI_CHECK_RC(Arr3.append(pDirect), VINF_SUCCESS); 1500 1614 } 1501 } 1502 1503 /* verify: */ 1504 RTCRestArray<RTCRestInt64> const *pConstArr2 = &Arr2; 1615 RTTESTI_CHECK(Arr3.size() == (size_t)i + 1); 1616 RTTESTI_CHECK(Arr3.isEmpty() == false); 1617 } 1618 1619 /* Verify insertions: */ 1620 size_t cElements = _64K; 1621 RTTESTI_CHECK(Arr3.size() == cElements); 1622 1505 1623 for (int64_t i = 0; i < _64K; i++) 1506 1624 { 1507 RTCRestInt64 *pCur = Arr 2.at(i);1625 RTCRestInt64 *pCur = Arr3.at(i); 1508 1626 RTTESTI_CHECK(pCur->m_iValue == i); 1509 1627 1510 RTCRestInt64 const *pCur2 = pConstArr 2->at(i);1628 RTCRestInt64 const *pCur2 = pConstArr3->at(i); 1511 1629 RTTESTI_CHECK(pCur2->m_iValue == i); 1512 1630 } 1513 RTTESTI_CHECK(Arr2.first()->m_iValue == 0); 1514 RTTESTI_CHECK(Arr2.last()->m_iValue == _64K - 1); 1515 RTTESTI_CHECK(pConstArr2->first()->m_iValue == 0); 1516 RTTESTI_CHECK(pConstArr2->last()->m_iValue == _64K - 1); 1517 1518 /* Remove every 3rd element. */ 1519 for (int64_t i = _64K - 1; i > 0; i -= 3) 1520 RTTESTI_CHECK_RC(Arr2.removeAt(i), VINF_SUCCESS); 1521 #if 0 1522 for (int64_t i = 0, iValue = 0; i < (ssize_t)Arr2.size(); i++, iValue++) 1631 RTTESTI_CHECK(Arr3.first()->m_iValue == 0); 1632 RTTESTI_CHECK(Arr3.last()->m_iValue == _64K - 1); 1633 RTTESTI_CHECK(pConstArr3->first()->m_iValue == 0); 1634 RTTESTI_CHECK(pConstArr3->last()->m_iValue == _64K - 1); 1635 1636 /* Remove every 3rd element: */ 1637 RTTESTI_CHECK(Arr3.size() == cElements); 1638 for (int64_t i = _64K - 1; i >= 0; i -= 3) 1639 { 1640 RTTESTI_CHECK_RC(Arr3.removeAt(i), VINF_SUCCESS); 1641 cElements--; 1642 RTTESTI_CHECK(Arr3.size() == cElements); 1643 } 1644 1645 /* Verify after removal: */ 1646 for (int64_t i = 0, iValue = 0; i < (ssize_t)Arr3.size(); i++, iValue++) 1523 1647 { 1524 1648 if ((iValue % 3) == 0) 1525 1649 iValue++; 1526 RTTESTI_CHECK_MSG(Arr2.at(i)->m_iValue == iValue, ("%RI64: %RI64 vs %RI64", i, Arr2.at(i)->m_iValue, iValue)); 1527 } 1528 #endif 1650 RTTESTI_CHECK_MSG(Arr3.at(i)->m_iValue == iValue, ("%RI64: %RI64 vs %RI64\n", i, Arr3.at(i)->m_iValue, iValue)); 1651 } 1652 1653 /* Clear it and we're done: */ 1654 Arr3.clear(); 1655 RTTESTI_CHECK(Arr3.size() == 0); 1656 RTTESTI_CHECK(Arr3.isEmpty() == true); 1657 } 1658 1659 { 1660 RTCRestArray<RTCRestInt32> Arr4; 1661 1662 /* Insert a range of numbers into a int32 array, in reverse order. */ 1663 for (int32_t i = 0; i < 2048; i++) 1664 { 1665 if (i & 1) 1666 { 1667 RTCRestInt32 toCopy(i); 1668 if (i & 2) 1669 RTTESTI_CHECK_RC(Arr4.insertCopy(0, toCopy), VINF_SUCCESS); 1670 else 1671 RTTESTI_CHECK_RC(Arr4.prependCopy(toCopy), VINF_SUCCESS); 1672 } 1673 else 1674 { 1675 RTCRestInt32 *pDirect = new RTCRestInt32(i); 1676 if (i & 2) 1677 RTTESTI_CHECK_RC(Arr4.insert(0, pDirect), VINF_SUCCESS); 1678 else 1679 RTTESTI_CHECK_RC(Arr4.prepend(pDirect), VINF_SUCCESS); 1680 } 1681 RTTESTI_CHECK(Arr4.size() == i + 1); 1682 RTTESTI_CHECK(Arr4.isEmpty() == false); 1683 } 1684 1685 for (int32_t i = 0, iValue = (int32_t)Arr4.size() - 1; i < (ssize_t)Arr4.size(); i++, iValue--) 1686 RTTESTI_CHECK_MSG(Arr4.at(i)->m_iValue == iValue, ("%RI32: %RI32 vs %RI32\n", i, Arr4.at(i)->m_iValue, iValue)); 1687 1688 for (int32_t i = 0; i < 512; i++) 1689 RTTESTI_CHECK_RC(Arr4.removeAt(0), VINF_SUCCESS); 1690 RTTESTI_CHECK(Arr4.size() == 1536); 1691 1692 for (int32_t i = 0; i < 512; i++) 1693 RTTESTI_CHECK_RC(Arr4.removeAt(~(size_t)0), VINF_SUCCESS); 1694 RTTESTI_CHECK(Arr4.size() == 1024); 1695 1696 for (int32_t i = 0, iValue = 1535; i < (ssize_t)Arr4.size(); i++, iValue--) 1697 RTTESTI_CHECK_MSG(Arr4.at(i)->m_iValue == iValue, ("%RI32: %RI32 vs %RI32\n", i, Arr4.at(i)->m_iValue, iValue)); 1529 1698 } 1530 1699 }
Note:
See TracChangeset
for help on using the changeset viewer.