string_test.cc 2.1 KB

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