]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/tests/testIoManip.cc
Add AtMostOnce stream manipulator (#1742)
[thirdparty/squid.git] / src / tests / testIoManip.cc
index 22abc609938929354225240f1f9a13b7fef67c9f..80b0f024f7fcf1e7cb90a6410cae4572bb38b38b 100644 (file)
@@ -19,10 +19,12 @@ class TestIoManip: public CPPUNIT_NS::TestFixture
 {
     CPPUNIT_TEST_SUITE(TestIoManip);
     CPPUNIT_TEST(testAsHex);
+    CPPUNIT_TEST(testAtMostOnce);
     CPPUNIT_TEST_SUITE_END();
 
 protected:
     void testAsHex();
+    void testAtMostOnce();
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION( TestIoManip );
@@ -153,6 +155,34 @@ TestIoManip::testAsHex()
     resetStream(ss);
 }
 
+void
+TestIoManip::testAtMostOnce()
+{
+    {
+        std::ostringstream ss;
+        auto textOnce = AtMostOnce("text1");
+        ss << textOnce;
+        CPPUNIT_ASSERT_EQUAL(std::string("text1"), ss.str());
+        ss << textOnce;
+        ss << textOnce;
+        CPPUNIT_ASSERT_EQUAL(std::string("text1"), ss.str());
+    }
+
+    {
+        std::ostringstream ss;
+        // Cannot create std::string when creating textOnce because the string may be
+        // destroyed before we are done with textOnce:
+        // auto textOnce = AtMostOnce(std::string("do not do this"));
+        const std::string s("text2");
+        auto textOnce = AtMostOnce(s);
+        ss << textOnce;
+        CPPUNIT_ASSERT_EQUAL(std::string("text2"), ss.str());
+        ss << textOnce;
+        ss << textOnce;
+        CPPUNIT_ASSERT_EQUAL(std::string("text2"), ss.str());
+    }
+}
+
 int
 main(int argc, char *argv[])
 {