Skip to content

Commit

Permalink
Add tests for SplitString
Browse files Browse the repository at this point in the history
  • Loading branch information
psx95 committed Oct 31, 2024
1 parent adc5fff commit 5b8eeb3
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions api/test/trace/propagation/detail/string_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,57 @@
// SPDX-License-Identifier: Apache-2.0

#include <gtest/gtest.h>
#include <stdint.h>
#include <opentelemetry/trace/propagation/b3_propagator.h>
#include <string>

#include "opentelemetry/nostd/string_view.h"

TEST(StringTest, SplitString)
struct SplitStringTestData
{
// TODO(psx95): Add tests
}
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 fixture
class SplitStringTestFixture : public ::testing::TestWithParam<SplitStringTestData> {};

TEST_P(SplitStringTestFixture, SplitsAsExpected)
{
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);
for (size_t i = 0; i < got_splits_num; i++)
{
// Checks for resulting strings in-order
EXPECT_EQ(fields[i], test_param.splits[i]);
}
}

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

0 comments on commit 5b8eeb3

Please sign in to comment.