Skip to content

Commit 9f7e37c

Browse files
committed
build: enhance test configuration and paths in CMakeLists
- Removed default install path setting from CMakeLists.txt. - Updated test CMakeLists to include Google Test and configure test paths. - Refactored load_test and unittest files to use defined paths for dictionaries and test data. - Added test paths header for better path management in tests. - Ensured all tests are properly linked and configured for execution.
1 parent aa410a6 commit 9f7e37c

11 files changed

+148
-72
lines changed

CMakeLists.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ PROJECT(CPPJIEBA)
44
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/deps/limonp/include
55
${PROJECT_SOURCE_DIR}/include)
66

7-
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
8-
set (CMAKE_INSTALL_PREFIX "/usr/local/cppjieba" CACHE PATH "default install path" FORCE )
9-
endif()
107

118
if(NOT DEFINED CMAKE_CXX_STANDARD)
129
set(CMAKE_CXX_STANDARD 11)
@@ -29,12 +26,7 @@ endif()
2926
option(CPPJIEBA_BUILD_TESTS "Build cppjieba tests" ${CPPJIEBA_TOP_LEVEL_PROJECT})
3027

3128
if(CPPJIEBA_BUILD_TESTS)
32-
ENABLE_TESTING()
33-
34-
message(STATUS "MSVC value: ${MSVC}")
3529
ADD_SUBDIRECTORY(test)
36-
ADD_TEST(NAME ./test/test.run COMMAND ./test/test.run)
37-
ADD_TEST(NAME ./load_test COMMAND ./load_test)
3830
endif()
3931

4032

test/CMakeLists.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,38 @@
1+
ENABLE_TESTING()
2+
13
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR})
24

5+
# Configure test paths
6+
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/test_paths.h.in" "${CMAKE_BINARY_DIR}/test/test_paths.h")
7+
8+
# Add include directories
9+
INCLUDE_DIRECTORIES(
10+
${CMAKE_CURRENT_BINARY_DIR}
11+
${CMAKE_BINARY_DIR}/test
12+
${PROJECT_SOURCE_DIR}/include
13+
${PROJECT_SOURCE_DIR}/deps/limonp/include
14+
${CMAKE_BINARY_DIR}/_deps/googletest-src/googletest/include
15+
)
16+
17+
# Add Google Test
18+
include(FetchContent)
19+
FetchContent_Declare(
20+
googletest
21+
GIT_REPOSITORY https://github.com/google/googletest.git
22+
GIT_TAG release-1.12.1
23+
)
24+
FetchContent_MakeAvailable(googletest)
25+
26+
# Add UTF-8 support for MSVC
27+
if(MSVC)
28+
add_compile_options(/utf-8)
29+
endif()
30+
331
ADD_EXECUTABLE(load_test load_test.cpp)
32+
TARGET_LINK_LIBRARIES(load_test gtest gtest_main)
33+
434
ADD_SUBDIRECTORY(unittest)
35+
36+
# Add test configurations
37+
ADD_TEST(NAME load_test COMMAND load_test)
38+
SET_TESTS_PROPERTIES(load_test PROPERTIES WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})

test/load_test.cpp

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
#include "cppjieba/MixSegment.hpp"
77
#include "cppjieba/KeywordExtractor.hpp"
88
#include "limonp/Colors.hpp"
9+
#include "cppjieba/Jieba.hpp"
10+
#include "gtest/gtest.h"
11+
#include "test_paths.h"
912

1013
using namespace cppjieba;
1114

