Readers.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #define ARDUINOJSON_ENABLE_ARDUINO_STREAM 1
  5. #include <ArduinoJson/Deserialization/Reader.hpp>
  6. #include <catch.hpp>
  7. using namespace ARDUINOJSON_NAMESPACE;
  8. TEST_CASE("Reader<std::istringstream>") {
  9. SECTION("read()") {
  10. std::istringstream src("\x01\xFF");
  11. Reader<std::istringstream> reader(src);
  12. REQUIRE(reader.read() == 0x01);
  13. REQUIRE(reader.read() == 0xFF);
  14. REQUIRE(reader.read() == -1);
  15. }
  16. SECTION("readBytes() all at once") {
  17. std::istringstream src("ABC");
  18. Reader<std::istringstream> reader(src);
  19. char buffer[8] = "abcd";
  20. REQUIRE(reader.readBytes(buffer, 4) == 3);
  21. REQUIRE(buffer[0] == 'A');
  22. REQUIRE(buffer[1] == 'B');
  23. REQUIRE(buffer[2] == 'C');
  24. REQUIRE(buffer[3] == 'd');
  25. }
  26. SECTION("readBytes() in two parts") {
  27. std::istringstream src("ABCDEF");
  28. Reader<std::istringstream> reader(src);
  29. char buffer[12] = "abcdefg";
  30. REQUIRE(reader.readBytes(buffer, 4) == 4);
  31. REQUIRE(reader.readBytes(buffer + 4, 4) == 2);
  32. REQUIRE(buffer[0] == 'A');
  33. REQUIRE(buffer[1] == 'B');
  34. REQUIRE(buffer[2] == 'C');
  35. REQUIRE(buffer[3] == 'D');
  36. REQUIRE(buffer[4] == 'E');
  37. REQUIRE(buffer[5] == 'F');
  38. REQUIRE(buffer[6] == 'g');
  39. }
  40. }
  41. TEST_CASE("BoundedReader<const char*>") {
  42. SECTION("read") {
  43. BoundedReader<const char*> reader("\x01\xFF", 2);
  44. REQUIRE(reader.read() == 0x01);
  45. REQUIRE(reader.read() == 0xFF);
  46. REQUIRE(reader.read() == -1);
  47. REQUIRE(reader.read() == -1);
  48. }
  49. SECTION("readBytes() all at once") {
  50. BoundedReader<const char*> reader("ABCD", 3);
  51. char buffer[8] = "abcd";
  52. REQUIRE(reader.readBytes(buffer, 4) == 3);
  53. REQUIRE(buffer[0] == 'A');
  54. REQUIRE(buffer[1] == 'B');
  55. REQUIRE(buffer[2] == 'C');
  56. REQUIRE(buffer[3] == 'd');
  57. }
  58. SECTION("readBytes() in two parts") {
  59. BoundedReader<const char*> reader("ABCDEF", 6);
  60. char buffer[8] = "abcdefg";
  61. REQUIRE(reader.readBytes(buffer, 4) == 4);
  62. REQUIRE(reader.readBytes(buffer + 4, 4) == 2);
  63. REQUIRE(buffer[0] == 'A');
  64. REQUIRE(buffer[1] == 'B');
  65. REQUIRE(buffer[2] == 'C');
  66. REQUIRE(buffer[3] == 'D');
  67. REQUIRE(buffer[4] == 'E');
  68. REQUIRE(buffer[5] == 'F');
  69. REQUIRE(buffer[6] == 'g');
  70. }
  71. }
  72. TEST_CASE("Reader<const char*>") {
  73. SECTION("read()") {
  74. Reader<const char*> reader("\x01\xFF\x00\x12");
  75. REQUIRE(reader.read() == 0x01);
  76. REQUIRE(reader.read() == 0xFF);
  77. REQUIRE(reader.read() == 0);
  78. REQUIRE(reader.read() == 0x12);
  79. }
  80. SECTION("readBytes() all at once") {
  81. Reader<const char*> reader("ABCD");
  82. char buffer[8] = "abcd";
  83. REQUIRE(reader.readBytes(buffer, 3) == 3);
  84. REQUIRE(buffer[0] == 'A');
  85. REQUIRE(buffer[1] == 'B');
  86. REQUIRE(buffer[2] == 'C');
  87. REQUIRE(buffer[3] == 'd');
  88. }
  89. SECTION("readBytes() in two parts") {
  90. Reader<const char*> reader("ABCDEF");
  91. char buffer[8] = "abcdefg";
  92. REQUIRE(reader.readBytes(buffer, 4) == 4);
  93. REQUIRE(reader.readBytes(buffer + 4, 2) == 2);
  94. REQUIRE(buffer[0] == 'A');
  95. REQUIRE(buffer[1] == 'B');
  96. REQUIRE(buffer[2] == 'C');
  97. REQUIRE(buffer[3] == 'D');
  98. REQUIRE(buffer[4] == 'E');
  99. REQUIRE(buffer[5] == 'F');
  100. REQUIRE(buffer[6] == 'g');
  101. }
  102. }
  103. TEST_CASE("IteratorReader") {
  104. SECTION("read()") {
  105. std::string src("\x01\xFF");
  106. IteratorReader<std::string::const_iterator> reader(src.begin(), src.end());
  107. REQUIRE(reader.read() == 0x01);
  108. REQUIRE(reader.read() == 0xFF);
  109. REQUIRE(reader.read() == -1);
  110. }
  111. SECTION("readBytes() all at once") {
  112. std::string src("ABC");
  113. IteratorReader<std::string::const_iterator> reader(src.begin(), src.end());
  114. char buffer[8] = "abcd";
  115. REQUIRE(reader.readBytes(buffer, 4) == 3);
  116. REQUIRE(buffer[0] == 'A');
  117. REQUIRE(buffer[1] == 'B');
  118. REQUIRE(buffer[2] == 'C');
  119. REQUIRE(buffer[3] == 'd');
  120. }
  121. SECTION("readBytes() in two parts") {
  122. std::string src("ABCDEF");
  123. IteratorReader<std::string::const_iterator> reader(src.begin(), src.end());
  124. char buffer[12] = "abcdefg";
  125. REQUIRE(reader.readBytes(buffer, 4) == 4);
  126. REQUIRE(reader.readBytes(buffer + 4, 4) == 2);
  127. REQUIRE(buffer[0] == 'A');
  128. REQUIRE(buffer[1] == 'B');
  129. REQUIRE(buffer[2] == 'C');
  130. REQUIRE(buffer[3] == 'D');
  131. REQUIRE(buffer[4] == 'E');
  132. REQUIRE(buffer[5] == 'F');
  133. REQUIRE(buffer[6] == 'g');
  134. }
  135. }
  136. class StreamStub : public Stream {
  137. public:
  138. StreamStub(const char* s) : _stream(s) {}
  139. int read() {
  140. return _stream.get();
  141. }
  142. size_t readBytes(char* buffer, size_t length) {
  143. _stream.read(buffer, static_cast<std::streamsize>(length));
  144. return static_cast<size_t>(_stream.gcount());
  145. }
  146. private:
  147. std::istringstream _stream;
  148. };
  149. TEST_CASE("Reader<Stream>") {
  150. SECTION("read()") {
  151. StreamStub src("\x01\xFF");
  152. Reader<StreamStub> reader(src);
  153. REQUIRE(reader.read() == 0x01);
  154. REQUIRE(reader.read() == 0xFF);
  155. REQUIRE(reader.read() == -1);
  156. }
  157. SECTION("readBytes() all at once") {
  158. StreamStub src("ABC");
  159. Reader<StreamStub> reader(src);
  160. char buffer[8] = "abcd";
  161. REQUIRE(reader.readBytes(buffer, 4) == 3);
  162. REQUIRE(buffer[0] == 'A');
  163. REQUIRE(buffer[1] == 'B');
  164. REQUIRE(buffer[2] == 'C');
  165. REQUIRE(buffer[3] == 'd');
  166. }
  167. SECTION("readBytes() in two parts") {
  168. StreamStub src("ABCDEF");
  169. Reader<StreamStub> reader(src);
  170. char buffer[12] = "abcdefg";
  171. REQUIRE(reader.readBytes(buffer, 4) == 4);
  172. REQUIRE(reader.readBytes(buffer + 4, 4) == 2);
  173. REQUIRE(buffer[0] == 'A');
  174. REQUIRE(buffer[1] == 'B');
  175. REQUIRE(buffer[2] == 'C');
  176. REQUIRE(buffer[3] == 'D');
  177. REQUIRE(buffer[4] == 'E');
  178. REQUIRE(buffer[5] == 'F');
  179. REQUIRE(buffer[6] == 'g');
  180. }
  181. }