Extract make metric name from ranking metric. (#8768)

- Extract the metric parsing routine from ranking.
- Add a test.
- Accept null for string view.
This commit is contained in:
Jiaming Yuan
2023-02-09 18:30:21 +08:00
committed by GitHub
parent 4ead65a28c
commit 5f76edd296
9 changed files with 142 additions and 34 deletions

View File

@@ -0,0 +1,38 @@
/**
* Copyright 2023 by XGBoost Contributors
*/
#include <gtest/gtest.h>
#include <cstdint> // std::uint32_t
#include "../../../src/common/ranking_utils.h"
namespace xgboost {
namespace ltr {
TEST(RankingUtils, MakeMetricName) {
std::uint32_t topn{32};
bool minus{false};
auto name = MakeMetricName("ndcg", "3-", &topn, &minus);
ASSERT_EQ(name, "ndcg@3-");
ASSERT_EQ(topn, 3);
ASSERT_TRUE(minus);
name = MakeMetricName("ndcg", "6", &topn, &minus);
ASSERT_EQ(topn, 6);
ASSERT_TRUE(minus); // unchanged
minus = false;
name = MakeMetricName("ndcg", "-", &topn, &minus);
ASSERT_EQ(topn, 6); // unchanged
ASSERT_TRUE(minus);
name = MakeMetricName("ndcg", nullptr, &topn, &minus);
ASSERT_EQ(topn, 6); // unchanged
ASSERT_TRUE(minus); // unchanged
name = MakeMetricName("ndcg", StringView{}, &topn, &minus);
ASSERT_EQ(topn, 6); // unchanged
ASSERT_TRUE(minus); // unchanged
}
} // namespace ltr
} // namespace xgboost

View File

@@ -1,3 +1,6 @@
/**
* Copyright 2021 by XGBoost Contributors
*/
#include <gtest/gtest.h>
#include "../../../src/common/ranking_utils.cuh"
#include "../../../src/common/device_helpers.cuh"

View File

@@ -1,9 +1,13 @@
/*!
* Copyright (c) by XGBoost Contributors 2021
/**
* Copyright 2021-2023 by XGBoost Contributors
*/
#include <gtest/gtest.h>
#include <xgboost/string_view.h>
#include <string_view>
#include <algorithm> // std::equal
#include <sstream> // std::stringstream
#include <string> // std::string
namespace xgboost {
TEST(StringView, Basic) {
StringView str{"This is a string."};
@@ -24,5 +28,16 @@ TEST(StringView, Basic) {
ASSERT_FALSE(substr == "i");
ASSERT_TRUE(std::equal(substr.crbegin(), substr.crend(), StringView{"si"}.cbegin()));
{
StringView empty{nullptr};
ASSERT_TRUE(empty.empty());
}
{
StringView empty{""};
ASSERT_TRUE(empty.empty());
StringView empty2{nullptr};
ASSERT_EQ(empty, empty2);
}
}
} // namespace xgboost