|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#include <gtest/gtest.h> |
| 5 | +#include <opentelemetry/trace/propagation/b3_propagator.h> |
| 6 | +#include <string> |
| 7 | + |
| 8 | +#include "opentelemetry/nostd/string_view.h" |
| 9 | + |
| 10 | +using namespace opentelemetry; |
| 11 | + |
| 12 | +namespace |
| 13 | +{ |
| 14 | + |
| 15 | +struct SplitStringTestData |
| 16 | +{ |
| 17 | + opentelemetry::nostd::string_view input; |
| 18 | + char separator; |
| 19 | + size_t max_count; |
| 20 | + size_t expected_number_strings; |
| 21 | + |
| 22 | + // When googletest registers parameterized tests, it uses this method to format the parameters. |
| 23 | + // The default implementation prints hex dump of all bytes in the object. If there is any padding |
| 24 | + // in these bytes, valgrind reports this as a warning - "Use of uninitialized bytes". |
| 25 | + // See https://github.com/google/googletest/issues/3805. |
| 26 | + friend void PrintTo(const SplitStringTestData &data, std::ostream *os) |
| 27 | + { |
| 28 | + *os << "(" << data.input << "," << data.separator << "," << data.max_count << "," |
| 29 | + << data.expected_number_strings << ")"; |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +const SplitStringTestData split_string_test_cases[] = { |
| 34 | + {"foo,bar,baz", ',', 4, 3}, |
| 35 | + {"foo,bar,baz,foobar", ',', 4, 4}, |
| 36 | + {"foo,bar,baz,foobar", '.', 4, 1}, |
| 37 | + {"foo,bar,baz,", ',', 4, 4}, |
| 38 | + {"foo,bar,baz,", ',', 2, 2}, |
| 39 | + {"foo ,bar, baz ", ',', 4, 3}, |
| 40 | + {"foo ,bar, baz ", ',', 4, 3}, |
| 41 | + {"00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01", '-', 4, 4}, |
| 42 | +}; |
| 43 | +} // namespace |
| 44 | + |
| 45 | +// Test fixture |
| 46 | +class SplitStringTestFixture : public ::testing::TestWithParam<SplitStringTestData> |
| 47 | +{}; |
| 48 | + |
| 49 | +TEST_P(SplitStringTestFixture, SplitsAsExpected) |
| 50 | +{ |
| 51 | + const SplitStringTestData test_param = GetParam(); |
| 52 | + std::vector<opentelemetry::nostd::string_view> fields(test_param.expected_number_strings); |
| 53 | + size_t got_splits_num = opentelemetry::trace::propagation::detail::SplitString( |
| 54 | + test_param.input, test_param.separator, fields.data(), test_param.max_count); |
| 55 | + |
| 56 | + // Assert on the output |
| 57 | + EXPECT_EQ(got_splits_num, test_param.expected_number_strings); |
| 58 | +} |
| 59 | + |
| 60 | +INSTANTIATE_TEST_SUITE_P(SplitStringTestCases, |
| 61 | + SplitStringTestFixture, |
| 62 | + ::testing::ValuesIn(split_string_test_cases)); |
0 commit comments