// test for case where we use big endian serializer // this is a harder case #define DMLC_IO_USE_LITTLE_ENDIAN 0 #include #include #include #include #include #include #include #include using namespace std; template inline void TestSaveLoad(T data) { std::string blob; dmlc::MemoryStringStream fs(&blob); { T temp(data); static_cast(&fs)->Write(temp); temp.clear(); } fs.Seek(0); T copy_data; CHECK(static_cast(&fs)->Read(©_data)); ASSERT_EQ(data, copy_data); } class MyClass { public: MyClass() {} MyClass(std::string data) : data_(data) {} inline void Save(dmlc::Stream *strm) const { strm->Write(this->data_); } inline bool Load(dmlc::Stream *strm) { return strm->Read(&data_); } inline bool operator==(const MyClass &other) const { return data_ == other.data_; } private: std::string data_; }; // need to declare the traits property of my class to dmlc namespace dmlc { DMLC_DECLARE_TRAITS(has_saveload, MyClass, true); } // test serializer TEST(Serializer, basics) { int n = 10; std::vector a; for (int i = 0; i < n; ++i) { a.push_back(i); } TestSaveLoad(a); std::vector b; for (int i = 0; i < n; ++i) { std::string ss(i, 'a' + (i % 26)); b.push_back(ss); } TestSaveLoad(b); std::vector > temp {{1,2,3}, {1,2}, {1,2,3,4}}; TestSaveLoad(temp); TestSaveLoad( std::map {{1, "hellkow"}, {2, "world"}}); TestSaveLoad( std::unordered_map {{1, "hellkow"}, {2, "world"}}); TestSaveLoad( std::unordered_multimap {{1, "hellkow"}, {1, "world"}, {2, "111"}}); TestSaveLoad(std::set {"hjhjm", "asasa"}); TestSaveLoad(std::unordered_set {"hjhjm", "asasa"}); LOG(INFO) << "jere"; TestSaveLoad(std::list {"hjhjm", "asasa"}); TestSaveLoad(std::list(a.begin(), a.end())); TestSaveLoad(std::list {MyClass("abc"), MyClass("def")}); } // test serializer TEST(Serializer, endian) { int n = 10; std::string blob; dmlc::MemoryStringStream fs(&blob); dmlc::Stream* strm = &fs; strm->Write(n); // big endians if (DMLC_IO_USE_LITTLE_ENDIAN == 0) { ASSERT_EQ(blob[0], 0); ASSERT_EQ(blob[1], 0); ASSERT_EQ(blob[2], 0); ASSERT_EQ(blob[3], 10); } else { ASSERT_EQ(blob[0], 10); ASSERT_EQ(blob[1], 0); ASSERT_EQ(blob[2], 0); ASSERT_EQ(blob[3], 0); } }