From 5692691533e1432c08101538c2f530cf151c1d30 Mon Sep 17 00:00:00 2001 From: paoqi1997 <604869221@qq.com> Date: Wed, 21 Apr 2021 10:53:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0algo=5Fgtest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pqalgo/gtests/CMakeLists.txt | 13 +++++++----- pqalgo/gtests/algo_gtest.cpp | 41 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 pqalgo/gtests/algo_gtest.cpp diff --git a/pqalgo/gtests/CMakeLists.txt b/pqalgo/gtests/CMakeLists.txt index d742fad..26ffc76 100644 --- a/pqalgo/gtests/CMakeLists.txt +++ b/pqalgo/gtests/CMakeLists.txt @@ -3,16 +3,19 @@ project(unittests) include_directories(../..) -set(UNITTESTS_SRC +set(UNITTESTS_SRCS + algo_gtest.cpp sort_gtest.cpp ) -set(UNITTESTS_INC +set(UNITTESTS_INCS + ../kmp.h ../sort.h + ../trie.h ) -source_group("src" FILES ${UNITTESTS_SRC}) -source_group("include" FILES ${UNITTESTS_INC}) +source_group("src" FILES ${UNITTESTS_SRCS}) +source_group("include" FILES ${UNITTESTS_INCS}) -add_executable(uts ${UNITTESTS_SRC} ${UNITTESTS_INC}) +add_executable(uts ${UNITTESTS_SRCS} ${UNITTESTS_INCS}) target_link_libraries(uts gtest gtest_main pthread) diff --git a/pqalgo/gtests/algo_gtest.cpp b/pqalgo/gtests/algo_gtest.cpp new file mode 100644 index 0000000..cd84922 --- /dev/null +++ b/pqalgo/gtests/algo_gtest.cpp @@ -0,0 +1,41 @@ +#include + +#include +#include + +#include "gtest/gtest.h" + +class CAlgoTest : public ::testing::Test +{ +protected: + CAlgoTest() {} + ~CAlgoTest() override {} + void SetUp() override {} + void TearDown() override {} +}; + +TEST_F(CAlgoTest, TestKmp) +{ + std::string s = "12 345 6789 0", p = "789"; + + int idx1 = pqalgo::bf(s.c_str(), p.c_str()); + int idx2 = pqalgo::kmp(s.c_str(), p.c_str()); + + EXPECT_STREQ(p.c_str(), s.substr(idx1, p.length()).c_str()); + EXPECT_STREQ(p.c_str(), s.substr(idx2, p.length()).c_str()); +} + +TEST_F(CAlgoTest, TestTrie) +{ + pqalgo::Trie trie; + + trie.insert("my"); + trie.insert("mydb"); + trie.insert("mysql"); + + EXPECT_FALSE(trie.find("milk")); + EXPECT_TRUE(trie.find("mysql")); + + EXPECT_FALSE(trie.startsWith("mike")); + EXPECT_TRUE(trie.startsWith("my")); +}