1215
void Cut(size_t times = 50) {
13-
MixSegment seg("../dict/jieba.dict.utf8", "../dict/hmm_model.utf8");
16+
MixSegment seg(DICT_DIR "/jieba.dict.utf8", DICT_DIR "/hmm_model.utf8");
1417
vector<string> res;
1518
string doc;
16-
ifstream ifs("../test/testdata/weicheng.utf8");
19+
ifstream ifs(TEST_DATA_DIR "/weicheng.utf8");
1720
assert(ifs);
1821
doc << ifs;
1922
long beginTime = clock();
@@ -29,10 +32,13 @@ void Cut(size_t times = 50) {
2932
}
3033

3134
void Extract(size_t times = 400) {
32-
KeywordExtractor Extractor("../dict/jieba.dict.utf8", "../dict/hmm_model.utf8", "../dict/idf.utf8", "../dict/stop_words.utf8");
35+
KeywordExtractor Extractor(DICT_DIR "/jieba.dict.utf8",
36+
DICT_DIR "/hmm_model.utf8",
37+
DICT_DIR "/idf.utf8",
38+
DICT_DIR "/stop_words.utf8");
3339
vector<string> words;
3440
string doc;
35-
ifstream ifs("../test/testdata/review.100");
41+
ifstream ifs(TEST_DATA_DIR "/review.100");
3642
assert(ifs);
3743
doc << ifs;
3844
long beginTime = clock();
@@ -47,8 +53,24 @@ void Extract(size_t times = 400) {
4753
ColorPrintln(GREEN, "Extract: [%.3lf seconds]time consumed.", double(endTime - beginTime)/CLOCKS_PER_SEC);
4854
}
4955

50-
int main(int argc, char ** argv) {
56+
TEST(LoadTest, Test1) {
57+
Jieba jieba(DICT_DIR "/jieba.dict.utf8",
58+
DICT_DIR "/hmm_model.utf8",
59+
DICT_DIR "/user.dict.utf8",
60+
DICT_DIR "/idf.utf8",
61+
DICT_DIR "/stop_words.utf8");
62+
vector<string> words;
63+
string result;
64+
65+
jieba.Cut("他来到了网易杭研大厦", words);
66+
result << words;
67+
string expected = "[\"\", \"来到\", \"\", \"网易\", \"杭研\", \"大厦\"]";
68+
ASSERT_EQ(expected, result);
69+
}
70+
71+
int main(int argc, char** argv) {
72+
testing::InitGoogleTest(&argc, argv);
5173
Cut();
5274
Extract();
53-
return EXIT_SUCCESS;
75+
return RUN_ALL_TESTS();
5476
}

test/test_paths.h.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef TEST_PATHS_H
2+
#define TEST_PATHS_H
3+
4+
#define TEST_DATA_DIR "@CMAKE_CURRENT_SOURCE_DIR@/testdata"
5+
#define DICT_DIR "@CMAKE_SOURCE_DIR@/dict"
6+
7+
#endif // TEST_PATHS_H

test/unittest/CMakeLists.txt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
message(STATUS "MSVC value: ${MSVC}")
12
if (MSVC)
23
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDebugDLL")
34
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
@@ -13,12 +14,20 @@ FetchContent_Declare(
1314
)
1415
FetchContent_MakeAvailable(googletest)
1516

16-
17-
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/test)
17+
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR})
1818
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
1919

2020
ADD_DEFINITIONS(-DLOGGING_LEVEL=LL_WARNING)
2121

22+
# Add include directories
23+
INCLUDE_DIRECTORIES(
24+
${CMAKE_CURRENT_BINARY_DIR}
25+
${CMAKE_BINARY_DIR}/test
26+
${PROJECT_SOURCE_DIR}/include
27+
${PROJECT_SOURCE_DIR}/deps/limonp/include
28+
${CMAKE_BINARY_DIR}/_deps/googletest-src/googletest/include
29+
)
30+
2231
ADD_EXECUTABLE(test.run
2332
gtest_main.cpp
2433
keyword_extractor_test.cpp
@@ -31,4 +40,8 @@ ADD_EXECUTABLE(test.run
3140
textrank_test.cpp
3241
)
3342

34-
TARGET_LINK_LIBRARIES(test.run gtest)
43+
TARGET_LINK_LIBRARIES(test.run gtest gtest_main)
44+
45+
enable_testing()
46+
ADD_TEST(NAME test.run COMMAND test.run)
47+
SET_TESTS_PROPERTIES(test.run PROPERTIES WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})

test/unittest/jieba_test.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cppjieba/Jieba.hpp"
22
#include "gtest/gtest.h"
3+
#include "test_paths.h"
34

45
using namespace cppjieba;
56

@@ -37,11 +38,11 @@ TEST(JiebaTest, Test0) {
3738
}
3839

