inputsource_unittest.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <dns/inputsource.h>
  15. #include <exceptions/exceptions.h>
  16. #include <gtest/gtest.h>
  17. #include <iostream>
  18. #include <sstream>
  19. #include <string>
  20. #include <string.h>
  21. using namespace std;
  22. using namespace isc::dns;
  23. using namespace isc::dns::master_lexer_internal;
  24. namespace {
  25. class InputSourceTest : public ::testing::Test {
  26. protected:
  27. InputSourceTest() :
  28. name_("a90wjer"),
  29. str_("Line1 to scan.\nLine2 to scan.\nLine3 to scan.\n"),
  30. str_length_(strlen(str_)),
  31. iss_(str_),
  32. source_(iss_, name_)
  33. {}
  34. string name_;
  35. const char* str_;
  36. size_t str_length_;
  37. stringstream iss_;
  38. InputSource source_;
  39. };
  40. // Test the default return values set during InputSource construction.
  41. TEST_F(InputSourceTest, defaults) {
  42. EXPECT_EQ(name_, source_.getName());
  43. EXPECT_EQ(1, source_.getCurrentLine());
  44. EXPECT_FALSE(source_.atEOF());
  45. }
  46. // getChar() should return characters from the input stream in
  47. // sequence. ungetChar() should skip backwards.
  48. TEST_F(InputSourceTest, getAndUngetChar) {
  49. for (size_t i = 0; i < str_length_; i++) {
  50. EXPECT_EQ(str_[i], source_.getChar());
  51. EXPECT_FALSE(source_.atEOF());
  52. }
  53. // At this point, we still have not reached EOF.
  54. EXPECT_FALSE(source_.atEOF());
  55. // This should cause EOF to be set.
  56. EXPECT_EQ(-1, source_.getChar());
  57. // Now, EOF should be set.
  58. EXPECT_TRUE(source_.atEOF());
  59. // Now, let's go backwards. This should cause the EOF to be set to
  60. // false.
  61. source_.ungetChar();
  62. // Now, EOF should be false.
  63. EXPECT_FALSE(source_.atEOF());
  64. // This should cause EOF to be set again.
  65. EXPECT_EQ(-1, source_.getChar());
  66. // Now, EOF should be set.
  67. EXPECT_TRUE(source_.atEOF());
  68. // Now, let's go backwards in a loop. Start by skipping the EOF.
  69. source_.ungetChar();
  70. for (size_t i = 0; i < str_length_; i++) {
  71. size_t index = str_length_ - 1 - i;
  72. // Skip one character.
  73. source_.ungetChar();
  74. EXPECT_EQ(str_[index], source_.getChar());
  75. // Skip the character we received again.
  76. source_.ungetChar();
  77. }
  78. // Skipping past the start of buffer should throw.
  79. EXPECT_THROW(source_.ungetChar(), InputSource::UngetError);
  80. }
  81. // ungetAll() should skip back to the place where the InputSource
  82. // started at construction, or the last saved start of line.
  83. TEST_F(InputSourceTest, ungetAll) {
  84. while (!source_.atEOF()) {
  85. source_.getChar();
  86. }
  87. // Now, we are at EOF.
  88. EXPECT_TRUE(source_.atEOF());
  89. EXPECT_EQ(4, source_.getCurrentLine());
  90. source_.ungetAll();
  91. // Now we are back to where we started.
  92. EXPECT_EQ(1, source_.getCurrentLine());
  93. EXPECT_FALSE(source_.atEOF());
  94. }
  95. // Test line counters.
  96. TEST_F(InputSourceTest, lines) {
  97. size_t line = 1;
  98. while (!source_.atEOF()) {
  99. if (source_.getChar() == '\n') {
  100. line++;
  101. }
  102. EXPECT_EQ(line, source_.getCurrentLine());
  103. }
  104. // Now, we are at EOF.
  105. EXPECT_TRUE(source_.atEOF());
  106. EXPECT_EQ(4, source_.getCurrentLine());
  107. // Go backwards 2 characters, skipping the last EOF and '\n'.
  108. source_.ungetChar();
  109. source_.ungetChar();
  110. EXPECT_FALSE(source_.atEOF());
  111. EXPECT_EQ(3, source_.getCurrentLine());
  112. source_.ungetAll();
  113. // Now we are back to where we started.
  114. EXPECT_EQ(1, source_.getCurrentLine());
  115. EXPECT_FALSE(source_.atEOF());
  116. // Now check that line numbers are decremented properly (as much as
  117. // possible using the available API).
  118. while (!source_.atEOF()) {
  119. source_.getChar();
  120. }
  121. line = source_.getCurrentLine();
  122. // Now, we are at EOF.
  123. EXPECT_TRUE(source_.atEOF());
  124. EXPECT_EQ(4, line);
  125. EXPECT_THROW({
  126. while (true) {
  127. source_.ungetChar();
  128. EXPECT_TRUE(((line == source_.getCurrentLine()) ||
  129. ((line - 1) == source_.getCurrentLine())));
  130. line = source_.getCurrentLine();
  131. }
  132. }, InputSource::UngetError);
  133. // Now we are back to where we started.
  134. EXPECT_EQ(1, source_.getCurrentLine());
  135. }
  136. // ungetAll() after saveLine() should skip back to the last-saved place.
  137. TEST_F(InputSourceTest, saveLine) {
  138. // First, skip to line 2.
  139. while (!source_.atEOF() &&
  140. (source_.getCurrentLine() != 2)) {
  141. source_.getChar();
  142. }
  143. EXPECT_FALSE(source_.atEOF());
  144. size_t line = source_.getCurrentLine();
  145. EXPECT_EQ(2, line);
  146. // Now, save the line.
  147. source_.saveLine();
  148. // Now, go to EOF
  149. while (!source_.atEOF()) {
  150. source_.getChar();
  151. }
  152. line = source_.getCurrentLine();
  153. // Now, we are at EOF.
  154. EXPECT_TRUE(source_.atEOF());
  155. EXPECT_EQ(4, line);
  156. // Now, ungetAll() and check where it goes back.
  157. source_.ungetAll();
  158. // Now we are back to where we last-saved.
  159. EXPECT_EQ(2, source_.getCurrentLine());
  160. EXPECT_FALSE(source_.atEOF());
  161. }
  162. } // end namespace