From b6953eeaa186b96f40b8f06253164829643249c7 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 5 May 2016 20:05:56 +0200 Subject: [PATCH] Add unit test for tag splitting --- src/tag/split_tags.cpp | 1 + test/unit_cli/CMakeLists.txt | 2 + test/unit_cli/test_tag_splitting.cpp | 110 +++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 test/unit_cli/test_tag_splitting.cpp diff --git a/src/tag/split_tags.cpp b/src/tag/split_tags.cpp index 2a6ea19..71fa5c3 100644 --- a/src/tag/split_tags.cpp +++ b/src/tag/split_tags.cpp @@ -22,6 +22,7 @@ #include #include #include +#include namespace din { std::vector split_tags (const std::string& parCommaSeparatedList) { diff --git a/test/unit_cli/CMakeLists.txt b/test/unit_cli/CMakeLists.txt index 06cd9cf..da1cb19 100644 --- a/test/unit_cli/CMakeLists.txt +++ b/test/unit_cli/CMakeLists.txt @@ -4,6 +4,8 @@ add_executable(${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/src/main/damerau_levenshtein.c ${CMAKE_SOURCE_DIR}/src/main/utf8_ops.c test_damerau_levenshtein.cpp + ${CMAKE_SOURCE_DIR}/src/tag/split_tags.cpp + test_tag_splitting.cpp ) target_include_directories(${PROJECT_NAME} diff --git a/test/unit_cli/test_tag_splitting.cpp b/test/unit_cli/test_tag_splitting.cpp new file mode 100644 index 0000000..3964af3 --- /dev/null +++ b/test/unit_cli/test_tag_splitting.cpp @@ -0,0 +1,110 @@ +/* Copyright 2015, 2016, Michele Santullo + * This file is part of "dindexer". + * + * "dindexer" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "dindexer" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with "dindexer". If not, see . + */ + +#include +#include +#include +#include "tag/split_tags.hpp" + +TEST(cli_main, split_tags) { + using din::split_tags; + + { + const std::string test_tags = "tag1,tag2"; + const auto tags = split_tags(test_tags); + EXPECT_EQ(static_cast(2), tags.size()); + ASSERT_GE(static_cast(2), tags.size()); + EXPECT_EQ("tag1", tags[0]); + EXPECT_EQ("tag2", tags[1]); + } + + { + const std::string test_tags = " "; + const auto tags = split_tags(test_tags); + EXPECT_TRUE(tags.empty()); + } + + { + const std::string test_tags; + const auto tags = split_tags(test_tags); + EXPECT_TRUE(tags.empty()); + } + + { + const std::string test_tags = ",,,,,, ,,,, , , , "; + const auto tags = split_tags(test_tags); + EXPECT_TRUE(tags.empty()); + } + + { + const std::string test_tags = ",,,a, , "; + const auto tags = split_tags(test_tags); + EXPECT_EQ(static_cast(1), tags.size()); + ASSERT_GE(static_cast(1), tags.size()); + EXPECT_EQ("a", tags[0]); + } + + { + const std::string test_tags = ",,,test1,,,DuckZ , King_DuckZ , "; + const auto tags = split_tags(test_tags); + EXPECT_EQ(static_cast(3), tags.size()); + ASSERT_GE(static_cast(3), tags.size()); + EXPECT_EQ("test1", tags[0]); + EXPECT_EQ("DuckZ", tags[1]); + EXPECT_EQ("King_DuckZ", tags[2]); + } + + { + const std::string test_tags = "test1"; + const auto tags = split_tags(test_tags); + EXPECT_EQ(static_cast(1), tags.size()); + ASSERT_GE(static_cast(1), tags.size()); + EXPECT_EQ("test1", tags[0]); + } + + { + const std::string test_tags = " test1"; + const auto tags = split_tags(test_tags); + EXPECT_EQ(static_cast(1), tags.size()); + ASSERT_GE(static_cast(1), tags.size()); + EXPECT_EQ("test1", tags[0]); + } + + { + const std::string test_tags = "test1 "; + const auto tags = split_tags(test_tags); + EXPECT_EQ(static_cast(1), tags.size()); + ASSERT_GE(static_cast(1), tags.size()); + EXPECT_EQ("test1", tags[0]); + } + + { + const int count = 1500; + std::ostringstream oss; + for (int z = 0; z < count; ++z) { + oss << z << " , "; + } + const std::string data = oss.str(); + const auto tags = split_tags(data); + EXPECT_EQ(static_cast(count), tags.size()); + ASSERT_GE(static_cast(count), tags.size()); + for (int z = 0; z < count; ++z) { + EXPECT_FALSE(tags[z].empty()); + EXPECT_EQ(boost::lexical_cast(z), tags[z]); + } + } +}