]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[2369] Add InputSource::compact() method
authorMukund Sivaraman <muks@isc.org>
Wed, 31 Oct 2012 05:23:31 +0000 (10:53 +0530)
committerMukund Sivaraman <muks@isc.org>
Wed, 31 Oct 2012 05:23:31 +0000 (10:53 +0530)
src/lib/dns/inputsource.cc
src/lib/dns/inputsource.h
src/lib/dns/tests/inputsource_unittest.cc

index 1da98b0b8de49cef5e985a6390e1540e41abe41d..b5b1e0858c7b8e2f772e14c1d3d4a3ba1e64eb46 100644 (file)
@@ -102,6 +102,17 @@ InputSource::ungetAll() {
     at_eof_ = false;
 }
 
+void
+InputSource::compact() {
+    if (buffer_pos_ == buffer_.size()) {
+        buffer_.clear();
+    } else {
+        buffer_.erase(buffer_.begin() + buffer_pos_);
+    }
+
+    buffer_pos_ = 0;
+}
+
 } // namespace master_lexer_internal
 } // namespace dns
 } // namespace isc
index d45fa2861af07d3a4845cc4c1071e9204dcc4227..ba2f77dab42dba54a70bce060a5d7823a905ae78 100644 (file)
@@ -92,6 +92,11 @@ public:
     /// it sets the current line number to the line number saved then.
     void ungetAll();
 
+    /// Removes buffered content before the current location in the
+    /// \c InputSource. It's not possible to \c ungetChar() after this,
+    /// unless we read more data using \c getChar().
+    void compact();
+
 private:
     bool at_eof_;
     size_t line_;
index 097adedf9b3e612356b216ccbeb0e7fb4d60c694..e2c386b429a276c7b853a47f495ba4cba1a431a9 100644 (file)
@@ -123,6 +123,96 @@ TEST_F(InputSourceTest, ungetAll) {
     EXPECT_FALSE(source_.atEOF());
 }
 
+TEST_F(InputSourceTest, compact) {
+    // Compact at the start
+    source_.compact();
+
+    // Ungetting here must throw.
+    EXPECT_THROW(source_.ungetChar(), InputSource::UngetError);
+
+    for (size_t i = 0; i < str_length_; i++) {
+        EXPECT_EQ(str_[i], source_.getChar());
+        EXPECT_FALSE(source_.atEOF());
+    }
+
+    // At this point, we still have not reached EOF.
+    EXPECT_FALSE(source_.atEOF());
+
+    // This should cause EOF to be set.
+    EXPECT_EQ(-1, source_.getChar());
+
+    // Now, EOF should be set.
+    EXPECT_TRUE(source_.atEOF());
+    EXPECT_EQ(4, source_.getCurrentLine());
+
+    // Compact again
+    source_.compact();
+
+    // We are still at EOF.
+    EXPECT_TRUE(source_.atEOF());
+    EXPECT_EQ(4, source_.getCurrentLine());
+
+    // Skip the EOF.
+    source_.ungetChar();
+
+    // Ungetting here must throw.
+    EXPECT_THROW(source_.ungetChar(), InputSource::UngetError);
+
+    EXPECT_EQ(-1, source_.getChar());
+    EXPECT_TRUE(source_.atEOF());
+}
+
+TEST_F(InputSourceTest, compactDuring) {
+    // First, skip to line 2.
+    while (!source_.atEOF() &&
+           (source_.getCurrentLine() != 2)) {
+        source_.getChar();
+    }
+    EXPECT_FALSE(source_.atEOF());
+    size_t line = source_.getCurrentLine();
+    EXPECT_EQ(2, line);
+
+    source_.saveLine();
+    source_.compact();
+
+    // Ungetting here must throw.
+    EXPECT_THROW(source_.ungetChar(), InputSource::UngetError);
+
+    for (size_t i = 15; i < str_length_; i++) {
+        EXPECT_EQ(str_[i], source_.getChar());
+        EXPECT_FALSE(source_.atEOF());
+    }
+
+    // At this point, we still have not reached EOF.
+    EXPECT_FALSE(source_.atEOF());
+
+    // This should cause EOF to be set.
+    EXPECT_EQ(-1, source_.getChar());
+
+    // Now, EOF should be set.
+    EXPECT_TRUE(source_.atEOF());
+
+    // Now, ungetAll() and check where it goes back.
+    source_.ungetAll();
+
+    // Ungetting here must throw.
+    EXPECT_THROW(source_.ungetChar(), InputSource::UngetError);
+
+    for (size_t i = 15; i < str_length_; i++) {
+        EXPECT_EQ(str_[i], source_.getChar());
+        EXPECT_FALSE(source_.atEOF());
+    }
+
+    // At this point, we still have not reached EOF.
+    EXPECT_FALSE(source_.atEOF());
+
+    // This should cause EOF to be set.
+    EXPECT_EQ(-1, source_.getChar());
+
+    // Now, EOF should be set.
+    EXPECT_TRUE(source_.atEOF());
+}
+
 // Test line counters.
 TEST_F(InputSourceTest, lines) {
     size_t line = 1;