| 1 | // | 
|---|---|
| 2 | // CountingStreamTest.cpp | 
| 3 | // | 
| 4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. | 
| 5 | // and Contributors. | 
| 6 | // | 
| 7 | // SPDX-License-Identifier: BSL-1.0 | 
| 8 | // | 
| 9 | |
| 10 | |
| 11 | #include "CountingStreamTest.h" | 
| 12 | #include "Poco/CppUnit/TestCaller.h" | 
| 13 | #include "Poco/CppUnit/TestSuite.h" | 
| 14 | #include "Poco/CountingStream.h" | 
| 15 | #include <sstream> | 
| 16 | |
| 17 | |
| 18 | using Poco::CountingInputStream; | 
| 19 | using Poco::CountingOutputStream; | 
| 20 | |
| 21 | |
| 22 | CountingStreamTest::CountingStreamTest(const std::string& rName): CppUnit::TestCase(rName) | 
| 23 | { | 
| 24 | } | 
| 25 | |
| 26 | |
| 27 | CountingStreamTest::~CountingStreamTest() | 
| 28 | { | 
| 29 | } | 
| 30 | |
| 31 | |
| 32 | void CountingStreamTest::testInput() | 
| 33 | { | 
| 34 | char c; | 
| 35 | std::istringstream istr1( "foo"); | 
| 36 | CountingInputStream ci1(istr1); | 
| 37 | while (ci1.good()) ci1.get(c); | 
| 38 | assertTrue (ci1.lines() == 1); | 
| 39 | assertTrue (ci1.chars() == 3); | 
| 40 | assertTrue (ci1.pos() == 3); | 
| 41 | |
| 42 | std::istringstream istr2( "foo\nbar"); | 
| 43 | CountingInputStream ci2(istr2); | 
| 44 | while (ci2.good()) ci2.get(c); | 
| 45 | assertTrue (ci2.lines() == 2); | 
| 46 | assertTrue (ci2.chars() == 7); | 
| 47 | assertTrue (ci2.pos() == 3); | 
| 48 | |
| 49 | std::istringstream istr3( "foo\nbar\n"); | 
| 50 | CountingInputStream ci3(istr3); | 
| 51 | while (ci3.good()) ci3.get(c); | 
| 52 | assertTrue (ci3.lines() == 2); | 
| 53 | assertTrue (ci3.chars() == 8); | 
| 54 | assertTrue (ci3.pos() == 0); | 
| 55 | |
| 56 | std::istringstream istr4( "foo"); | 
| 57 | CountingInputStream ci4(istr4); | 
| 58 | while (ci4.good()) ci4.get(c); | 
| 59 | ci4.addChars(10); | 
| 60 | ci4.addLines(2); | 
| 61 | ci4.addPos(3); | 
| 62 | assertTrue (ci4.lines() == 1 + 2); | 
| 63 | assertTrue (ci4.chars() == 3 + 10); | 
| 64 | assertTrue (ci4.pos() == 3 + 3); | 
| 65 | } | 
| 66 | |
| 67 | |
| 68 | void CountingStreamTest::testOutput() | 
| 69 | { | 
| 70 | std::ostringstream ostr1; | 
| 71 | CountingOutputStream co1(ostr1); | 
| 72 | co1 << "foo"; | 
| 73 | assertTrue (ostr1.str() == "foo"); | 
| 74 | assertTrue (co1.lines() == 1); | 
| 75 | assertTrue (co1.chars() == 3); | 
| 76 | assertTrue (co1.pos() == 3); | 
| 77 | |
| 78 | std::ostringstream ostr2; | 
| 79 | CountingOutputStream co2(ostr2); | 
| 80 | co2 << "foo\nbar"; | 
| 81 | assertTrue (ostr2.str() == "foo\nbar"); | 
| 82 | assertTrue (co2.lines() == 2); | 
| 83 | assertTrue (co2.chars() == 7); | 
| 84 | assertTrue (co2.pos() == 3); | 
| 85 | |
| 86 | CountingOutputStream co3; | 
| 87 | co3 << "foo\nbar\n"; | 
| 88 | assertTrue (co3.lines() == 2); | 
| 89 | assertTrue (co3.chars() == 8); | 
| 90 | assertTrue (co3.pos() == 0); | 
| 91 | |
| 92 | std::ostringstream ostr4; | 
| 93 | CountingOutputStream co4(ostr4); | 
| 94 | co4 << "foo"; | 
| 95 | co4.addChars(10); | 
| 96 | co4.addLines(2); | 
| 97 | co4.addPos(3); | 
| 98 | assertTrue (co4.lines() == 1 + 2); | 
| 99 | assertTrue (co4.chars() == 3 + 10); | 
| 100 | assertTrue (co4.pos() == 3 + 3); | 
| 101 | } | 
| 102 | |
| 103 | |
| 104 | void CountingStreamTest::setUp() | 
| 105 | { | 
| 106 | } | 
| 107 | |
| 108 | |
| 109 | void CountingStreamTest::tearDown() | 
| 110 | { | 
| 111 | } | 
| 112 | |
| 113 | |
| 114 | CppUnit::Test* CountingStreamTest::suite() | 
| 115 | { | 
| 116 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite( "CountingStreamTest"); | 
| 117 | |
| 118 | CppUnit_addTest(pSuite, CountingStreamTest, testInput); | 
| 119 | CppUnit_addTest(pSuite, CountingStreamTest, testOutput); | 
| 120 | |
| 121 | return pSuite; | 
| 122 | } | 
| 123 | 
