#include #include #include "gtest/gtest.h" #include "../src/core/RandomAccessBinary.h" class RABTest : public ::testing::Test { protected: virtual void SetUp(){ uint64_t bigdata = 0x0CAFEBABE; test2.set_uint64(0, bigdata); test4.setEndianness(RandomAccessBinary::Endianness::LITTLE); } virtual void TearDown(){ test.clear(); test2.clear(); test3.clear(); test4.clear(); } RandomAccessBinary test; RandomAccessBinary test2; RandomAccessBinary test3; RandomAccessBinary test4; }; TEST_F(RABTest, IsEmptyInitially) { EXPECT_EQ(0, test.size()); EXPECT_EQ("", test.get_hex_string()); } TEST_F(RABTest, SomeData) { EXPECT_EQ(8, test2.size()); EXPECT_EQ("00000000cafebabe", test2.get_hex_string()); EXPECT_EQ("cafebabe", test2.get_hex_string(4,4)); } TEST_F(RABTest, Double) { //double a = 3.1415; double a = 2.7182818284; test3.set_double(0, a); EXPECT_EQ("4005bf0a8b12500b", test3.get_hex_string()); EXPECT_DOUBLE_EQ(2.7182818284, test3.get_double(0)); // LITTLE ENDIAN test4.set_double(0, a); EXPECT_EQ("0b50128b0abf0540", test4.get_hex_string()); EXPECT_DOUBLE_EQ(2.7182818284, test4.get_double(0)); } TEST_F(RABTest, Float) { float a = 2.71828f; test3.set_float(0, a); EXPECT_EQ("402df84d", test3.get_hex_string()); EXPECT_FLOAT_EQ(2.71828f, test3.get_float(0)); // LITTLE ENDIAN test4.set_float(0, a); EXPECT_EQ("4df82d40", test4.get_hex_string()); EXPECT_FLOAT_EQ(2.71828f, test4.get_float(0)); } TEST_F(RABTest, VariousInteger) { uint8_t byte = 42; uint16_t a_short = 31337; uint32_t a_int = 0xdeadbeef; uint64_t a_long = 0xcafebabedeadbeef; test3.setByte(0, byte); test3.set_uint16(1, a_short); test3.set_uint32(3, a_int); test3.set_uint64(7, a_long); test4.setByte(0, byte); test4.set_uint16(1, a_short); test4.set_uint32(3, a_int); test4.set_uint64(7, a_long); std::cout << "BIG ENDIAN: " << test3.get_hex_string() << std::endl; std::cout << "LITTLE ENDIAN: "<< test4.get_hex_string() << std::endl; EXPECT_EQ("2a7a69deadbeefcafebabedeadbeef", test3.get_hex_string()); EXPECT_EQ("2a697aefbeaddeefbeaddebebafeca", test4.get_hex_string()); } TEST_F(RABTest, Strings) { std::string a = "Dies ist ein Test."; test3.setBytes(0, std::vector(a.begin(), a.end())); EXPECT_EQ("Dies ist ein Test.", test3.get_string(0, test3.size())); EXPECT_EQ("44696573206973742065696e20546573742e", test3.get_hex_string()); } TEST_F(RABTest, Bits) { bool bit = true; test3.setBit(4, bit); EXPECT_EQ(0x08, test3.getByte(0)); uint8_t bits = 0b101; test3.setBits(3, 3, bits); EXPECT_EQ(0x14, test3.getByte(0)); } TEST_F(RABTest, BitVectors) { test3.set_uint64(0, 0xdeadbeefcafebabe); auto vec = test3.getBits(18, 20); std::stringstream stream; for( auto it = vec.begin(); it != vec.end(); ++it) stream << (*it); EXPECT_EQ("11111011101111110010", stream.str()); test4.set_uint64(0, 0xdeadbeefcafebabe); auto vec2 = test4.getBits(18, 20); std::stringstream stream2; for( auto it = vec2.begin(); it != vec2.end(); ++it) stream2 << (*it); EXPECT_EQ("11111011001010111011", stream2.str()); }