TEST_F(RequestWriteTest, WriteInt16) { { ResetStream(); WriteInt16(0, *stream); Bytes bytes = ReadEverything(); ASSERT_TRUE(bytes); ASSERT_EQ(2, bytes->size()); ASSERT_EQ(0x00, bytes->at(0)); ASSERT_EQ(0x00, bytes->at(1)); } { ResetStream(); WriteInt16(-1, *stream); Bytes bytes = ReadEverything(); ASSERT_TRUE(bytes); ASSERT_EQ(2, bytes->size()); ASSERT_EQ(0xff, bytes->at(0)); ASSERT_EQ(0xff, bytes->at(1)); } { // Big-endian ResetStream(); WriteInt16(256, *stream); Bytes bytes = ReadEverything(); ASSERT_TRUE(bytes); ASSERT_EQ(2, bytes->size()); ASSERT_EQ(0x01, bytes->at(0)); ASSERT_EQ(0x00, bytes->at(1)); } }
void AssertBytesEq(const Bytes& expected, const Bytes& actual) { ASSERT_TRUE(expected); ASSERT_TRUE(actual); ASSERT_EQ(expected->size(), actual->size()); for (size_t i = 0; i < actual->size(); ++i) { ASSERT_EQ(expected->at(i), actual->at(i)); } }
TEST_F(RequestWriteTest, WriteString) { String test_string = "foo"; WriteString(test_string, *stream); Bytes bytes = ReadEverything(); ASSERT_TRUE(bytes); ASSERT_EQ(2 + 3, bytes->size()); ASSERT_EQ(0, bytes->at(0)); ASSERT_EQ(3, bytes->at(1)); ASSERT_EQ('f', bytes->at(2)); ASSERT_EQ('o', bytes->at(3)); ASSERT_EQ('o', bytes->at(4)); }
TEST_F(RequestWriteTest, WriteBytes) { Bytes test_bytes(new Bytes::element_type(3, 0xfe)); WriteBytes(test_bytes, *stream); Bytes bytes = ReadEverything(); ASSERT_TRUE(bytes); ASSERT_EQ(4 + 3, bytes->size()); ASSERT_EQ(0, bytes->at(0)); ASSERT_EQ(0, bytes->at(1)); ASSERT_EQ(0, bytes->at(2)); ASSERT_EQ(3, bytes->at(3)); ASSERT_EQ(0xfe, bytes->at(4)); ASSERT_EQ(0xfe, bytes->at(5)); ASSERT_EQ(0xfe, bytes->at(6)); }
TEST_F(RequestWriteTest, WriteInt64) { { ResetStream(); WriteInt64(0, *stream); Bytes bytes = ReadEverything(); ASSERT_TRUE(bytes); ASSERT_EQ(8, bytes->size()); ASSERT_EQ(0x00, bytes->at(0)); ASSERT_EQ(0x00, bytes->at(1)); ASSERT_EQ(0x00, bytes->at(2)); ASSERT_EQ(0x00, bytes->at(3)); ASSERT_EQ(0x00, bytes->at(4)); ASSERT_EQ(0x00, bytes->at(5)); ASSERT_EQ(0x00, bytes->at(6)); ASSERT_EQ(0x00, bytes->at(7)); } { ResetStream(); WriteInt64(-1, *stream); Bytes bytes = ReadEverything(); ASSERT_TRUE(bytes); ASSERT_EQ(8, bytes->size()); ASSERT_EQ(0xff, bytes->at(0)); ASSERT_EQ(0xff, bytes->at(1)); ASSERT_EQ(0xff, bytes->at(2)); ASSERT_EQ(0xff, bytes->at(3)); ASSERT_EQ(0xff, bytes->at(4)); ASSERT_EQ(0xff, bytes->at(5)); ASSERT_EQ(0xff, bytes->at(6)); ASSERT_EQ(0xff, bytes->at(7)); } { // Big-endian ResetStream(); WriteInt64(4294967296, *stream); Bytes bytes = ReadEverything(); ASSERT_TRUE(bytes); ASSERT_EQ(8, bytes->size()); ASSERT_EQ(0x00, bytes->at(0)); ASSERT_EQ(0x00, bytes->at(1)); ASSERT_EQ(0x00, bytes->at(2)); ASSERT_EQ(0x01, bytes->at(3)); ASSERT_EQ(0x00, bytes->at(4)); ASSERT_EQ(0x00, bytes->at(5)); ASSERT_EQ(0x00, bytes->at(6)); ASSERT_EQ(0x00, bytes->at(7)); } }