Skip to content

Commit

Permalink
添加algo_gtest
Browse files Browse the repository at this point in the history
  • Loading branch information
paoqi1997 committed Apr 21, 2021
1 parent bda032b commit 5692691
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pqalgo/gtests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
41 changes: 41 additions & 0 deletions pqalgo/gtests/algo_gtest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <string>

#include <pqalgo/kmp.h>
#include <pqalgo/trie.h>

#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"));
}

0 comments on commit 5692691

Please sign in to comment.