Skip to content

Instantly share code, notes, and snippets.

@motonacciu
Last active December 17, 2015 11:39
Show Gist options
  • Select an option

  • Save motonacciu/5603554 to your computer and use it in GitHub Desktop.

Select an option

Save motonacciu/5603554 to your computer and use it in GitHub Desktop.
TEST(Deserialize, Ints) {
int v1 = deserialize<uint32_t>({0xA, 0, 0, 0});
EXPECT_EQ(v1, 10);
auto v2 = deserialize<uint64_t>({0x40, 0, 0, 0, 0, 0, 0, 0});
EXPECT_EQ(v2, 64u);
auto v3 = deserialize<int>({0xFF, 0xFF, 0xFF, 0xFF});
EXPECT_EQ(v3, -1);
}
TEST(Deserialize, Vector) {
auto v1 = deserialize<std::vector<int>>({2,0,0,0,0,0,0,0,1,0,0,0,2,0,0,0});
EXPECT_EQ(v1, std::vector<int>({1,2}));
auto v2 = deserialize<std::vector<std::vector<uint8_t>>>(
{2, 0, 0, 0, 0, 0, 0, 0, /* size */
2, 0, 0, 0, 0, 0, 0, 0, /* size */ 1, 2,
2, 0, 0, 0, 0, 0, 0, 0, /* size */ 3, 4
});
EXPECT_EQ(v2, std::vector<std::vector<uint8_t>>({{1,2},{3,4}}));
}
TEST(Deserialize, IntTuple) {
auto t1 = deserialize<std::tuple<int,int>>({1, 0, 0, 0, 2, 0, 0, 0});
EXPECT_EQ(t1, std::make_tuple(1,2));
auto t2 = deserialize<std::tuple<int,int,char>>({1, 0, 0, 0, 2, 0, 0, 0, 3});
EXPECT_EQ(t2, std::make_tuple(1,2,3));
}
TEST(Deserialize, TupleVec) {
auto t = deserialize<std::tuple<int,int,std::vector<uint8_t>>>(
{
10, 0, 0, 0, /* get<0> */
20, 0, 0, 0, /* get<1> */
2, 0, 0, 0, 0, /* size */ 0, 0, 0, 1, 2 /* get<2> */
});
EXPECT_EQ(t, std::make_tuple(10,20,std::vector<uint8_t>({1,2})));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment