Skip to content

Commit

Permalink
Remove vector from testcase struct
Browse files Browse the repository at this point in the history
  • Loading branch information
psx95 committed Nov 1, 2024
1 parent e7cfb9a commit 06283f7
Showing 1 changed file with 47 additions and 74 deletions.
121 changes: 47 additions & 74 deletions api/test/trace/propagation/detail/string_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,84 +8,57 @@
#include "opentelemetry/nostd/string_view.h"

using namespace opentelemetry;
//
// struct SplitStringTestData
//{
// opentelemetry::nostd::string_view input;
// char separator;
// size_t max_count;
// std::vector<opentelemetry::nostd::string_view> splits;
// size_t expected_number_strings;
// };
//
// const SplitStringTestData split_string_test_cases[] = {
// {"foo,bar,baz", ',', 4, std::vector<opentelemetry::nostd::string_view>{"foo", "bar", "baz"},
// 3},
// {"foo,bar,baz,foobar", ',', 4,
// std::vector<opentelemetry::nostd::string_view>{"foo", "bar", "baz", "foobar"}, 4},
// {"foo,bar,baz,foobar", '.', 4,
// std::vector<opentelemetry::nostd::string_view>{"foo,bar,baz,foobar"}, 1},
// {"foo,bar,baz,", ',', 4,
// std::vector<opentelemetry::nostd::string_view>{"foo", "bar", "baz", ""}, 4},
// {"foo,bar,baz,", ',', 2, std::vector<opentelemetry::nostd::string_view>{"foo", "bar"}, 2},
// {"foo ,bar, baz ", ',', 4,
// std::vector<opentelemetry::nostd::string_view>{"foo ", "bar", " baz "}, 3},
// {"foo ,bar, baz ", ',', 4,
// std::vector<opentelemetry::nostd::string_view>{"foo ", "bar", " baz "}, 3},
// {"00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01", '-', 4,
// std::vector<opentelemetry::nostd::string_view>{"00", "0af7651916cd43dd8448eb211c80319c",
// "00f067aa0ba902b7", "01"},
// 4},
// };

TEST(StringTest, SplitStringTest)
namespace
{
struct
{
opentelemetry::nostd::string_view input;
char separator;
size_t max_count;
std::vector<opentelemetry::nostd::string_view> splits;
size_t expected_number_strings;
} test_cases[] = {
{"foo,bar,baz", ',', 4, std::vector<opentelemetry::nostd::string_view>{"foo", "bar", "baz"},
3},
// {"foo,bar,baz,foobar", ',', 4,
// std::vector<opentelemetry::nostd::string_view>{"foo", "bar", "baz", "foobar"}, 4},
// {"foo,bar,baz,foobar", '.', 4,
// std::vector<opentelemetry::nostd::string_view>{"foo,bar,baz,foobar"}, 1},
// {"foo,bar,baz,", ',', 4,
// std::vector<opentelemetry::nostd::string_view>{"foo", "bar", "baz", ""}, 4},
// {"foo,bar,baz,", ',', 2, std::vector<opentelemetry::nostd::string_view>{"foo", "bar"}, 2},
// {"foo ,bar, baz ", ',', 4,
// std::vector<opentelemetry::nostd::string_view>{"foo ", "bar", " baz "}, 3},
// {"foo ,bar, baz ", ',', 4,
// std::vector<opentelemetry::nostd::string_view>{"foo ", "bar", " baz "}, 3},
// {"00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01", '-', 4,
// std::vector<opentelemetry::nostd::string_view>{"00", "0af7651916cd43dd8448eb211c80319c",
// "00f067aa0ba902b7", "01"},4}
};
for (auto &test_param : test_cases)
{
std::vector<opentelemetry::nostd::string_view> fields{};
fields.reserve(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);
for (size_t i = 0; i < got_splits_num; i++)
{
// Checks for resulting strings in-order
EXPECT_EQ(fields[i], test_param.splits[i]);
}
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)
{
std::stringstream ss;
*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(StringTest, SimpleTest)
TEST_P(SplitStringTestFixture, SplitsAsExpected)
{
nostd::string_view input = "foo,bar,baz";
std::array<nostd::string_view, 4> fields{};
size_t got_splits = ::trace::propagation::detail::SplitString(input, ',', fields.data(), 4);
EXPECT_EQ(3, got_splits);
const SplitStringTestData test_param = GetParam();
std::vector<opentelemetry::nostd::string_view> fields{};
fields.reserve(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));

0 comments on commit 06283f7

Please sign in to comment.