diff --git a/CMakeLists.txt b/CMakeLists.txt index f68b278..6060021 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,6 +114,7 @@ add_subdirectory(src/navigate) #Tests add_subdirectory(test/gtest) add_subdirectory(test/unit) +add_subdirectory(test/unit_cli) target_link_libraries(${PROJECT_NAME} INTERFACE ${PostgreSQL_LIBRARIES} diff --git a/src/main/damerau_levenshtein.h b/src/main/damerau_levenshtein.h index a16cccb..1bbcf74 100644 --- a/src/main/damerau_levenshtein.h +++ b/src/main/damerau_levenshtein.h @@ -19,7 +19,15 @@ #define id88738025C6B24BDEB604A5AE3C36EE8D #include "helpers/compatibility.h" -#include "stddef.h" +#if defined(__cplusplus) +# include +#else +# include +#endif + +#if defined(__cplusplus) +extern "C" { +#endif int damerau_levenshtein ( const char* parSource, @@ -41,4 +49,8 @@ int damerau_levenshtein_with_size ( int parSwapCost ) a_pure; +#if defined(__cplusplus) +} +#endif + #endif diff --git a/test/unit_cli/CMakeLists.txt b/test/unit_cli/CMakeLists.txt new file mode 100644 index 0000000..349cbe1 --- /dev/null +++ b/test/unit_cli/CMakeLists.txt @@ -0,0 +1,25 @@ +project(${bare_name}-test_cli CXX C) + +add_executable(${PROJECT_NAME} + ${CMAKE_SOURCE_DIR}/src/main/damerau_levenshtein.c + ${CMAKE_SOURCE_DIR}/src/main/utf8_ops.c + test_damerau_levenshtein.cpp +) + +target_include_directories(${PROJECT_NAME} + PRIVATE ${CMAKE_SOURCE_DIR}/src + PRIVATE ${CMAKE_SOURCE_DIR}/lib/pbl/pbl/src/src +) + +target_include_directories(${PROJECT_NAME} SYSTEM + PRIVATE ../gtest/include +) + +target_link_libraries(${PROJECT_NAME} + PRIVATE ${bare_name}-if + PRIVATE ${bare_name}-common + PRIVATE ${bare_name}-machinery + PRIVATE gtest + PRIVATE gtest_main + PRIVATE pbl +) diff --git a/test/unit_cli/test_damerau_levenshtein.cpp b/test/unit_cli/test_damerau_levenshtein.cpp new file mode 100644 index 0000000..d2a01be --- /dev/null +++ b/test/unit_cli/test_damerau_levenshtein.cpp @@ -0,0 +1,72 @@ +/* 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 "main/damerau_levenshtein.h" + +TEST(cli_main, damerau_levenshtein) { + { + const char source1[] = u8"navigte"; + const char source2[] = u8"navigäte"; + const char source3[] = u8"navigbte"; + const char source4[] = u8"narigate"; + const char source5[] = u8"navigate"; + const char target[] = u8"navigate"; + EXPECT_EQ(1, damerau_levenshtein(source1, target, 1, 1, 1, 1)); + EXPECT_EQ(1, damerau_levenshtein(source2, target, 1, 1, 1, 1)); + EXPECT_EQ(1, damerau_levenshtein(source3, target, 1, 1, 1, 1)); + EXPECT_EQ(1, damerau_levenshtein(source4, target, 1, 1, 1, 1)); + EXPECT_EQ(0, damerau_levenshtein(source5, target, 1, 1, 1, 1)); + } + { + const char source[] = u8"navigaäte"; + const char target[] = u8"navigate"; + EXPECT_EQ(1, damerau_levenshtein(source, target, 1, 1, 1, 1)); + } + { + const char source1[] = u8"navigaäbcde"; + const char source2[] = u8"navigaabcde"; + const char target[] = u8"navigate"; + EXPECT_EQ(4, damerau_levenshtein(source1, target, 1, 1, 1, 1)); + EXPECT_EQ(4, damerau_levenshtein(source2, target, 1, 1, 1, 1)); + } + { + const char source[] = u8""; + const char target[] = u8"navigate"; + EXPECT_EQ(8, damerau_levenshtein(source, target, 1, 1, 1, 1)); + EXPECT_EQ(8, damerau_levenshtein(target, source, 1, 1, 1, 1)); + } + { + const char source[] = u8"ありがとうございました"; + const char target[] = u8"りがとうございました"; + EXPECT_EQ(1, damerau_levenshtein(source, target, 1, 1, 1, 1)); + } + { + const char source1[] = u8"Kanji 金 reads かね."; + const char source2[] = u8"Kanji ⿔ reads かめ."; + const char target[] = u8"Kanji 亀 reads かめ."; + EXPECT_EQ(2, damerau_levenshtein(source1, target, 1, 1, 1, 1)); + EXPECT_EQ(1, damerau_levenshtein(source2, target, 1, 1, 1, 1)); + } + { + const char source[] = u8"\"槪\" and \"🚮\" have a long utf8 sequence."; + const char target1[] = u8"\"槪\" or \"🚮\" have a long utf8 sequence."; + const char target2[] = u8"\"槪\" or \"🚻\" have a long utf8 sequence."; + EXPECT_EQ(3, damerau_levenshtein(source, target1, 1, 1, 1, 1)); + EXPECT_EQ(4, damerau_levenshtein(source, target2, 1, 1, 1, 1)); + } +}