feat(EditorConfig): Add .editorconfig files

This commit is contained in:
Jiří Štefka 2023-12-10 01:25:53 +01:00
parent 1f8dce3a86
commit 96477dfba3
Signed by: jiriks74
GPG key ID: 1D5E30D3DB2264DE
12 changed files with 400 additions and 0 deletions

80
01/tests/CMakeLists.txt Normal file
View file

@ -0,0 +1,80 @@
set(PROJECT_NAME ${PROJECT_NAME} PARENT_SCOPE )
# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Get GoogleTest
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
add_subdirectory(src)
add_executable(tests ${TESTS})
# Link test executable against gtest & gtest_main
target_link_libraries(
tests
GTest::gtest_main
)
target_include_directories(tests PRIVATE
${CMAKE_SOURCE_DIR}/src
)
# Discover tests
include(GoogleTest)
gtest_discover_tests(tests)
add_dependencies(tests ${PROJECT_NAME})
# The following section is inspired by https://github.com/cmake-modules/lcov
if(ENABLE_COVERAGE)
message("Test coverage enabled")
# set(exclude_dir "*/tests/* */_deps/* /usr/include/c++/11/**/* /usr/include/c++/**/*")
# set(exclude_dir "*/tests/* */_deps/* /usr/include/c++/11/tuple /usr/include/c++/11/**/*")
# Check for lcov, gcov and genhtml
find_program(GCOV gcov)
if (NOT GCOV)
message(WARNING "gcov not found")
endif()
find_program(LCOV lcov)
if (NOT LCOV)
message(WARNING "lcov not found")
endif()
find_program(GENHTML genhtml)
if (NOT GENHTML)
message(WARNING "genhtml not found")
endif()
if (GCOV AND LCOV AND GENHTML)
# Set C compiler flags
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage -fprofile-arcs -ftest-coverage")
# Set C++ compiler flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage -fprofile-arcs -ftest-coverage")
set(covname cov.info)
add_custom_target(coverage DEPENDS ${covname})
add_dependencies(coverage tests ${PROJECT_NAME})
add_custom_command(
OUTPUT ${covname}
COMMAND ${LCOV} -c -o ${covname} -d ${CMAKE_BINARY_DIR}/tests/CMakeFiles/tests.dir/ -b . --gcov-tool ${GCOV}
COMMAND ${LCOV} -r ${covname} -o ${covname} "*/tests/*" "*/_deps/**/*" "/usr/include/c++/**/*" "/usr/include/c++/11/**/*"
COMMAND ${LCOV} -l ${covname}
COMMAND ${GENHTML} ${covname} -output coverage
COMMAND ${LCOV} -l ${covname} 2>/dev/null | grep Total | sed 's/|//g' | sed 's/Total://g' | awk '{print $1}' | sed s/%//g > coverage/total
)
set_directory_properties(PROPERTIES
ADDITIONAL_CLEAN_FILES ${covname}
)
set_directory_properties(PROPERTIES
ADDITIONAL_CLEAN_FILES coverage/
)
else()
message(WARNING "Cannot enable coverage. Missing the required tools")
endif()
endif()

View file

@ -0,0 +1,17 @@
# Make an explicit list of all source files in `IFJ23_TESTS`. This is important
# because CMake is not a build system: it is a build system generator. Suppose
# you add a file foo.cpp to src/ after running cmake .. . If you set
# `IFJ23_TESTS` with `file(GLOB ... )`, this is not passed to the makefile;
# the makefile doesn't know that foo.cpp exists and will not re-run cmake. Your
# collaborator's builds will fail and it will be unclear why. Whether you use
# file(GLOB ...) or not, you will need to re-run cmake, but with an explicit
# file list, you know beforehand why your code isn't compiling.
set(TESTS
test.cpp
)
# Form the full path to the source files...
PREPEND(TESTS)
# ... and pass the variable to the parent scope.
set(TESTS ${TESTS} PARENT_SCOPE)

31
01/tests/src/test.cpp Normal file
View file

@ -0,0 +1,31 @@
#include <gtest/gtest.h>
#define TESTING
// Include the source file(s) to be tested.
#include "main.c"
// Create a test fixture class template - this will be like a "conlection" of
// tests. the : public ::testing::Test part is important! Add it to your fixture
// class.
class HelloTest : public ::testing::Test {
HelloTest() {}
~HelloTest() {}
void SetUp() {}
void TearDown() {}
};
// Add tests to the test fixture class.
// @param fixture_class_name The name of the test fixture class.
// @param test_name The name of the test.
TEST(HelloTest, BasicAssertions) {
// Execute the code to be tested.
// Expect two strings not to be equal.
EXPECT_STRNE("hello", "world");
// Expect equality.
EXPECT_EQ(7 * 6, 42);
}