File: test_string_view.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 (43 lines) | stat: -rw-r--r-- 1,041 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
36
37
38
39
40
41
42
43
/**
 * Copyright 2021-2023 by XGBoost Contributors
 */
#include <gtest/gtest.h>
#include <xgboost/string_view.h>

#include <algorithm>  // std::equal
#include <sstream>    // std::stringstream
#include <string>     // std::string

namespace xgboost {
TEST(StringView, Basic) {
  StringView str{"This is a string."};
  std::stringstream ss;
  ss << str;

  std::string res = ss.str();
  ASSERT_EQ(str.size(), res.size());
  ASSERT_TRUE(std::equal(res.cbegin(), res.cend(), str.cbegin()));

  auto substr = str.substr(5, 2);
  ASSERT_EQ(substr.size(), 2);

  ASSERT_EQ(StringView{"is"}.size(), 2);
  ASSERT_TRUE(substr == "is");
  ASSERT_FALSE(substr != "is");
  ASSERT_FALSE(substr == "foobar");
  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