Skip to content

Commit

Permalink
Merge pull request #32270 from vespa-engine/toregge/rewrite-vespalib-…
Browse files Browse the repository at this point in the history
…nbostream-unit-test-to-gtest

Rewrite vespalib nbostream unit test to gtest.
  • Loading branch information
baldersheim authored Aug 27, 2024
2 parents c15b0b1 + 73e74fc commit 9f10d7c
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 96 deletions.
1 change: 1 addition & 0 deletions vespalib/src/tests/objects/nbostream/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ vespa_add_executable(vespalib_nbostream_test_app TEST
nbostream_test.cpp
DEPENDS
vespalib
GTest::gtest
)
vespa_add_test(NAME vespalib_nbostream_test_app COMMAND vespalib_nbostream_test_app)
195 changes: 99 additions & 96 deletions vespalib/src/tests/objects/nbostream/nbostream_test.cpp
Original file line number Diff line number Diff line change
@@ -1,51 +1,40 @@
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/vespalib/testkit/test_master.hpp>
#include <vespa/vespalib/gtest/gtest.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <vespa/vespalib/objects/hexdump.h>
#include <vespa/vespalib/test/insertion_operators.h>
#include <initializer_list>
#include <ostream>

using vespalib::nbostream;
using vespalib::alloc::Alloc;
using ExpBuffer = std::vector<uint8_t>;

namespace std
{
struct ExpBuffer {
std::vector<uint8_t> buf;
ExpBuffer(std::initializer_list<uint8_t> buf_in)
: buf(buf_in)
{
}
ExpBuffer(size_t size)
: buf(size)
{
}
bool operator==(const ExpBuffer& rhs) const noexcept { return buf == rhs.buf; }
uint8_t* data() noexcept { return buf.data(); }
const uint8_t* data() const noexcept { return buf.data(); }
size_t size() const noexcept { return buf.size(); }
};

bool operator==(const std::vector<uint8_t> &exp, const nbostream &stream)
bool operator==(const ExpBuffer& exp, const nbostream& stream)
{
return ((exp.size() == stream.size()) &&
(memcmp(&exp[0], stream.peek(), exp.size()) == 0));
(memcmp(exp.data(), stream.peek(), exp.size()) == 0));
}

std::ostream &operator<<(std::ostream &out, const std::vector<uint8_t> &rhs)
{
out << vespalib::HexDump(&rhs[0], rhs.size());
return out;
}

template <typename T>
std::ostream &
operator<<(std::ostream &os, const vespalib::Array<T> &set)
{
os << "{";
bool first = true;
for (const auto &entry : set) {
if (!first) {
os << ",";
}
os << entry;
first = false;
}
os << "}";
return os;
void PrintTo(const ExpBuffer& value, std::ostream* os) {
*os << vespalib::HexDump(value.buf.data(), value.buf.size());
}


} // namespace std

struct Fixture
{
nbostream _stream;
Expand All @@ -55,220 +44,234 @@ struct Fixture
assertSerialize(const ExpBuffer &exp, const T &val)
{
_stream << val;
EXPECT_EQUAL(exp, _stream);
EXPECT_EQ(exp, _stream);
T checkVal = T();
_stream >> checkVal;
EXPECT_EQUAL(val, checkVal);
EXPECT_EQ(val, checkVal);
}
};

