From a942dfd4ab41c754bb4decf61e38c403ede79d8a Mon Sep 17 00:00:00 2001 From: Casey Bodley Date: Fri, 3 Nov 2017 16:25:24 -0400 Subject: [PATCH] test/common: extend str_list tests to include set Signed-off-by: Casey Bodley --- src/test/test_str_list.cc | 82 +++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 43 deletions(-) diff --git a/src/test/test_str_list.cc b/src/test/test_str_list.cc index 30a3937c8ad9f..9c8e5355a2a2e 100644 --- a/src/test/test_str_list.cc +++ b/src/test/test_str_list.cc @@ -1,54 +1,50 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab -#include "include/types.h" #include "include/str_list.h" -#include -#include -#include - #include "gtest/gtest.h" +// SplitTest is parameterized for list/vector/set +using Types = ::testing::Types, + std::vector, + std::set>; -const char *tests[][10] = { - { "foo,bar", "foo", "bar", 0 }, - { "foo", "foo", 0 }, - { "foo;bar", "foo", "bar", 0 }, - { "foo bar", "foo", "bar", 0 }, - { " foo bar", "foo", "bar", 0 }, - { " foo bar ", "foo", "bar", 0 }, - { "a,b,c", "a", "b", "c", 0 }, - { " a\tb\tc\t", "a", "b", "c", 0 }, - { "a, b, c", "a", "b", "c", 0 }, - { "a b c", "a", "b", "c", 0 }, - { "a=b=c", "a", "b", "c", 0 }, - { 0 }, +template +struct SplitTest : ::testing::Test { + void test(const char* input, const char *delim, + const std::list& expected) { + EXPECT_EQ(expected, get_str_list(input, delim)); + } + void test(const char* input, const char *delim, + const std::vector& expected) { + EXPECT_EQ(expected, get_str_vec(input, delim)); + } + void test(const char* input, const char *delim, + const std::set& expected) { + EXPECT_EQ(expected, get_str_set(input, delim)); + } }; -TEST(StrList, get_str_list) -{ - for (unsigned i=0; tests[i][0]; ++i) { - std::string src = tests[i][0]; - std::list expected; - for (unsigned j=1; tests[i][j]; ++j) - expected.push_back(tests[i][j]); - std::list actual; - get_str_list(src, actual); - std::cout << "'" << src << "' -> " << actual << std::endl; - ASSERT_EQ(actual, expected); - } -} +TYPED_TEST_CASE(SplitTest, Types); -TEST(StrList, get_str_vec) +TYPED_TEST(SplitTest, Get) { - for (unsigned i=0; tests[i][0]; ++i) { - std::string src = tests[i][0]; - std::vector expected; - for (unsigned j=1; tests[i][j]; ++j) - expected.push_back(tests[i][j]); - std::vector actual; - get_str_vec (src, actual); - std::cout << "'" << src << "' -> " << actual << std::endl; - ASSERT_EQ(actual, expected); - } - + this->test("", " ", TypeParam{}); + this->test(" ", " ", TypeParam{}); + this->test("foo", " ", TypeParam{"foo"}); + this->test("foo bar", " ", TypeParam{"foo","bar"}); + this->test(" foo bar", " ", TypeParam{"foo","bar"}); + this->test("foo bar ", " ", TypeParam{"foo","bar"}); + this->test("foo bar ", " ", TypeParam{"foo","bar"}); + + // default delimiter + const char *delims = ";,= \t"; + this->test(" ; , = \t ", delims, TypeParam{}); + this->test(" ; foo = \t ", delims, TypeParam{"foo"}); + this->test("a,b,c", delims, TypeParam{"a","b","c"}); + this->test("a\tb\tc\t", delims, TypeParam{"a","b","c"}); + this->test("a, b, c", delims, TypeParam{"a","b","c"}); + this->test("a b c", delims, TypeParam{"a","b","c"}); + this->test("a=b=c", delims, TypeParam{"a","b","c"}); } -- 2.39.5