// Copyright by Contributors #include #include #include #include #include #include #include TEST(Any, basics) { std::unordered_map dict; dict["1"] = 1; dict["vec"] = std::vector{1,2,3}; dict["shapex"] = std::string("xtyz"); std::unordered_map dict2(std::move(dict)); dmlc::get(dict2["1"]) += 1; CHECK_EQ(dmlc::get(dict2["1"]), 2); CHECK_EQ(dmlc::get >(dict2["vec"])[1], 2); } TEST(Any, cover) { dmlc::any a = std::string("abc"); dmlc::any b = 1; CHECK_EQ(dmlc::get(a), "abc"); a = std::move(b); CHECK(b.empty()); CHECK_EQ(dmlc::get(a), 1); std::shared_ptr x = std::make_shared(10); { dmlc::any aa(x); CHECK_EQ(*dmlc::get >(aa), 10); } // aa must be destructed. CHECK(x.unique()); } DMLC_JSON_ENABLE_ANY(std::vector, IntVector); DMLC_JSON_ENABLE_ANY(int, Int); TEST(Any, json) { std::unordered_map x; x["vec"] = std::vector{1, 2, 3}; x["int"] = 300; #ifndef _LIBCPP_SGX_NO_IOSTREAMS std::ostringstream os; #else std::string os; #endif { std::unordered_map temp(x); dmlc::JSONWriter writer(&os); writer.Write(temp); temp.clear(); } #ifndef _LIBCPP_SGX_NO_IOSTREAMS std::string json = os.str(); std::istringstream is(json); #else std::string json = os; std::string is(json); #endif LOG(INFO) << json; dmlc::JSONReader reader(&is); std::unordered_map copy_data; reader.Read(©_data); ASSERT_EQ(dmlc::get >(x["vec"]), dmlc::get >(copy_data["vec"])); ASSERT_EQ(dmlc::get(x["int"]), dmlc::get(copy_data["int"])); }