From: Amos Jeffries Date: Fri, 17 Jun 2016 16:29:29 +0000 (+1200) Subject: Unit test for SBuf::reserve() X-Git-Tag: SQUID_4_0_12~15 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=41dd1b867562b478d1799e7b2ce965ab05272e36;p=thirdparty%2Fsquid.git Unit test for SBuf::reserve() --- diff --git a/src/tests/stub_SBufDetailedStats.cc b/src/tests/stub_SBufDetailedStats.cc index 0290de2aa8..6120dd153a 100644 --- a/src/tests/stub_SBufDetailedStats.cc +++ b/src/tests/stub_SBufDetailedStats.cc @@ -14,8 +14,8 @@ class StatHist; -void recordSBufSizeAtDestruct(SBuf::size_type) STUB_NOP +void recordSBufSizeAtDestruct(SBuf::size_type) {} // STUB_NOP const StatHist * collectSBufDestructTimeStats() STUB_RETVAL(nullptr) -void recordMemBlobSizeAtDestruct(SBuf::size_type) STUB_NOP +void recordMemBlobSizeAtDestruct(SBuf::size_type) {} // STUB_NOP const StatHist * collectMemBlobDestructTimeStats() STUB_RETVAL(nullptr) diff --git a/src/tests/testSBuf.cc b/src/tests/testSBuf.cc index f86528a192..dc2280f78c 100644 --- a/src/tests/testSBuf.cc +++ b/src/tests/testSBuf.cc @@ -812,6 +812,41 @@ testSBuf::testGrow() CPPUNIT_ASSERT_EQUAL(ref,match); } +void +testSBuf::testReserve() +{ + SBufReservationRequirements requirements; + // use unusual numbers to ensure we dont hit a lucky boundary situation + requirements.minSpace = 10; + requirements.idealSpace = 82; + requirements.maxCapacity = 259; + requirements.allowShared = true; + + // for each possible starting buffer length within the capacity + for (SBuf::size_type startLength = 0; startLength <= requirements.maxCapacity; ++startLength) { + std::cerr << "."; + SBuf b; + b.reserveCapacity(startLength); + CPPUNIT_ASSERT_EQUAL(b.length(), static_cast(0)); + CPPUNIT_ASSERT_EQUAL(b.spaceSize(), startLength); + + // check that it never grows outside capacity. + // do 5 excess cycles to check that. + for (SBuf::size_type filled = 0; filled < requirements.maxCapacity +5; ++filled) { + CPPUNIT_ASSERT_EQUAL(b.length(), min(filled, requirements.maxCapacity)); + CPPUNIT_ASSERT(b.spaceSize() >= 0); + auto x = b.reserve(requirements); + // the amount of space advertized must not cause users to exceed capacity + CPPUNIT_ASSERT(x <= requirements.maxCapacity - filled); + CPPUNIT_ASSERT(b.spaceSize() <= requirements.maxCapacity - filled); + // the total size of buffer must not cause users to exceed capacity + CPPUNIT_ASSERT(b.length() + b.spaceSize() <= requirements.maxCapacity); + if (x > 0) + b.append('X'); + } + } +} + void testSBuf::testStartsWith() { diff --git a/src/tests/testSBuf.h b/src/tests/testSBuf.h index 40e8d0774f..fb54af4140 100644 --- a/src/tests/testSBuf.h +++ b/src/tests/testSBuf.h @@ -49,6 +49,7 @@ class testSBuf : public CPPUNIT_NS::TestFixture CPPUNIT_TEST( testCopy ); CPPUNIT_TEST( testStringOps ); CPPUNIT_TEST( testGrow ); + CPPUNIT_TEST( testReserve ); CPPUNIT_TEST( testSBufStream ); CPPUNIT_TEST( testAutoFind ); CPPUNIT_TEST( testStdStringOps ); @@ -87,6 +88,7 @@ protected: void testCopy(); void testStringOps(); void testGrow(); + void testReserve(); void testStartsWith(); void testSBufStream(); void testFindFirstOf();