TEST("test that move of owned buffer does not copy") {
TEST(NbostreamTest, test_that_move_of_owned_buffer_does_not_copy)
{
Alloc buf = Alloc::allocHeap(1000);
const void * ptr = buf.get();
nbostream os(std::move(buf), 0);
os << static_cast<int64_t>(0x567);
EXPECT_EQUAL(ptr, os.peek());
EXPECT_EQUAL(8ul, os.size());
EXPECT_EQ(ptr, os.peek());
EXPECT_EQ(8ul, os.size());
nbostream moved(std::move(os));
EXPECT_TRUE(nullptr == os.peek());
EXPECT_EQUAL(0ul, os.size());
EXPECT_EQUAL(ptr, moved.peek());
EXPECT_EQUAL(8ul, moved.size());
EXPECT_EQ(0ul, os.size());
EXPECT_EQ(ptr, moved.peek());
EXPECT_EQ(8ul, moved.size());
int64_t tmp(0);
moved >> tmp;
EXPECT_EQUAL(static_cast<int64_t>(0x567), tmp);
EXPECT_EQ(static_cast<int64_t>(0x567), tmp);
}

TEST("test that move of non-owned buffer does copy") {
TEST(NbostreamTest, test_that_move_of_non_owned_buffer_does_copy)
{
Alloc buf = Alloc::allocHeap(1000);
const void * ptr = buf.get();
nbostream os(std::move(buf), 0);
os << static_cast<int64_t>(0x567);
EXPECT_EQUAL(ptr, os.peek());
EXPECT_EQUAL(8ul, os.size());
EXPECT_EQ(ptr, os.peek());
EXPECT_EQ(8ul, os.size());
nbostream refering(os.peek(), os.size());
EXPECT_EQUAL(ptr, os.peek());
EXPECT_EQUAL(8ul, os.size());
EXPECT_EQUAL(ptr, refering.peek());
EXPECT_EQUAL(8ul, refering.size());
EXPECT_EQ(ptr, os.peek());
EXPECT_EQ(8ul, os.size());
EXPECT_EQ(ptr, refering.peek());
EXPECT_EQ(8ul, refering.size());
nbostream moved(std::move(refering));
EXPECT_TRUE(nullptr == refering.peek());
EXPECT_EQUAL(0ul, refering.size());
EXPECT_EQ(0ul, refering.size());
EXPECT_TRUE(ptr != moved.peek());
EXPECT_EQUAL(8ul, moved.size());
EXPECT_EQ(8ul, moved.size());
int64_t tmp(0);
moved >> tmp;
EXPECT_EQUAL(static_cast<int64_t>(0x567), tmp);
EXPECT_EQ(static_cast<int64_t>(0x567), tmp);
}

TEST_F("test serializing 64-bit signed integers", Fixture)
TEST(NbostreamTest, test_serializing_64_bit_signed_integers)
{
Fixture f;
int64_t val = 0x0123456789ABCDEF;
f.assertSerialize({ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, val);
}


TEST_F("test serializing 64-bit unsigned integers", Fixture)
TEST(NbostreamTest, test_serializing_64_bit_unsigned_integers)
{
Fixture f;
uint64_t val = 0x0123456789ABCDEF;
f.assertSerialize({ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, val);
}


TEST_F("test serializing 32-bit signed integers", Fixture)
TEST(NbostreamTest, test_serializing_32_bit_signed_integers)
{
Fixture f;
int32_t val = 0x01234567;
f.assertSerialize({ 0x01, 0x23, 0x45, 0x67 }, val);
}


TEST_F("test serializing 32-bit unsigned integers", Fixture)
TEST(NbostreamTest, test_serializing_32_bit_unsigned_integers)
{
Fixture f;
uint32_t val = 0x01234567;
f.assertSerialize({ 0x01, 0x23, 0x45, 0x67 }, val);
}

TEST_F("test serializing 16-bit signed integers", Fixture)
TEST(NbostreamTest, test_serializing_16_bit_signed_integers)
{
Fixture f;
int16_t val = 0x0123;
f.assertSerialize({ 0x01, 0x23 }, val);
}


TEST_F("test serializing 16-bit unsigned integers", Fixture)
TEST(NbostreamTest, test_serializing_16_bit_unsigned_integers)
{
Fixture f;
uint16_t val = 0x0123;
f.assertSerialize({ 0x01, 0x23 }, val);
}

TEST_F("test serializing 8-bit signed integers", Fixture)
TEST(NbostreamTest, test_serializing_8_bit_signed_integers)
{
Fixture f;
int8_t val = 0x23;
f.assertSerialize({ 0x23 }, val);
}


TEST_F("test serializing 8-bit unsigned integers", Fixture)
TEST(NbostreamTest, test_serializing_8_bit_unsigned_integers)
{
Fixture f;
uint8_t val = 0x23;
f.assertSerialize({ 0x23 }, val);
}

TEST_F("test serializing char", Fixture)
TEST(NbostreamTest, test_serializing_char)
{
Fixture f;
char val('A');
f.assertSerialize({ 0x41 }, val);
}

TEST_F("test serializing bool", Fixture)
TEST(NbostreamTest, test_serializing_bool)
{
Fixture f;
bool myfalse = false;
bool mytrue = true;
ExpBuffer exp({ 0x00, 0x01 });
f._stream << myfalse << mytrue;
EXPECT_EQUAL(exp, f._stream);
EXPECT_EQ(exp, f._stream);
bool checkFalse = true;
bool checkTrue = false;
f._stream >> checkFalse >> checkTrue;
EXPECT_FALSE(checkFalse);
EXPECT_TRUE(checkTrue);
}

TEST_F("test serializing double", Fixture)
TEST(NbostreamTest, test_serializing_double)
{
Fixture f;
double val = 1.5;
f.assertSerialize({ 0x3F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, val);
}


TEST_F("test serializing float", Fixture)
TEST(NbostreamTest, test_serializing_float)
{
Fixture f;
float val = -1.5;
f.assertSerialize({ 0xBF, 0xC0, 0x00, 0x00 }, val);
}

TEST_F("Test serializing c string", Fixture)
TEST(NbostreamTest, Test_serializing_c_string)
{
Fixture f;
const char *cstr = "Hello";
ExpBuffer exp({ 0x00, 0x00, 0x00, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f });
f._stream << cstr;
EXPECT_EQUAL(exp, f._stream);
EXPECT_EQ(exp, f._stream);
}

TEST_F("Test serializing std::string_view", Fixture)
TEST(NbostreamTest, Test_serializing_std_string_view)
{
Fixture f;
std::string_view val("Hello");
ExpBuffer exp({ 0x00, 0x00, 0x00, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f });
f._stream << val;
EXPECT_EQUAL(exp, f._stream);
EXPECT_EQ(exp, f._stream);
}

TEST_F("Test serializing std::string", Fixture)
TEST(NbostreamTest, Test_serializing_std_string)
{
Fixture f;
std::string val("Hello");
ExpBuffer exp({ 0x00, 0x00, 0x00, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f });
f.assertSerialize(exp, val);
}

TEST_F("Test serializing std::string", Fixture)
{
std::string val("Hello");
ExpBuffer exp({ 0x00, 0x00, 0x00, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f });
f.assertSerialize(exp, val);
}

TEST_F("Test serializing std::vector", Fixture)
TEST(NbostreamTest, Test_serializing_std_vector)
{
Fixture f;
std::vector<int16_t> val({ 0x0123, 0x4567 });
ExpBuffer exp({ 0x00, 0x00, 0x00, 0x02, 0x01, 0x23, 0x45, 0x67 });
f.assertSerialize(exp, val);
}

TEST_F("Test serializing std::pair", Fixture)
TEST(NbostreamTest, Test_serializing_std_pair)
{
Fixture f;
std::pair<int16_t, int16_t> val({ 0x0123, 0x4567 });
ExpBuffer exp({ 0x01, 0x23, 0x45, 0x67 });
f.assertSerialize(exp, val);
}

TEST_F("Test write", Fixture)
TEST(NbostreamTest, Test_write)
{
Fixture f;
f._stream.write("Hello", 5);
ExpBuffer exp({ 0x48, 0x65, 0x6c, 0x6c, 0x6f });
EXPECT_EQUAL(exp, f._stream);
EXPECT_EQUAL(5u, f._stream.size());
EXPECT_EQ(exp, f._stream);
EXPECT_EQ(5u, f._stream.size());
ExpBuffer rval(5);
f._stream.read(&rval[0], 5);
EXPECT_EQUAL(exp, rval);
f._stream.read(rval.data(), 5);
EXPECT_EQ(exp, rval);
}


TEST_F("Test putInt1_4", Fixture)
TEST(NbostreamTest, Test_putInt1_4)
{
Fixture f;
f._stream.putInt1_4Bytes(5);
EXPECT_EQUAL(ExpBuffer({ 0x05 }), f._stream);
EXPECT_EQ(ExpBuffer({ 0x05 }), f._stream);
uint32_t checkInt = f._stream.getInt1_4Bytes();
EXPECT_EQUAL(5u, checkInt);
EXPECT_EQUAL(0u, f._stream.size());
EXPECT_EQ(5u, checkInt);
EXPECT_EQ(0u, f._stream.size());
f._stream.clear();
f._stream.putInt1_4Bytes(1000);
EXPECT_EQUAL(ExpBuffer({ 0x80, 0x00, 0x03, 0xe8 }), f._stream);
EXPECT_EQ(ExpBuffer({ 0x80, 0x00, 0x03, 0xe8 }), f._stream);
checkInt = f._stream.getInt1_4Bytes();
EXPECT_EQUAL(1000u, checkInt);
EXPECT_EQUAL(0u, f._stream.size());
EXPECT_EQ(1000u, checkInt);
EXPECT_EQ(0u, f._stream.size());
}


TEST_F("Test writeSmallString", Fixture)
TEST(NbostreamTest, Test_writeSmallString)
{
Fixture f;
f._stream.writeSmallString("Hello");
ExpBuffer exp({ 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f });
EXPECT_EQUAL(exp, f._stream);
EXPECT_EQ(exp, f._stream);
std::string checkString;
f._stream.readSmallString(checkString);
EXPECT_EQUAL("Hello", checkString);
EXPECT_EQUAL(0u, f._stream.size());
EXPECT_EQ("Hello", checkString);
EXPECT_EQ(0u, f._stream.size());
}


TEST_MAIN() { TEST_RUN_ALL(); }
GTEST_MAIN_RUN_ALL_TESTS()

0 comments on commit 9f10d7c

Please sign in to comment.