You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

90 lines
2.3 KiB

  1. #include <gtest/gtest.h>
  2. #include <cppcore/misc/vector_streambuf.h>
  3. using namespace ::cppcore;
  4. using namespace ::testing;
  5. TEST(vector_streambuf_tests, simple_write)
  6. {
  7. vector_streambuf buf;
  8. std::ostream os(&buf);
  9. os << "012345678901234567890123456789"
  10. << "012345678901234567890123456789";
  11. std::vector<uint8_t> expected({
  12. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  13. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  14. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  15. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  16. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  17. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  18. });
  19. auto vec0 = buf.get();
  20. auto vec1 = buf.extract();
  21. EXPECT_EQ(vec0, expected);
  22. EXPECT_EQ(vec1, expected);
  23. EXPECT_NE(vec0.data(), vec1.data());
  24. }
  25. TEST(vector_streambuf_tests, simple_read)
  26. {
  27. vector_streambuf buf;
  28. std::istream is(&buf);
  29. buf.set(std::vector<uint8_t>({ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39 }));
  30. std::string s;
  31. is >> s;
  32. EXPECT_EQ(s, "0123456789");
  33. }
  34. TEST(vector_streambuf_tests, simple_write_read)
  35. {
  36. vector_streambuf buf;
  37. std::iostream ios(&buf);
  38. std::string s;
  39. ios >> s;
  40. EXPECT_EQ(s, "");
  41. ios.clear();
  42. ios << "012345678901234567890123456789";
  43. ios >> s;
  44. EXPECT_EQ(s, "012345678901234567890123456789");
  45. ios.clear();
  46. ios << "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  47. ios >> s;
  48. EXPECT_EQ(s, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  49. }
  50. TEST(vector_streambuf_tests, simple_write_extract_write_read)
  51. {
  52. vector_streambuf buf;
  53. std::iostream ios(&buf);
  54. /* write */
  55. std::string s;
  56. ios << "012345678901234567890123456789";
  57. /* extract */
  58. auto vec = buf.extract();
  59. std::vector<uint8_t> expected({
  60. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  61. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  62. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  63. });
  64. ASSERT_EQ(vec, expected);
  65. /* write */
  66. ios << "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  67. ASSERT_EQ(vec, expected);
  68. /* read */
  69. ios >> s;
  70. ASSERT_EQ(s, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  71. }