From: JINMEI Tatuya Date: Thu, 10 Jan 2013 01:31:14 +0000 (-0800) Subject: [2572] added MasterLexer::getTotalSourceSize(). X-Git-Tag: bind10-1.0.0-rc-release~95^2~11^2~7 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e81f84bdabd3a5b7370bd2f4c41d9c7de333fafd;p=thirdparty%2Fkea.git [2572] added MasterLexer::getTotalSourceSize(). --- diff --git a/src/lib/dns/master_lexer.cc b/src/lib/dns/master_lexer.cc index e4ddedc640..064e99d25d 100644 --- a/src/lib/dns/master_lexer.cc +++ b/src/lib/dns/master_lexer.cc @@ -18,6 +18,7 @@ #include #include +#include #include #include @@ -172,6 +173,15 @@ MasterLexer::getSourceLine() const { return (impl_->sources_.back()->getCurrentLine()); } +size_t +MasterLexer::getTotalSourceSize() const { + size_t total_size = 0; + BOOST_FOREACH(InputSourcePtr& src, impl_->sources_) { + total_size += src->getSize(); + } + return (total_size); +} + const MasterToken& MasterLexer::getNextToken(Options options) { if (impl_->source_ == NULL) { diff --git a/src/lib/dns/master_lexer.h b/src/lib/dns/master_lexer.h index 1aa42556ef..26ffa184a3 100644 --- a/src/lib/dns/master_lexer.h +++ b/src/lib/dns/master_lexer.h @@ -443,6 +443,22 @@ public: /// \return The current line number of the source (see the description) size_t getSourceLine() const; + /// \brief Return the total size of pushed sources. + /// + /// This method returns the sum of the size of sources that have been + /// pushed to the lexer by the time of the call. It would give the + /// caller of some hint about the amount of data the lexer is working on. + /// + /// The size of a normal file is equal to the file size at the time of + /// the source is pushed. The size of other type of input stream is + /// the size of the data available in the stream at the time of the + /// source is pushed. + /// + /// If there is no source pushed in the lexer, it returns 0. + /// + /// \throw None + size_t getTotalSourceSize() const; + /// \brief Parse and return another token from the input. /// /// It reads a bit of the last opened source and produces another token diff --git a/src/lib/dns/master_lexer_inputsource.h b/src/lib/dns/master_lexer_inputsource.h index 510f3bbf28..85b12e0675 100644 --- a/src/lib/dns/master_lexer_inputsource.h +++ b/src/lib/dns/master_lexer_inputsource.h @@ -85,11 +85,8 @@ public: /// \brief Returns the size of the input source in bytes. /// - /// If the input source is a normal file, the return value should be - /// equal to the file size at the time of the source is created. - /// If the input source is other type of input stream, its the size of - /// the data available in the stream at the time of the construction of - /// the source. + /// See \c MasterLexer::getTotalSourceSize() for the definition of + /// the size of sources. /// /// \throw None size_t getSize() const { return (input_size_); } diff --git a/src/lib/dns/tests/master_lexer_inputsource_unittest.cc b/src/lib/dns/tests/master_lexer_inputsource_unittest.cc index d7a05dea8b..1dc3408f3b 100644 --- a/src/lib/dns/tests/master_lexer_inputsource_unittest.cc +++ b/src/lib/dns/tests/master_lexer_inputsource_unittest.cc @@ -357,7 +357,8 @@ TEST_F(InputSourceTest, getSize) { } TEST_F(InputSourceTest, getPosition) { - // Initially the position is set to 0. + // Initially the position is set to 0. Other cases are tested in tests + // for get and unget. EXPECT_EQ(0, source_.getPosition()); EXPECT_EQ(0, InputSource(TEST_DATA_SRCDIR "/masterload.txt").getPosition()); } diff --git a/src/lib/dns/tests/master_lexer_unittest.cc b/src/lib/dns/tests/master_lexer_unittest.cc index 62ad79de61..3bd6a70228 100644 --- a/src/lib/dns/tests/master_lexer_unittest.cc +++ b/src/lib/dns/tests/master_lexer_unittest.cc @@ -52,6 +52,7 @@ void checkEmptySource(const MasterLexer& lexer) { EXPECT_TRUE(lexer.getSourceName().empty()); EXPECT_EQ(0, lexer.getSourceLine()); + EXPECT_EQ(0, lexer.getTotalSourceSize()); } TEST_F(MasterLexerTest, preOpen) { @@ -61,9 +62,11 @@ TEST_F(MasterLexerTest, preOpen) { TEST_F(MasterLexerTest, pushStream) { EXPECT_EQ(0, lexer.getSourceCount()); + ss << "test"; lexer.pushSource(ss); EXPECT_EQ(expected_stream_name, lexer.getSourceName()); EXPECT_EQ(1, lexer.getSourceCount()); + EXPECT_EQ(4, lexer.getTotalSourceSize()); // 4 = len("test") // From the point of view of this test, we only have to check (though // indirectly) getSourceLine calls InputSource::getCurrentLine. It should @@ -85,6 +88,10 @@ TEST_F(MasterLexerTest, pushFile) { EXPECT_EQ(TEST_DATA_SRCDIR "/masterload.txt", lexer.getSourceName()); EXPECT_EQ(1, lexer.getSourceLine()); + // 143 = size of the test zone file. hardcode it assuming it won't change + // too often. + EXPECT_EQ(143, lexer.getTotalSourceSize()); + lexer.popSource(); checkEmptySource(lexer); EXPECT_EQ(0, lexer.getSourceCount()); @@ -116,16 +123,19 @@ TEST_F(MasterLexerTest, pushFileFail) { } TEST_F(MasterLexerTest, nestedPush) { + ss << "test"; lexer.pushSource(ss); EXPECT_EQ(expected_stream_name, lexer.getSourceName()); // We can push another source without popping the previous one. lexer.pushSource(TEST_DATA_SRCDIR "/masterload.txt"); EXPECT_EQ(TEST_DATA_SRCDIR "/masterload.txt", lexer.getSourceName()); + EXPECT_EQ(143 + 4, lexer.getTotalSourceSize()); // see above for magic nums // popSource() works on the "topmost" (last-pushed) source lexer.popSource(); EXPECT_EQ(expected_stream_name, lexer.getSourceName()); + EXPECT_EQ(4, lexer.getTotalSourceSize()); lexer.popSource(); EXPECT_TRUE(lexer.getSourceName().empty());