/*----------------------------------------------------------------------------*/ /* Copyright (c) FIRST 2015. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ /*----------------------------------------------------------------------------*/ #include "WireEncoder.h" #include "gtest/gtest.h" #include #include #include #include "llvm/StringRef.h" #define BUFSIZE 1024 namespace nt { class WireEncoderTest : public ::testing::Test { protected: WireEncoderTest() { v_empty = std::make_shared(); v_boolean = Value::MakeBoolean(true); v_double = Value::MakeDouble(1.0); v_string = Value::MakeString(llvm::StringRef("hello")); v_raw = Value::MakeRaw(llvm::StringRef("hello")); v_boolean_array = Value::MakeBooleanArray(std::vector{0, 1, 0}); v_boolean_array_big = Value::MakeBooleanArray(std::vector(256)); v_double_array = Value::MakeDoubleArray(std::vector{0.5, 0.25}); v_double_array_big = Value::MakeDoubleArray(std::vector(256)); std::vector sa; sa.push_back("hello"); sa.push_back("goodbye"); v_string_array = Value::MakeStringArray(std::move(sa)); sa.clear(); for (int i=0; i<256; ++i) sa.push_back("h"); v_string_array_big = Value::MakeStringArray(std::move(sa)); s_normal = "hello"; s_long.clear(); s_long.append(127, '*'); s_long.push_back('x'); s_big.clear(); s_big.append(65534, '*'); s_big.append(3, 'x'); } std::shared_ptr v_empty; std::shared_ptr v_boolean, v_double, v_string, v_raw; std::shared_ptr v_boolean_array, v_boolean_array_big; std::shared_ptr v_double_array, v_double_array_big; std::shared_ptr v_string_array, v_string_array_big; std::string s_normal, s_long, s_big; }; TEST_F(WireEncoderTest, Construct) { WireEncoder e(0x0300u); EXPECT_EQ(0u, e.size()); EXPECT_EQ(nullptr, e.error()); EXPECT_EQ(0x0300u, e.proto_rev()); } TEST_F(WireEncoderTest, SetProtoRev) { WireEncoder e(0x0300u); e.set_proto_rev(0x0200u); EXPECT_EQ(0x0200u, e.proto_rev()); } TEST_F(WireEncoderTest, Write8) { std::size_t off = BUFSIZE-1; WireEncoder e(0x0300u); for(std::size_t i=0; i::infinity()); e.WriteDouble(DBL_MIN); e.WriteDouble(DBL_MAX); ASSERT_EQ(40u, e.size()-off); // golden values except min and max from // http://www.binaryconvert.com/result_double.html ASSERT_EQ(llvm::StringRef( "\x00\x00\x00\x00\x00\x00\x00\x00" "\x41\x0c\x13\x80\x00\x00\x00\x00" "\x7f\xf0\x00\x00\x00\x00\x00\x00" "\x00\x10\x00\x00\x00\x00\x00\x00" "\x7f\xef\xff\xff\xff\xff\xff\xff", 40), llvm::StringRef(e.data(), e.size()).substr(off)); } TEST_F(WireEncoderTest, WriteUleb128) { std::size_t off = BUFSIZE-2; WireEncoder e(0x0300u); for(std::size_t i=0; i