1 // Copyright by Contributors
2 #include <gtest/gtest.h>
3 #include <xgboost/objective.h>
4 #include <xgboost/generic_parameters.h>
5 
6 #include "../helpers.h"
7 
TEST(Objective,UnknownFunction)8 TEST(Objective, UnknownFunction) {
9   xgboost::ObjFunction* obj = nullptr;
10   xgboost::GenericParameter tparam;
11   std::vector<std::pair<std::string, std::string>> args;
12   tparam.UpdateAllowUnknown(args);
13 
14   EXPECT_ANY_THROW(obj = xgboost::ObjFunction::Create("unknown_name", &tparam));
15   EXPECT_NO_THROW(obj = xgboost::ObjFunction::Create("reg:squarederror", &tparam));
16   if (obj) {
17     delete obj;
18   }
19 }
20 
21 namespace xgboost {
TEST(Objective,PredTransform)22 TEST(Objective, PredTransform) {
23   // Test that show PredTransform uses the same device with predictor.
24   xgboost::GenericParameter tparam;
25   tparam.UpdateAllowUnknown(Args{{"gpu_id", "0"}});
26   size_t n = 100;
27 
28   for (const auto &entry :
29        ::dmlc::Registry<::xgboost::ObjFunctionReg>::List()) {
30     std::unique_ptr<xgboost::ObjFunction> obj{
31         xgboost::ObjFunction::Create(entry->name, &tparam)};
32     obj->Configure(Args{{"num_class", "2"}});
33     HostDeviceVector<float> predts;
34     predts.Resize(n, 3.14f);  // prediction is performed on host.
35     ASSERT_FALSE(predts.DeviceCanRead());
36     obj->PredTransform(&predts);
37     ASSERT_FALSE(predts.DeviceCanRead());
38     ASSERT_TRUE(predts.HostCanWrite());
39   }
40 }
41 } // namespace xgboost
42