From 03c81e96e9e112461e0bf7995d7266628f2fd868 Mon Sep 17 00:00:00 2001 From: Ruben Ayrapetyan Date: Mon, 16 Feb 2015 19:18:30 +0300 Subject: [PATCH] Turning on unit tests build and run during precommit. --- CMakeLists.txt | 69 ++++++++++++++-------------- Makefile | 20 ++++---- core/CMakeLists.txt | 15 ++++-- jerry-libc/CMakeLists.txt | 5 +- plugins/CMakeLists.txt | 5 +- tests/unit/common.h | 3 +- tests/unit/test_number_to_string.cpp | 28 +++++------ tests/unit/test_poolman.cpp | 13 ++---- tests/unit/test_preparser.cpp | 3 +- tests/unit/test_string_to_number.cpp | 41 ++++++++--------- 10 files changed, 101 insertions(+), 101 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d87a5471..d95548c8b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,14 +76,15 @@ project (Jerry CXX C ASM) set(MCU_SCRIPT_GENERATED_HEADER ${CMAKE_BINARY_DIR}/generated.h) # Build modes - set(BUILD_MODES DEBUG RELEASE) - # Debug set(BUILD_MODE_PREFIX_DEBUG debug) # Release set(BUILD_MODE_PREFIX_RELEASE release) + # Unit tests + set(BUILD_MODE_PREFIX_UNITTESTS unittests) + # Modifiers set(MODIFIERS COMPACT_PROFILE @@ -128,7 +129,7 @@ project (Jerry CXX C ASM) # Compiler / Linker flags set(COMPILE_FLAGS_JERRY "-fno-builtin") - set(LINKER_FLAGS_COMMON "-nostdlib") + set(LINKER_FLAGS_COMMON "") # Turn off stack protector set(COMPILE_FLAGS_JERRY "${COMPILE_FLAGS_JERRY} -fno-stack-protector") @@ -163,17 +164,20 @@ project (Jerry CXX C ASM) set(LINKER_FLAGS_COMMON_MCU_STM32F4 "-T${CMAKE_SOURCE_DIR}/third-party/stm32f4.ld") # Debug - set(FLAGS_COMMON_DEBUG "") + set(FLAGS_COMMON_DEBUG "-nostdlib") + set(LINKER_FLAGS_COMMON_DEBUG "-nostdlib") # Release - set(FLAGS_COMMON_RELEASE "-Os -flto") + set(FLAGS_COMMON_RELEASE "-Os -flto -nostdlib") + set(LINKER_FLAGS_COMMON_RELEASE "-nostdlib") # Unit tests - set(FLAGS_UNIT_TEST "${FLAGS_COMMON_RELEASE}") + set(FLAGS_COMMON_UNITTESTS "-O3 -flto -nodefaultlibs") # Include directories - set(INCLUDE_CORE - ${CMAKE_SOURCE_DIR}/core) + # Core interface + set(INCLUDE_CORE_INTERFACE + ${CMAKE_SOURCE_DIR}/core) # Sources # Platform-specific @@ -256,7 +260,7 @@ project (Jerry CXX C ASM) set_property(TARGET ${TARGET_NAME} PROPERTY LINK_FLAGS "${COMPILE_FLAGS_JERRY} ${CXX_FLAGS_JERRY} ${FLAGS_COMMON_${BUILD_MODE}} ${LINKER_FLAGS_COMMON} ${LINKER_FLAGS_STATIC}") target_compile_definitions(${TARGET_NAME} PRIVATE ${DEFINES_JERRY}) - target_include_directories(${TARGET_NAME} PRIVATE ${INCLUDE_CORE}) + target_include_directories(${TARGET_NAME} PRIVATE ${INCLUDE_CORE_INTERFACE}) target_link_libraries(${TARGET_NAME} ${CORE_TARGET_NAME} ${LIBC_TARGET_NAME} imported_libgcc) if("${PLATFORM}" STREQUAL "MCU") @@ -285,31 +289,26 @@ project (Jerry CXX C ASM) endforeach() endfunction() - foreach(BUILD_MODE ${BUILD_MODES}) - declare_targets_for_build_mode(${BUILD_MODE}) - endforeach() + declare_targets_for_build_mode(DEBUG) + declare_targets_for_build_mode(RELEASE) # Unit tests declaration -# --- add_custom_target(unittests) -# --- -# --- add_library(unit_tests.lib STATIC ${SOURCE_CORE} ${SOURCE_JERRY_LIBC_LINUX}) -# --- target_compile_options(unit_tests.lib PRIVATE ${CXX_FLAGS_JERRY} ${CXX_FLAGS_COMMON_ARCH} ${CXX_FLAGS_UNIT_TEST}) -# --- target_compile_definitions(unit_tests.lib PRIVATE ${DEFINES_JERRY} ${DEFINES_UNIT_TEST}) -# --- target_include_directories(unit_tests.lib PRIVATE ${INCLUDE_CORE} ${INCLUDE_UNIT_TEST}) -# --- -# --- foreach(SOURCE_UNIT_TEST_MAIN ${SOURCE_UNIT_TEST_MAIN_MODULES}) -# --- get_filename_component(TARGET_NAME ${SOURCE_UNIT_TEST_MAIN} NAME_WE) -# --- set(TARGET_NAME unit_${TARGET_NAME}) -# --- -# --- add_executable(${TARGET_NAME} ${SOURCE_UNIT_TEST_MAIN}) -# --- target_compile_options(${TARGET_NAME} PRIVATE -# --- ${CXX_FLAGS_JERRY} -# --- ${CXX_FLAGS_COMMON_ARCH} -# --- ${CXX_FLAGS_UNIT_TEST}) -# --- set_property(TARGET ${TARGET_NAME} PROPERTY LINK_FLAGS "${LINKER_FLAGS_COMMON} ${LINKER_FLAGS_UNIT_TEST}") -# --- target_compile_definitions(${TARGET_NAME} PRIVATE ${DEFINES_JERRY} ${DEFINES_UNIT_TEST}) -# --- target_include_directories(${TARGET_NAME} PRIVATE ${INCLUDE_CORE} ${INCLUDE_UNIT_TEST}) -# --- target_link_libraries(${TARGET_NAME} unit_tests.lib imported_libc imported_libgcc) -# --- -# --- add_dependencies(unittests ${TARGET_NAME}) -# --- endforeach() + add_custom_target(unittests) + + foreach(SOURCE_UNIT_TEST_MAIN ${SOURCE_UNIT_TEST_MAIN_MODULES}) + get_filename_component(TARGET_NAME ${SOURCE_UNIT_TEST_MAIN} NAME_WE) + set(TARGET_NAME unit_${TARGET_NAME}) + + set(CORE_TARGET_NAME unittests.jerry-core) + + add_executable(${TARGET_NAME} ${SOURCE_UNIT_TEST_MAIN}) + set_property(TARGET ${TARGET_NAME} + PROPERTY COMPILE_FLAGS "${COMPILE_FLAGS_JERRY} ${CXX_FLAGS_JERRY} ${FLAGS_UNIT_TEST}") + set_property(TARGET ${TARGET_NAME} + PROPERTY LINK_FLAGS "${COMPILE_FLAGS_JERRY} ${CXX_FLAGS_JERRY} ${FLAGS_COMMON_UNITTESTS} ${LINKER_FLAGS_COMMON}") + target_compile_definitions(${TARGET_NAME} PRIVATE ${DEFINES_UNIT_TEST}) + target_include_directories(${TARGET_NAME} PRIVATE ${INCLUDE_CORE}) + target_link_libraries(${TARGET_NAME} ${CORE_TARGET_NAME} imported_libc imported_libgcc) + + add_dependencies(unittests ${TARGET_NAME}) + endforeach() diff --git a/Makefile b/Makefile index 5af576671..881020133 100644 --- a/Makefile +++ b/Makefile @@ -29,10 +29,8 @@ # For list of modifiers for PC target - see TARGET_PC_MODS, for MCU target - TARGET_MCU_MODS. # # Target action part (optional, after second dot): -# check - run cppcheck on src folder, unit and other tests # flash - flash specified mcu target binary # -# # Unit test target: unittests_run # @@ -93,7 +91,7 @@ export SHELL=/bin/bash BUILD_DIRS_STM32F4 := $(foreach _OPTIONS_COMBINATION,$(OPTIONS_COMBINATIONS),$(BUILD_DIR_PREFIX)$(_OPTIONS_COMBINATION)/stm32f4) # All together - BUILD_DIRS_ALL := $(BUILD_DIRS_NATIVE) $(BUILD_DIRS_STM32F3) $(BUILD_DIRS_STM32F4) + BUILD_DIRS_ALL := $(BUILD_DIRS_NATIVE) $(BUILD_DIRS_STM32F3) $(BUILD_DIRS_STM32F4) # Current BUILD_DIR := ./build/obj$(OPTIONS_STRING) @@ -142,7 +140,7 @@ $(JERRY_STM32F4_TARGETS): $(BUILD_DIR)/stm32f4 @ cp $(BUILD_DIR)/stm32f4/$@ $(OUT_DIR)/$@/jerry @ cp $(BUILD_DIR)/stm32f4/$@.bin $(OUT_DIR)/$@/jerry.bin -build: $(JERRY_TARGETS) # unittests +build: $(JERRY_TARGETS) unittests $(FLASH_TARGETS): $(BUILD_DIR)/mcu @$(MAKE) -C $(BUILD_DIR)/mcu VERBOSE=1 $@ 1>/dev/null @@ -167,10 +165,10 @@ precommit: clean @ echo -e "\nBuilding...\n\n" @ $(MAKE) build @ echo -e "\n================ Build completed successfully. Running precommit tests ================\n" - @ #echo -e "All targets were built successfully. Starting unit tests' run.\n" - @ #$(MAKE) unittests_run TESTS_OPTS="--silent" - @ #echo -e "Unit tests completed successfully. Starting parse-only testing.\n" - @ #echo -e "All targets were built successfully. Starting parse-only testing.\n" + @ echo -e "All targets were built successfully. Starting unit tests' run.\n" + @ $(MAKE) unittests_run TESTS_OPTS="--silent" + @ echo -e "Unit tests completed successfully. Starting parse-only testing.\n" + @ echo -e "All targets were built successfully. Starting parse-only testing.\n" @ # Parse-only testing @ for path in "./tests/jerry" "./tests/benchmarks/jerry"; \ do \ @@ -221,10 +219,10 @@ precommit: clean done @ echo -e "Full testing completed successfully\n\n================\n\n" -#unittests_run: unittests -# @$(MAKE) -s -f Makefile.mk TARGET=$@ $@ +unittests_run: unittests + @$(MAKE) -s -f Makefile.mk TARGET=$@ $@ clean: - @ rm -rf $(BUILD_DIRS_ALL) $(OUT_DIR) + @ rm -rf $(BUILD_DIR_PREFIX)* $(OUT_DIR) .PHONY: clean build unittests_run $(JERRY_TARGETS) $(FLASH_TARGETS) diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 85fb64c89..ee6b0cb44 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -41,6 +41,9 @@ project (JerryCore CXX C ASM) # Release set(DEFINES_JERRY_RELEASE JERRY_NDEBUG) + + # Unit tests + set(DEFINES_JERRY_UNITTESTS ) # Modifiers # Full profile @@ -139,6 +142,11 @@ project (JerryCore CXX C ASM) PROPERTY COMPILE_FLAGS "${COMPILE_FLAGS_JERRY} ${CXX_FLAGS_JERRY} ${FLAGS_COMMON_${BUILD_MODE}}") target_compile_definitions(${TARGET_NAME}.jerry-core PRIVATE ${DEFINES_JERRY}) target_include_directories(${TARGET_NAME}.jerry-core PRIVATE ${INCLUDE_CORE}) + + if("${BUILD_MODE}" STREQUAL "UNITTESTS") + target_compile_definitions(${TARGET_NAME}.jerry-core INTERFACE ${DEFINES_JERRY}) + target_include_directories(${TARGET_NAME}.jerry-core INTERFACE ${INCLUDE_CORE}) + endif() endfunction() foreach(MODIFIERS_LIST ${MODIFIERS_LISTS}) @@ -148,6 +156,7 @@ project (JerryCore CXX C ASM) endforeach() endfunction() - foreach(BUILD_MODE ${BUILD_MODES}) - declare_targets_for_build_mode(${BUILD_MODE}) - endforeach() + declare_targets_for_build_mode(DEBUG) + declare_targets_for_build_mode(RELEASE) + declare_targets_for_build_mode(UNITTESTS) + diff --git a/jerry-libc/CMakeLists.txt b/jerry-libc/CMakeLists.txt index b921343f7..85159eb4e 100644 --- a/jerry-libc/CMakeLists.txt +++ b/jerry-libc/CMakeLists.txt @@ -146,6 +146,5 @@ set(COMPILE_FLAGS_LIBC "${COMPILE_FLAGS_JERRY} ${C_FLAGS_JERRY}") endforeach() endfunction() - foreach(BUILD_MODE ${BUILD_MODES}) - declare_targets_for_build_mode(${BUILD_MODE}) - endforeach() + declare_targets_for_build_mode(DEBUG) + declare_targets_for_build_mode(RELEASE) diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 7e525f69e..957c2a3e0 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -106,6 +106,5 @@ project (Jerry_Plugins CXX ASM) endif() endfunction() - foreach(BUILD_MODE ${BUILD_MODES}) - declare_targets_for_build_mode(${BUILD_MODE}) - endforeach() + declare_targets_for_build_mode(DEBUG) + declare_targets_for_build_mode(RELEASE) diff --git a/tests/unit/common.h b/tests/unit/common.h index f897d4aea..eb55f2b77 100644 --- a/tests/unit/common.h +++ b/tests/unit/common.h @@ -16,13 +16,12 @@ #ifndef COMMON_H #define COMMON_H -#include "jerry-libc.h" #include "literal.h" #define NAME_TO_ID(op) (__op__idx_##op) #define __OPCODE_SIZE(name, arg1, arg2, arg3) \ - sizeof (__op_##name) + 1, + (uint8_t) (sizeof (__op_##name) + 1), #define LP(s) create_literal_from_str_compute_len (s) #define NUM(s) create_literal_from_num (s) diff --git a/tests/unit/test_number_to_string.cpp b/tests/unit/test_number_to_string.cpp index b45c25d13..a0052f892 100644 --- a/tests/unit/test_number_to_string.cpp +++ b/tests/unit/test_number_to_string.cpp @@ -13,12 +13,12 @@ * limitations under the License. */ +#include +#include + #include "ecma-globals.h" #include "ecma-helpers.h" #include "jrt.h" -#include "jerry-libc.h" - -#include /** * Unit test's main function. @@ -44,17 +44,17 @@ main( int __attr_unused___ argc, const ecma_number_t nums[] = { - 1.0, - 0.5, - 12345.0, - 12345.123, - 1.0e-45, - -2.5e+38, - NAN, - INFINITY, - -INFINITY, - +0.0, - -0.0 + (ecma_number_t) 1.0, + (ecma_number_t) 0.5, + (ecma_number_t) 12345.0, + (ecma_number_t) 12345.123, + (ecma_number_t) 1.0e-45, + (ecma_number_t) -2.5e+38, + (ecma_number_t) NAN, + (ecma_number_t) INFINITY, + (ecma_number_t) -INFINITY, + (ecma_number_t) +0.0, + (ecma_number_t) -0.0 }; for (uint32_t i = 0; diff --git a/tests/unit/test_poolman.cpp b/tests/unit/test_poolman.cpp index 5eb053789..bd79e677d 100644 --- a/tests/unit/test_poolman.cpp +++ b/tests/unit/test_poolman.cpp @@ -17,21 +17,18 @@ * Unit test for pool manager. */ +#include +#include +#include +#include + #define JERRY_MEM_POOL_INTERNAL #include "jrt.h" -#include "jerry-libc.h" #include "mem-allocator.h" #include "mem-pool.h" #include "mem-poolman.h" -extern "C" -{ - extern void srand (unsigned int __seed); - extern int rand (void); - extern long int time (long int *__timer); -} - // Iterations count const uint32_t test_iters = 16384; diff --git a/tests/unit/test_preparser.cpp b/tests/unit/test_preparser.cpp index 5846c6207..99ee24833 100644 --- a/tests/unit/test_preparser.cpp +++ b/tests/unit/test_preparser.cpp @@ -13,13 +13,14 @@ * limitations under the License. */ +#include + #include "jrt.h" #include "mem-allocator.h" #include "opcodes.h" #include "deserializer.h" #include "common.h" #include "parser.h" -#include "jerry-libc.h" /** * Unit test's main function. diff --git a/tests/unit/test_string_to_number.cpp b/tests/unit/test_string_to_number.cpp index 2ef79147b..e02b5c342 100644 --- a/tests/unit/test_string_to_number.cpp +++ b/tests/unit/test_string_to_number.cpp @@ -13,12 +13,11 @@ * limitations under the License. */ +#include + #include "ecma-globals.h" #include "ecma-helpers.h" #include "jrt.h" -#include "jerry-libc.h" - -#include /** * Unit test's main function. @@ -51,24 +50,24 @@ main( int __attr_unused___ argc, const ecma_number_t nums[] = { - 1.0, - 0.5, - 12345.0, - 1.0e-45, - -2.5e+38, - -2.5e+38, - NAN, - NAN, - NAN, - NAN, - NAN, - NAN, - NAN, - NAN, - INFINITY, - -INFINITY, - +0.0, - -0.0 + (ecma_number_t) 1.0, + (ecma_number_t) 0.5, + (ecma_number_t) 12345.0, + (ecma_number_t) 1.0e-45, + (ecma_number_t) -2.5e+38, + (ecma_number_t) -2.5e+38, + (ecma_number_t) NAN, + (ecma_number_t) NAN, + (ecma_number_t) NAN, + (ecma_number_t) NAN, + (ecma_number_t) NAN, + (ecma_number_t) NAN, + (ecma_number_t) NAN, + (ecma_number_t) NAN, + (ecma_number_t) INFINITY, + (ecma_number_t) -INFINITY, + (ecma_number_t) +0.0, + (ecma_number_t) -0.0 }; for (uint32_t i = 0;