3940
TEST(JiebaTest, Test1) {
40-
cppjieba::Jieba jieba("../dict/jieba.dict.utf8",
41-
"../dict/hmm_model.utf8",
42-
"../dict/user.dict.utf8",
43-
"../dict/idf.utf8",
44-
"../dict/stop_words.utf8");
41+
cppjieba::Jieba jieba(DICT_DIR "/jieba.dict.utf8",
42+
DICT_DIR "/hmm_model.utf8",
43+
DICT_DIR "/user.dict.utf8",
44+
DICT_DIR "/idf.utf8",
45+
DICT_DIR "/stop_words.utf8");
4546
vector<string> words;
4647
string result;
4748

@@ -71,14 +72,14 @@ TEST(JiebaTest, Test1) {
7172
jieba.CutForSearch("他来到了网易杭研大厦", words);
7273
result << words;
7374
ASSERT_EQ("[\"\", \"来到\", \"\", \"网易\", \"杭研\", \"大厦\"]", result);
74-
7575
}
76+
7677
TEST(JiebaTest, WordTest) {
77-
cppjieba::Jieba jieba("../dict/jieba.dict.utf8",
78-
"../dict/hmm_model.utf8",
79-
"../dict/user.dict.utf8",
80-
"../dict/idf.utf8",
81-
"../dict/stop_words.utf8");
78+
cppjieba::Jieba jieba(DICT_DIR "/jieba.dict.utf8",
79+
DICT_DIR "/hmm_model.utf8",
80+
DICT_DIR "/user.dict.utf8",
81+
DICT_DIR "/idf.utf8",
82+
DICT_DIR "/stop_words.utf8");
8283
vector<Word> words;
8384
string result;
8485

@@ -116,11 +117,11 @@ TEST(JiebaTest, WordTest) {
116117
}
117118

118119
TEST(JiebaTest, InsertUserWord) {
119-
cppjieba::Jieba jieba("../dict/jieba.dict.utf8",
120-
"../dict/hmm_model.utf8",
121-
"../dict/user.dict.utf8",
122-
"../dict/idf.utf8",
123-
"../dict/stop_words.utf8");
120+
cppjieba::Jieba jieba(DICT_DIR "/jieba.dict.utf8",
121+
DICT_DIR "/hmm_model.utf8",
122+
DICT_DIR "/user.dict.utf8",
123+
DICT_DIR "/idf.utf8",
124+
DICT_DIR "/stop_words.utf8");
124125
vector<string> words;
125126
string result;
126127

test/unittest/keyword_extractor_test.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#include "cppjieba/KeywordExtractor.hpp"
22
#include "gtest/gtest.h"
3+
#include "test_paths.h"
34

45
using namespace cppjieba;
56

67
TEST(KeywordExtractorTest, Test1) {
7-
KeywordExtractor Extractor("../test/testdata/extra_dict/jieba.dict.small.utf8", "../dict/hmm_model.utf8", "../dict/idf.utf8", "../dict/stop_words.utf8");
8+
KeywordExtractor Extractor(TEST_DATA_DIR "/extra_dict/jieba.dict.small.utf8",
9+
DICT_DIR "/hmm_model.utf8",
10+
DICT_DIR "/idf.utf8",
11+
DICT_DIR "/stop_words.utf8");
812

913
{
1014
string s("你好世界世界而且而且");
@@ -55,7 +59,11 @@ TEST(KeywordExtractorTest, Test1) {
5559
}
5660

5761
TEST(KeywordExtractorTest, Test2) {
58-
KeywordExtractor Extractor("../test/testdata/extra_dict/jieba.dict.small.utf8", "../dict/hmm_model.utf8", "../dict/idf.utf8", "../dict/stop_words.utf8", "../test/testdata/userdict.utf8");
62+
KeywordExtractor Extractor(TEST_DATA_DIR "/extra_dict/jieba.dict.small.utf8",
63+
DICT_DIR "/hmm_model.utf8",
64+
DICT_DIR "/idf.utf8",
65+
DICT_DIR "/stop_words.utf8",
66+
TEST_DATA_DIR "/userdict.utf8");
5967

6068
{
6169
string s("蓝翔优秀毕业生");

test/unittest/pos_tagger_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cppjieba/MixSegment.hpp"
22
#include "gtest/gtest.h"
3+
#include "test_paths.h"
34

45
using namespace cppjieba;
56

@@ -13,7 +14,7 @@ static const char * const ANS_TEST3 = "[iPhone6:eng, 手机:n, 的:uj, 最大:a,
1314
//static const char * const ANS_TEST3 = "";
1415

1516
TEST(PosTaggerTest, Test) {
16-
MixSegment tagger("../dict/jieba.dict.utf8", "../dict/hmm_model.utf8");
17+
MixSegment tagger(DICT_DIR "/jieba.dict.utf8", DICT_DIR "/hmm_model.utf8");
1718
{
1819
vector<pair<string, string> > res;
1920
tagger.Tag(QUERY_TEST1, res);
@@ -23,7 +24,7 @@ TEST(PosTaggerTest, Test) {
2324
}
2425
}
2526
TEST(PosTagger, TestUserDict) {
26-
MixSegment tagger("../dict/jieba.dict.utf8", "../dict/hmm_model.utf8", "../test/testdata/userdict.utf8");
27+
MixSegment tagger(DICT_DIR "/jieba.dict.utf8", DICT_DIR "/hmm_model.utf8", TEST_DATA_DIR "/userdict.utf8");
2728
{
2829
vector<pair<string, string> > res;
2930
tagger.Tag(QUERY_TEST2, res);

0 commit comments

Comments
 (0)