|
- #include <gtest/gtest.h>
- #include <cppcore/misc/vector_streambuf.h>
-
- using namespace ::cppcore;
- using namespace ::testing;
-
- TEST(vector_streambuf_tests, simple_write)
- {
- vector_streambuf buf;
- std::ostream os(&buf);
-
- os << "012345678901234567890123456789"
- << "012345678901234567890123456789";
-
- std::vector<uint8_t> expected({
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
- });
- auto vec0 = buf.get();
- auto vec1 = buf.extract();
-
- EXPECT_EQ(vec0, expected);
- EXPECT_EQ(vec1, expected);
- EXPECT_NE(vec0.data(), vec1.data());
- }
-
- TEST(vector_streambuf_tests, simple_read)
- {
- vector_streambuf buf;
- std::istream is(&buf);
-
- buf.set(std::vector<uint8_t>({ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39 }));
-
- std::string s;
- is >> s;
-
- EXPECT_EQ(s, "0123456789");
- }
-
- TEST(vector_streambuf_tests, simple_write_read)
- {
- vector_streambuf buf;
- std::iostream ios(&buf);
-
- std::string s;
- ios >> s;
- EXPECT_EQ(s, "");
-
- ios.clear();
- ios << "012345678901234567890123456789";
- ios >> s;
- EXPECT_EQ(s, "012345678901234567890123456789");
-
- ios.clear();
- ios << "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- ios >> s;
- EXPECT_EQ(s, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
- }
-
- TEST(vector_streambuf_tests, simple_write_extract_write_read)
- {
- vector_streambuf buf;
- std::iostream ios(&buf);
-
- /* write */
- std::string s;
- ios << "012345678901234567890123456789";
-
- /* extract */
- auto vec = buf.extract();
- std::vector<uint8_t> expected({
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
- });
- ASSERT_EQ(vec, expected);
-
- /* write */
- ios << "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- ASSERT_EQ(vec, expected);
-
- /* read */
- ios >> s;
- ASSERT_EQ(s, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
- }
|