1 /*
2  * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #include "squid.h"
10 #include "base/PackableStream.h"
11 #include "CapturingStoreEntry.h"
12 #include "Store.h"
13 #include "testPackableStream.h"
14 #include "testStore.h"
15 
16 #include <iomanip>
17 #include <cppunit/TestAssert.h>
18 
19 CPPUNIT_TEST_SUITE_REGISTRATION( testPackableStream );
20 
21 /* init memory pools */
22 
setUp()23 void testPackableStream::setUp()
24 {
25     Mem::Init();
26 }
27 
28 // TODO: test streaming to a MemBuf as well.
29 
30 void
testGetStream()31 testPackableStream::testGetStream()
32 {
33     /* Setup a store root so we can create a StoreEntry */
34     Store::Init();
35 
36     CapturingStoreEntry * anEntry = new CapturingStoreEntry();
37     {
38         anEntry->lock("test");
39         PackableStream stream(*anEntry);
40         CPPUNIT_ASSERT_EQUAL(1, anEntry->_buffer_calls);
41         CPPUNIT_ASSERT_EQUAL(0, anEntry->_flush_calls);
42 
43         stream.setf(std::ios::fixed);
44         stream << 123456 << std::setprecision(1) << 77.7;
45         stream << " some text" << std::setw(4) << "!" << '.';
46         CPPUNIT_ASSERT_EQUAL(1, anEntry->_buffer_calls);
47 
48         const int preFlushCount = anEntry->_flush_calls;
49         // may have already flushed
50         CPPUNIT_ASSERT(preFlushCount >= 0);
51         stream.flush();
52         // flushed at least once more
53         CPPUNIT_ASSERT(anEntry->_flush_calls > preFlushCount);
54 
55         CPPUNIT_ASSERT_EQUAL(1, anEntry->_buffer_calls);
56         CPPUNIT_ASSERT_EQUAL(String("12345677.7 some text   !."), anEntry->_appended_text);
57     }
58     delete anEntry; // does the unlock()
59     Store::FreeMemory();
60 }
61 
62