[flang] unit test support for out-of-tree and in-tree using google tests framework

Review comments addressed.
This commit is contained in:
sameeran joshi 2020-06-02 22:45:44 +05:30 committed by Sameeran joshi
parent 7e54df6829
commit 93f602b339
8 changed files with 200 additions and 4 deletions

View File

@ -130,7 +130,50 @@ if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
option(FLANG_INCLUDE_TESTS
"Generate build targets for the Flang unit tests."
ON)
add_custom_target(check-all DEPENDS check-flang)
#Handle unittests when out-of-tree
#LLVM_BUILD_MAIN_SRC_DIR - Path to llvm source when out-of-tree.
set(FLANG_GTEST_AVAIL 0)
if (FLANG_INCLUDE_TESTS)
set(UNITTEST_DIR ${LLVM_BUILD_MAIN_SRC_DIR}/utils/unittest)
if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h)
if (TARGET gtest)
# LLVM Doesn't export gtest's include directorys, so do that here
set_target_properties(gtest
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
"${UNITTEST_DIR}/googletest/include;${UNITTEST_DIR}/googlemock/include"
)
else()
add_library(gtest
${UNITTEST_DIR}/googletest/src/gtest-all.cc
${UNITTEST_DIR}/googlemock/src/gmock-all.cc
)
target_include_directories(gtest
PUBLIC
"${UNITTEST_DIR}/googletest/include"
"${UNITTEST_DIR}/googlemock/include"
PRIVATE
"${UNITTEST_DIR}/googletest"
"${UNITTEST_DIR}/googlemock"
)
find_package(Threads)
target_link_libraries(gtest PUBLIC Threads::Threads)
add_library(gtest_main ${UNITTEST_DIR}/UnitTestMain/TestMain.cpp)
target_link_libraries(gtest_main PUBLIC gtest)
endif()
set(FLANG_GTEST_AVAIL 1)
else()
message(WARNING
"Unit-tests will be skipped as LLVM install does not include google-test related headers and libraries.")
set(FLANG_GTEST_AVAIL 0)
endif()
endif()
if (FLANG_GTEST_AVAIL)
add_custom_target(check-all DEPENDS check-flang FlangUnitTests)
else()
add_custom_target(check-all DEPENDS check-flang )
endif()
if (LLVM_BUILD_DOCS)
add_custom_target(doxygen ALL)
endif()
@ -139,6 +182,8 @@ else()
option(FLANG_INCLUDE_TESTS
"Generate build targets for the Flang unit tests."
${LLVM_INCLUDE_TESTS})
set(FLANG_GTEST_AVAIL 1)
set(FLANG_BINARY_DIR ${CMAKE_BINARY_DIR}/tools/flang)
set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}")
if (LINK_WITH_FIR)
@ -344,7 +389,9 @@ add_subdirectory(runtime)
if (FLANG_INCLUDE_TESTS)
enable_testing()
add_subdirectory(test)
add_subdirectory(unittests)
if (FLANG_GTEST_AVAIL)
add_subdirectory(unittests)
endif ()
endif()
option(FLANG_INCLUDE_DOCS "Generate build targets for the Flang docs."

View File

@ -140,8 +140,13 @@ cd ~/flang/build
cmake -DLLVM_DIR=$LLVM -DMLIR_DIR=$MLIR ~/flang/src
make
```
### How to Run the Regression Tests
# How to Run Tests
Flang supports 2 different categories of tests
1. Regression tests (https://www.llvm.org/docs/TestingGuide.html#regression-tests)
2. Unit tests (https://www.llvm.org/docs/TestingGuide.html#unit-tests)
## For out of tree builds
To run all tests:
```
cd ~/flang/build
@ -157,6 +162,38 @@ flang_site_config and flang_config. And they can be set as shown bellow:
--param flang_site_config=<path-to-flang-build>/test-lit/lit.site.cfg.py \
--param flang_config=<path-to-flang-build>/test-lit/lit.cfg.py \
<path-to-fortran-test>
```
Unit tests:
If flang was built with `-DFLANG_INCLUDE_TESTS=On` (`ON` by default), it is possible to generate unittests.
Note: Unit-tests will be skipped for LLVM install for an out-of-tree build as it does not include googletest related headers and libraries.
There are various ways to run unit-tests.
```
1. make check-flang-unit
2. make check-all or make check-flang
3. <path-to-llvm-lit>/llvm-lit \
test/Unit
4. Invoking tests from <out-of-tree flang build>/unittests/<respective unit test folder>
```
## For in tree builds
If flang was built with `-DFLANG_INCLUDE_TESTS=On` (`On` by default), it is possible to
generate unittests.
To run all of the flang unit tests use the `check-flang-unit` target:
```
make check-flang-unit
```
To run all of the flang regression tests use the `check-flang` target:
```
make check-flang
```
# How to Generate Documentation
@ -179,4 +216,3 @@ It will generate html in
<build-dir>/tools/flang/docs/doxygen/html # for flang docs
```

View File

@ -12,6 +12,13 @@ configure_lit_site_cfg(
${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py
)
configure_lit_site_cfg(
${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.site.cfg.py.in
${CMAKE_CURRENT_BINARY_DIR}/Unit/lit.site.cfg.py
MAIN_CONFIG
${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.cfg.py
)
set(FLANG_TEST_PARAMS
flang_site_config=${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py)
@ -23,6 +30,12 @@ if (LINK_WITH_FIR)
list(APPEND FLANG_TEST_DEPENDS tco)
endif()
if (FLANG_INCLUDE_TESTS)
if (FLANG_GTEST_AVAIL)
list(APPEND FLANG_TEST_DEPENDS FlangUnitTests)
endif()
endif()
add_custom_target(flang-test-depends DEPENDS ${FLANG_TEST_DEPENDS})
add_lit_testsuite(check-flang "Running the Flang regression tests"

View File

@ -0,0 +1,32 @@
# -*- Python -*-
# Configuration file for the 'lit' test runner.
import os
import lit.formats
# name: The name of this test suite.
config.name = 'flang-Unit'
# suffixes: A list of file extensions to treat as test files.
config.suffixes = []
# test_source_root: The root path where unit test binaries are located.
# test_exec_root: The root path where tests should be run.
config.test_source_root = os.path.join(config.flang_obj_root, 'unittests')
config.test_exec_root = config.test_source_root
# testFormat: The test format to use to interpret tests.
config.test_format = lit.formats.GoogleTest(config.llvm_build_mode, 'Tests')
# Tweak the PATH to include the tools dir.
path = os.path.pathsep.join((config.flang_tools_dir, config.llvm_tools_dir, config.environment['PATH']))
config.environment['PATH'] = path
path = os.path.pathsep.join((config.flang_libs_dir, config.llvm_libs_dir,
config.environment.get('LD_LIBRARY_PATH','')))
config.environment['LD_LIBRARY_PATH'] = path
# Propagate PYTHON_EXECUTABLE into the environment
#config.environment['PYTHON_EXECUTABLE'] = sys.executable

View File

@ -0,0 +1,27 @@
@LIT_SITE_CFG_IN_HEADER@
config.llvm_src_root = "@LLVM_SOURCE_DIR@"
config.llvm_obj_root = "@LLVM_BINARY_DIR@"
config.llvm_tools_dir = "@LLVM_TOOLS_DIR@"
config.llvm_libs_dir = "@LLVM_LIBS_DIR@"
config.llvm_build_mode = "@LLVM_BUILD_MODE@"
config.lit_tools_dir = "@LLVM_LIT_TOOLS_DIR@"
config.flang_obj_root = "@FLANG_BINARY_DIR@"
config.flang_src_root = "@FLANG_SOURCE_DIR@"
config.flang_libs_dir = "@LLVM_LIBRARY_OUTPUT_INTDIR@"
config.flang_tools_dir = "@LLVM_RUNTIME_OUTPUT_INTDIR@"
config.target_triple = "@TARGET_TRIPLE@"
config.python_executable = "@Python3_EXECUTABLE@"
# Support substitution of the tools and libs dirs with user parameters. This is
# used when we can't determine the tool dir at configuration time.
try:
config.llvm_tools_dir = config.llvm_tools_dir % lit_config.params
config.llvm_libs_dir = config.llvm_libs_dir % lit_config.params
config.llvm_build_mode = config.llvm_build_mode % lit_config.params
except KeyError as e:
key, = e.args
lit_config.fatal("unable to find %r parameter, use '--param=%s=VALUE'" % (key,key))
# Let the main config do the real work.
lit_config.load_config(config, "@FLANG_SOURCE_DIR@/test/Unit/lit.cfg.py")

View File

@ -1,3 +1,11 @@
add_custom_target(FlangUnitTests)
set_target_properties(FlangUnitTests PROPERTIES FOLDER "Flang Unit Tests")
function(add_flang_unittest test_dirname)
add_unittest(FlangUnitTests ${test_dirname} ${ARGN})
endfunction()
add_subdirectory(Optimizer)
add_subdirectory(Decimal)
add_subdirectory(Evaluate)
add_subdirectory(Runtime)

View File

@ -0,0 +1,13 @@
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
set(LIBS
FIRDialect
${dialect_libs}
)
add_flang_unittest(FlangOptimizerTests
InternalNamesTest.cpp
)
target_link_libraries(FlangOptimizerTests
PRIVATE
${LIBS})

View File

@ -0,0 +1,20 @@
//===- InternalNames.cpp - InternalNames unit tests ---------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "flang/Optimizer/Support/InternalNames.h"
#include "gtest/gtest.h"
using namespace fir;
using namespace llvm;
TEST(genericName, MyTest) {
NameUniquer obj;
std::string val = obj.doCommonBlock("hello");
std::string val2 = "_QBhello";
EXPECT_EQ(val, val2);
}