/*----------------------------------------------------------------------------*/ /* 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 "StringValueTest.h" #include "ValueTest.h" #include "gtest/gtest.h" #include #include #include #include "llvm/StringRef.h" #define BUFSIZE 1024 namespace ntimpl { class WireEncoderTest : public ::testing::Test { protected: virtual void SetUp() { Value v; v_boolean.SetBoolean(true); v_double.SetDouble(1.0); v_string.SetString("hello"); v_raw.SetRaw("hello"); v_boolean_array.SetBooleanArray(std::vector{0, 1, 0}); v_boolean_array_big.SetBooleanArray(std::vector(256)); v_double_array.SetDoubleArray(std::vector{0.5, 0.25}); v_double_array_big.SetDoubleArray(std::vector(256)); std::vector sa; sa.push_back(StringValue("hello")); sa.push_back(StringValue("goodbye")); v_string_array.SetStringArray(sa); sa.clear(); for (int i=0; i<256; ++i) sa.push_back(StringValue("h")); v_string_array_big.SetStringArray(sa); sv_normal = StringValue("hello"); std::string longstr; longstr.append(127, '*'); longstr.push_back('x'); sv_long = StringValue(longstr); longstr.clear(); longstr.append(65534, '*'); longstr.append(3, 'x'); sv_big = StringValue(longstr); } Value v_empty; Value v_boolean, v_double, v_string, v_raw; Value v_boolean_array, v_boolean_array_big; Value v_double_array, v_double_array_big; Value v_string_array, v_string_array_big; StringValue sv_normal, sv_long, sv_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