Created
May 17, 2013 05:21
-
-
Save motonacciu/5597099 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| TEST(Serialize, Ints) { | |
| uint32_t v1 = 10; | |
| StreamType res; | |
| serialize(v1,res); | |
| EXPECT_EQ(sizeof(uint32_t), res.size()); | |
| EXPECT_EQ(res, std::vector<uint8_t>({0xA, 0, 0, 0})); | |
| res.clear(); | |
| uint64_t v2 = 64; | |
| serialize(v2,res); | |
| EXPECT_EQ(sizeof(uint64_t), res.size()); | |
| EXPECT_EQ(res, std::vector<uint8_t>({0x40, 0, 0, 0, 0, 0, 0, 0})); | |
| res.clear(); | |
| int v3 = -1; | |
| serialize(v3,res); | |
| EXPECT_EQ(sizeof(int), res.size()); | |
| EXPECT_EQ(res, std::vector<uint8_t>({0xFF, 0xFF, 0xFF, 0xFF})); | |
| } | |
| TEST(Serialize, Vector) { | |
| auto t1 = std::vector<int>{1,2}; | |
| StreamType res; | |
| serialize(t1,res); | |
| EXPECT_EQ(sizeof(decltype(t1)::value_type)*t1.size()+sizeof(size_t), res.size()); | |
| EXPECT_EQ(res, std::vector<uint8_t>({/*size(*/2, 0, 0, 0, 0, 0, 0, 0,/*)*/ 1, 0, 0, 0, 2, 0, 0, 0})); | |
| res.clear(); | |
| auto t2 = std::vector<std::vector<uint8_t>>{{1,2}, {3,4}}; | |
| serialize(t2,res); | |
| EXPECT_EQ(get_size(t2), res.size()); | |
| EXPECT_EQ(28u, res.size()); | |
| EXPECT_EQ(res, std::vector<uint8_t>( | |
| {/*size(*/2, 0, 0, 0, 0, 0, 0, 0, /*) size(*/ 2, 0, 0, 0, 0, 0, 0, 0, /*)*/ 1, 2, | |
| /*size(*/2, 0, 0, 0, 0, 0, 0, 0, /*)*/ 3, 4 })); | |
| } | |
| TEST(Serialize, IntTuple) { | |
| auto t1 = std::make_tuple(1,2); | |
| StreamType res; | |
| serialize(t1,res); | |
| EXPECT_EQ(sizeof(decltype(t1)), res.size()); | |
| EXPECT_EQ(res, std::vector<uint8_t>({1, 0, 0, 0, 2, 0, 0, 0})); | |
| res.clear(); | |
| auto t2 = std::make_tuple(256,256*2,256*3); | |
| serialize(t2,res); | |
| EXPECT_EQ(sizeof(decltype(t2)), res.size()); | |
| EXPECT_EQ(res, std::vector<uint8_t>({0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0})); | |
| res.clear(); | |
| auto t3 = std::tuple<boost::uint32_t, boost::uint64_t>(0,1); | |
| serialize(t3,res); | |
| EXPECT_EQ(get_size(t3), res.size()); | |
| EXPECT_EQ(res, std::vector<uint8_t>({0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0})); | |
| } | |
| TEST(Serialize, TupleVec) { | |
| auto t1 = std::tuple<int,int,std::vector<uint8_t>>(10, 20, std::vector<uint8_t>{1,2}); | |
| StreamType res; | |
| serialize(t1,res); | |
| EXPECT_EQ(18u, res.size()); | |
| EXPECT_EQ(res, std::vector<uint8_t>({ | |
| /*get<0>*/ 10, 0, 0, 0, | |
| /*get<1>*/ 20, 0, 0, 0, | |
| /*size(*/ 2, 0, 0, 0, 0, 0, 0, 0,/*)*/ 1, 2})); | |
| } | |
| TEST(Serialize, String) { | |
| std::string s = "string"; | |
| StreamType res; | |
| serialize(s,res); | |
| EXPECT_EQ(14u, get_size(s)); | |
| EXPECT_EQ(14u, res.size()); | |
| EXPECT_EQ(res, std::vector<uint8_t>({/*size(*/6, 0, 0, 0, 0, 0, 0, 0,/*)*/ 's', 't', 'r', 'i', 'n', 'g'})); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment