]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Unit test for SBuf::reserve()
authorAmos Jeffries <squid3@treenet.co.nz>
Fri, 17 Jun 2016 16:29:29 +0000 (04:29 +1200)
committerAmos Jeffries <squid3@treenet.co.nz>
Fri, 17 Jun 2016 16:29:29 +0000 (04:29 +1200)
src/tests/stub_SBufDetailedStats.cc
src/tests/testSBuf.cc
src/tests/testSBuf.h

index 0290de2aa86ebca490859df10dddf54e336dac72..6120dd153a1db68a391877da1a60afb6a0f9225f 100644 (file)
@@ -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)
 
index f86528a192df4c2a20f537d68603bc42b15b1199..dc2280f78c147875f49a20e8a38437e1cad17b3a 100644 (file)
@@ -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<unsigned int>(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()
 {
index 40e8d0774fff4be68e3cc5c96c436b110a2ecee4..fb54af4140d994aa91cad2ea13b9e50ded228146 100644 (file)
@@ -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();