File: string_test.cc

package info (click to toggle)
opentelemetry-cpp 1.23.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,368 kB
  • sloc: cpp: 96,239; sh: 1,766; makefile: 38; python: 31
file content (64 lines) | stat: -rw-r--r-- 2,150 bytes parent folder | download | duplicates (3)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <gtest/gtest.h>
#include <stddef.h>
#include <ostream>
#include <vector>

#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/trace/propagation/detail/string.h"

using namespace opentelemetry;

namespace
{

struct SplitStringTestData
{
  opentelemetry::nostd::string_view input;
  char separator;
  size_t max_count;
  size_t expected_number_strings;

  // When googletest registers parameterized tests, it uses this method to format the parameters.
  // The default implementation prints hex dump of all bytes in the object. If there is any padding
  // in these bytes, valgrind reports this as a warning - "Use of uninitialized bytes".
  // See https://github.com/google/googletest/issues/3805.
  friend void PrintTo(const SplitStringTestData &data, std::ostream *os)
  {
    *os << "(" << data.input << "," << data.separator << "," << data.max_count << ","
        << data.expected_number_strings << ")";
  }
};

const SplitStringTestData split_string_test_cases[] = {
    {"foo,bar,baz", ',', 4, 3},
    {"foo,bar,baz,foobar", ',', 4, 4},
    {"foo,bar,baz,foobar", '.', 4, 1},
    {"foo,bar,baz,", ',', 4, 4},
    {"foo,bar,baz,", ',', 2, 2},
    {"foo ,bar, baz ", ',', 4, 3},
    {"foo ,bar, baz ", ',', 4, 3},
    {"00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01", '-', 4, 4},
};
}  // namespace

// Test fixture
class SplitStringTestFixture : public ::testing::TestWithParam<SplitStringTestData>
{};

TEST_P(SplitStringTestFixture, SplitsAsExpected)
{
  const SplitStringTestData test_param = GetParam();
  std::vector<opentelemetry::nostd::string_view> fields(test_param.expected_number_strings);
  size_t got_splits_num = opentelemetry::trace::propagation::detail::SplitString(
      test_param.input, test_param.separator, fields.data(), test_param.max_count);

  // Assert on the output
  EXPECT_EQ(got_splits_num, test_param.expected_number_strings);
}

INSTANTIATE_TEST_SUITE_P(SplitStringTestCases,
                         SplitStringTestFixture,
                         ::testing::ValuesIn(split_string_test_cases));