Skip to content

Commit

Permalink
Added a test program, which produces random format strings and applie…
Browse files Browse the repository at this point in the history
…s them to random values (of the appropriate type) - checking the results of this library against the standard C library's `sprintf()`. This is not intended to replace the existing tests nor even be run by default, but rather to allow developers/maintainers to more easily find corner-case discrepancies while working on the library.
  • Loading branch information
mickjc750 authored and eyalroz committed Aug 3, 2021
1 parent 03697d3 commit 3183f66
Show file tree
Hide file tree
Showing 3 changed files with 697 additions and 52 deletions.
63 changes: 63 additions & 0 deletions autotest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
cmake_minimum_required(VERSION 3.9)

enable_language(CXX)

add_executable(autotest "autotest.cpp")

set_target_properties(
autotest
PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
)

add_definitions(-DNO_OVERRIDE_STDIO)

option(TEST_BROKEN_FORMATS "Include tests using non-standard-compliant format strings?" ON)
# ... don't worry, we'll suppress the compiler warnings for those.
if (TEST_BROKEN_FORMATS)
target_compile_definitions(autotest PRIVATE TEST_WITH_NON_STANDARD_FORMAT_STRINGS)
endif()

target_link_libraries(autotest PRIVATE mpaland-printf)

if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
target_compile_options(autoteste PRIVATE /W4)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR
CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# lots of warnings and all warnings as errors
target_compile_options(autotest PRIVATE
-g
-Wall
-Wextra
-pedantic
-Wundef
-Wsign-conversion
-Wuninitialized
-Wshadow
-Wunreachable-code
-Wswitch-default
-Wswitch
-Wcast-align
-Wmissing-include-dirs
-Winit-self
-Wdouble-promotion
-gdwarf-2
-fno-exceptions
-ffunction-sections
-fdata-sections
-fverbose-asm
-Wunused-parameter
)
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(autotest PRIVATE -ffat-lto-objects)
endif()
endif()

add_test(
NAME ${PROJECT_NAME}.autotest
COMMAND autotest # ${TEST_RUNNER_PARAMS}
)


109 changes: 57 additions & 52 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,71 @@ cmake_minimum_required(VERSION 3.9)

enable_language(CXX)

add_executable(test_suite "test_suite.cpp")
set(test_targets test_suite autotest)

set_target_properties(
test_suite
PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
)
option(TEST_NONSTD_FORMATS "Include tests using non-standard-compliant format strings?" ON)
# ... don't worry, we'll suppress the compiler warnings for those.

# These two lines are necessary, since the test suite does not actually use the
# compiled library - it includes the library's source .c file; and that means we
# need to include the generated config.h file.
target_compile_definitions(test_suite PRIVATE PRINTF_INCLUDE_CONFIG_H)
target_include_directories(test_suite PRIVATE "${GENERATED_INCLUDE_DIR}")
foreach(tgt ${test_targets})
add_executable(${tgt} "${tgt}.cpp")
set_target_properties(
${tgt}
PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
)

option(TEST_BROKEN_FORMATS "Include tests using non-standard-compliant format strings?" ON)
# ... don't worry, we'll suppress the compiler warnings for those.
if (TEST_BROKEN_FORMATS)
target_compile_definitions(test_suite PRIVATE TEST_WITH_NON_STANDARD_FORMAT_STRINGS)
endif()
if (TEST_NONSTD_FORMATS)
target_compile_definitions(${tgt} PRIVATE TEST_WITH_NON_STANDARD_FORMAT_STRINGS)
endif()

target_link_libraries(test_suite PRIVATE mpaland-printf)

if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
target_compile_options(test_suite PRIVATE /W4)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR
CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# lots of warnings and all warnings as errors
target_compile_options(test_suite PRIVATE
-g
-Wall
-Wextra
-pedantic
-Wundef
-Wsign-conversion
-Wuninitialized
-Wshadow
-Wunreachable-code
-Wswitch-default
-Wswitch
-Wcast-align
-Wmissing-include-dirs
-Winit-self
-Wdouble-promotion
-gdwarf-2
-fno-exceptions
-ffunction-sections
-fdata-sections
-fverbose-asm
-Wunused-parameter
)
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(test_suite PRIVATE -ffat-lto-objects)
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
target_compile_options(test_suite PRIVATE /W4)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR
CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# lots of warnings and all warnings as errors
target_compile_options(${tgt} PRIVATE
-g
-Wall
-Wextra
-pedantic
-Wundef
-Wsign-conversion
-Wuninitialized
-Wshadow
-Wunreachable-code
-Wswitch-default
-Wswitch
-Wcast-align
-Wmissing-include-dirs
-Winit-self
-Wdouble-promotion
-gdwarf-2
-fno-exceptions
-ffunction-sections
-fdata-sections
-fverbose-asm
-Wunused-parameter
)
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(${tgt} PRIVATE -ffat-lto-objects)
endif()
endif()
endif()

endforeach()

# These two lines are necessary, since the test suite does not actually use the
# compiled library - it includes the library's source .c file; and that means we
# need to include the generated config.h file.
target_compile_definitions(test_suite PRIVATE PRINTF_INCLUDE_CONFIG_H)
target_include_directories(test_suite PRIVATE "${GENERATED_INCLUDE_DIR}")
add_test(
NAME ${PROJECT_NAME}.test_suite
COMMAND test_suite # ${TEST_RUNNER_PARAMS}
NAME "${PROJECT_NAME}.test_suite"
COMMAND "test_suite" # ${TEST_RUNNER_PARAMS}
)

target_link_libraries(autotest PRIVATE mpaland-printf)
# Not running autotest by default - it's randomized after all.

Loading

0 comments on commit 3183f66

Please sign in to comment.