File: test_algorithm.cc

package info (click to toggle)
xgboost 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 13,796 kB
  • sloc: cpp: 67,502; python: 35,503; java: 4,676; ansic: 1,426; sh: 1,320; xml: 1,197; makefile: 204; javascript: 19
file content (35 lines) | stat: -rw-r--r-- 945 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
 * Copyright 2020-2023 by XGBoost Contributors
 */
#include <gtest/gtest.h>
#include <xgboost/context.h>  // Context
#include <xgboost/span.h>

#include <algorithm>  // is_sorted

#include "../../../src/common/algorithm.h"

namespace xgboost {
namespace common {
TEST(Algorithm, ArgSort) {
  Context ctx;
  std::vector<float> inputs{3.0, 2.0, 1.0};
  auto ret = ArgSort<bst_feature_t>(&ctx, inputs.cbegin(), inputs.cend());
  std::vector<bst_feature_t> sol{2, 1, 0};
  ASSERT_EQ(ret, sol);
}

TEST(Algorithm, Sort) {
  Context ctx;
  ctx.Init(Args{{"nthread", "8"}});
  std::vector<float> inputs{3.0, 1.0, 2.0};

  Sort(&ctx, inputs.begin(), inputs.end(), std::less<>{});
  ASSERT_TRUE(std::is_sorted(inputs.cbegin(), inputs.cend()));

  inputs = {3.0, 1.0, 2.0};
  StableSort(&ctx, inputs.begin(), inputs.end(), std::less<>{});
  ASSERT_TRUE(std::is_sorted(inputs.cbegin(), inputs.cend()));
}
}  // namespace common
}  // namespace xgboost