testPackableStream.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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 "compat/cppunit.h"
13 #include "Store.h"
14 #include "testStore.h"
15 
16 #include <iomanip>
17 #include <cppunit/TestAssert.h>
18 
19 class TestPackableStream : public CPPUNIT_NS::TestFixture
20 {
24 
25 protected:
26  void testGetStream();
27 };
28 
30 
31 // TODO: test streaming to a MemBuf as well.
32 
33 void
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 }
60 
61 // This test uses main() from ./testStore.cc.
62 
void lock(const char *context)
Definition: store.cc:445
CPPUNIT_TEST_SUITE(TestPackableStream)
CPPUNIT_TEST_SUITE_REGISTRATION(TestPackableStream)
CPPUNIT_TEST(testGetStream)

 

Introduction

Documentation

Support

Miscellaneous