88#include " opentelemetry/nostd/string_view.h"
99
1010using namespace opentelemetry ;
11- //
12- // struct SplitStringTestData
13- // {
14- // opentelemetry::nostd::string_view input;
15- // char separator;
16- // size_t max_count;
17- // std::vector<opentelemetry::nostd::string_view> splits;
18- // size_t expected_number_strings;
19- // };
20- //
21- // const SplitStringTestData split_string_test_cases[] = {
22- // {"foo,bar,baz", ',', 4, std::vector<opentelemetry::nostd::string_view>{"foo", "bar", "baz"},
23- // 3},
24- // {"foo,bar,baz,foobar", ',', 4,
25- // std::vector<opentelemetry::nostd::string_view>{"foo", "bar", "baz", "foobar"}, 4},
26- // {"foo,bar,baz,foobar", '.', 4,
27- // std::vector<opentelemetry::nostd::string_view>{"foo,bar,baz,foobar"}, 1},
28- // {"foo,bar,baz,", ',', 4,
29- // std::vector<opentelemetry::nostd::string_view>{"foo", "bar", "baz", ""}, 4},
30- // {"foo,bar,baz,", ',', 2, std::vector<opentelemetry::nostd::string_view>{"foo", "bar"}, 2},
31- // {"foo ,bar, baz ", ',', 4,
32- // std::vector<opentelemetry::nostd::string_view>{"foo ", "bar", " baz "}, 3},
33- // {"foo ,bar, baz ", ',', 4,
34- // std::vector<opentelemetry::nostd::string_view>{"foo ", "bar", " baz "}, 3},
35- // {"00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01", '-', 4,
36- // std::vector<opentelemetry::nostd::string_view>{"00", "0af7651916cd43dd8448eb211c80319c",
37- // "00f067aa0ba902b7", "01"},
38- // 4},
39- // };
4011
41- TEST (StringTest, SplitStringTest)
12+ namespace
4213{
43- struct
44- {
45- opentelemetry::nostd::string_view input;
46- char separator;
47- size_t max_count;
48- std::vector<opentelemetry::nostd::string_view> splits;
49- size_t expected_number_strings;
50- } test_cases[] = {
51- {" foo,bar,baz" , ' ,' , 4 , std::vector<opentelemetry::nostd::string_view>{" foo" , " bar" , " baz" },
52- 3 },
53- // {"foo,bar,baz,foobar", ',', 4,
54- // std::vector<opentelemetry::nostd::string_view>{"foo", "bar", "baz", "foobar"}, 4},
55- // {"foo,bar,baz,foobar", '.', 4,
56- // std::vector<opentelemetry::nostd::string_view>{"foo,bar,baz,foobar"}, 1},
57- // {"foo,bar,baz,", ',', 4,
58- // std::vector<opentelemetry::nostd::string_view>{"foo", "bar", "baz", ""}, 4},
59- // {"foo,bar,baz,", ',', 2, std::vector<opentelemetry::nostd::string_view>{"foo", "bar"}, 2},
60- // {"foo ,bar, baz ", ',', 4,
61- // std::vector<opentelemetry::nostd::string_view>{"foo ", "bar", " baz "}, 3},
62- // {"foo ,bar, baz ", ',', 4,
63- // std::vector<opentelemetry::nostd::string_view>{"foo ", "bar", " baz "}, 3},
64- // {"00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01", '-', 4,
65- // std::vector<opentelemetry::nostd::string_view>{"00", "0af7651916cd43dd8448eb211c80319c",
66- // "00f067aa0ba902b7", "01"},4}
67- };
68- for (auto &test_param : test_cases)
69- {
70- std::vector<opentelemetry::nostd::string_view> fields{};
71- fields.reserve (test_param.expected_number_strings );
72- size_t got_splits_num = opentelemetry::trace::propagation::detail::SplitString (
73- test_param.input , test_param.separator , fields.data (), test_param.max_count );
7414
75- // Assert on the output
76- EXPECT_EQ (got_splits_num, test_param.expected_number_strings );
77- for (size_t i = 0 ; i < got_splits_num; i++)
15+ struct SplitStringTestData
16+ {
17+ opentelemetry::nostd::string_view input;
18+ char separator;
19+ size_t max_count;
20+ std::vector<opentelemetry::nostd::string_view> splits;
21+ size_t expected_number_strings;
22+
23+ SplitStringTestData (const opentelemetry::nostd::string_view &input,
24+ char separator,
25+ size_t maxCount,
26+ const std::vector<opentelemetry::nostd::string_view> &splits,
27+ size_t expectedNumberStrings)
28+ : input(input),
29+ separator (separator),
30+ max_count(maxCount),
31+ splits(splits),
32+ expected_number_strings(expectedNumberStrings)
33+ {}
34+
35+ // When googletest registers parameterized tests, it uses this method to format the parameters.
36+ // The default implementation prints hex dump of all bytes in the object. If there is any padding
37+ // in these bytes, valgrind reports this as a warning - "Use of uninitialized bytes".
38+ // See https://github.com/google/googletest/issues/3805.
39+ friend void PrintTo (const SplitStringTestData &data, std::ostream *os)
40+ {
41+ std::stringstream ss;
42+ for (auto it = data.splits .begin (); it != data.splits .end (); it++)
7843 {
79- // Checks for resulting strings in-order
80- EXPECT_EQ (fields[i], test_param.splits [i]);
44+ if (it != data.splits .begin ())
45+ {
46+ ss << " ," ;
47+ }
48+ ss << *it;
8149 }
50+ *os << " (" << data.input << " ," << data.separator << " ," << data.max_count << " ," << ss.str ()
51+ << " ," << data.expected_number_strings << " )" ;
8252 }
83- }
53+ };
8454
85- TEST (StringTest, SimpleTest)
55+ const SplitStringTestData *split_string_test_cases[2 ] = {
56+ new SplitStringTestData (" foo,bar,baz" ,
57+ ' ,' ,
58+ 4 ,
59+ std::vector<opentelemetry::nostd::string_view>{" foo" , " bar" , " baz" },
60+ 3 ),
61+ new SplitStringTestData (
62+ " foo,bar,baz,foobar" ,
63+ ' ,' ,
64+ 4 ,
65+ std::vector<opentelemetry::nostd::string_view>{" foo" , " bar" , " baz" , " foobar" },
66+ 4 ),
67+ // {"foo,bar,baz,foobar", '.', 4,
68+ // std::vector<opentelemetry::nostd::string_view>{"foo,bar,baz,foobar"}, 1},
69+ // {"foo,bar,baz,", ',', 4,
70+ // std::vector<opentelemetry::nostd::string_view>{"foo", "bar", "baz", ""}, 4},
71+ // {"foo,bar,baz,", ',', 2, std::vector<opentelemetry::nostd::string_view>{"foo", "bar"}, 2},
72+ // {"foo ,bar, baz ", ',', 4,
73+ // std::vector<opentelemetry::nostd::string_view>{"foo ", "bar", " baz "}, 3},
74+ // {"foo ,bar, baz ", ',', 4,
75+ // std::vector<opentelemetry::nostd::string_view>{"foo ", "bar", " baz "}, 3},
76+ // {"00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01", '-', 4,
77+ // std::vector<opentelemetry::nostd::string_view>{"00", "0af7651916cd43dd8448eb211c80319c",
78+ // "00f067aa0ba902b7", "01"},
79+ // 4},
80+ };
81+ } // namespace
82+
83+ // Test fixture
84+ class SplitStringTestFixture : public ::testing::TestWithParam<const SplitStringTestData *>
85+ {};
86+
87+ TEST_P (SplitStringTestFixture, SplitsAsExpected)
8688{
87- nostd::string_view input = " foo,bar,baz" ;
88- std::array<nostd::string_view, 4 > fields{};
89- size_t got_splits = ::trace::propagation::detail::SplitString (input, ' ,' , fields.data (), 4 );
90- EXPECT_EQ (3 , got_splits);
89+ const SplitStringTestData *test_param = GetParam ();
90+ std::vector<opentelemetry::nostd::string_view> fields{};
91+ fields.reserve (test_param->expected_number_strings );
92+ size_t got_splits_num = opentelemetry::trace::propagation::detail::SplitString (
93+ test_param->input , test_param->separator , fields.data (), test_param->max_count );
94+
95+ // Assert on the output
96+ EXPECT_EQ (got_splits_num, test_param->expected_number_strings );
97+ for (size_t i = 0 ; i < got_splits_num; i++)
98+ {
99+ // Checks for resulting strings in-order
100+ EXPECT_EQ (fields[i], test_param->splits [i]);
101+ }
91102}
103+
104+ INSTANTIATE_TEST_SUITE_P (SplitStringTestCases,
105+ SplitStringTestFixture,
106+ ::testing::ValuesIn (split_string_test_cases));
0 commit comments