Skip to content

Commit

Permalink
Support building and running unit tests with CMake toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
phymbert committed Dec 16, 2023
1 parent ea0b245 commit 546561f
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 10 deletions.
17 changes: 7 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ option(ENABLE_NATIVE
# see
# https://stackoverflow.com/questions/52653025/why-is-march-native-used-so-rarely
option(BUILD_TRAINING_TOOLS "Build training tools" ON)
option(BUILD_TESTS "Build tests" OFF)
option(BUILD_TESTING "Build tests" OFF)
option(USE_SYSTEM_ICU "Use system ICU" OFF)
option(DISABLE_TIFF "Disable build with libtiff (if available)" OFF)
option(DISABLE_ARCHIVE "Disable build with libarchive (if available)" OFF)
Expand Down Expand Up @@ -564,7 +564,7 @@ message(STATUS "Disable the legacy OCR engine [DISABLED_LEGACY_ENGINE]: "
"${DISABLED_LEGACY_ENGINE}")
message(STATUS "Build training tools [BUILD_TRAINING_TOOLS]: "
"${BUILD_TRAINING_TOOLS}")
message(STATUS "Build tests [BUILD_TESTS]: ${BUILD_TESTS}")
message(STATUS "Build tests [BUILD_TESTING]: ${BUILD_TESTING}")
if(ENABLE_OPENCL)
message(
STATUS
Expand Down Expand Up @@ -910,18 +910,15 @@ if(OPENMP_BUILD AND UNIX)
endif()

# ##############################################################################

if(BUILD_TESTS
AND EXISTS
${CMAKE_CURRENT_SOURCE_DIR}/unittest/third_party/googletest/CMakeLists.txt
)
add_subdirectory(unittest/third_party/googletest)
endif()

if(BUILD_TRAINING_TOOLS)
add_subdirectory(src/training)
endif()

if(BUILD_TESTING)
include(CTest)
add_subdirectory(unittest)
endif()

get_target_property(tesseract_NAME libtesseract NAME)
get_target_property(tesseract_VERSION libtesseract VERSION)
get_target_property(tesseract_OUTPUT_NAME libtesseract OUTPUT_NAME)
Expand Down
152 changes: 152 additions & 0 deletions unittest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#
# tesseract unittest
#

# Prefetch googletest if it is not already initialized with `git submodule update --init`
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG main # as .gitmodules does not enforce releases tag we track the main branch
SOURCE_DIR ${CMAKE_SOURCE_DIR}/unittest/third_party/googletest
)
FetchContent_Populate(googletest)
add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR})

include(GoogleTest)

# core tests
set(TESSERACT_UNITTESTS
apiexample
cleanapi
colpartition
denorm
fileio
heap
imagedata
intsimdmatrix
lang_model
layout
ligature_table
linlsq
list
loadlang
matrix
networkio
nthitem
pagesegmode
paragraphs
progress
qrsequence
recodebeam
rect
resultiterator
scanutils
stats
stridemap
stringrenderer
tablefind
tablerecog
tabvector
tatweel
tfile
)

# Legacy engine tests
if (NOT DISABLED_LEGACY_ENGINE)
set(TESSERACT_UNITTESTS
${TESSERACT_UNITTESTS}
equationdetect
indexmapbidi
intfeaturemap
osd
params_model
shapetable
textlineprojection
)
endif ()

# training tests
if (BUILD_TRAINING_TOOLS)
set(TESSERACT_UNITTESTS
${TESSERACT_UNITTESTS}
baseapi
baseapi_thread
commandlineflags
dawg
lstm_recode
lstm_squashed
lstm
lstmtrainer
normstrngs
pango_font_info
unichar
unicharcompress
unicharset
validate_grapheme
validate_indic
validate_khmer
validate_myanmar
validator
)
if (NOT DISABLED_LEGACY_ENGINE)
set(TESSERACT_UNITTESTS
${TESSERACT_UNITTESTS}
applybox
bitvector
)
endif ()
endif ()

set(TESSBIN_DIR ${CMAKE_BINARY_DIR}/bin/tesseract)
set(TESSDATA_DIR ${CMAKE_SOURCE_DIR}/../tessdata)
set(LANGDATA_DIR ${CMAKE_SOURCE_DIR}/../langdata_lstm)
set(TESTING_DIR ${CMAKE_SOURCE_DIR}/test/testing)
set(TESTDATA_DIR ${CMAKE_SOURCE_DIR}/test/testdata)

set(TESSERACT_UNITTEST_CXXFLAG
-DTESSBIN_DIR="${TESSBIN_DIR}"
-DTESSDATA_DIR="${TESSDATA_DIR}"
-DLANGDATA_DIR="${LANGDATA_DIR}"
-DTESTING_DIR="${TESTING_DIR}"
-DTESTDATA_DIR="${TESTDATA_DIR}"
)

add_custom_target(build_tests)

foreach (unittest ${TESSERACT_UNITTESTS})
add_executable(
${unittest}_test
${unittest}_test.cc
third_party/utf/rune.c
util/utf8/unicodetext.cc
util/utf8/unilib.cc
)
add_dependencies(${unittest}_test tesseract)
target_compile_options(${unittest}_test PRIVATE ${TESSERACT_UNITTEST_CXXFLAG})
target_include_directories(${unittest}_test PRIVATE ${CMAKE_SOURCE_DIR}/unittest)

target_link_libraries(
${unittest}_test
gtest
gmock
gtest_main
libtesseract
)

if (BUILD_TRAINING_TOOLS)
target_link_libraries(
${unittest}_test
common_training
unicharset_training
pango_training
)
endif ()

gtest_discover_tests(${unittest}_test)
add_test(NAME ${unittest}_test COMMAND ${unittest}_test)
add_dependencies(build_tests ${unittest}_test)
endforeach ()

add_custom_target(check COMMAND ctest)
add_dependencies(check build_tests)
13 changes: 13 additions & 0 deletions unittest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,16 @@ git submodule update --init
export TESSDATA_PREFIX=/prefix/to/path/to/tessdata
make check
```

## Run tests using CMake toolchain
```
cmake -DBUILD_TESTING=ON -B build
export TESSDATA_PREFIX=/prefix/to/path/to/tessdata
cmake --build build --target check
```

### Run a single test target
```
cmake --build build --target fileio_test
./build/bin/fileio_test
```

0 comments on commit 546561f

Please sign in to comment.