Compare commits
2 Commits
develop
...
0cf7cb79af
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0cf7cb79af | ||
|
|
46d2e75d2f |
81
.drone.yml
81
.drone.yml
@ -4,84 +4,41 @@ type: docker
|
|||||||
name: main
|
name: main
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: fetch tags
|
- name: Tag commit
|
||||||
image: alpine/git
|
image: alpine
|
||||||
commands:
|
commands:
|
||||||
- git fetch --tags
|
- echo This would create a tag
|
||||||
- name: update version and changelog
|
|
||||||
image: registry.riba-interactive.de/conventi:0.1.0
|
|
||||||
- name: commit changelog updates
|
|
||||||
image: alpine/git
|
|
||||||
commands:
|
|
||||||
- git add .
|
|
||||||
- git commit -m "[CI SKIP] version and changelog update"
|
|
||||||
- git push origin $DRONE_TARGET_BRANCH
|
|
||||||
- name: tag commit
|
|
||||||
image: registry.riba-interactive.de/conventi:0.1.0
|
|
||||||
commands:
|
|
||||||
- export version=$(conventi.sh get_version)
|
|
||||||
- git tag -am "Tagging new version $version" "$version"
|
|
||||||
- git push origin "$version"
|
|
||||||
|
|
||||||
trigger:
|
trigger:
|
||||||
branch:
|
branch:
|
||||||
- main
|
- main
|
||||||
event:
|
|
||||||
- push
|
|
||||||
|
|
||||||
image_pull_secrets:
|
|
||||||
- dockerconfig
|
|
||||||
---
|
---
|
||||||
kind: pipeline
|
kind: pipeline
|
||||||
type: docker
|
type: docker
|
||||||
name: tag
|
name: develop
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Build release
|
- name: Tag commit
|
||||||
image: registry.riba-interactive.de/alpine-build:4
|
|
||||||
commands:
|
|
||||||
- cmake -S. -Bcmake-build-ci -DCMAKE_BUILD_TYPE=Release
|
|
||||||
- cd cmake-build-ci
|
|
||||||
- cmake --build .
|
|
||||||
- echo "Done"
|
|
||||||
|
|
||||||
trigger:
|
|
||||||
event:
|
|
||||||
- tag
|
|
||||||
|
|
||||||
image_pull_secrets:
|
|
||||||
- dockerconfig
|
|
||||||
---
|
|
||||||
kind: pipeline
|
|
||||||
type: docker
|
|
||||||
name: Build and Test
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Build and run tests
|
|
||||||
image: registry.riba-interactive.de/alpine-build:4
|
|
||||||
commands:
|
|
||||||
- cmake -S. -Bcmake-build-ci -DCMAKE_BUILD_TYPE=Debug -DENABLE_TEST_COVERAGE=on
|
|
||||||
- cd cmake-build-ci
|
|
||||||
- cmake --build .
|
|
||||||
- make coverage
|
|
||||||
- name: Generate memory report
|
|
||||||
image: registry.riba-interactive.de/reportly:latest
|
|
||||||
settings:
|
|
||||||
input_path: cmake-build-ci
|
|
||||||
output_path: .reports
|
|
||||||
debug: true
|
|
||||||
|
|
||||||
- name: Report test and coverage
|
|
||||||
image: alpine
|
image: alpine
|
||||||
commands:
|
commands:
|
||||||
- echo "Do some curl or so"
|
- echo This would build the app and run test
|
||||||
when:
|
|
||||||
event:
|
|
||||||
- pull_request
|
|
||||||
|
|
||||||
trigger:
|
trigger:
|
||||||
branch:
|
branch:
|
||||||
- develop
|
- develop
|
||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: feature
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Run tests
|
||||||
|
image: registry.riba-interactive.de/alpine-build:latest
|
||||||
|
commands:
|
||||||
|
- ctest
|
||||||
|
|
||||||
|
trigger:
|
||||||
|
branch:
|
||||||
- feature/*
|
- feature/*
|
||||||
|
|
||||||
image_pull_secrets:
|
image_pull_secrets:
|
||||||
|
|||||||
@ -1,5 +0,0 @@
|
|||||||
{
|
|
||||||
"version": "0.1.1",
|
|
||||||
"sha": "dc2e644",
|
|
||||||
"url": "https://git.riba-interactive.de/rick/waitui"
|
|
||||||
}
|
|
||||||
33
CHANGELOG.md
33
CHANGELOG.md
@ -1,33 +0,0 @@
|
|||||||
# Changelog
|
|
||||||
|
|
||||||
All notable changes to this project will be documented in this file. See [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) for commit guidelines.
|
|
||||||
|
|
||||||
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
||||||
|
|
||||||
----
|
|
||||||
|
|
||||||
## [0.1.1](https://git.riba-interactive.de/rick/waitui/compare/3fe3a2a...dc2e644) (2022-06-01)
|
|
||||||
|
|
||||||
|
|
||||||
### Bug fixes 🩹
|
|
||||||
|
|
||||||
* **conventi:** fix `conventi.sh` function `get_commit_body()` ([dc2e644](https://git.riba-interactive.de/rick/waitui/commit/dc2e644))
|
|
||||||
|
|
||||||
## [0.1.0](https://git.riba-interactive.de/rick/waitui/compare/dafd1ae...3fe3a2a) (2022-06-01)
|
|
||||||
|
|
||||||
|
|
||||||
### Bug fixes 🩹
|
|
||||||
|
|
||||||
* **pipeline:** fix `git push` parameters ([3fe3a2a](https://git.riba-interactive.de/rick/waitui/commit/3fe3a2a))
|
|
||||||
* **pipeline:** push version and changelog after update ([e992129](https://git.riba-interactive.de/rick/waitui/commit/e992129))
|
|
||||||
* **pipeline:** fix image pull in pipeline ([cda7b95](https://git.riba-interactive.de/rick/waitui/commit/cda7b95))
|
|
||||||
|
|
||||||
|
|
||||||
### Features 📦
|
|
||||||
|
|
||||||
* add version and changelog update to pipeline ([7dd39a9](https://git.riba-interactive.de/rick/waitui/commit/7dd39a9))
|
|
||||||
|
|
||||||
|
|
||||||
### Chores 🧽
|
|
||||||
|
|
||||||
* **release:** release the lastest develop ([cae8167](https://git.riba-interactive.de/rick/waitui/commit/cae8167))
|
|
||||||
@ -13,34 +13,16 @@ set(CMAKE_C_STANDARD 17)
|
|||||||
|
|
||||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
|
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
|
||||||
|
|
||||||
option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF)
|
|
||||||
|
|
||||||
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
|
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
|
||||||
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
|
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
|
||||||
include(CTest)
|
include(CTest)
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
add_subdirectory(app)
|
add_subdirectory(app)
|
||||||
add_subdirectory(library/hashtable)
|
|
||||||
add_subdirectory(library/list)
|
add_subdirectory(library/list)
|
||||||
add_subdirectory(library/log)
|
add_subdirectory(library/log)
|
||||||
add_subdirectory(library/str)
|
|
||||||
|
|
||||||
if ((CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR MODERN_CMAKE_BUILD_TESTING)
|
if ((CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR MODERN_CMAKE_BUILD_TESTING)
|
||||||
AND BUILD_TESTING)
|
AND BUILD_TESTING)
|
||||||
add_subdirectory(tests)
|
add_subdirectory(tests)
|
||||||
|
|
||||||
if (ENABLE_TEST_COVERAGE)
|
|
||||||
include(CodeCoverage)
|
|
||||||
|
|
||||||
set(MEMORYCHECK_COMMAND_OPTIONS "--trace-children=yes --leak-check=full")
|
|
||||||
|
|
||||||
setup_target_for_coverage_lcov(
|
|
||||||
NAME coverage
|
|
||||||
EXECUTABLE ${CMAKE_CTEST_COMMAND} -T memcheck
|
|
||||||
EXCLUDE "tests/*"
|
|
||||||
LCOV_ARGS --rc lcov_branch_coverage=1
|
|
||||||
GENHTML_ARGS --rc genhtml_branch_coverage=1
|
|
||||||
)
|
|
||||||
endif ()
|
|
||||||
endif ()
|
endif ()
|
||||||
|
|||||||
@ -33,4 +33,4 @@
|
|||||||
|
|
||||||
#define WAITUI_VERSION_LONG @PROJECT_VERSION_LONG@L
|
#define WAITUI_VERSION_LONG @PROJECT_VERSION_LONG@L
|
||||||
|
|
||||||
#endif //WAITUI_VERSION_H
|
#endif //WAITUI_VERSION_H
|
||||||
@ -1,4 +1,4 @@
|
|||||||
set(project_version 0.1.1)
|
set(project_version 0.0.1)
|
||||||
set(project_description "waitui executable")
|
set(project_description "waitui executable")
|
||||||
set(project_homepage "http://example.com")
|
set(project_homepage "http://example.com")
|
||||||
set(project_prerelease "")
|
set(project_prerelease "")
|
||||||
@ -160,4 +160,4 @@ int main(int argc, char **argv) {
|
|||||||
waitui_log_debug("waitui execution done");
|
waitui_log_debug("waitui execution done");
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -1,12 +0,0 @@
|
|||||||
FROM alpine:3.16.0
|
|
||||||
|
|
||||||
RUN echo "@testing https://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories; \
|
|
||||||
apk --no-cache add \
|
|
||||||
bison \
|
|
||||||
build-base \
|
|
||||||
cmake \
|
|
||||||
flex \
|
|
||||||
git \
|
|
||||||
ncurses-dev\
|
|
||||||
lcov@testing \
|
|
||||||
valgrind
|
|
||||||
@ -1,721 +0,0 @@
|
|||||||
# Copyright (c) 2012 - 2017, Lars Bilke
|
|
||||||
# All rights reserved.
|
|
||||||
#
|
|
||||||
# Redistribution and use in source and binary forms, with or without modification,
|
|
||||||
# are permitted provided that the following conditions are met:
|
|
||||||
#
|
|
||||||
# 1. Redistributions of source code must retain the above copyright notice, this
|
|
||||||
# list of conditions and the following disclaimer.
|
|
||||||
#
|
|
||||||
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
||||||
# this list of conditions and the following disclaimer in the documentation
|
|
||||||
# and/or other materials provided with the distribution.
|
|
||||||
#
|
|
||||||
# 3. Neither the name of the copyright holder nor the names of its contributors
|
|
||||||
# may be used to endorse or promote products derived from this software without
|
|
||||||
# specific prior written permission.
|
|
||||||
#
|
|
||||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
||||||
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
||||||
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
||||||
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
||||||
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
||||||
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
||||||
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
||||||
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
||||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
||||||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
#
|
|
||||||
# CHANGES:
|
|
||||||
#
|
|
||||||
# 2012-01-31, Lars Bilke
|
|
||||||
# - Enable Code Coverage
|
|
||||||
#
|
|
||||||
# 2013-09-17, Joakim Söderberg
|
|
||||||
# - Added support for Clang.
|
|
||||||
# - Some additional usage instructions.
|
|
||||||
#
|
|
||||||
# 2016-02-03, Lars Bilke
|
|
||||||
# - Refactored functions to use named parameters
|
|
||||||
#
|
|
||||||
# 2017-06-02, Lars Bilke
|
|
||||||
# - Merged with modified version from github.com/ufz/ogs
|
|
||||||
#
|
|
||||||
# 2019-05-06, Anatolii Kurotych
|
|
||||||
# - Remove unnecessary --coverage flag
|
|
||||||
#
|
|
||||||
# 2019-12-13, FeRD (Frank Dana)
|
|
||||||
# - Deprecate COVERAGE_LCOVR_EXCLUDES and COVERAGE_GCOVR_EXCLUDES lists in favor
|
|
||||||
# of tool-agnostic COVERAGE_EXCLUDES variable, or EXCLUDE setup arguments.
|
|
||||||
# - CMake 3.4+: All excludes can be specified relative to BASE_DIRECTORY
|
|
||||||
# - All setup functions: accept BASE_DIRECTORY, EXCLUDE list
|
|
||||||
# - Set lcov basedir with -b argument
|
|
||||||
# - Add automatic --demangle-cpp in lcovr, if 'c++filt' is available (can be
|
|
||||||
# overridden with NO_DEMANGLE option in setup_target_for_coverage_lcovr().)
|
|
||||||
# - Delete output dir, .info file on 'make clean'
|
|
||||||
# - Remove Python detection, since version mismatches will break gcovr
|
|
||||||
# - Minor cleanup (lowercase function names, update examples...)
|
|
||||||
#
|
|
||||||
# 2019-12-19, FeRD (Frank Dana)
|
|
||||||
# - Rename Lcov outputs, make filtered file canonical, fix cleanup for targets
|
|
||||||
#
|
|
||||||
# 2020-01-19, Bob Apthorpe
|
|
||||||
# - Added gfortran support
|
|
||||||
#
|
|
||||||
# 2020-02-17, FeRD (Frank Dana)
|
|
||||||
# - Make all add_custom_target()s VERBATIM to auto-escape wildcard characters
|
|
||||||
# in EXCLUDEs, and remove manual escaping from gcovr targets
|
|
||||||
#
|
|
||||||
# 2021-01-19, Robin Mueller
|
|
||||||
# - Add CODE_COVERAGE_VERBOSE option which will allow to print out commands which are run
|
|
||||||
# - Added the option for users to set the GCOVR_ADDITIONAL_ARGS variable to supply additional
|
|
||||||
# flags to the gcovr command
|
|
||||||
#
|
|
||||||
# 2020-05-04, Mihchael Davis
|
|
||||||
# - Add -fprofile-abs-path to make gcno files contain absolute paths
|
|
||||||
# - Fix BASE_DIRECTORY not working when defined
|
|
||||||
# - Change BYPRODUCT from folder to index.html to stop ninja from complaining about double defines
|
|
||||||
#
|
|
||||||
# 2021-05-10, Martin Stump
|
|
||||||
# - Check if the generator is multi-config before warning about non-Debug builds
|
|
||||||
#
|
|
||||||
# 2022-02-22, Marko Wehle
|
|
||||||
# - Change gcovr output from -o <filename> for --xml <filename> and --html <filename> output respectively.
|
|
||||||
# This will allow for Multiple Output Formats at the same time by making use of GCOVR_ADDITIONAL_ARGS, e.g. GCOVR_ADDITIONAL_ARGS "--txt".
|
|
||||||
#
|
|
||||||
# USAGE:
|
|
||||||
#
|
|
||||||
# 1. Copy this file into your cmake modules path.
|
|
||||||
#
|
|
||||||
# 2. Add the following line to your CMakeLists.txt (best inside an if-condition
|
|
||||||
# using a CMake option() to enable it just optionally):
|
|
||||||
# include(CodeCoverage)
|
|
||||||
#
|
|
||||||
# 3. Append necessary compiler flags for all supported source files:
|
|
||||||
# append_coverage_compiler_flags()
|
|
||||||
# Or for specific target:
|
|
||||||
# append_coverage_compiler_flags_to_target(YOUR_TARGET_NAME)
|
|
||||||
#
|
|
||||||
# 3.a (OPTIONAL) Set appropriate optimization flags, e.g. -O0, -O1 or -Og
|
|
||||||
#
|
|
||||||
# 4. If you need to exclude additional directories from the report, specify them
|
|
||||||
# using full paths in the COVERAGE_EXCLUDES variable before calling
|
|
||||||
# setup_target_for_coverage_*().
|
|
||||||
# Example:
|
|
||||||
# set(COVERAGE_EXCLUDES
|
|
||||||
# '${PROJECT_SOURCE_DIR}/src/dir1/*'
|
|
||||||
# '/path/to/my/src/dir2/*')
|
|
||||||
# Or, use the EXCLUDE argument to setup_target_for_coverage_*().
|
|
||||||
# Example:
|
|
||||||
# setup_target_for_coverage_lcov(
|
|
||||||
# NAME coverage
|
|
||||||
# EXECUTABLE testrunner
|
|
||||||
# EXCLUDE "${PROJECT_SOURCE_DIR}/src/dir1/*" "/path/to/my/src/dir2/*")
|
|
||||||
#
|
|
||||||
# 4.a NOTE: With CMake 3.4+, COVERAGE_EXCLUDES or EXCLUDE can also be set
|
|
||||||
# relative to the BASE_DIRECTORY (default: PROJECT_SOURCE_DIR)
|
|
||||||
# Example:
|
|
||||||
# set(COVERAGE_EXCLUDES "dir1/*")
|
|
||||||
# setup_target_for_coverage_gcovr_html(
|
|
||||||
# NAME coverage
|
|
||||||
# EXECUTABLE testrunner
|
|
||||||
# BASE_DIRECTORY "${PROJECT_SOURCE_DIR}/src"
|
|
||||||
# EXCLUDE "dir2/*")
|
|
||||||
#
|
|
||||||
# 5. Use the functions described below to create a custom make target which
|
|
||||||
# runs your test executable and produces a code coverage report.
|
|
||||||
#
|
|
||||||
# 6. Build a Debug build:
|
|
||||||
# cmake -DCMAKE_BUILD_TYPE=Debug ..
|
|
||||||
# make
|
|
||||||
# make my_coverage_target
|
|
||||||
#
|
|
||||||
|
|
||||||
include(CMakeParseArguments)
|
|
||||||
|
|
||||||
option(CODE_COVERAGE_VERBOSE "Verbose information" FALSE)
|
|
||||||
|
|
||||||
# Check prereqs
|
|
||||||
find_program( GCOV_PATH gcov )
|
|
||||||
find_program( LCOV_PATH NAMES lcov lcov.bat lcov.exe lcov.perl)
|
|
||||||
find_program( FASTCOV_PATH NAMES fastcov fastcov.py )
|
|
||||||
find_program( GENHTML_PATH NAMES genhtml genhtml.perl genhtml.bat )
|
|
||||||
find_program( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/scripts/test)
|
|
||||||
find_program( CPPFILT_PATH NAMES c++filt )
|
|
||||||
|
|
||||||
if(NOT GCOV_PATH)
|
|
||||||
message(FATAL_ERROR "gcov not found! Aborting...")
|
|
||||||
endif() # NOT GCOV_PATH
|
|
||||||
|
|
||||||
get_property(LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
|
|
||||||
list(GET LANGUAGES 0 LANG)
|
|
||||||
|
|
||||||
if("${CMAKE_${LANG}_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang")
|
|
||||||
if("${CMAKE_${LANG}_COMPILER_VERSION}" VERSION_LESS 3)
|
|
||||||
message(FATAL_ERROR "Clang version must be 3.0.0 or greater! Aborting...")
|
|
||||||
endif()
|
|
||||||
elseif (CMAKE_${LANG}_COMPILER_ID STREQUAL "GNU")
|
|
||||||
# Do nothing; exit conditional without error if true
|
|
||||||
elseif(NOT CMAKE_COMPILER_IS_GNUCXX)
|
|
||||||
if("${CMAKE_Fortran_COMPILER_ID}" MATCHES "[Ff]lang")
|
|
||||||
# Do nothing; exit conditional without error if true
|
|
||||||
elseif("${CMAKE_Fortran_COMPILER_ID}" MATCHES "GNU")
|
|
||||||
# Do nothing; exit conditional without error if true
|
|
||||||
else()
|
|
||||||
message(FATAL_ERROR "Compiler is not GNU gcc! Aborting...")
|
|
||||||
endif()
|
|
||||||
endif()
|
|
||||||
|
|
||||||
set(COVERAGE_COMPILER_FLAGS "-g -fprofile-arcs -ftest-coverage"
|
|
||||||
CACHE INTERNAL "")
|
|
||||||
if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)")
|
|
||||||
include(CheckCXXCompilerFlag)
|
|
||||||
check_cxx_compiler_flag(-fprofile-abs-path HAVE_fprofile_abs_path)
|
|
||||||
if(HAVE_fprofile_abs_path)
|
|
||||||
set(COVERAGE_COMPILER_FLAGS "${COVERAGE_COMPILER_FLAGS} -fprofile-abs-path")
|
|
||||||
endif()
|
|
||||||
endif()
|
|
||||||
|
|
||||||
set(CMAKE_Fortran_FLAGS_COVERAGE
|
|
||||||
${COVERAGE_COMPILER_FLAGS}
|
|
||||||
CACHE STRING "Flags used by the Fortran compiler during coverage builds."
|
|
||||||
FORCE )
|
|
||||||
set(CMAKE_CXX_FLAGS_COVERAGE
|
|
||||||
${COVERAGE_COMPILER_FLAGS}
|
|
||||||
CACHE STRING "Flags used by the C++ compiler during coverage builds."
|
|
||||||
FORCE )
|
|
||||||
set(CMAKE_C_FLAGS_COVERAGE
|
|
||||||
${COVERAGE_COMPILER_FLAGS}
|
|
||||||
CACHE STRING "Flags used by the C compiler during coverage builds."
|
|
||||||
FORCE )
|
|
||||||
set(CMAKE_EXE_LINKER_FLAGS_COVERAGE
|
|
||||||
""
|
|
||||||
CACHE STRING "Flags used for linking binaries during coverage builds."
|
|
||||||
FORCE )
|
|
||||||
set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
|
|
||||||
""
|
|
||||||
CACHE STRING "Flags used by the shared libraries linker during coverage builds."
|
|
||||||
FORCE )
|
|
||||||
mark_as_advanced(
|
|
||||||
CMAKE_Fortran_FLAGS_COVERAGE
|
|
||||||
CMAKE_CXX_FLAGS_COVERAGE
|
|
||||||
CMAKE_C_FLAGS_COVERAGE
|
|
||||||
CMAKE_EXE_LINKER_FLAGS_COVERAGE
|
|
||||||
CMAKE_SHARED_LINKER_FLAGS_COVERAGE )
|
|
||||||
|
|
||||||
get_property(GENERATOR_IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
|
|
||||||
if(NOT (CMAKE_BUILD_TYPE STREQUAL "Debug" OR GENERATOR_IS_MULTI_CONFIG))
|
|
||||||
message(WARNING "Code coverage results with an optimised (non-Debug) build may be misleading")
|
|
||||||
endif() # NOT (CMAKE_BUILD_TYPE STREQUAL "Debug" OR GENERATOR_IS_MULTI_CONFIG)
|
|
||||||
|
|
||||||
if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
|
|
||||||
link_libraries(gcov)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# Defines a target for running and collection code coverage information
|
|
||||||
# Builds dependencies, runs the given executable and outputs reports.
|
|
||||||
# NOTE! The executable should always have a ZERO as exit code otherwise
|
|
||||||
# the coverage generation will not complete.
|
|
||||||
#
|
|
||||||
# setup_target_for_coverage_lcov(
|
|
||||||
# NAME testrunner_coverage # New target name
|
|
||||||
# EXECUTABLE testrunner -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR
|
|
||||||
# DEPENDENCIES testrunner # Dependencies to build first
|
|
||||||
# BASE_DIRECTORY "../" # Base directory for report
|
|
||||||
# # (defaults to PROJECT_SOURCE_DIR)
|
|
||||||
# EXCLUDE "src/dir1/*" "src/dir2/*" # Patterns to exclude (can be relative
|
|
||||||
# # to BASE_DIRECTORY, with CMake 3.4+)
|
|
||||||
# NO_DEMANGLE # Don't demangle C++ symbols
|
|
||||||
# # even if c++filt is found
|
|
||||||
# )
|
|
||||||
function(setup_target_for_coverage_lcov)
|
|
||||||
|
|
||||||
set(options NO_DEMANGLE)
|
|
||||||
set(oneValueArgs BASE_DIRECTORY NAME)
|
|
||||||
set(multiValueArgs EXCLUDE EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES LCOV_ARGS GENHTML_ARGS)
|
|
||||||
cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
|
|
||||||
|
|
||||||
if(NOT LCOV_PATH)
|
|
||||||
message(FATAL_ERROR "lcov not found! Aborting...")
|
|
||||||
endif() # NOT LCOV_PATH
|
|
||||||
|
|
||||||
if(NOT GENHTML_PATH)
|
|
||||||
message(FATAL_ERROR "genhtml not found! Aborting...")
|
|
||||||
endif() # NOT GENHTML_PATH
|
|
||||||
|
|
||||||
# Set base directory (as absolute path), or default to PROJECT_SOURCE_DIR
|
|
||||||
if(DEFINED Coverage_BASE_DIRECTORY)
|
|
||||||
get_filename_component(BASEDIR ${Coverage_BASE_DIRECTORY} ABSOLUTE)
|
|
||||||
else()
|
|
||||||
set(BASEDIR ${PROJECT_SOURCE_DIR})
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# Collect excludes (CMake 3.4+: Also compute absolute paths)
|
|
||||||
set(LCOV_EXCLUDES "")
|
|
||||||
foreach(EXCLUDE ${Coverage_EXCLUDE} ${COVERAGE_EXCLUDES} ${COVERAGE_LCOV_EXCLUDES})
|
|
||||||
if(CMAKE_VERSION VERSION_GREATER 3.4)
|
|
||||||
get_filename_component(EXCLUDE ${EXCLUDE} ABSOLUTE BASE_DIR ${BASEDIR})
|
|
||||||
endif()
|
|
||||||
list(APPEND LCOV_EXCLUDES "${EXCLUDE}")
|
|
||||||
endforeach()
|
|
||||||
list(REMOVE_DUPLICATES LCOV_EXCLUDES)
|
|
||||||
|
|
||||||
# Conditional arguments
|
|
||||||
if(CPPFILT_PATH AND NOT ${Coverage_NO_DEMANGLE})
|
|
||||||
set(GENHTML_EXTRA_ARGS "--demangle-cpp")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# Setting up commands which will be run to generate coverage data.
|
|
||||||
# Cleanup lcov
|
|
||||||
set(LCOV_CLEAN_CMD
|
|
||||||
${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} -directory .
|
|
||||||
-b ${BASEDIR} --zerocounters
|
|
||||||
)
|
|
||||||
# Create baseline to make sure untouched files show up in the report
|
|
||||||
set(LCOV_BASELINE_CMD
|
|
||||||
${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} -c -i -d . -b
|
|
||||||
${BASEDIR} -o ${Coverage_NAME}.base
|
|
||||||
)
|
|
||||||
# Run tests
|
|
||||||
set(LCOV_EXEC_TESTS_CMD
|
|
||||||
${Coverage_EXECUTABLE} ${Coverage_EXECUTABLE_ARGS}
|
|
||||||
)
|
|
||||||
# Capturing lcov counters and generating report
|
|
||||||
set(LCOV_CAPTURE_CMD
|
|
||||||
${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} --directory . -b
|
|
||||||
${BASEDIR} --capture --output-file ${Coverage_NAME}.capture
|
|
||||||
)
|
|
||||||
# add baseline counters
|
|
||||||
set(LCOV_BASELINE_COUNT_CMD
|
|
||||||
${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} -a ${Coverage_NAME}.base
|
|
||||||
-a ${Coverage_NAME}.capture --output-file ${Coverage_NAME}.total
|
|
||||||
)
|
|
||||||
# filter collected data to final coverage report
|
|
||||||
set(LCOV_FILTER_CMD
|
|
||||||
${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} --remove
|
|
||||||
${Coverage_NAME}.total ${LCOV_EXCLUDES} --output-file ${Coverage_NAME}.info
|
|
||||||
)
|
|
||||||
# Generate HTML output
|
|
||||||
set(LCOV_GEN_HTML_CMD
|
|
||||||
${GENHTML_PATH} ${GENHTML_EXTRA_ARGS} ${Coverage_GENHTML_ARGS} -o
|
|
||||||
${Coverage_NAME} ${Coverage_NAME}.info
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
if(CODE_COVERAGE_VERBOSE)
|
|
||||||
message(STATUS "Executed command report")
|
|
||||||
message(STATUS "Command to clean up lcov: ")
|
|
||||||
string(REPLACE ";" " " LCOV_CLEAN_CMD_SPACED "${LCOV_CLEAN_CMD}")
|
|
||||||
message(STATUS "${LCOV_CLEAN_CMD_SPACED}")
|
|
||||||
|
|
||||||
message(STATUS "Command to create baseline: ")
|
|
||||||
string(REPLACE ";" " " LCOV_BASELINE_CMD_SPACED "${LCOV_BASELINE_CMD}")
|
|
||||||
message(STATUS "${LCOV_BASELINE_CMD_SPACED}")
|
|
||||||
|
|
||||||
message(STATUS "Command to run the tests: ")
|
|
||||||
string(REPLACE ";" " " LCOV_EXEC_TESTS_CMD_SPACED "${LCOV_EXEC_TESTS_CMD}")
|
|
||||||
message(STATUS "${LCOV_EXEC_TESTS_CMD_SPACED}")
|
|
||||||
|
|
||||||
message(STATUS "Command to capture counters and generate report: ")
|
|
||||||
string(REPLACE ";" " " LCOV_CAPTURE_CMD_SPACED "${LCOV_CAPTURE_CMD}")
|
|
||||||
message(STATUS "${LCOV_CAPTURE_CMD_SPACED}")
|
|
||||||
|
|
||||||
message(STATUS "Command to add baseline counters: ")
|
|
||||||
string(REPLACE ";" " " LCOV_BASELINE_COUNT_CMD_SPACED "${LCOV_BASELINE_COUNT_CMD}")
|
|
||||||
message(STATUS "${LCOV_BASELINE_COUNT_CMD_SPACED}")
|
|
||||||
|
|
||||||
message(STATUS "Command to filter collected data: ")
|
|
||||||
string(REPLACE ";" " " LCOV_FILTER_CMD_SPACED "${LCOV_FILTER_CMD}")
|
|
||||||
message(STATUS "${LCOV_FILTER_CMD_SPACED}")
|
|
||||||
|
|
||||||
message(STATUS "Command to generate lcov HTML output: ")
|
|
||||||
string(REPLACE ";" " " LCOV_GEN_HTML_CMD_SPACED "${LCOV_GEN_HTML_CMD}")
|
|
||||||
message(STATUS "${LCOV_GEN_HTML_CMD_SPACED}")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# Setup target
|
|
||||||
add_custom_target(${Coverage_NAME}
|
|
||||||
COMMAND ${LCOV_CLEAN_CMD}
|
|
||||||
COMMAND ${LCOV_BASELINE_CMD}
|
|
||||||
COMMAND ${LCOV_EXEC_TESTS_CMD}
|
|
||||||
COMMAND ${LCOV_CAPTURE_CMD}
|
|
||||||
COMMAND ${LCOV_BASELINE_COUNT_CMD}
|
|
||||||
COMMAND ${LCOV_FILTER_CMD}
|
|
||||||
COMMAND ${LCOV_GEN_HTML_CMD}
|
|
||||||
|
|
||||||
# Set output files as GENERATED (will be removed on 'make clean')
|
|
||||||
BYPRODUCTS
|
|
||||||
${Coverage_NAME}.base
|
|
||||||
${Coverage_NAME}.capture
|
|
||||||
${Coverage_NAME}.total
|
|
||||||
${Coverage_NAME}.info
|
|
||||||
${Coverage_NAME}/index.html
|
|
||||||
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
|
|
||||||
DEPENDS ${Coverage_DEPENDENCIES}
|
|
||||||
VERBATIM # Protect arguments to commands
|
|
||||||
COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report."
|
|
||||||
)
|
|
||||||
|
|
||||||
# Show where to find the lcov info report
|
|
||||||
add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
|
|
||||||
COMMAND ;
|
|
||||||
COMMENT "Lcov code coverage info report saved in ${Coverage_NAME}.info."
|
|
||||||
)
|
|
||||||
|
|
||||||
# Show info where to find the report
|
|
||||||
add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
|
|
||||||
COMMAND ;
|
|
||||||
COMMENT "Open ./${Coverage_NAME}/index.html in your browser to view the coverage report."
|
|
||||||
)
|
|
||||||
|
|
||||||
endfunction() # setup_target_for_coverage_lcov
|
|
||||||
|
|
||||||
# Defines a target for running and collection code coverage information
|
|
||||||
# Builds dependencies, runs the given executable and outputs reports.
|
|
||||||
# NOTE! The executable should always have a ZERO as exit code otherwise
|
|
||||||
# the coverage generation will not complete.
|
|
||||||
#
|
|
||||||
# setup_target_for_coverage_gcovr_xml(
|
|
||||||
# NAME ctest_coverage # New target name
|
|
||||||
# EXECUTABLE ctest -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR
|
|
||||||
# DEPENDENCIES executable_target # Dependencies to build first
|
|
||||||
# BASE_DIRECTORY "../" # Base directory for report
|
|
||||||
# # (defaults to PROJECT_SOURCE_DIR)
|
|
||||||
# EXCLUDE "src/dir1/*" "src/dir2/*" # Patterns to exclude (can be relative
|
|
||||||
# # to BASE_DIRECTORY, with CMake 3.4+)
|
|
||||||
# )
|
|
||||||
# The user can set the variable GCOVR_ADDITIONAL_ARGS to supply additional flags to the
|
|
||||||
# GCVOR command.
|
|
||||||
function(setup_target_for_coverage_gcovr_xml)
|
|
||||||
|
|
||||||
set(options NONE)
|
|
||||||
set(oneValueArgs BASE_DIRECTORY NAME)
|
|
||||||
set(multiValueArgs EXCLUDE EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES)
|
|
||||||
cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
|
|
||||||
|
|
||||||
if(NOT GCOVR_PATH)
|
|
||||||
message(FATAL_ERROR "gcovr not found! Aborting...")
|
|
||||||
endif() # NOT GCOVR_PATH
|
|
||||||
|
|
||||||
# Set base directory (as absolute path), or default to PROJECT_SOURCE_DIR
|
|
||||||
if(DEFINED Coverage_BASE_DIRECTORY)
|
|
||||||
get_filename_component(BASEDIR ${Coverage_BASE_DIRECTORY} ABSOLUTE)
|
|
||||||
else()
|
|
||||||
set(BASEDIR ${PROJECT_SOURCE_DIR})
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# Collect excludes (CMake 3.4+: Also compute absolute paths)
|
|
||||||
set(GCOVR_EXCLUDES "")
|
|
||||||
foreach(EXCLUDE ${Coverage_EXCLUDE} ${COVERAGE_EXCLUDES} ${COVERAGE_GCOVR_EXCLUDES})
|
|
||||||
if(CMAKE_VERSION VERSION_GREATER 3.4)
|
|
||||||
get_filename_component(EXCLUDE ${EXCLUDE} ABSOLUTE BASE_DIR ${BASEDIR})
|
|
||||||
endif()
|
|
||||||
list(APPEND GCOVR_EXCLUDES "${EXCLUDE}")
|
|
||||||
endforeach()
|
|
||||||
list(REMOVE_DUPLICATES GCOVR_EXCLUDES)
|
|
||||||
|
|
||||||
# Combine excludes to several -e arguments
|
|
||||||
set(GCOVR_EXCLUDE_ARGS "")
|
|
||||||
foreach(EXCLUDE ${GCOVR_EXCLUDES})
|
|
||||||
list(APPEND GCOVR_EXCLUDE_ARGS "-e")
|
|
||||||
list(APPEND GCOVR_EXCLUDE_ARGS "${EXCLUDE}")
|
|
||||||
endforeach()
|
|
||||||
|
|
||||||
# Set up commands which will be run to generate coverage data
|
|
||||||
# Run tests
|
|
||||||
set(GCOVR_XML_EXEC_TESTS_CMD
|
|
||||||
${Coverage_EXECUTABLE} ${Coverage_EXECUTABLE_ARGS}
|
|
||||||
)
|
|
||||||
# Running gcovr
|
|
||||||
set(GCOVR_XML_CMD
|
|
||||||
${GCOVR_PATH} --xml ${Coverage_NAME}.xml -r ${BASEDIR} ${GCOVR_ADDITIONAL_ARGS}
|
|
||||||
${GCOVR_EXCLUDE_ARGS} --object-directory=${PROJECT_BINARY_DIR}
|
|
||||||
)
|
|
||||||
|
|
||||||
if(CODE_COVERAGE_VERBOSE)
|
|
||||||
message(STATUS "Executed command report")
|
|
||||||
|
|
||||||
message(STATUS "Command to run tests: ")
|
|
||||||
string(REPLACE ";" " " GCOVR_XML_EXEC_TESTS_CMD_SPACED "${GCOVR_XML_EXEC_TESTS_CMD}")
|
|
||||||
message(STATUS "${GCOVR_XML_EXEC_TESTS_CMD_SPACED}")
|
|
||||||
|
|
||||||
message(STATUS "Command to generate gcovr XML coverage data: ")
|
|
||||||
string(REPLACE ";" " " GCOVR_XML_CMD_SPACED "${GCOVR_XML_CMD}")
|
|
||||||
message(STATUS "${GCOVR_XML_CMD_SPACED}")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
add_custom_target(${Coverage_NAME}
|
|
||||||
COMMAND ${GCOVR_XML_EXEC_TESTS_CMD}
|
|
||||||
COMMAND ${GCOVR_XML_CMD}
|
|
||||||
|
|
||||||
BYPRODUCTS ${Coverage_NAME}.xml
|
|
||||||
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
|
|
||||||
DEPENDS ${Coverage_DEPENDENCIES}
|
|
||||||
VERBATIM # Protect arguments to commands
|
|
||||||
COMMENT "Running gcovr to produce Cobertura code coverage report."
|
|
||||||
)
|
|
||||||
|
|
||||||
# Show info where to find the report
|
|
||||||
add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
|
|
||||||
COMMAND ;
|
|
||||||
COMMENT "Cobertura code coverage report saved in ${Coverage_NAME}.xml."
|
|
||||||
)
|
|
||||||
endfunction() # setup_target_for_coverage_gcovr_xml
|
|
||||||
|
|
||||||
# Defines a target for running and collection code coverage information
|
|
||||||
# Builds dependencies, runs the given executable and outputs reports.
|
|
||||||
# NOTE! The executable should always have a ZERO as exit code otherwise
|
|
||||||
# the coverage generation will not complete.
|
|
||||||
#
|
|
||||||
# setup_target_for_coverage_gcovr_html(
|
|
||||||
# NAME ctest_coverage # New target name
|
|
||||||
# EXECUTABLE ctest -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR
|
|
||||||
# DEPENDENCIES executable_target # Dependencies to build first
|
|
||||||
# BASE_DIRECTORY "../" # Base directory for report
|
|
||||||
# # (defaults to PROJECT_SOURCE_DIR)
|
|
||||||
# EXCLUDE "src/dir1/*" "src/dir2/*" # Patterns to exclude (can be relative
|
|
||||||
# # to BASE_DIRECTORY, with CMake 3.4+)
|
|
||||||
# )
|
|
||||||
# The user can set the variable GCOVR_ADDITIONAL_ARGS to supply additional flags to the
|
|
||||||
# GCVOR command.
|
|
||||||
function(setup_target_for_coverage_gcovr_html)
|
|
||||||
|
|
||||||
set(options NONE)
|
|
||||||
set(oneValueArgs BASE_DIRECTORY NAME)
|
|
||||||
set(multiValueArgs EXCLUDE EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES)
|
|
||||||
cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
|
|
||||||
|
|
||||||
if(NOT GCOVR_PATH)
|
|
||||||
message(FATAL_ERROR "gcovr not found! Aborting...")
|
|
||||||
endif() # NOT GCOVR_PATH
|
|
||||||
|
|
||||||
# Set base directory (as absolute path), or default to PROJECT_SOURCE_DIR
|
|
||||||
if(DEFINED Coverage_BASE_DIRECTORY)
|
|
||||||
get_filename_component(BASEDIR ${Coverage_BASE_DIRECTORY} ABSOLUTE)
|
|
||||||
else()
|
|
||||||
set(BASEDIR ${PROJECT_SOURCE_DIR})
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# Collect excludes (CMake 3.4+: Also compute absolute paths)
|
|
||||||
set(GCOVR_EXCLUDES "")
|
|
||||||
foreach(EXCLUDE ${Coverage_EXCLUDE} ${COVERAGE_EXCLUDES} ${COVERAGE_GCOVR_EXCLUDES})
|
|
||||||
if(CMAKE_VERSION VERSION_GREATER 3.4)
|
|
||||||
get_filename_component(EXCLUDE ${EXCLUDE} ABSOLUTE BASE_DIR ${BASEDIR})
|
|
||||||
endif()
|
|
||||||
list(APPEND GCOVR_EXCLUDES "${EXCLUDE}")
|
|
||||||
endforeach()
|
|
||||||
list(REMOVE_DUPLICATES GCOVR_EXCLUDES)
|
|
||||||
|
|
||||||
# Combine excludes to several -e arguments
|
|
||||||
set(GCOVR_EXCLUDE_ARGS "")
|
|
||||||
foreach(EXCLUDE ${GCOVR_EXCLUDES})
|
|
||||||
list(APPEND GCOVR_EXCLUDE_ARGS "-e")
|
|
||||||
list(APPEND GCOVR_EXCLUDE_ARGS "${EXCLUDE}")
|
|
||||||
endforeach()
|
|
||||||
|
|
||||||
# Set up commands which will be run to generate coverage data
|
|
||||||
# Run tests
|
|
||||||
set(GCOVR_HTML_EXEC_TESTS_CMD
|
|
||||||
${Coverage_EXECUTABLE} ${Coverage_EXECUTABLE_ARGS}
|
|
||||||
)
|
|
||||||
# Create folder
|
|
||||||
set(GCOVR_HTML_FOLDER_CMD
|
|
||||||
${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/${Coverage_NAME}
|
|
||||||
)
|
|
||||||
# Running gcovr
|
|
||||||
set(GCOVR_HTML_CMD
|
|
||||||
${GCOVR_PATH} --html ${Coverage_NAME}/index.html --html-details -r ${BASEDIR} ${GCOVR_ADDITIONAL_ARGS}
|
|
||||||
${GCOVR_EXCLUDE_ARGS} --object-directory=${PROJECT_BINARY_DIR}
|
|
||||||
)
|
|
||||||
|
|
||||||
if(CODE_COVERAGE_VERBOSE)
|
|
||||||
message(STATUS "Executed command report")
|
|
||||||
|
|
||||||
message(STATUS "Command to run tests: ")
|
|
||||||
string(REPLACE ";" " " GCOVR_HTML_EXEC_TESTS_CMD_SPACED "${GCOVR_HTML_EXEC_TESTS_CMD}")
|
|
||||||
message(STATUS "${GCOVR_HTML_EXEC_TESTS_CMD_SPACED}")
|
|
||||||
|
|
||||||
message(STATUS "Command to create a folder: ")
|
|
||||||
string(REPLACE ";" " " GCOVR_HTML_FOLDER_CMD_SPACED "${GCOVR_HTML_FOLDER_CMD}")
|
|
||||||
message(STATUS "${GCOVR_HTML_FOLDER_CMD_SPACED}")
|
|
||||||
|
|
||||||
message(STATUS "Command to generate gcovr HTML coverage data: ")
|
|
||||||
string(REPLACE ";" " " GCOVR_HTML_CMD_SPACED "${GCOVR_HTML_CMD}")
|
|
||||||
message(STATUS "${GCOVR_HTML_CMD_SPACED}")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
add_custom_target(${Coverage_NAME}
|
|
||||||
COMMAND ${GCOVR_HTML_EXEC_TESTS_CMD}
|
|
||||||
COMMAND ${GCOVR_HTML_FOLDER_CMD}
|
|
||||||
COMMAND ${GCOVR_HTML_CMD}
|
|
||||||
|
|
||||||
BYPRODUCTS ${PROJECT_BINARY_DIR}/${Coverage_NAME}/index.html # report directory
|
|
||||||
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
|
|
||||||
DEPENDS ${Coverage_DEPENDENCIES}
|
|
||||||
VERBATIM # Protect arguments to commands
|
|
||||||
COMMENT "Running gcovr to produce HTML code coverage report."
|
|
||||||
)
|
|
||||||
|
|
||||||
# Show info where to find the report
|
|
||||||
add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
|
|
||||||
COMMAND ;
|
|
||||||
COMMENT "Open ./${Coverage_NAME}/index.html in your browser to view the coverage report."
|
|
||||||
)
|
|
||||||
|
|
||||||
endfunction() # setup_target_for_coverage_gcovr_html
|
|
||||||
|
|
||||||
# Defines a target for running and collection code coverage information
|
|
||||||
# Builds dependencies, runs the given executable and outputs reports.
|
|
||||||
# NOTE! The executable should always have a ZERO as exit code otherwise
|
|
||||||
# the coverage generation will not complete.
|
|
||||||
#
|
|
||||||
# setup_target_for_coverage_fastcov(
|
|
||||||
# NAME testrunner_coverage # New target name
|
|
||||||
# EXECUTABLE testrunner -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR
|
|
||||||
# DEPENDENCIES testrunner # Dependencies to build first
|
|
||||||
# BASE_DIRECTORY "../" # Base directory for report
|
|
||||||
# # (defaults to PROJECT_SOURCE_DIR)
|
|
||||||
# EXCLUDE "src/dir1/" "src/dir2/" # Patterns to exclude.
|
|
||||||
# NO_DEMANGLE # Don't demangle C++ symbols
|
|
||||||
# # even if c++filt is found
|
|
||||||
# SKIP_HTML # Don't create html report
|
|
||||||
# POST_CMD perl -i -pe s!${PROJECT_SOURCE_DIR}/!!g ctest_coverage.json # E.g. for stripping source dir from file paths
|
|
||||||
# )
|
|
||||||
function(setup_target_for_coverage_fastcov)
|
|
||||||
|
|
||||||
set(options NO_DEMANGLE SKIP_HTML)
|
|
||||||
set(oneValueArgs BASE_DIRECTORY NAME)
|
|
||||||
set(multiValueArgs EXCLUDE EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES FASTCOV_ARGS GENHTML_ARGS POST_CMD)
|
|
||||||
cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
|
|
||||||
|
|
||||||
if(NOT FASTCOV_PATH)
|
|
||||||
message(FATAL_ERROR "fastcov not found! Aborting...")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if(NOT Coverage_SKIP_HTML AND NOT GENHTML_PATH)
|
|
||||||
message(FATAL_ERROR "genhtml not found! Aborting...")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# Set base directory (as absolute path), or default to PROJECT_SOURCE_DIR
|
|
||||||
if(Coverage_BASE_DIRECTORY)
|
|
||||||
get_filename_component(BASEDIR ${Coverage_BASE_DIRECTORY} ABSOLUTE)
|
|
||||||
else()
|
|
||||||
set(BASEDIR ${PROJECT_SOURCE_DIR})
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# Collect excludes (Patterns, not paths, for fastcov)
|
|
||||||
set(FASTCOV_EXCLUDES "")
|
|
||||||
foreach(EXCLUDE ${Coverage_EXCLUDE} ${COVERAGE_EXCLUDES} ${COVERAGE_FASTCOV_EXCLUDES})
|
|
||||||
list(APPEND FASTCOV_EXCLUDES "${EXCLUDE}")
|
|
||||||
endforeach()
|
|
||||||
list(REMOVE_DUPLICATES FASTCOV_EXCLUDES)
|
|
||||||
|
|
||||||
# Conditional arguments
|
|
||||||
if(CPPFILT_PATH AND NOT ${Coverage_NO_DEMANGLE})
|
|
||||||
set(GENHTML_EXTRA_ARGS "--demangle-cpp")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# Set up commands which will be run to generate coverage data
|
|
||||||
set(FASTCOV_EXEC_TESTS_CMD ${Coverage_EXECUTABLE} ${Coverage_EXECUTABLE_ARGS})
|
|
||||||
|
|
||||||
set(FASTCOV_CAPTURE_CMD ${FASTCOV_PATH} ${Coverage_FASTCOV_ARGS} --gcov ${GCOV_PATH}
|
|
||||||
--search-directory ${BASEDIR}
|
|
||||||
--process-gcno
|
|
||||||
--output ${Coverage_NAME}.json
|
|
||||||
--exclude ${FASTCOV_EXCLUDES}
|
|
||||||
--exclude ${FASTCOV_EXCLUDES}
|
|
||||||
)
|
|
||||||
|
|
||||||
set(FASTCOV_CONVERT_CMD ${FASTCOV_PATH}
|
|
||||||
-C ${Coverage_NAME}.json --lcov --output ${Coverage_NAME}.info
|
|
||||||
)
|
|
||||||
|
|
||||||
if(Coverage_SKIP_HTML)
|
|
||||||
set(FASTCOV_HTML_CMD ";")
|
|
||||||
else()
|
|
||||||
set(FASTCOV_HTML_CMD ${GENHTML_PATH} ${GENHTML_EXTRA_ARGS} ${Coverage_GENHTML_ARGS}
|
|
||||||
-o ${Coverage_NAME} ${Coverage_NAME}.info
|
|
||||||
)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
set(FASTCOV_POST_CMD ";")
|
|
||||||
if(Coverage_POST_CMD)
|
|
||||||
set(FASTCOV_POST_CMD ${Coverage_POST_CMD})
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if(CODE_COVERAGE_VERBOSE)
|
|
||||||
message(STATUS "Code coverage commands for target ${Coverage_NAME} (fastcov):")
|
|
||||||
|
|
||||||
message(" Running tests:")
|
|
||||||
string(REPLACE ";" " " FASTCOV_EXEC_TESTS_CMD_SPACED "${FASTCOV_EXEC_TESTS_CMD}")
|
|
||||||
message(" ${FASTCOV_EXEC_TESTS_CMD_SPACED}")
|
|
||||||
|
|
||||||
message(" Capturing fastcov counters and generating report:")
|
|
||||||
string(REPLACE ";" " " FASTCOV_CAPTURE_CMD_SPACED "${FASTCOV_CAPTURE_CMD}")
|
|
||||||
message(" ${FASTCOV_CAPTURE_CMD_SPACED}")
|
|
||||||
|
|
||||||
message(" Converting fastcov .json to lcov .info:")
|
|
||||||
string(REPLACE ";" " " FASTCOV_CONVERT_CMD_SPACED "${FASTCOV_CONVERT_CMD}")
|
|
||||||
message(" ${FASTCOV_CONVERT_CMD_SPACED}")
|
|
||||||
|
|
||||||
if(NOT Coverage_SKIP_HTML)
|
|
||||||
message(" Generating HTML report: ")
|
|
||||||
string(REPLACE ";" " " FASTCOV_HTML_CMD_SPACED "${FASTCOV_HTML_CMD}")
|
|
||||||
message(" ${FASTCOV_HTML_CMD_SPACED}")
|
|
||||||
endif()
|
|
||||||
if(Coverage_POST_CMD)
|
|
||||||
message(" Running post command: ")
|
|
||||||
string(REPLACE ";" " " FASTCOV_POST_CMD_SPACED "${FASTCOV_POST_CMD}")
|
|
||||||
message(" ${FASTCOV_POST_CMD_SPACED}")
|
|
||||||
endif()
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# Setup target
|
|
||||||
add_custom_target(${Coverage_NAME}
|
|
||||||
|
|
||||||
# Cleanup fastcov
|
|
||||||
COMMAND ${FASTCOV_PATH} ${Coverage_FASTCOV_ARGS} --gcov ${GCOV_PATH}
|
|
||||||
--search-directory ${BASEDIR}
|
|
||||||
--zerocounters
|
|
||||||
|
|
||||||
COMMAND ${FASTCOV_EXEC_TESTS_CMD}
|
|
||||||
COMMAND ${FASTCOV_CAPTURE_CMD}
|
|
||||||
COMMAND ${FASTCOV_CONVERT_CMD}
|
|
||||||
COMMAND ${FASTCOV_HTML_CMD}
|
|
||||||
COMMAND ${FASTCOV_POST_CMD}
|
|
||||||
|
|
||||||
# Set output files as GENERATED (will be removed on 'make clean')
|
|
||||||
BYPRODUCTS
|
|
||||||
${Coverage_NAME}.info
|
|
||||||
${Coverage_NAME}.json
|
|
||||||
${Coverage_NAME}/index.html # report directory
|
|
||||||
|
|
||||||
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
|
|
||||||
DEPENDS ${Coverage_DEPENDENCIES}
|
|
||||||
VERBATIM # Protect arguments to commands
|
|
||||||
COMMENT "Resetting code coverage counters to zero. Processing code coverage counters and generating report."
|
|
||||||
)
|
|
||||||
|
|
||||||
set(INFO_MSG "fastcov code coverage info report saved in ${Coverage_NAME}.info and ${Coverage_NAME}.json.")
|
|
||||||
if(NOT Coverage_SKIP_HTML)
|
|
||||||
string(APPEND INFO_MSG " Open ${PROJECT_BINARY_DIR}/${Coverage_NAME}/index.html in your browser to view the coverage report.")
|
|
||||||
endif()
|
|
||||||
# Show where to find the fastcov info report
|
|
||||||
add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
|
|
||||||
COMMAND ${CMAKE_COMMAND} -E echo ${INFO_MSG}
|
|
||||||
)
|
|
||||||
|
|
||||||
endfunction() # setup_target_for_coverage_fastcov
|
|
||||||
|
|
||||||
function(append_coverage_compiler_flags)
|
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
|
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
|
|
||||||
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
|
|
||||||
message(STATUS "Appending code coverage compiler flags: ${COVERAGE_COMPILER_FLAGS}")
|
|
||||||
endfunction() # append_coverage_compiler_flags
|
|
||||||
|
|
||||||
# Setup coverage for specific library
|
|
||||||
function(append_coverage_compiler_flags_to_target name)
|
|
||||||
target_compile_options(${name}
|
|
||||||
PRIVATE ${COVERAGE_COMPILER_FLAGS})
|
|
||||||
endfunction()
|
|
||||||
@ -1,22 +0,0 @@
|
|||||||
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
|
|
||||||
|
|
||||||
include("project-meta-info.in")
|
|
||||||
|
|
||||||
project(waitui-hashtable
|
|
||||||
VERSION ${project_version}
|
|
||||||
DESCRIPTION ${project_description}
|
|
||||||
HOMEPAGE_URL ${project_homepage}
|
|
||||||
LANGUAGES C)
|
|
||||||
|
|
||||||
add_library(hashtable OBJECT)
|
|
||||||
|
|
||||||
target_sources(hashtable
|
|
||||||
PRIVATE
|
|
||||||
"src/hashtable.c"
|
|
||||||
PUBLIC
|
|
||||||
"include/waitui/hashtable.h"
|
|
||||||
)
|
|
||||||
|
|
||||||
target_include_directories(hashtable PUBLIC "include")
|
|
||||||
|
|
||||||
target_link_libraries(hashtable PUBLIC str)
|
|
||||||
@ -1,185 +0,0 @@
|
|||||||
/**
|
|
||||||
* @file hashtable.h
|
|
||||||
* @author rick
|
|
||||||
* @date 23.07.20
|
|
||||||
* @brief File for the HashTable implementation
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef WAITUI_HASHTABLE_H
|
|
||||||
#define WAITUI_HASHTABLE_H
|
|
||||||
|
|
||||||
#include <waitui/str.h>
|
|
||||||
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
// Public types
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Type representing a HashTable node
|
|
||||||
*/
|
|
||||||
typedef struct waitui_hashtable_node waitui_hashtable_node;
|
|
||||||
struct waitui_hashtable_node {
|
|
||||||
str key;
|
|
||||||
void *value;
|
|
||||||
int isStolen;
|
|
||||||
waitui_hashtable_node *next;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Type for value destroy function
|
|
||||||
*/
|
|
||||||
typedef void (*waitui_hashtable_value_destroy_fn)(void **value);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Type for value checking function
|
|
||||||
*/
|
|
||||||
typedef int (*waitui_hashtable_value_check_fn)(void *value, void *arg);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Type representing a HashTable
|
|
||||||
*/
|
|
||||||
typedef struct waitui_hashtable {
|
|
||||||
waitui_hashtable_node **list;
|
|
||||||
waitui_hashtable_value_destroy_fn valueDestroyFn;
|
|
||||||
unsigned long int size;
|
|
||||||
} waitui_hashtable;
|
|
||||||
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
// Public functions
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Create a HashTable
|
|
||||||
* @param[in] size The HashTable size
|
|
||||||
* @param[in] valueDestroyFn Function to call for value destruction
|
|
||||||
* @return A pointer to waitui_hashtable or NULL if memory allocation failed
|
|
||||||
*/
|
|
||||||
extern waitui_hashtable *
|
|
||||||
waitui_hashtable_new(unsigned long int size,
|
|
||||||
waitui_hashtable_value_destroy_fn valueDestroyFn);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Destroy a HashTable
|
|
||||||
* @param[in,out] this The HashTable to destroy
|
|
||||||
* @note This will free call for every value the valueDestroyFn
|
|
||||||
*/
|
|
||||||
extern void waitui_hashtable_destroy(waitui_hashtable **this);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Insert a value for the key into the HashTable
|
|
||||||
* @param[in,out] this The HashTable to insert the value
|
|
||||||
* @param[in] key The key to insert the value for
|
|
||||||
* @param[in] value The value to insert
|
|
||||||
* @param[in] valueCheckFn The value checking function to call
|
|
||||||
* @param[in] arg The value for the second parameter to the valueCheckFn
|
|
||||||
* @retval 1 Ok
|
|
||||||
* @retval 0 Memory allocation failed or key already exists
|
|
||||||
*/
|
|
||||||
extern int
|
|
||||||
waitui_hashtable_insertCheck(waitui_hashtable *this, str key, void *value,
|
|
||||||
waitui_hashtable_value_check_fn valueCheckFn,
|
|
||||||
void *arg);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Search for the key inside the HashTable
|
|
||||||
* @param[in] this The HashTable to lookup the key
|
|
||||||
* @param[in] key The key to search for in the HashTable
|
|
||||||
* @param[in] valueCheckFn The value checking function to call
|
|
||||||
* @param[in] arg The value for the second parameter to the valueCheckFn
|
|
||||||
* @return The pointer to the value or NULL if not found
|
|
||||||
*/
|
|
||||||
extern void *
|
|
||||||
waitui_hashtable_lookupCheck(waitui_hashtable *this, str key,
|
|
||||||
waitui_hashtable_value_check_fn valueCheckFn,
|
|
||||||
void *arg);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Test whether the HashTable has the key
|
|
||||||
* @param[in] this The HashTable to check for the key
|
|
||||||
* @param[in] key The key to look for in the HashTable
|
|
||||||
* @param[in] valueCheckCallback The value checking function to call
|
|
||||||
* @param[in] arg The value for the second parameter to the valueCheckCallback
|
|
||||||
* @retval 1 The HashTable has the key
|
|
||||||
* @retval 0 The HashTable does not have the key
|
|
||||||
*/
|
|
||||||
extern int
|
|
||||||
waitui_hashtable_hasCheck(waitui_hashtable *this, str key,
|
|
||||||
waitui_hashtable_value_check_fn valueCheckCallback,
|
|
||||||
void *arg);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Mark value for key as stolen in the HashTable
|
|
||||||
* @param[in] this The HashTable to check for the key
|
|
||||||
* @param[in] key The key to look for in the HashTable
|
|
||||||
* @param[in] valueCheckFn The value checking function to call
|
|
||||||
* @param[in] arg The value for the second parameter to the valueCheckFn
|
|
||||||
* @retval 1 The HashTable has the key
|
|
||||||
* @retval 0 The HashTable does not have the key
|
|
||||||
*/
|
|
||||||
extern int
|
|
||||||
waitui_hashtable_markStolenCheck(waitui_hashtable *this, str key,
|
|
||||||
waitui_hashtable_value_check_fn valueCheckFn,
|
|
||||||
void *arg);
|
|
||||||
/**
|
|
||||||
* @brief Tests whether the value for key is marked as stolen in the HashTable
|
|
||||||
* @param[in] this The HashTable to check for the key
|
|
||||||
* @param[in] key The key to look for in the HashTable
|
|
||||||
* @param[in] valueCheckFn The value checking function to call
|
|
||||||
* @param[in] arg The value for the second parameter to the valueCheckFn
|
|
||||||
* @retval 1 The HashTable has the key
|
|
||||||
* @retval 0 The HashTable does not have the key
|
|
||||||
*/
|
|
||||||
extern int
|
|
||||||
waitui_hashtable_isStolenCheck(waitui_hashtable *this, str key,
|
|
||||||
waitui_hashtable_value_check_fn valueCheckFn,
|
|
||||||
void *arg);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Insert a value for the key into the HashTable
|
|
||||||
* @param[in,out] this The HashTable to insert the value
|
|
||||||
* @param[in] key The key to insert the value for
|
|
||||||
* @param[in] value The value to insert
|
|
||||||
* @retval 1 Ok
|
|
||||||
* @retval 0 Memory allocation failed or key already exists
|
|
||||||
*/
|
|
||||||
extern int waitui_hashtable_insert(waitui_hashtable *this, str key,
|
|
||||||
void *value);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Search for the key inside the HashTable
|
|
||||||
* @param[in] this The HashTable to lookup the key
|
|
||||||
* @param[in] key The key to search for in the HashTable
|
|
||||||
* @return The pointer to the value or NULL if not found
|
|
||||||
*/
|
|
||||||
extern void *waitui_hashtable_lookup(waitui_hashtable *this, str key);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Test whether the HashTable has the key
|
|
||||||
* @param[in] this The HashTable to check for the key
|
|
||||||
* @param[in] key The key to look for in the HashTable
|
|
||||||
* @retval 1 The HashTable has the key
|
|
||||||
* @retval 0 The HashTable does not have the key
|
|
||||||
*/
|
|
||||||
extern int waitui_hashtable_has(waitui_hashtable *this, str key);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Mark value for key as stolen in the HashTable
|
|
||||||
* @param[in] this The HashTable to check for the key
|
|
||||||
* @param[in] key The key to look for in the HashTable
|
|
||||||
* @retval 1 The HashTable has the key
|
|
||||||
* @retval 0 The HashTable does not have the key
|
|
||||||
*/
|
|
||||||
extern int waitui_hashtable_markStolen(waitui_hashtable *this, str key);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Tests whether the value for key is marked as stolen in the HashTable
|
|
||||||
* @param[in] this The HashTable to check for the key
|
|
||||||
* @param[in] key The key to look for in the HashTable
|
|
||||||
* @retval 1 The HashTable has the key
|
|
||||||
* @retval 0 The HashTable does not have the key
|
|
||||||
*/
|
|
||||||
extern int waitui_hashtable_isStolen(waitui_hashtable *this, str key);
|
|
||||||
|
|
||||||
#endif//WAITUI_HASHTABLE_H
|
|
||||||
@ -1,118 +0,0 @@
|
|||||||
/**
|
|
||||||
* @file hashtable_generic.h
|
|
||||||
* @author rick
|
|
||||||
* @date 26.07.22
|
|
||||||
* @brief File for the generic HashTable implementation
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef WAITUI_HASHTABLE_GENERIC_H
|
|
||||||
#define WAITUI_HASHTABLE_GENERIC_H
|
|
||||||
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
// Public defines
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
#define INTERFACE_HASHTABLE_TYPEDEF(type) \
|
|
||||||
typedef waitui_hashtable type##_hashtable
|
|
||||||
|
|
||||||
#define INTERFACE_HASHTABLE_VALUE_CHECK_FN_TYPEDEF(type) \
|
|
||||||
typedef waitui_hashtable_value_check_fn type##_hashtable_value_check_fn
|
|
||||||
|
|
||||||
#define INTERFACE_HASHTABLE_NEW(type) \
|
|
||||||
extern type##_hashtable *type##_hashtable_new(unsigned long int length)
|
|
||||||
|
|
||||||
#define INTERFACE_HASHTABLE_NEW_CUSTOM(type, elem_destroy) \
|
|
||||||
extern type##_hashtable *type##_hashtable_new(unsigned long int length)
|
|
||||||
|
|
||||||
#define INTERFACE_HASHTABLE_DESTROY(type) \
|
|
||||||
extern void type##_hashtable_destroy(type##_hashtable **this)
|
|
||||||
|
|
||||||
#define INTERFACE_HASHTABLE_INSERT_CHECK(type) \
|
|
||||||
extern int type##_hashtable_insertCheck( \
|
|
||||||
type##_hashtable *this, str key, type *type##Element, \
|
|
||||||
type##_hashtable_value_check_fn valueCheckFn, void *arg)
|
|
||||||
|
|
||||||
#define INTERFACE_HASHTABLE_LOOKUP_CHECK(type) \
|
|
||||||
extern type *type##_hashtable_lookupCheck( \
|
|
||||||
type##_hashtable *this, str key, \
|
|
||||||
type##_hashtable_value_check_fn valueCheckFn, void *arg)
|
|
||||||
|
|
||||||
#define INTERFACE_HASHTABLE_HAS_CHECK(type) \
|
|
||||||
extern int type##_hashtable_hasCheck( \
|
|
||||||
type##_hashtable *this, str key, \
|
|
||||||
type##_hashtable_value_check_fn valueCheckFn, void *arg)
|
|
||||||
|
|
||||||
#define INTERFACE_HASHTABLE_MARK_STOLEN_CHECK(type) \
|
|
||||||
extern int type##_hashtable_markStolenCheck( \
|
|
||||||
type##_hashtable *this, str key, \
|
|
||||||
type##_hashtable_value_check_fn valueCheckFn, void *arg)
|
|
||||||
|
|
||||||
#define INTERFACE_HASHTABLE_IS_STOLEN_CHECK(type) \
|
|
||||||
extern int type##_hashtable_isStolenCheck( \
|
|
||||||
type##_hashtable *this, str key, \
|
|
||||||
type##_hashtable_value_check_fn valueCheckFn, void *arg)
|
|
||||||
|
|
||||||
#define INTERFACE_HASHTABLE_INSERT(type) \
|
|
||||||
extern int type##_hashtable_insert(type##_hashtable *this, str key, \
|
|
||||||
type *type##Element)
|
|
||||||
|
|
||||||
#define INTERFACE_HASHTABLE_LOOKUP(type) \
|
|
||||||
extern type *type##_hashtable_lookup(type##_hashtable *this, str key)
|
|
||||||
|
|
||||||
#define INTERFACE_HASHTABLE_HAS(type) \
|
|
||||||
extern int type##_hashtable_has(type##_hashtable *this, str key)
|
|
||||||
|
|
||||||
#define INTERFACE_HASHTABLE_MARK_STOLEN(type) \
|
|
||||||
extern int type##_hashtable_markStolen(type##_hashtable *this, str key)
|
|
||||||
|
|
||||||
#define INTERFACE_HASHTABLE_IS_STOLEN(type) \
|
|
||||||
extern int type##_hashtable_isStolen(type##_hashtable *this, str key)
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Define for quickly created hashtable implementations for a value type
|
|
||||||
* @param[in] kind Whether to create interface hashtable definition
|
|
||||||
* or actual implementation
|
|
||||||
* @param[in] type For what type to create the hashtable
|
|
||||||
*/
|
|
||||||
#define CREATE_HASHTABLE_TYPE(kind, type) \
|
|
||||||
kind##_HASHTABLE_TYPEDEF(type); \
|
|
||||||
kind##_HASHTABLE_VALUE_CHECK_FN_TYPEDEF(type); \
|
|
||||||
kind##_HASHTABLE_NEW(type); \
|
|
||||||
kind##_HASHTABLE_DESTROY(type); \
|
|
||||||
kind##_HASHTABLE_INSERT_CHECK(type); \
|
|
||||||
kind##_HASHTABLE_INSERT(type); \
|
|
||||||
kind##_HASHTABLE_LOOKUP_CHECK(type); \
|
|
||||||
kind##_HASHTABLE_LOOKUP(type); \
|
|
||||||
kind##_HASHTABLE_HAS_CHECK(type); \
|
|
||||||
kind##_HASHTABLE_HAS(type); \
|
|
||||||
kind##_HASHTABLE_MARK_STOLEN_CHECK(type); \
|
|
||||||
kind##_HASHTABLE_MARK_STOLEN(type); \
|
|
||||||
kind##_HASHTABLE_IS_STOLEN_CHECK(type); \
|
|
||||||
kind##_HASHTABLE_IS_STOLEN(type);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Define for quickly created hashtable implementations for a value type
|
|
||||||
* @param[in] kind Whether to create interface hashtable definition
|
|
||||||
* or actual implementation
|
|
||||||
* @param[in] type For what type to create the hashtable
|
|
||||||
* @param[in] elem_destroy Custom element destroy function
|
|
||||||
*/
|
|
||||||
#define CREATE_HASHTABLE_TYPE_CUSTOM(kind, type, elem_destroy) \
|
|
||||||
kind##_HASHTABLE_TYPEDEF(type); \
|
|
||||||
kind##_HASHTABLE_VALUE_CHECK_FN_TYPEDEF(type); \
|
|
||||||
kind##_HASHTABLE_NEW_CUSTOM(type, elem_destroy); \
|
|
||||||
kind##_HASHTABLE_DESTROY(type); \
|
|
||||||
kind##_HASHTABLE_INSERT_CHECK(type); \
|
|
||||||
kind##_HASHTABLE_INSERT(type); \
|
|
||||||
kind##_HASHTABLE_LOOKUP_CHECK(type); \
|
|
||||||
kind##_HASHTABLE_LOOKUP(type); \
|
|
||||||
kind##_HASHTABLE_HAS_CHECK(type); \
|
|
||||||
kind##_HASHTABLE_HAS(type); \
|
|
||||||
kind##_HASHTABLE_MARK_STOLEN_CHECK(type); \
|
|
||||||
kind##_HASHTABLE_MARK_STOLEN(type); \
|
|
||||||
kind##_HASHTABLE_IS_STOLEN_CHECK(type); \
|
|
||||||
kind##_HASHTABLE_IS_STOLEN(type);
|
|
||||||
|
|
||||||
#endif//WAITUI_HASHTABLE_GENERIC_H
|
|
||||||
@ -1,113 +0,0 @@
|
|||||||
/**
|
|
||||||
* @file hashtable_generic_impl.h
|
|
||||||
* @author rick
|
|
||||||
* @date 26.07.22
|
|
||||||
* @brief File for the generic HashTable implementation
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef WAITUI_HASHTABLE_GENERIC_IMPL_H
|
|
||||||
#define WAITUI_HASHTABLE_GENERIC_IMPL_H
|
|
||||||
|
|
||||||
#include "waitui/hashtable.h"
|
|
||||||
#include "waitui/hashtable_generic.h"
|
|
||||||
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
// Public defines
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_HASHTABLE_TYPEDEF(type)
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_HASHTABLE_VALUE_CHECK_FN_TYPEDEF(type)
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_HASHTABLE_NEW(type) \
|
|
||||||
type##_hashtable *type##_hashtable_new(unsigned long int length) { \
|
|
||||||
return (type##_hashtable *) waitui_hashtable_new( \
|
|
||||||
length, (waitui_hashtable_value_destroy_fn) type##_destroy); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_HASHTABLE_NEW_CUSTOM(type, elem_destroy) \
|
|
||||||
type##_hashtable *type##_hashtable_new(unsigned long int length) { \
|
|
||||||
return (type##_hashtable *) waitui_hashtable_new( \
|
|
||||||
length, (waitui_hashtable_value_destroy_fn) (elem_destroy)); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_HASHTABLE_DESTROY(type) \
|
|
||||||
void type##_hashtable_destroy(type##_hashtable **this) { \
|
|
||||||
waitui_hashtable_destroy((waitui_hashtable **) this); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_HASHTABLE_INSERT_CHECK(type) \
|
|
||||||
int type##_hashtable_insertCheck( \
|
|
||||||
type##_hashtable *this, str key, type *type##Element, \
|
|
||||||
waitui_hashtable_value_check_fn valueCheckFn, void *arg) { \
|
|
||||||
return waitui_hashtable_insertCheck( \
|
|
||||||
(waitui_hashtable *) this, key, (void *) type##Element, \
|
|
||||||
(waitui_hashtable_value_check_fn) valueCheckFn, arg); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_HASHTABLE_LOOKUP_CHECK(type) \
|
|
||||||
type *type##_hashtable_lookupCheck( \
|
|
||||||
type##_hashtable *this, str key, \
|
|
||||||
waitui_hashtable_value_check_fn valueCheckFn, void *arg) { \
|
|
||||||
return (type *) waitui_hashtable_lookupCheck( \
|
|
||||||
(waitui_hashtable *) this, key, \
|
|
||||||
(waitui_hashtable_value_check_fn) valueCheckFn, arg); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_HASHTABLE_HAS_CHECK(type) \
|
|
||||||
int type##_hashtable_hasCheck( \
|
|
||||||
type##_hashtable *this, str key, \
|
|
||||||
waitui_hashtable_value_check_fn valueCheckFn, void *arg) { \
|
|
||||||
return waitui_hashtable_hasCheck( \
|
|
||||||
(waitui_hashtable *) this, key, \
|
|
||||||
(waitui_hashtable_value_check_fn) valueCheckFn, arg); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_HASHTABLE_MARK_STOLEN_CHECK(type) \
|
|
||||||
int type##_hashtable_markStolenCheck( \
|
|
||||||
type##_hashtable *this, str key, \
|
|
||||||
waitui_hashtable_value_check_fn valueCheckFn, void *arg) { \
|
|
||||||
return waitui_hashtable_markStolenCheck( \
|
|
||||||
(waitui_hashtable *) this, key, \
|
|
||||||
(waitui_hashtable_value_check_fn) valueCheckFn, arg); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_HASHTABLE_IS_STOLEN_CHECK(type) \
|
|
||||||
int type##_hashtable_isStolenCheck( \
|
|
||||||
type##_hashtable *this, str key, \
|
|
||||||
waitui_hashtable_value_check_fn valueCheckFn, void *arg) { \
|
|
||||||
return waitui_hashtable_isStolenCheck( \
|
|
||||||
(waitui_hashtable *) this, key, \
|
|
||||||
(waitui_hashtable_value_check_fn) valueCheckFn, arg); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_HASHTABLE_INSERT(type) \
|
|
||||||
int type##_hashtable_insert(type##_hashtable *this, str key, \
|
|
||||||
type *type##Element) { \
|
|
||||||
return waitui_hashtable_insert((waitui_hashtable *) this, key, \
|
|
||||||
(void *) type##Element); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_HASHTABLE_LOOKUP(type) \
|
|
||||||
type *type##_hashtable_lookup(type##_hashtable *this, str key) { \
|
|
||||||
return (type *) waitui_hashtable_lookup((waitui_hashtable *) this, \
|
|
||||||
key); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_HASHTABLE_HAS(type) \
|
|
||||||
int type##_hashtable_has(type##_hashtable *this, str key) { \
|
|
||||||
return waitui_hashtable_has((waitui_hashtable *) this, key); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_HASHTABLE_MARK_STOLEN(type) \
|
|
||||||
int type##_hashtable_markStolen(type##_hashtable *this, str key) { \
|
|
||||||
return waitui_hashtable_markStolen((waitui_hashtable *) this, key); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_HASHTABLE_IS_STOLEN(type) \
|
|
||||||
int type##_hashtable_isStolen(type##_hashtable *this, str key) { \
|
|
||||||
return waitui_hashtable_isStolen((waitui_hashtable *) this, key); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif//WAITUI_HASHTABLE_GENERIC_IMPL_H
|
|
||||||
@ -1,3 +0,0 @@
|
|||||||
set(project_version 0.0.1)
|
|
||||||
set(project_description "waitui hashtable library")
|
|
||||||
set(project_homepage "http://example.com")
|
|
||||||
@ -1,215 +0,0 @@
|
|||||||
/**
|
|
||||||
* @file hashtable.c
|
|
||||||
* @author rick
|
|
||||||
* @date 23.07.20
|
|
||||||
* @brief File for the HashTable implementation
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "waitui/hashtable.h"
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
// Local functions
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Calculate the HashTable slot for the given key
|
|
||||||
* @param[in] this The HashTable to calculate the slot for
|
|
||||||
* @param[in] key The key to calculate the slot for
|
|
||||||
* @return The slot in the HashTable for the given key
|
|
||||||
*/
|
|
||||||
static inline unsigned long int waitui_hashtable_hash(waitui_hashtable *this,
|
|
||||||
str key) {
|
|
||||||
unsigned long int hashValue = 0;
|
|
||||||
|
|
||||||
if (key.len < 1 || !key.s) { return 0; }
|
|
||||||
|
|
||||||
for (unsigned long int i = 0; i < key.len; ++i) { hashValue += key.s[i]; }
|
|
||||||
hashValue += key.s[0] % 11 + (((unsigned char) key.s[0]) << 3U) - key.s[0];
|
|
||||||
|
|
||||||
return hashValue % this->size;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
// Public functions
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
waitui_hashtable *
|
|
||||||
waitui_hashtable_new(unsigned long int size,
|
|
||||||
waitui_hashtable_value_destroy_fn valueDestroyFn) {
|
|
||||||
waitui_hashtable *this = NULL;
|
|
||||||
|
|
||||||
this = calloc(1, sizeof(*this));
|
|
||||||
if (!this) { return NULL; }
|
|
||||||
|
|
||||||
this->size = size;
|
|
||||||
this->valueDestroyFn = valueDestroyFn;
|
|
||||||
this->list = calloc(size, sizeof(this->list));
|
|
||||||
if (!this->list) {
|
|
||||||
free(this);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (unsigned long int i = 0; i < this->size; ++i) { this->list[i] = NULL; }
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
void waitui_hashtable_destroy(waitui_hashtable **this) {
|
|
||||||
if (!this || !(*this)) { return; }
|
|
||||||
|
|
||||||
if ((*this)->list) {
|
|
||||||
for (unsigned long int i = 0; i < (*this)->size; ++i) {
|
|
||||||
while ((*this)->list[i]) {
|
|
||||||
waitui_hashtable_node *temp = (*this)->list[i];
|
|
||||||
(*this)->list[i] = (*this)->list[i]->next;
|
|
||||||
|
|
||||||
if (!temp->isStolen) { (*this)->valueDestroyFn(&temp->value); }
|
|
||||||
|
|
||||||
STR_FREE(&temp->key);
|
|
||||||
|
|
||||||
free(temp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
free((*this)->list);
|
|
||||||
}
|
|
||||||
|
|
||||||
free(*this);
|
|
||||||
*this = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int waitui_hashtable_insertCheck(waitui_hashtable *this, str key, void *value,
|
|
||||||
waitui_hashtable_value_check_fn valueCheckFn,
|
|
||||||
void *arg) {
|
|
||||||
if (!this || !key.s) { return 0; }
|
|
||||||
|
|
||||||
unsigned long int hashSlot = waitui_hashtable_hash(this, key);
|
|
||||||
waitui_hashtable_node *node = this->list[hashSlot];
|
|
||||||
str keyCopy = STR_NULL_INIT;
|
|
||||||
|
|
||||||
while (node && (key.len != node->key.len ||
|
|
||||||
memcmp(key.s, node->key.s, key.len) != 0 ||
|
|
||||||
(valueCheckFn && !valueCheckFn(node->value, arg)))) {
|
|
||||||
node = node->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (node) { return 0; }
|
|
||||||
|
|
||||||
node = calloc(1, sizeof(*node));
|
|
||||||
if (!node) { return 0; }
|
|
||||||
|
|
||||||
STR_COPY(&keyCopy, &key);
|
|
||||||
if (!keyCopy.s) {
|
|
||||||
free(node);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
node->key = keyCopy;
|
|
||||||
node->value = value;
|
|
||||||
node->next = this->list[hashSlot];
|
|
||||||
this->list[hashSlot] = node;
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *
|
|
||||||
waitui_hashtable_lookupCheck(waitui_hashtable *this, str key,
|
|
||||||
waitui_hashtable_value_check_fn valueCheckFn,
|
|
||||||
void *arg) {
|
|
||||||
if (!this || !key.s) { return NULL; }
|
|
||||||
|
|
||||||
unsigned long int hashValue = waitui_hashtable_hash(this, key);
|
|
||||||
waitui_hashtable_node *node = this->list[hashValue];
|
|
||||||
|
|
||||||
while (node && (key.len != node->key.len ||
|
|
||||||
memcmp(key.s, node->key.s, key.len) != 0 ||
|
|
||||||
(valueCheckFn && !valueCheckFn(node->value, arg)))) {
|
|
||||||
node = node->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!node) { return NULL; }
|
|
||||||
|
|
||||||
return node->value;
|
|
||||||
}
|
|
||||||
|
|
||||||
int waitui_hashtable_hasCheck(
|
|
||||||
waitui_hashtable *this, str key,
|
|
||||||
waitui_hashtable_value_check_fn valueCheckCallback, void *arg) {
|
|
||||||
if (!this || !key.s) { return 0; }
|
|
||||||
|
|
||||||
unsigned long int hashValue = waitui_hashtable_hash(this, key);
|
|
||||||
waitui_hashtable_node *node = this->list[hashValue];
|
|
||||||
|
|
||||||
while (node &&
|
|
||||||
(key.len != node->key.len ||
|
|
||||||
memcmp(key.s, node->key.s, key.len) != 0 ||
|
|
||||||
(valueCheckCallback && !valueCheckCallback(node->value, arg)))) {
|
|
||||||
node = node->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
return node != NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int waitui_hashtable_markStolenCheck(
|
|
||||||
waitui_hashtable *this, str key,
|
|
||||||
waitui_hashtable_value_check_fn valueCheckFn, void *arg) {
|
|
||||||
if (!this || !key.s) { return 0; }
|
|
||||||
|
|
||||||
unsigned long int hashValue = waitui_hashtable_hash(this, key);
|
|
||||||
waitui_hashtable_node *node = this->list[hashValue];
|
|
||||||
|
|
||||||
while (node && (key.len != node->key.len ||
|
|
||||||
memcmp(key.s, node->key.s, key.len) != 0 ||
|
|
||||||
(valueCheckFn && !valueCheckFn(node->value, arg)))) {
|
|
||||||
node = node->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!node) { return 0; }
|
|
||||||
|
|
||||||
node->isStolen = 1;
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int waitui_hashtable_isStolenCheck(
|
|
||||||
waitui_hashtable *this, str key,
|
|
||||||
waitui_hashtable_value_check_fn valueCheckFn, void *arg) {
|
|
||||||
if (!this || !key.s) { return 0; }
|
|
||||||
|
|
||||||
unsigned long int hashValue = waitui_hashtable_hash(this, key);
|
|
||||||
waitui_hashtable_node *node = this->list[hashValue];
|
|
||||||
|
|
||||||
while (node && (key.len != node->key.len ||
|
|
||||||
memcmp(key.s, node->key.s, key.len) != 0 ||
|
|
||||||
(valueCheckFn && !valueCheckFn(node->value, arg)))) {
|
|
||||||
node = node->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!node) { return 0; }
|
|
||||||
|
|
||||||
return node->isStolen;
|
|
||||||
}
|
|
||||||
|
|
||||||
int waitui_hashtable_insert(waitui_hashtable *this, str key, void *value) {
|
|
||||||
return waitui_hashtable_insertCheck(this, key, value, NULL, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
void *waitui_hashtable_lookup(waitui_hashtable *this, str key) {
|
|
||||||
return waitui_hashtable_lookupCheck(this, key, NULL, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
int waitui_hashtable_has(waitui_hashtable *this, str key) {
|
|
||||||
return waitui_hashtable_hasCheck(this, key, NULL, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
int waitui_hashtable_markStolen(waitui_hashtable *this, str key) {
|
|
||||||
return waitui_hashtable_markStolenCheck(this, key, NULL, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
int waitui_hashtable_isStolen(waitui_hashtable *this, str key) {
|
|
||||||
return waitui_hashtable_isStolenCheck(this, key, NULL, NULL);
|
|
||||||
}
|
|
||||||
@ -15,8 +15,6 @@ target_sources(list
|
|||||||
"src/list.c"
|
"src/list.c"
|
||||||
PUBLIC
|
PUBLIC
|
||||||
"include/waitui/list.h"
|
"include/waitui/list.h"
|
||||||
"include/waitui/list_generic.h"
|
|
||||||
"include/waitui/list_generic_impl.h"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(list PUBLIC "include")
|
target_include_directories(list PUBLIC "include")
|
||||||
@ -1,9 +1,9 @@
|
|||||||
/**
|
/**
|
||||||
* @file list.h
|
* @file list.h
|
||||||
* @author rick
|
* @author rick
|
||||||
* @date 22.07.20
|
* @date 22.07.20
|
||||||
* @brief File for the List implementation
|
* @brief File for the List implementation
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef WAITUI_LIST_H
|
#ifndef WAITUI_LIST_H
|
||||||
#define WAITUI_LIST_H
|
#define WAITUI_LIST_H
|
||||||
@ -16,115 +16,223 @@
|
|||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Type representing a List node
|
* @brief Type representing a List node.
|
||||||
*/
|
*/
|
||||||
typedef struct waitui_list_node waitui_list_node;
|
typedef struct waitui_list_node waitui_list_node;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Type for element destroy function
|
* @brief Type for element destroy function.
|
||||||
*/
|
*/
|
||||||
typedef void (*waitui_list_element_destroy)(void **element);
|
typedef void (*waitui_list_element_destroy)(void **element);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Type representing a List
|
* @brief Type representing a List.
|
||||||
*/
|
*/
|
||||||
typedef struct waitui_list waitui_list;
|
typedef struct waitui_list waitui_list;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Type representing a List iterator
|
* @brief Type representing a List iterator.
|
||||||
*/
|
*/
|
||||||
typedef struct waitui_list_iter waitui_list_iter;
|
typedef struct waitui_list_iter waitui_list_iter;
|
||||||
|
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// Public defines
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#define INTERFACE_LIST_TYPEDEF(type) \
|
||||||
|
typedef waitui_list type##_list; \
|
||||||
|
typedef waitui_list_iter type##_list_iter
|
||||||
|
#define IMPLEMENTATION_LIST_TYPEDEF(type)
|
||||||
|
|
||||||
|
#define INTERFACE_LIST_NEW(type) extern type##_list *type##_list_new()
|
||||||
|
#define IMPLEMENTATION_LIST_NEW(type) \
|
||||||
|
type##_list *type##_list_new() { \
|
||||||
|
return (type##_list *) waitui_list_new( \
|
||||||
|
(waitui_list_element_destroy) type##_destroy); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define INTERFACE_LIST_DESTROY(type) \
|
||||||
|
extern void type##_list_destroy(type##_list **this)
|
||||||
|
#define IMPLEMENTATION_LIST_DESTROY(type) \
|
||||||
|
void type##_list_destroy(type##_list **this) { \
|
||||||
|
waitui_list_destroy((waitui_list **) this); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define INTERFACE_LIST_PUSH(type) \
|
||||||
|
extern int type##_list_push(type##_list *this, type *type##Element)
|
||||||
|
#define IMPLEMENTATION_LIST_PUSH(type) \
|
||||||
|
int type##_list_push(type##_list *this, type *type##Element) { \
|
||||||
|
return waitui_list_push((waitui_list *) this, (void *) type##Element); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define INTERFACE_LIST_POP(type) extern type *type##_list_pop(type##_list *this)
|
||||||
|
#define IMPLEMENTATION_LIST_POP(type) \
|
||||||
|
type *type##_list_pop(type##_list *this) { \
|
||||||
|
return (type *) waitui_list_pop((waitui_list *) this); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define INTERFACE_LIST_UNSHIFT(type) \
|
||||||
|
extern int type##_list_unshift(type##_list *this, type *type##Element)
|
||||||
|
#define IMPLEMENTATION_LIST_UNSHIFT(type) \
|
||||||
|
int type##_list_unshift(type##_list *this, type *type##Element) { \
|
||||||
|
return waitui_list_unshift((waitui_list *) this, \
|
||||||
|
(void *) type##Element); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define INTERFACE_LIST_SHIFT(type) \
|
||||||
|
extern type *type##_list_shift(type##_list *this)
|
||||||
|
#define IMPLEMENTATION_LIST_SHIFT(type) \
|
||||||
|
type *type##_list_shift(type##_list *this) { \
|
||||||
|
return (type *) waitui_list_shift((waitui_list *) this); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define INTERFACE_LIST_PEEK(type) \
|
||||||
|
extern type *type##_list_peek(type##_list *this)
|
||||||
|
#define IMPLEMENTATION_LIST_PEEK(type) \
|
||||||
|
type *type##_list_peek(type##_list *this) { \
|
||||||
|
return (type *) waitui_list_peek((waitui_list *) this); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define INTERFACE_LIST_GET_ITERATOR(type) \
|
||||||
|
extern type##_list_iter *type##_list_getIterator(type##_list *this)
|
||||||
|
#define IMPLEMENTATION_LIST_GET_ITERATOR(type) \
|
||||||
|
type##_list_iter *type##_list_getIterator(type##_list *this) { \
|
||||||
|
return (type##_list_iter *) waitui_list_getIterator( \
|
||||||
|
(waitui_list *) this); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define INTERFACE_LIST_ITER_HAS_NEXT(type) \
|
||||||
|
extern bool type##_list_iter_hasNext(type##_list_iter *this)
|
||||||
|
#define IMPLEMENTATION_LIST_ITER_HAS_NEXT(type) \
|
||||||
|
bool type##_list_iter_hasNext(type##_list_iter *this) { \
|
||||||
|
return waitui_list_iter_hasNext((waitui_list_iter *) this); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define INTERFACE_LIST_ITER_NEXT(type) \
|
||||||
|
extern type *type##_list_iter_next(type##_list_iter *this)
|
||||||
|
#define IMPLEMENTATION_LIST_ITER_NEXT(type) \
|
||||||
|
type *type##_list_iter_next(type##_list_iter *this) { \
|
||||||
|
return (type *) waitui_list_iter_next((waitui_list_iter *) this); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define INTERFACE_LIST_ITER_DESTROY(type) \
|
||||||
|
extern void type##_list_iter_destroy(type##_list_iter **this)
|
||||||
|
#define IMPLEMENTATION_LIST_ITER_DESTROY(type) \
|
||||||
|
void type##_list_iter_destroy(type##_list_iter **this) { \
|
||||||
|
waitui_list_iter_destroy((waitui_list_iter **) this); \
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Define for quickly created list implementations for a value type.
|
||||||
|
* @param[in] kind Whether to create interface list definition
|
||||||
|
* or actual implementation
|
||||||
|
* @param[in] type For what type to create the list
|
||||||
|
*/
|
||||||
|
#define CREATE_LIST_TYPE(kind, type) \
|
||||||
|
kind##_LIST_TYPEDEF(type); \
|
||||||
|
kind##_LIST_NEW(type); \
|
||||||
|
kind##_LIST_DESTROY(type); \
|
||||||
|
kind##_LIST_PUSH(type); \
|
||||||
|
kind##_LIST_POP(type); \
|
||||||
|
kind##_LIST_UNSHIFT(type); \
|
||||||
|
kind##_LIST_SHIFT(type); \
|
||||||
|
kind##_LIST_PEEK(type); \
|
||||||
|
kind##_LIST_GET_ITERATOR(type); \
|
||||||
|
kind##_LIST_ITER_HAS_NEXT(type); \
|
||||||
|
kind##_LIST_ITER_NEXT(type); \
|
||||||
|
kind##_LIST_ITER_DESTROY(type);
|
||||||
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
// Public functions
|
// Public functions
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Create a List
|
* @brief Create a List.
|
||||||
* @param[in] elementDestroyCallback Function to call for element destruction
|
* @param[in] elementDestroyCallback Function to call for element destruction
|
||||||
* @return A pointer to waitui_list or NULL if memory allocation failed
|
* @return A pointer to waitui_list or NULL if memory allocation failed
|
||||||
*/
|
*/
|
||||||
extern waitui_list *
|
extern waitui_list *
|
||||||
waitui_list_new(waitui_list_element_destroy elementDestroyCallback);
|
waitui_list_new(waitui_list_element_destroy elementDestroyCallback);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Destroy a List
|
* @brief Destroy a List.
|
||||||
* @param[in,out] this The List to destroy
|
* @param[in,out] this The List to destroy
|
||||||
* @note This will free call for every element the elementDestroyCallback
|
* @note This will free call for every element the elementDestroyCallback
|
||||||
*/
|
*/
|
||||||
extern void waitui_list_destroy(waitui_list **this);
|
extern void waitui_list_destroy(waitui_list **this);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Add the element to the end of the List
|
* @brief Add the element to the end of the List.
|
||||||
* @param[in,out] this The List to add the element at the end
|
* @param[in,out] this The List to add the element at the end
|
||||||
* @param[in] element The element to add
|
* @param[in] element The element to add
|
||||||
* @note This function does steel the pointer to the element
|
* @note This function does steel the pointer to the element.
|
||||||
* @retval 1 Ok
|
* @retval 1 Ok
|
||||||
* @retval 0 Memory allocation failed
|
* @retval 0 Memory allocation failed
|
||||||
*/
|
*/
|
||||||
extern int waitui_list_push(waitui_list *this, void *element);
|
extern int waitui_list_push(waitui_list *this, void *element);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Remove the element from the end of the List and return it
|
* @brief Remove the element from the end of the List and return it.
|
||||||
* @param[in,out] this The List to remove the element from
|
* @param[in,out] this The List to remove the element from
|
||||||
* @note The caller has to destroy element on its own
|
* @note The caller has to destroy element on its own.
|
||||||
* @return The element or NULL if waitui_list is empty
|
* @return The element or NULL if waitui_list is empty
|
||||||
*/
|
*/
|
||||||
extern void *waitui_list_pop(waitui_list *this);
|
extern void *waitui_list_pop(waitui_list *this);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Add the element to the beginning of the List
|
* @brief Add the element to the beginning of the List.
|
||||||
* @param[in,out] this The List to add the element at the beginning
|
* @param[in,out] this The List to add the element at the beginning
|
||||||
* @param[in] element The element to add
|
* @param[in] element The element to add
|
||||||
* @note This function does steel the pointer to the element
|
* @note This function does steel the pointer to the element.
|
||||||
* @retval 1 Ok
|
* @retval 1 Ok
|
||||||
* @retval 0 Memory allocation failed
|
* @retval 0 Memory allocation failed
|
||||||
*/
|
*/
|
||||||
extern int waitui_list_unshift(waitui_list *this, void *element);
|
extern int waitui_list_unshift(waitui_list *this, void *element);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Remove the element from the beginning of the List and return it
|
* @brief Remove the element from the beginning of the List and return it.
|
||||||
* @param[in,out] this The List to remove the element from
|
* @param[in,out] this The List to remove the element from
|
||||||
* @note The caller has to destroy element on its own
|
* @note The caller has to destroy element on its own.
|
||||||
* @return The element or NULL if waitui_list is empty
|
* @return The element or NULL if waitui_list is empty
|
||||||
*/
|
*/
|
||||||
extern void *waitui_list_shift(waitui_list *this);
|
extern void *waitui_list_shift(waitui_list *this);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Return the element from the end of the List, without removing it
|
* @brief Return the element from the end of the List, without removing it.
|
||||||
* @param[in] this The List to get the last element from
|
* @param[in] this The List to get the last element from
|
||||||
* @warning The caller has not to destroy element on its own
|
* @warning The caller has not to destroy element on its own.
|
||||||
* @return The element or NULL if waitui_list is empty
|
* @return The element or NULL if waitui_list is empty
|
||||||
*/
|
*/
|
||||||
extern void *waitui_list_peek(waitui_list *this);
|
extern void *waitui_list_peek(waitui_list *this);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Return the iterator to iterate over the List
|
* @brief Return the iterator to iterate over the List.
|
||||||
* @param[in] this The List to get the iterator for
|
* @param[in] this The List to get the iterator for
|
||||||
* @return A pointer to waitui_list_iter or NULL if memory allocation failed
|
* @return A pointer to waitui_list_iter or NULL if memory allocation failed
|
||||||
*/
|
*/
|
||||||
extern waitui_list_iter *waitui_list_getIterator(waitui_list *this);
|
extern waitui_list_iter *waitui_list_getIterator(waitui_list *this);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Return true if the List iterator has a next element
|
* @brief Return true if the List iterator has a next element.
|
||||||
* @param[in] this The List iterator to test for next element available
|
* @param[in] this The List iterator to test for next element available
|
||||||
* @retval true If the iterator has a next element available
|
* @retval true If the iterator has a next element available
|
||||||
* @retval false If the iterator has no next element available
|
* @retval false If the iterator has no next element available
|
||||||
*/
|
*/
|
||||||
extern bool waitui_list_iter_hasNext(waitui_list_iter *this);
|
extern bool waitui_list_iter_hasNext(waitui_list_iter *this);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Return the next element from the List iterator
|
* @brief Return the next element from the List iterator.
|
||||||
* @param[in] this The List iterator to get the next element
|
* @param[in] this The List iterator to get the next element
|
||||||
* @return The element or NULL if no more elements are available
|
* @return The element or NULL if no more elements are available
|
||||||
*/
|
*/
|
||||||
extern void *waitui_list_iter_next(waitui_list_iter *this);
|
extern void *waitui_list_iter_next(waitui_list_iter *this);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Destroy a List iterator
|
* @brief Destroy a List iterator.
|
||||||
* @param[in,out] this The List iterator to destroy
|
* @param[in,out] this The List iterator to destroy
|
||||||
*/
|
*/
|
||||||
extern void waitui_list_iter_destroy(waitui_list_iter **this);
|
extern void waitui_list_iter_destroy(waitui_list_iter **this);
|
||||||
|
|
||||||
#endif//WAITUI_LIST_H
|
#endif//WAITUI_LIST_H
|
||||||
@ -1,78 +0,0 @@
|
|||||||
/**
|
|
||||||
* @file list_generic.h
|
|
||||||
* @author rick
|
|
||||||
* @date 26.07.22
|
|
||||||
* @brief File for the generic List implementation
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef WAITUI_LIST_GENERIC_H
|
|
||||||
#define WAITUI_LIST_GENERIC_H
|
|
||||||
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
// Public defines
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
#define INTERFACE_LIST_TYPEDEF(type) \
|
|
||||||
typedef waitui_list type##_list; \
|
|
||||||
typedef waitui_list_iter type##_list_iter
|
|
||||||
|
|
||||||
#define INTERFACE_LIST_NEW(type) extern type##_list *type##_list_new()
|
|
||||||
|
|
||||||
#define INTERFACE_LIST_DESTROY(type) \
|
|
||||||
extern void type##_list_destroy(type##_list **this)
|
|
||||||
|
|
||||||
#define INTERFACE_LIST_PUSH(type) \
|
|
||||||
extern int type##_list_push(type##_list *this, type *type##Element)
|
|
||||||
|
|
||||||
#define INTERFACE_LIST_POP(type) extern type *type##_list_pop(type##_list *this)
|
|
||||||
|
|
||||||
#define INTERFACE_LIST_UNSHIFT(type) \
|
|
||||||
extern int type##_list_unshift(type##_list *this, type *type##Element)
|
|
||||||
|
|
||||||
|
|
||||||
#define INTERFACE_LIST_SHIFT(type) \
|
|
||||||
extern type *type##_list_shift(type##_list *this)
|
|
||||||
|
|
||||||
|
|
||||||
#define INTERFACE_LIST_PEEK(type) \
|
|
||||||
extern type *type##_list_peek(type##_list *this)
|
|
||||||
|
|
||||||
|
|
||||||
#define INTERFACE_LIST_GET_ITERATOR(type) \
|
|
||||||
extern type##_list_iter *type##_list_getIterator(type##_list *this)
|
|
||||||
|
|
||||||
|
|
||||||
#define INTERFACE_LIST_ITER_HAS_NEXT(type) \
|
|
||||||
extern bool type##_list_iter_hasNext(type##_list_iter *this)
|
|
||||||
|
|
||||||
|
|
||||||
#define INTERFACE_LIST_ITER_NEXT(type) \
|
|
||||||
extern type *type##_list_iter_next(type##_list_iter *this)
|
|
||||||
|
|
||||||
|
|
||||||
#define INTERFACE_LIST_ITER_DESTROY(type) \
|
|
||||||
extern void type##_list_iter_destroy(type##_list_iter **this)
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Define for quickly created list implementations for a value type
|
|
||||||
* @param[in] kind Whether to create interface list definition
|
|
||||||
* or actual implementation
|
|
||||||
* @param[in] type For what type to create the list
|
|
||||||
*/
|
|
||||||
#define CREATE_LIST_TYPE(kind, type) \
|
|
||||||
kind##_LIST_TYPEDEF(type); \
|
|
||||||
kind##_LIST_NEW(type); \
|
|
||||||
kind##_LIST_DESTROY(type); \
|
|
||||||
kind##_LIST_PUSH(type); \
|
|
||||||
kind##_LIST_POP(type); \
|
|
||||||
kind##_LIST_UNSHIFT(type); \
|
|
||||||
kind##_LIST_SHIFT(type); \
|
|
||||||
kind##_LIST_PEEK(type); \
|
|
||||||
kind##_LIST_GET_ITERATOR(type); \
|
|
||||||
kind##_LIST_ITER_HAS_NEXT(type); \
|
|
||||||
kind##_LIST_ITER_NEXT(type); \
|
|
||||||
kind##_LIST_ITER_DESTROY(type);
|
|
||||||
|
|
||||||
#endif//WAITUI_LIST_GENERIC_H
|
|
||||||
@ -1,79 +0,0 @@
|
|||||||
/**
|
|
||||||
* @file list_generic_impl.h
|
|
||||||
* @author rick
|
|
||||||
* @date 26.07.22
|
|
||||||
* @brief File for the generic List implementation
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef WAITUI_LIST_GENERIC_IMPL_H
|
|
||||||
#define WAITUI_LIST_GENERIC_IMPL_H
|
|
||||||
|
|
||||||
#include "waitui/list_generic.h"
|
|
||||||
#include "waitui/list.h"
|
|
||||||
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
// Public defines
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_LIST_TYPEDEF(type)
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_LIST_NEW(type) \
|
|
||||||
type##_list *type##_list_new() { \
|
|
||||||
return (type##_list *) waitui_list_new( \
|
|
||||||
(waitui_list_element_destroy) type##_destroy); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_LIST_DESTROY(type) \
|
|
||||||
void type##_list_destroy(type##_list **this) { \
|
|
||||||
waitui_list_destroy((waitui_list **) this); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_LIST_PUSH(type) \
|
|
||||||
int type##_list_push(type##_list *this, type *type##Element) { \
|
|
||||||
return waitui_list_push((waitui_list *) this, (void *) type##Element); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_LIST_POP(type) \
|
|
||||||
type *type##_list_pop(type##_list *this) { \
|
|
||||||
return (type *) waitui_list_pop((waitui_list *) this); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_LIST_UNSHIFT(type) \
|
|
||||||
int type##_list_unshift(type##_list *this, type *type##Element) { \
|
|
||||||
return waitui_list_unshift((waitui_list *) this, \
|
|
||||||
(void *) type##Element); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_LIST_SHIFT(type) \
|
|
||||||
type *type##_list_shift(type##_list *this) { \
|
|
||||||
return (type *) waitui_list_shift((waitui_list *) this); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_LIST_PEEK(type) \
|
|
||||||
type *type##_list_peek(type##_list *this) { \
|
|
||||||
return (type *) waitui_list_peek((waitui_list *) this); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_LIST_GET_ITERATOR(type) \
|
|
||||||
type##_list_iter *type##_list_getIterator(type##_list *this) { \
|
|
||||||
return (type##_list_iter *) waitui_list_getIterator( \
|
|
||||||
(waitui_list *) this); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_LIST_ITER_HAS_NEXT(type) \
|
|
||||||
bool type##_list_iter_hasNext(type##_list_iter *this) { \
|
|
||||||
return waitui_list_iter_hasNext((waitui_list_iter *) this); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_LIST_ITER_NEXT(type) \
|
|
||||||
type *type##_list_iter_next(type##_list_iter *this) { \
|
|
||||||
return (type *) waitui_list_iter_next((waitui_list_iter *) this); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IMPLEMENTATION_LIST_ITER_DESTROY(type) \
|
|
||||||
void type##_list_iter_destroy(type##_list_iter **this) { \
|
|
||||||
waitui_list_iter_destroy((waitui_list_iter **) this); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif//WAITUI_LIST_GENERIC_IMPL_H
|
|
||||||
@ -1,9 +1,9 @@
|
|||||||
/**
|
/**
|
||||||
* @file list.c
|
* @file list.c
|
||||||
* @author rick
|
* @author rick
|
||||||
* @date 22.07.20
|
* @date 22.07.20
|
||||||
* @brief File for the List implementation
|
* @brief File for the List implementation
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "waitui/list.h"
|
#include "waitui/list.h"
|
||||||
|
|
||||||
@ -15,8 +15,8 @@
|
|||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Struct representing a List node
|
* @brief Struct representing a List node.
|
||||||
*/
|
*/
|
||||||
struct waitui_list_node {
|
struct waitui_list_node {
|
||||||
void *element;
|
void *element;
|
||||||
waitui_list_node *next;
|
waitui_list_node *next;
|
||||||
@ -24,8 +24,8 @@ struct waitui_list_node {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Struct representing a List
|
* @brief Struct representing a List.
|
||||||
*/
|
*/
|
||||||
struct waitui_list {
|
struct waitui_list {
|
||||||
waitui_list_node *head;
|
waitui_list_node *head;
|
||||||
waitui_list_node *tail;
|
waitui_list_node *tail;
|
||||||
@ -34,8 +34,8 @@ struct waitui_list {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Struct representing a List iterator
|
* @brief Struct representing a List iterator.
|
||||||
*/
|
*/
|
||||||
struct waitui_list_iter {
|
struct waitui_list_iter {
|
||||||
waitui_list_node *node;
|
waitui_list_node *node;
|
||||||
};
|
};
|
||||||
@ -46,10 +46,10 @@ struct waitui_list_iter {
|
|||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Create a List iterator
|
* @brief Create a List iterator.
|
||||||
* @param[in] node The node to use as a start for the iterator
|
* @param[in] node The node to use as a start for the iterator
|
||||||
* @return A pointer to waitui_list_iter or NULL if memory allocation failed
|
* @return A pointer to waitui_list_iter or NULL if memory allocation failed
|
||||||
*/
|
*/
|
||||||
static waitui_list_iter *waitui_list_iter_new(waitui_list_node *node) {
|
static waitui_list_iter *waitui_list_iter_new(waitui_list_node *node) {
|
||||||
waitui_list_iter *this = NULL;
|
waitui_list_iter *this = NULL;
|
||||||
|
|
||||||
@ -216,4 +216,4 @@ void waitui_list_iter_destroy(waitui_list_iter **this) {
|
|||||||
|
|
||||||
free(*this);
|
free(*this);
|
||||||
*this = NULL;
|
*this = NULL;
|
||||||
}
|
}
|
||||||
@ -19,7 +19,7 @@
|
|||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The type for log events
|
* @brief The type for log events.
|
||||||
*/
|
*/
|
||||||
typedef struct waitui_log_event {
|
typedef struct waitui_log_event {
|
||||||
va_list ap;
|
va_list ap;
|
||||||
@ -32,17 +32,17 @@ typedef struct waitui_log_event {
|
|||||||
} waitui_log_event;
|
} waitui_log_event;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The logging callback function type
|
* @brief The logging callback function type.
|
||||||
*/
|
*/
|
||||||
typedef void (*waitui_log_logging_fn)(waitui_log_event *event);
|
typedef void (*waitui_log_logging_fn)(waitui_log_event *event);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The locking callback function type
|
* @brief The locking callback function type.
|
||||||
*/
|
*/
|
||||||
typedef void (*waitui_log_lock_fn)(bool lock, void *userData);
|
typedef void (*waitui_log_lock_fn)(bool lock, void *userData);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The different log levels
|
* @brief The different log levels.
|
||||||
*/
|
*/
|
||||||
typedef enum {
|
typedef enum {
|
||||||
WAITUI_LOG_TRACE,
|
WAITUI_LOG_TRACE,
|
||||||
@ -78,26 +78,26 @@ typedef enum {
|
|||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Set locking function for log
|
* @brief Set locking function for log.
|
||||||
* @param lockFn The function to be called for lock and unlock when writing logs
|
* @param lockFn The function to be called for lock and unlock when writing logs
|
||||||
* @param[in] userData Extra user data to pass to the locking function
|
* @param[in] userData Extra user data to pass to the locking function
|
||||||
*/
|
*/
|
||||||
extern void waitui_log_set_lock(waitui_log_lock_fn lockFn, void *userData);
|
extern void waitui_log_set_lock(waitui_log_lock_fn lockFn, void *userData);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Set minimum log level to log on stderr
|
* @brief Set minimum log level to log on stderr.
|
||||||
* @param level The minimum log level from which on log appear on stderr
|
* @param level The minimum log level from which on log appear on stderr
|
||||||
*/
|
*/
|
||||||
extern void waitui_log_setLevel(waitui_log_level level);
|
extern void waitui_log_setLevel(waitui_log_level level);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Disable or enable logging to stderr
|
* @brief Disable or enable logging to stderr.
|
||||||
* @param enable True to disable logging to stderr, false to enable
|
* @param enable True to disable logging to stderr, false to enable
|
||||||
*/
|
*/
|
||||||
extern void waitui_log_setQuiet(bool enable);
|
extern void waitui_log_setQuiet(bool enable);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Add the logFn as a log callback to write logs starting at the level
|
* @brief Add the logFn as a log callback to write logs starting at the level.
|
||||||
* @param logFn The function to add as a log callback
|
* @param logFn The function to add as a log callback
|
||||||
* @param[in] userData Extra user data to pass to the logFn
|
* @param[in] userData Extra user data to pass to the logFn
|
||||||
* @param level The level from which on this log callback is executed
|
* @param level The level from which on this log callback is executed
|
||||||
@ -108,23 +108,23 @@ extern int waitui_log_addCallback(waitui_log_logging_fn logFn, void *userData,
|
|||||||
waitui_log_level level);
|
waitui_log_level level);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Add a log callback to write logs starting at the level into the file
|
* @brief Add a log callback to write logs starting at the level into the file.
|
||||||
* @param[in] file The file into which to write the log
|
* @param[in] file The file into which to write the log
|
||||||
* @param level The level from which on this log callback is executed
|
* @param level The level from which on this log callback is executed
|
||||||
* @retval 1 Successful added the file callback
|
* @retval 1 Successful added the file callback
|
||||||
* @retval 0 No more space to add the callback
|
* @retval 0 No more space to add the callback
|
||||||
*/
|
*/
|
||||||
extern int waitui_log_addFile(FILE *file, waitui_log_level level);
|
extern int waitui_log_addFile(FILE *file, waitui_log_level level);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Write the log message to log with the given parameters
|
* @brief Write the log message to log with the given parameters.
|
||||||
* @param level The log level for this message
|
* @param level The log level for this message
|
||||||
* @param[in] file The filename where this log is written
|
* @param[in] file The filename where this log is written
|
||||||
* @param line The line number where this log is written
|
* @param line The line number where this log is written
|
||||||
* @param[in] format The message to write to the log
|
* @param[in] format The message to write to the log
|
||||||
* @param ... Extra values to be used inside the message
|
* @param ... Extra values to be used inside the message
|
||||||
*/
|
*/
|
||||||
extern void waitui_log_writeLog(waitui_log_level level, const char *file,
|
extern void waitui_log_writeLog(waitui_log_level level, const char *file,
|
||||||
int line, const char *format, ...);
|
int line, const char *format, ...);
|
||||||
|
|
||||||
#endif//WAITUI_LOG_H
|
#endif//WAITUI_LOG_H
|
||||||
@ -13,7 +13,7 @@
|
|||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The maximum number of possible callbacks to register
|
* @brief The maximum number of possible callbacks to register.
|
||||||
*/
|
*/
|
||||||
#define MAX_CALLBACKS 32
|
#define MAX_CALLBACKS 32
|
||||||
|
|
||||||
@ -23,7 +23,7 @@
|
|||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Internal type to store the log callback function with all its info
|
* @brief Internal type to store the log callback function with all its info.
|
||||||
*/
|
*/
|
||||||
typedef struct waitui_log_callback {
|
typedef struct waitui_log_callback {
|
||||||
waitui_log_logging_fn logFn;
|
waitui_log_logging_fn logFn;
|
||||||
@ -32,7 +32,7 @@ typedef struct waitui_log_callback {
|
|||||||
} waitui_log_callback;
|
} waitui_log_callback;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Internal type to store the log with all its info
|
* @brief Internal type to store the log with all its info.
|
||||||
*/
|
*/
|
||||||
typedef struct waitui_log {
|
typedef struct waitui_log {
|
||||||
waitui_log_lock_fn lockFn;
|
waitui_log_lock_fn lockFn;
|
||||||
@ -48,12 +48,12 @@ typedef struct waitui_log {
|
|||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The internal state of the log lib
|
* @brief The internal state of the log lib.
|
||||||
*/
|
*/
|
||||||
static struct waitui_log L;
|
static struct waitui_log L;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief String representations of the log levels
|
* @brief String representations of the log levels.
|
||||||
*/
|
*/
|
||||||
static const char *waitui_log_level_strings[] = {
|
static const char *waitui_log_level_strings[] = {
|
||||||
"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL",
|
"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL",
|
||||||
@ -61,7 +61,7 @@ static const char *waitui_log_level_strings[] = {
|
|||||||
|
|
||||||
#ifdef LOG_USE_COLOR
|
#ifdef LOG_USE_COLOR
|
||||||
/**
|
/**
|
||||||
* @brief Color representations of the log levels
|
* @brief Color representations of the log levels.
|
||||||
*/
|
*/
|
||||||
static const char *waitui_log_level_colors[] = {
|
static const char *waitui_log_level_colors[] = {
|
||||||
"\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m",
|
"\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m",
|
||||||
@ -74,7 +74,7 @@ static const char *waitui_log_level_colors[] = {
|
|||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Return the string representations of the log level
|
* @brief Return the string representations of the log level.
|
||||||
* @param level The level to return as a string.
|
* @param level The level to return as a string.
|
||||||
* @return The string representations of the log level or empty when out of bounds.
|
* @return The string representations of the log level or empty when out of bounds.
|
||||||
*/
|
*/
|
||||||
@ -84,7 +84,7 @@ static inline const char *waitui_log_levelAsString(waitui_log_level level) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Return the color representations of the log level
|
* @brief Return the color representations of the log level.
|
||||||
* @param level The level to return as a color.
|
* @param level The level to return as a color.
|
||||||
* @return The color representations of the log level or empty when out of bounds.
|
* @return The color representations of the log level or empty when out of bounds.
|
||||||
*/
|
*/
|
||||||
@ -94,21 +94,21 @@ static inline const char *waitui_log_levelAsColor(waitui_log_level level) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Calls the locking callback function to get the lock
|
* @brief Calls the locking callback function to get the lock.
|
||||||
*/
|
*/
|
||||||
static inline void waitui_log_lock(void) {
|
static inline void waitui_log_lock(void) {
|
||||||
if (L.lockFn) { L.lockFn(true, L.userData); }
|
if (L.lockFn) { L.lockFn(true, L.userData); }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Calls the locking callback function to release the lock
|
* @brief Calls the locking callback function to release the lock.
|
||||||
*/
|
*/
|
||||||
static inline void waitui_log_unlock(void) {
|
static inline void waitui_log_unlock(void) {
|
||||||
if (L.lockFn) { L.lockFn(false, L.userData); }
|
if (L.lockFn) { L.lockFn(false, L.userData); }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Callback that write the log event to the console
|
* @brief Callback that write the log event to the console.
|
||||||
* @param[in] event log event to write to the console
|
* @param[in] event log event to write to the console
|
||||||
*/
|
*/
|
||||||
static void waitui_log_console_callback(waitui_log_event *event) {
|
static void waitui_log_console_callback(waitui_log_event *event) {
|
||||||
@ -130,7 +130,7 @@ static void waitui_log_console_callback(waitui_log_event *event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Callback that write the log event to a file
|
* @brief Callback that write the log event to a file.
|
||||||
* @param[in] event log event to write to a file
|
* @param[in] event log event to write to a file
|
||||||
*/
|
*/
|
||||||
static void waitui_log_file_callback(waitui_log_event *event) {
|
static void waitui_log_file_callback(waitui_log_event *event) {
|
||||||
@ -147,7 +147,7 @@ static void waitui_log_file_callback(waitui_log_event *event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Initializes the log event with time and userdata
|
* @brief Initializes the log event with time and userdata.
|
||||||
* @param[in,out] event log event to write to initialize
|
* @param[in,out] event log event to write to initialize
|
||||||
* @param[in] user_data user data to set for the log event
|
* @param[in] user_data user data to set for the log event
|
||||||
*/
|
*/
|
||||||
@ -221,4 +221,4 @@ void waitui_log_writeLog(waitui_log_level level, const char *file, int line,
|
|||||||
}
|
}
|
||||||
|
|
||||||
waitui_log_unlock();
|
waitui_log_unlock();
|
||||||
}
|
}
|
||||||
@ -1,20 +0,0 @@
|
|||||||
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
|
|
||||||
|
|
||||||
include("project-meta-info.in")
|
|
||||||
|
|
||||||
project(waitui-str
|
|
||||||
VERSION ${project_version}
|
|
||||||
DESCRIPTION ${project_description}
|
|
||||||
HOMEPAGE_URL ${project_homepage}
|
|
||||||
LANGUAGES C)
|
|
||||||
|
|
||||||
add_library(str OBJECT)
|
|
||||||
|
|
||||||
target_sources(str
|
|
||||||
PRIVATE
|
|
||||||
"src/str.c"
|
|
||||||
PUBLIC
|
|
||||||
"include/waitui/str.h"
|
|
||||||
)
|
|
||||||
|
|
||||||
target_include_directories(str PUBLIC "include")
|
|
||||||
@ -1,113 +0,0 @@
|
|||||||
/**
|
|
||||||
* @file str.h
|
|
||||||
* @author rick
|
|
||||||
* @date 19.02.20
|
|
||||||
* @brief File for the String implementation
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef WAITUI_STR_H
|
|
||||||
#define WAITUI_STR_H
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
// Public types
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Type for strings that do not need to be '\0' terminated
|
|
||||||
*/
|
|
||||||
typedef struct str {
|
|
||||||
char *s;
|
|
||||||
unsigned long int len;
|
|
||||||
} str;
|
|
||||||
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
// Public defines
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
#define STR_NULL_INIT \
|
|
||||||
{ NULL, 0 }
|
|
||||||
|
|
||||||
#define STR_STATIC_INIT(_str_) \
|
|
||||||
{ (_str_), sizeof((_str_)) - 1 }
|
|
||||||
|
|
||||||
#define STR_FMT(_pstr_) \
|
|
||||||
(((_pstr_) != (str *) 0) ? (int) (_pstr_)->len : 0), \
|
|
||||||
(((_pstr_) != (str *) 0) ? (_pstr_)->s : "")
|
|
||||||
|
|
||||||
#define STR_STATIC_SET(_pstr_, _str_) \
|
|
||||||
do { \
|
|
||||||
if ((_pstr_)) { \
|
|
||||||
(_pstr_)->s = (_str_); \
|
|
||||||
(_pstr_)->len = sizeof((_str_)) - 1; \
|
|
||||||
} \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define STR_STATIC_COPY(_dstr_, _str_) \
|
|
||||||
do { \
|
|
||||||
if ((_dstr_)) { \
|
|
||||||
char *tmp = (_str_); \
|
|
||||||
(_dstr_)->len = sizeof((_str_)) - 1; \
|
|
||||||
(_dstr_)->s = calloc((_dstr_)->len, sizeof(*(_dstr_)->s)); \
|
|
||||||
if ((_dstr_)->s) { \
|
|
||||||
memcpy((_dstr_)->s, tmp, (_dstr_)->len); \
|
|
||||||
} else { \
|
|
||||||
(_dstr_)->len = 0; \
|
|
||||||
} \
|
|
||||||
} \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define STR_COPY(_dstr_, _pstr_) \
|
|
||||||
do { \
|
|
||||||
if ((_pstr_) && (_dstr_)) { \
|
|
||||||
(_dstr_)->len = (_pstr_)->len; \
|
|
||||||
(_dstr_)->s = calloc((_pstr_)->len, sizeof(*(_pstr_)->s)); \
|
|
||||||
if ((_dstr_)->s) { \
|
|
||||||
memcpy((_dstr_)->s, (_pstr_)->s, (_dstr_)->len); \
|
|
||||||
} else { \
|
|
||||||
(_dstr_)->len = 0; \
|
|
||||||
} \
|
|
||||||
} \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define STR_COPY_WITH_NUL(_dstr_, _pstr_) \
|
|
||||||
do { \
|
|
||||||
if ((_pstr_) && (_dstr_)) { \
|
|
||||||
(_dstr_)->len = (_pstr_)->len; \
|
|
||||||
(_dstr_)->s = calloc((_pstr_)->len + 1, sizeof(*(_pstr_)->s)); \
|
|
||||||
if ((_dstr_)->s) { \
|
|
||||||
memcpy((_dstr_)->s, (_pstr_)->s, (_dstr_)->len); \
|
|
||||||
} else { \
|
|
||||||
(_dstr_)->len = 0; \
|
|
||||||
} \
|
|
||||||
} \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define STR_SNPRINTF(_dstr_, _fmt_, ...) \
|
|
||||||
do { \
|
|
||||||
if ((_dstr_)) { \
|
|
||||||
(_dstr_)->len = snprintf(NULL, 0, (_fmt_), __VA_ARGS__); \
|
|
||||||
(_dstr_)->s = calloc((_dstr_)->len + 1, sizeof(*(_dstr_)->s)); \
|
|
||||||
if ((_dstr_)->s) { \
|
|
||||||
snprintf((_dstr_)->s, (_dstr_)->len + 1, (_fmt_), \
|
|
||||||
__VA_ARGS__); \
|
|
||||||
} else { \
|
|
||||||
(_dstr_)->len = 0; \
|
|
||||||
} \
|
|
||||||
} \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define STR_FREE(_pstr_) \
|
|
||||||
do { \
|
|
||||||
if ((_pstr_)) { \
|
|
||||||
if ((_pstr_)->s) { free((_pstr_)->s); }; \
|
|
||||||
(_pstr_)->len = 0; \
|
|
||||||
(_pstr_)->s = NULL; \
|
|
||||||
} \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#endif//WAITUI_STR_H
|
|
||||||
@ -1,3 +0,0 @@
|
|||||||
set(project_version 0.0.1)
|
|
||||||
set(project_description "waitui str library")
|
|
||||||
set(project_homepage "http://example.com")
|
|
||||||
@ -1,8 +0,0 @@
|
|||||||
/**
|
|
||||||
* @file str.c
|
|
||||||
* @author rick
|
|
||||||
* @date 19.02.20
|
|
||||||
* @brief File for the String implementation
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "waitui/str.h"
|
|
||||||
@ -1,12 +1 @@
|
|||||||
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
|
add_subdirectory(library/list)
|
||||||
|
|
||||||
include("project-meta-info.in")
|
|
||||||
|
|
||||||
project(waitui-tests
|
|
||||||
VERSION ${project_version}
|
|
||||||
DESCRIPTION ${project_description}
|
|
||||||
HOMEPAGE_URL ${project_homepage}
|
|
||||||
LANGUAGES C)
|
|
||||||
|
|
||||||
add_subdirectory(library/hashtable)
|
|
||||||
add_subdirectory(library/list)
|
|
||||||
@ -1,164 +0,0 @@
|
|||||||
//
|
|
||||||
// Created by Rick Barenthin on 09.02.22.
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef WAITUI_BDD_FOR_C_EXT_H
|
|
||||||
#define WAITUI_BDD_FOR_C_EXT_H
|
|
||||||
|
|
||||||
#include "bdd-for-c.h"
|
|
||||||
|
|
||||||
#define __BDD_EXT_1ST_PARAM_FMT__ "'%s'"
|
|
||||||
#define __BDD_EXT_OTHER_PARAM_FMT__ ", '%s'"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create the format based on the count passed.
|
|
||||||
* @param num The number of format string parameters
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
static inline char *__bdd_ext_build_format__(int num) {
|
|
||||||
size_t length = (sizeof(__BDD_EXT_1ST_PARAM_FMT__) - 1) +
|
|
||||||
(num - 1) * (sizeof(__BDD_EXT_OTHER_PARAM_FMT__) - 1) + 1;
|
|
||||||
char *fmt = calloc(length, sizeof(*fmt));
|
|
||||||
if (!fmt) {
|
|
||||||
perror("calloc(__bdd_ext_build_format__)");
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < num; i++) {
|
|
||||||
if (i != 0) {
|
|
||||||
strcat(fmt, __BDD_EXT_OTHER_PARAM_FMT__);
|
|
||||||
} else {
|
|
||||||
strcat(fmt, __BDD_EXT_1ST_PARAM_FMT__);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a format string from a test description and format parameters.
|
|
||||||
*
|
|
||||||
* @param description The description of the test case
|
|
||||||
* @param num The number of format string parameters
|
|
||||||
* @param ... The format string parameters a comma
|
|
||||||
* separated arguments
|
|
||||||
* @return The created format string
|
|
||||||
*/
|
|
||||||
static inline char *__bdd_ext_format_string_params__(const char *description,
|
|
||||||
int num, ...) {
|
|
||||||
va_list args;
|
|
||||||
va_list copy;
|
|
||||||
|
|
||||||
int length = 1;
|
|
||||||
int written = 0;
|
|
||||||
char *fmt_string = NULL;
|
|
||||||
char *fmt = __bdd_ext_build_format__(num);
|
|
||||||
|
|
||||||
va_start(args, num);
|
|
||||||
va_copy(copy, args);
|
|
||||||
length += snprintf(NULL, 0, "%s [", description);
|
|
||||||
length += vsnprintf(NULL, 0, fmt, copy);
|
|
||||||
length += snprintf(NULL, 0, "]");
|
|
||||||
fmt_string = calloc(length, sizeof(*fmt_string));
|
|
||||||
if (!fmt_string) {
|
|
||||||
perror("calloc(__bdd_ext_format_string_params__)");
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
va_end(copy);
|
|
||||||
written += snprintf(fmt_string, length, "%s [", description);
|
|
||||||
written += vsnprintf(fmt_string + written, length - written, fmt, args);
|
|
||||||
written += snprintf(fmt_string + written, length - written, "]");
|
|
||||||
va_end(args);
|
|
||||||
|
|
||||||
free(fmt);
|
|
||||||
|
|
||||||
return fmt_string;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create an array of `#test_param` structs.
|
|
||||||
*
|
|
||||||
* @note This macro must be used for passing test parameters to `for_it()`.
|
|
||||||
* @example: test_params({"input", "expected"}, {"input1", "expected1"})
|
|
||||||
*/
|
|
||||||
#define test_params(...) \
|
|
||||||
{ __VA_ARGS__ }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Define arguments for a format string as one macro argument.
|
|
||||||
*
|
|
||||||
* @note This macro must be used for passing test parameters to `for_it()`.
|
|
||||||
* @example: format_args(STR_FMT(&string), STR_FMT(&string1));
|
|
||||||
*/
|
|
||||||
#define format_args(...) __VA_ARGS__
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Define arguments for a format string as one macro argument.
|
|
||||||
*
|
|
||||||
* The first argument must be the number of format string parameters.
|
|
||||||
*
|
|
||||||
* @example: params_format(1, "%s", "%d");
|
|
||||||
*/
|
|
||||||
#define params_format(...) __VA_ARGS__
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a parametrized test case.
|
|
||||||
*
|
|
||||||
* Each test parameter results in an isolated test case which is
|
|
||||||
* executed independently and has its own output showing the test
|
|
||||||
* description followed by the given test parameters.
|
|
||||||
*
|
|
||||||
* @note: The given test parameters can be accessed by `#test_param`.
|
|
||||||
* @note: You must call `for_it_end()` after the test code to free
|
|
||||||
* allocated memory.
|
|
||||||
*
|
|
||||||
* @example: for_it("returns 1 and sets claim on existing claim",
|
|
||||||
* params_format(2, "%s", "%d"),
|
|
||||||
* format_args(test_param.input, test_param.expected),
|
|
||||||
* { char *input; int expected; },
|
|
||||||
* test_params(
|
|
||||||
* {"test input 1", 0},
|
|
||||||
* {"test input 2", 1},
|
|
||||||
* {"test input 4", 2},
|
|
||||||
* {"test input 5", 3}
|
|
||||||
* )
|
|
||||||
* )
|
|
||||||
*
|
|
||||||
* @param description: The description of the test
|
|
||||||
* @param params_format: A format string for printing the parameters
|
|
||||||
* of a test. Use `params_format()` macro for
|
|
||||||
* format creation.
|
|
||||||
* @param format_args: The arguments for the parameter format. Use
|
|
||||||
* `format_args()` macro for arguments creation.
|
|
||||||
* @param param_struct_body: The struct to be created holding the test
|
|
||||||
* parameters.
|
|
||||||
* @param params: The test parameters to be used
|
|
||||||
*/
|
|
||||||
#define for_it(description, params_format, format_args, param_struct_body, \
|
|
||||||
params) \
|
|
||||||
do { \
|
|
||||||
struct __bdd_ext_test_param__ param_struct_body \
|
|
||||||
__bdd_ext_test_params__[] = params; \
|
|
||||||
int __bdd_ext_input_size__ = sizeof(__bdd_ext_test_params__) / \
|
|
||||||
sizeof(__bdd_ext_test_params__[0]); \
|
|
||||||
for (int __bdd_ext_i__ = 0; __bdd_ext_i__ < __bdd_ext_input_size__; \
|
|
||||||
__bdd_ext_i__++) { \
|
|
||||||
struct __bdd_ext_test_param__ test_param = \
|
|
||||||
__bdd_ext_test_params__[__bdd_ext_i__]; \
|
|
||||||
char *__bdd_ext_param_format_string__ = \
|
|
||||||
__bdd_ext_format_string_params__(description, \
|
|
||||||
params_format); \
|
|
||||||
it(__bdd_ext_param_format_string__, format_args) {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* End a parametrized test and free all resources allocated in #for_it.
|
|
||||||
*/
|
|
||||||
#define for_it_end() \
|
|
||||||
} \
|
|
||||||
free(__bdd_ext_param_format_string__); \
|
|
||||||
} \
|
|
||||||
} \
|
|
||||||
while (0)
|
|
||||||
|
|
||||||
#endif//WAITUI_BDD_FOR_C_EXT_H
|
|
||||||
@ -664,7 +664,7 @@ for(\
|
|||||||
|
|
||||||
#define __BDD_CHECK_ONE__(condition) __BDD_CHECK__(condition, #condition)
|
#define __BDD_CHECK_ONE__(condition) __BDD_CHECK__(condition, #condition)
|
||||||
|
|
||||||
#define check(...) do { __BDD_MACRO__(__BDD_CHECK_, __VA_ARGS__) } while (0)
|
#define check(...) __BDD_MACRO__(__BDD_CHECK_, __VA_ARGS__)
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#pragma warning(pop)
|
#pragma warning(pop)
|
||||||
|
|||||||
@ -1,15 +0,0 @@
|
|||||||
add_executable(waitui-test_hashtable)
|
|
||||||
|
|
||||||
target_sources(waitui-test_hashtable
|
|
||||||
PRIVATE
|
|
||||||
"test_hashtable.c"
|
|
||||||
)
|
|
||||||
|
|
||||||
target_link_libraries(waitui-test_hashtable PRIVATE hashtable)
|
|
||||||
|
|
||||||
add_test(NAME waitui-test_hashtable COMMAND waitui-test_hashtable)
|
|
||||||
|
|
||||||
if(ENABLE_TEST_COVERAGE)
|
|
||||||
target_compile_options(hashtable PUBLIC -O0 -g -fprofile-arcs -ftest-coverage)
|
|
||||||
target_link_options(hashtable PUBLIC -fprofile-arcs -ftest-coverage)
|
|
||||||
endif()
|
|
||||||
@ -1,340 +0,0 @@
|
|||||||
/**
|
|
||||||
* @file test_hashtable.c
|
|
||||||
* @author rick
|
|
||||||
* @date 27.07.22
|
|
||||||
* @brief Test for the HashTable implementation
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "waitui/hashtable_generic.h"
|
|
||||||
#include "waitui/hashtable_generic_impl.h"
|
|
||||||
|
|
||||||
#include "../../bdd-for-c-ext.h"
|
|
||||||
|
|
||||||
typedef struct value {
|
|
||||||
int count;
|
|
||||||
int i;
|
|
||||||
} value;
|
|
||||||
|
|
||||||
static value *value_new(int i) {
|
|
||||||
value *this = calloc(1, sizeof(*this));
|
|
||||||
if (!this) { return NULL; }
|
|
||||||
this->i = i;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void value_destroy(value **this) {
|
|
||||||
if (!this || !(*this)) { return; }
|
|
||||||
free(*this);
|
|
||||||
*this = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int value_check_counter(value *this, int *count) {
|
|
||||||
if (!this) { return 0; }
|
|
||||||
return this->count == *count;
|
|
||||||
}
|
|
||||||
|
|
||||||
CREATE_HASHTABLE_TYPE(INTERFACE, value)
|
|
||||||
CREATE_HASHTABLE_TYPE(IMPLEMENTATION, value)
|
|
||||||
|
|
||||||
spec("hashtable") {
|
|
||||||
context("use as a normal hashtable with no forced collisions") {
|
|
||||||
static value_hashtable *hashtable = NULL;
|
|
||||||
|
|
||||||
before_each() {
|
|
||||||
hashtable = value_hashtable_new(42);
|
|
||||||
check(hashtable != NULL, "hashtable should not be NULL");
|
|
||||||
}
|
|
||||||
|
|
||||||
after_each() {
|
|
||||||
value_hashtable_destroy(&hashtable);
|
|
||||||
check(hashtable == NULL, "hashtable should be NULL");
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("value_hashtable_insert()") {
|
|
||||||
for_it(
|
|
||||||
"should add values to the end of the list and it should be there",
|
|
||||||
params_format(2, "start: %d", "elements: %d"),
|
|
||||||
format_args(test_param.start, test_param.count),
|
|
||||||
{
|
|
||||||
int start;
|
|
||||||
int count;
|
|
||||||
},
|
|
||||||
test_params(
|
|
||||||
{0, 0},
|
|
||||||
{0, 1},
|
|
||||||
{1, 1},
|
|
||||||
{4, 20},
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
for (int i = test_param.start; i < test_param.start + test_param.count; ++i) {
|
|
||||||
str key = STR_NULL_INIT;
|
|
||||||
value *value = NULL;
|
|
||||||
|
|
||||||
STR_SNPRINTF(&key, "key-%d", i);
|
|
||||||
check(key.s != NULL, "key.s should be not NULL");
|
|
||||||
|
|
||||||
check(value_hashtable_has(hashtable, key) == 0, "hashtable should not have the key already");
|
|
||||||
check(value_hashtable_insert(hashtable, key, value_new(i)) == 1);
|
|
||||||
check(value_hashtable_has(hashtable, key) == 1, "hashtable should have the key");
|
|
||||||
value = value_hashtable_lookup(hashtable, key);
|
|
||||||
check(value != NULL, "value should not be NULL");
|
|
||||||
check(value->i == i, "value should be %d but is %d", i, value->i);
|
|
||||||
|
|
||||||
STR_FREE(&key);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = test_param.start + test_param.count - 1; i >= test_param.start; --i) {
|
|
||||||
str key = STR_NULL_INIT;
|
|
||||||
value *value = NULL;
|
|
||||||
|
|
||||||
STR_SNPRINTF(&key, "key-%d", i);
|
|
||||||
check(key.s != NULL, "key.s should be not NULL");
|
|
||||||
|
|
||||||
value = value_hashtable_lookup(hashtable, key);
|
|
||||||
check(value != NULL, "value should not be NULL");
|
|
||||||
check(value->i == i, "value should be %d but is %d", i, value->i);
|
|
||||||
|
|
||||||
STR_FREE(&key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for_it_end();
|
|
||||||
|
|
||||||
it("should work with the empty str") {
|
|
||||||
str key = STR_STATIC_INIT("");
|
|
||||||
|
|
||||||
check(value_hashtable_has(hashtable, key) == 0, "hashtable should not have the key already");
|
|
||||||
check(value_hashtable_insert(hashtable, key, value_new(1337)) == 1);
|
|
||||||
check(value_hashtable_has(hashtable, key) == 1, "hashtable should have the key");
|
|
||||||
}
|
|
||||||
|
|
||||||
it("should not work with the str having NULL as string") {
|
|
||||||
str key = STR_NULL_INIT;
|
|
||||||
value *value = value_new(1337);
|
|
||||||
check(value_hashtable_insert(hashtable, key, value) == 0);
|
|
||||||
check(value != NULL, "value should not be NULL");
|
|
||||||
value_destroy(&value);
|
|
||||||
}
|
|
||||||
|
|
||||||
it("should return 0 with NULL as this param") {
|
|
||||||
str key = STR_STATIC_INIT("leet");
|
|
||||||
value *value = value_new(1);
|
|
||||||
check(value_hashtable_insert(NULL, key, value) == 0, "return should be 0");
|
|
||||||
check(value != NULL, "value should not be NULL");
|
|
||||||
value_destroy(&value);
|
|
||||||
}
|
|
||||||
|
|
||||||
it("should not allow double entry") {
|
|
||||||
str key = STR_STATIC_INIT("42");
|
|
||||||
|
|
||||||
check(value_hashtable_has(hashtable, key) == 0, "hashtable should not have the key already");
|
|
||||||
check(value_hashtable_insert(hashtable, key, value_new(1337)) == 1);
|
|
||||||
check(value_hashtable_has(hashtable, key) == 1, "hashtable should have the key");
|
|
||||||
|
|
||||||
value *value = value_new(1337);
|
|
||||||
check(value_hashtable_insert(hashtable, key, value) == 0, "return should be 0");
|
|
||||||
check(value != NULL, "value should not be NULL");
|
|
||||||
value_destroy(&value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("value_hashtable_insertCheck()") {
|
|
||||||
it("should allow a double insert when check test the value and returns true") {
|
|
||||||
int counter;
|
|
||||||
str key = STR_STATIC_INIT("foo");
|
|
||||||
|
|
||||||
value *value1 = value_new(1337);
|
|
||||||
value1->count = 0;
|
|
||||||
|
|
||||||
value *value2 = value_new(7331);
|
|
||||||
value2->count = 1;
|
|
||||||
|
|
||||||
value *resultValue = NULL;
|
|
||||||
|
|
||||||
counter = 0;
|
|
||||||
|
|
||||||
check(value_hashtable_insertCheck(hashtable, key, value1, (value_hashtable_value_check_fn) value_check_counter, &counter) == 1);
|
|
||||||
check(value_hashtable_hasCheck(hashtable, key, (value_hashtable_value_check_fn) value_check_counter, &counter) == 1);
|
|
||||||
check(value_hashtable_insertCheck(hashtable, key, value1, (value_hashtable_value_check_fn) value_check_counter, &counter) == 0);
|
|
||||||
|
|
||||||
counter = 1;
|
|
||||||
|
|
||||||
check(value_hashtable_insertCheck(hashtable, key, value2, (value_hashtable_value_check_fn) value_check_counter, &counter) == 1);
|
|
||||||
check(value_hashtable_hasCheck(hashtable, key, (value_hashtable_value_check_fn) value_check_counter, &counter) == 1);
|
|
||||||
check(value_hashtable_insertCheck(hashtable, key, value2, (value_hashtable_value_check_fn) value_check_counter, &counter) == 0);
|
|
||||||
|
|
||||||
counter = 0;
|
|
||||||
resultValue = value_hashtable_lookupCheck(hashtable, key, (value_hashtable_value_check_fn) value_check_counter, &counter);
|
|
||||||
check(resultValue != NULL, "resultValue should not be NULL");
|
|
||||||
check(resultValue->i == 1337, "resultValue->i should be %d", 1337);
|
|
||||||
check(resultValue->count == 0, "resultValue->count should be %d", 0);
|
|
||||||
|
|
||||||
counter = 1;
|
|
||||||
resultValue = value_hashtable_lookupCheck(hashtable, key, (value_hashtable_value_check_fn) value_check_counter, &counter);
|
|
||||||
check(resultValue != NULL, "resultValue should not be NULL");
|
|
||||||
check(resultValue->i == 7331, "resultValue->i should be %d", 7331);
|
|
||||||
check(resultValue->count == 1, "resultValue->count should be %d", 1);
|
|
||||||
check(value_hashtable_markStolenCheck(hashtable, key, (value_hashtable_value_check_fn) value_check_counter, &counter) == 1);
|
|
||||||
check(value_hashtable_isStolenCheck(hashtable, key, (value_hashtable_value_check_fn) value_check_counter, &counter) == 1);
|
|
||||||
value_destroy(&resultValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("value_hashtable_destroy()") {
|
|
||||||
it("should work with NULL as this param") {
|
|
||||||
value_hashtable_destroy(NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
it("should work with a pointer to NULL as this param") {
|
|
||||||
static value_hashtable *empty = NULL;
|
|
||||||
value_hashtable_destroy(&empty);
|
|
||||||
check(empty == NULL, "empty should be NULL");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("value_hashtable_lookup()") {
|
|
||||||
it("should work with the empty str") {
|
|
||||||
str key = STR_STATIC_INIT("");
|
|
||||||
value *value = value_hashtable_lookup(hashtable, key);
|
|
||||||
check(value == NULL, "value should be NULL");
|
|
||||||
}
|
|
||||||
|
|
||||||
it("should not work with the str having NULL as string") {
|
|
||||||
str key = STR_NULL_INIT;
|
|
||||||
value *value = value_hashtable_lookup(hashtable, key);
|
|
||||||
check(value == NULL, "value should be NULL");
|
|
||||||
}
|
|
||||||
|
|
||||||
it("should return NULL with NULL as this param") {
|
|
||||||
str key = STR_STATIC_INIT("leet");
|
|
||||||
value *value = value_hashtable_lookup(NULL, key);
|
|
||||||
check(value == NULL, "value should be NULL");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("value_hashtable_markStolen()") {
|
|
||||||
it("should mark the value for the given key as stolen") {
|
|
||||||
str key = STR_STATIC_INIT("leet");
|
|
||||||
value *value = NULL;
|
|
||||||
check(value_hashtable_has(hashtable, key) == 0,"hashtable should not have the key already");
|
|
||||||
check(value_hashtable_insert(hashtable, key, value_new(1337)) == 1);
|
|
||||||
check(value_hashtable_has(hashtable, key) == 1, "hashtable should have the key");
|
|
||||||
value = value_hashtable_lookup(hashtable, key);
|
|
||||||
check(value != NULL, "value should not be NULL");
|
|
||||||
check(value_hashtable_markStolen(hashtable, key) == 1, "hashtable should mark the value for the key as stolen");
|
|
||||||
check(value_hashtable_isStolen(hashtable, key) == 1, "hashtable should have the value for the key marked as stolen");
|
|
||||||
value_destroy(&value);
|
|
||||||
}
|
|
||||||
|
|
||||||
it("should return 0 the given key that is not existing") {
|
|
||||||
str keyExists = STR_STATIC_INIT("other");
|
|
||||||
check(value_hashtable_insert(hashtable, keyExists, value_new(1337)) == 1);
|
|
||||||
str key = STR_STATIC_INIT("leet");
|
|
||||||
check(value_hashtable_has(hashtable, key) == 0,"hashtable should not have the key already");
|
|
||||||
check(value_hashtable_markStolen(hashtable, key) == 0, "hashtable should mark the value for the key as stolen");
|
|
||||||
}
|
|
||||||
|
|
||||||
it("should work with the empty str") {
|
|
||||||
str key = STR_STATIC_INIT("");
|
|
||||||
value *value = NULL;
|
|
||||||
check(value_hashtable_insert(hashtable, key, value_new(1337)) == 1);
|
|
||||||
value = value_hashtable_lookup(hashtable, key);
|
|
||||||
check(value != NULL, "value should not be NULL");
|
|
||||||
check(value_hashtable_markStolen(hashtable, key) == 1, "return should be 1");
|
|
||||||
value_destroy(&value);
|
|
||||||
}
|
|
||||||
|
|
||||||
it("should not work with the str having NULL as string") {
|
|
||||||
str key = STR_NULL_INIT;
|
|
||||||
check(value_hashtable_markStolen(hashtable, key) == 0, "return should be 0");
|
|
||||||
}
|
|
||||||
|
|
||||||
it("should return 0 with NULL as this param") {
|
|
||||||
str key = STR_STATIC_INIT("leet");
|
|
||||||
check(value_hashtable_markStolen(NULL, key) == 0, "return should be 0");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("value_hashtable_has()") {
|
|
||||||
it("should work with the empty str") {
|
|
||||||
str key = STR_STATIC_INIT("");
|
|
||||||
check(value_hashtable_insert(hashtable, key, value_new(1337)) == 1);
|
|
||||||
check(value_hashtable_has(hashtable, key) == 1, "return should be 1");
|
|
||||||
}
|
|
||||||
|
|
||||||
it("should not work with the str having NULL as string") {
|
|
||||||
str key = STR_NULL_INIT;
|
|
||||||
check(value_hashtable_has(hashtable, key) == 0, "return should be 0");
|
|
||||||
}
|
|
||||||
|
|
||||||
it("should return NULL with NULL as this param") {
|
|
||||||
str key = STR_STATIC_INIT("leet");
|
|
||||||
check(value_hashtable_has(NULL, key) == 0, "return should be 0");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("value_hashtable_isStolen()") {
|
|
||||||
it("should work with the empty str") {
|
|
||||||
str key = STR_STATIC_INIT("");
|
|
||||||
check(value_hashtable_insert(hashtable, key, value_new(1337)) == 1);
|
|
||||||
check(value_hashtable_isStolen(hashtable, key) == 0, "return should be 0");
|
|
||||||
}
|
|
||||||
|
|
||||||
it("should not work with the str having NULL as string") {
|
|
||||||
str key = STR_NULL_INIT;
|
|
||||||
check(value_hashtable_isStolen(hashtable, key) == 0, "return should be 0");
|
|
||||||
}
|
|
||||||
|
|
||||||
it("should return NULL with NULL as this param") {
|
|
||||||
str key = STR_STATIC_INIT("leet");
|
|
||||||
check(value_hashtable_isStolen(NULL, key) == 0, "return should be 0");
|
|
||||||
}
|
|
||||||
|
|
||||||
it("should return 0 with a not stolen value") {
|
|
||||||
str key = STR_STATIC_INIT("test");
|
|
||||||
str otherKey1 = STR_STATIC_INIT("otherKey1");
|
|
||||||
check(value_hashtable_insert(hashtable, otherKey1, value_new(1337)) == 1);
|
|
||||||
check(value_hashtable_isStolen(hashtable, key) == 0, "return should be 0");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
context("use as a normal hashtable with forced collisions") {
|
|
||||||
static value_hashtable *hashtable = NULL;
|
|
||||||
|
|
||||||
before_each() {
|
|
||||||
hashtable = value_hashtable_new(2);
|
|
||||||
check(hashtable != NULL, "hashtable should not be NULL");
|
|
||||||
}
|
|
||||||
|
|
||||||
after_each() {
|
|
||||||
value_hashtable_destroy(&hashtable);
|
|
||||||
check(hashtable == NULL, "hashtable should be NULL");
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("value_hashtable_markStolen()") {
|
|
||||||
it("should mark the value for the given key as stolen") {
|
|
||||||
str key = STR_STATIC_INIT("leet");
|
|
||||||
str otherKey1 = STR_STATIC_INIT("otherKey1");
|
|
||||||
str otherKey2 = STR_STATIC_INIT("otherKey2");
|
|
||||||
str otherKey3 = STR_STATIC_INIT("otherKey3");
|
|
||||||
str otherKey4 = STR_STATIC_INIT("otherKey4");
|
|
||||||
value *value = NULL;
|
|
||||||
|
|
||||||
check(value_hashtable_has(hashtable, key) == 0,"hashtable should not have the key already");
|
|
||||||
check(value_hashtable_insert(hashtable, key, value_new(1337)) == 1);
|
|
||||||
check(value_hashtable_has(hashtable, key) == 1, "hashtable should have the key");
|
|
||||||
|
|
||||||
check(value_hashtable_insert(hashtable, otherKey1, value_new(1337)) == 1);
|
|
||||||
check(value_hashtable_insert(hashtable, otherKey2, value_new(1337)) == 1);
|
|
||||||
check(value_hashtable_insert(hashtable, otherKey3, value_new(1337)) == 1);
|
|
||||||
check(value_hashtable_insert(hashtable, otherKey4, value_new(1337)) == 1);
|
|
||||||
|
|
||||||
value = value_hashtable_lookup(hashtable, key);
|
|
||||||
check(value != NULL, "value should not be NULL");
|
|
||||||
check(value_hashtable_markStolen(hashtable, key) == 1, "hashtable should mark the value for the key as stolen");
|
|
||||||
check(value_hashtable_isStolen(hashtable, key) == 1, "hashtable should have the value for the key marked as stolen");
|
|
||||||
value_destroy(&value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -7,9 +7,5 @@ target_sources(waitui-test_list
|
|||||||
|
|
||||||
target_link_libraries(waitui-test_list PRIVATE list)
|
target_link_libraries(waitui-test_list PRIVATE list)
|
||||||
|
|
||||||
add_test(NAME waitui-test_list COMMAND waitui-test_list)
|
add_test(waitui-test_list waitui-test_list)
|
||||||
|
set_tests_properties(waitui-test_list PROPERTIES ENVIRONMENT "BDD_USE_TAP=1")
|
||||||
if(ENABLE_TEST_COVERAGE)
|
|
||||||
target_compile_options(list PUBLIC -O0 -g -fprofile-arcs -ftest-coverage)
|
|
||||||
target_link_options(list PUBLIC -fprofile-arcs -ftest-coverage)
|
|
||||||
endif()
|
|
||||||
@ -5,10 +5,9 @@
|
|||||||
* @brief Test for the List implementation
|
* @brief Test for the List implementation
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "waitui/list_generic.h"
|
#include "waitui/list.h"
|
||||||
#include "waitui/list_generic_impl.h"
|
|
||||||
|
|
||||||
#include "../../bdd-for-c-ext.h"
|
#include "../../bdd-for-c.h"
|
||||||
|
|
||||||
typedef struct value {
|
typedef struct value {
|
||||||
int count;
|
int count;
|
||||||
@ -32,227 +31,54 @@ CREATE_LIST_TYPE(INTERFACE, value)
|
|||||||
CREATE_LIST_TYPE(IMPLEMENTATION, value)
|
CREATE_LIST_TYPE(IMPLEMENTATION, value)
|
||||||
|
|
||||||
spec("list") {
|
spec("list") {
|
||||||
context("use as a normal list") {
|
context("use as a stack") {
|
||||||
static value_list *list = NULL;
|
static value_list *stack = NULL;
|
||||||
|
|
||||||
before_each() {
|
before() { stack = value_list_new(); }
|
||||||
list = value_list_new();
|
|
||||||
check(list != NULL, "list should not be NULL");
|
after() { value_list_destroy(&stack); }
|
||||||
|
|
||||||
|
it("should have a stack") {
|
||||||
|
check(stack != NULL, "stack should not be NULL");
|
||||||
}
|
}
|
||||||
|
|
||||||
after_each() {
|
it("should allow to push a value onto the stack") {
|
||||||
value_list_destroy(&list);
|
value *value1 = value_new(1);
|
||||||
check(list == NULL, "list should be NULL");
|
check(value1 != NULL, "value1 should not be NULL");
|
||||||
|
check(value_list_push(stack, value1) == true,
|
||||||
|
"value1 should be added to the stack");
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("value_list_push()") {
|
it("should allow to push a second value onto the stack") {
|
||||||
for_it(
|
value *value2 = value_new(2);
|
||||||
"should add values to the end of the list and it should be there",
|
check(value2 != NULL, "value2 should not be NULL");
|
||||||
params_format(2, "start: %d", "elements: %d"),
|
check(value_list_push(stack, value2) == true,
|
||||||
format_args(test_param.start, test_param.count),
|
"value1 should be added to the stack");
|
||||||
{
|
|
||||||
int start;
|
|
||||||
int count;
|
|
||||||
},
|
|
||||||
test_params(
|
|
||||||
{0, 0},
|
|
||||||
{0, 1},
|
|
||||||
{1, 1},
|
|
||||||
{4, 20},
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
for (int i = test_param.start; i < test_param.start + test_param.count; ++i) {
|
|
||||||
check(value_list_push(list, value_new(i)) == 1);
|
|
||||||
value *value = value_list_peek(list);
|
|
||||||
check(value != NULL, "value should not be NULL");
|
|
||||||
check(value->i == i, "value should be %d but is %d", i, value->i);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = test_param.start + test_param.count - 1; i >= test_param.start; --i) {
|
|
||||||
value *value = value_list_pop(list);
|
|
||||||
check(value != NULL, "value should not be NULL");
|
|
||||||
check(value->i == i, "value should be %d but is %d", i, value->i);
|
|
||||||
value_destroy(&value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for_it_end();
|
|
||||||
|
|
||||||
it("should return 0 with NULL as this param") {
|
|
||||||
value *value = value_new(1);
|
|
||||||
check(value_list_push(NULL, value) == 0, "return should not be 0");
|
|
||||||
check(value != NULL, "value should not be NULL");
|
|
||||||
value_destroy(&value);
|
|
||||||
}
|
|
||||||
|
|
||||||
it("should return 0 with NULL as valueElement param") {
|
|
||||||
check(value_list_push(list, NULL) == 0, "return should not be 0");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("value_list_pop()") {
|
it("should return the last added element on pop") {
|
||||||
it("should return NULL with NULL as this param") {
|
value *test = value_list_pop(stack);
|
||||||
static value *value = NULL;
|
check(test != NULL, "value_list_pop should not return NULL");
|
||||||
value = value_list_pop(NULL);
|
check(test->i == 2, "test should be 2");
|
||||||
check(value == NULL, "value should be NULL");
|
value_destroy(&test);
|
||||||
}
|
|
||||||
|
|
||||||
it("should return NULL with an empty list as this param") {
|
|
||||||
static value *value = NULL;
|
|
||||||
value = value_list_pop(list);
|
|
||||||
check(value == NULL, "value should be NULL");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("value_list_unshift()") {
|
it("should be able to peek the top element") {
|
||||||
for_it(
|
value *test = value_list_peek(stack);
|
||||||
"should add values to the begin of the list and it should be there",
|
check(test != NULL, "value_list_peek should not return NULL");
|
||||||
params_format(2, "start: %d", "elements: %d"),
|
check(test->i == 1, "test should be 1");
|
||||||
format_args(test_param.start, test_param.count),
|
|
||||||
{
|
|
||||||
int start;
|
|
||||||
int count;
|
|
||||||
},
|
|
||||||
test_params(
|
|
||||||
{0, 0},
|
|
||||||
{0, 1},
|
|
||||||
{1, 1},
|
|
||||||
{4, 20},
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
for (int i = test_param.start; i < test_param.start + test_param.count; ++i) {
|
|
||||||
check(value_list_unshift(list, value_new(i)) == 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = test_param.start + test_param.count - 1; i >= test_param.start; --i) {
|
|
||||||
value *value = value_list_shift(list);
|
|
||||||
check(value != NULL, "value should not be NULL");
|
|
||||||
check(value->i == i, "value should be %d but is %d", i, value->i);
|
|
||||||
value_destroy(&value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for_it_end();
|
|
||||||
|
|
||||||
it("should return 0 with NULL as this param") {
|
|
||||||
value *value = value_new(1);
|
|
||||||
check(value_list_unshift(NULL, value) == 0, "return should not be 0");
|
|
||||||
value_destroy(&value);
|
|
||||||
}
|
|
||||||
|
|
||||||
it("should return 0 with NULL as valueElement param") {
|
|
||||||
check(value_list_unshift(list, NULL) == 0, "return should not be 0");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("value_list_shift()") {
|
it("should return the first added element on pop") {
|
||||||
it("should return NULL with NULL as this param") {
|
value *test = value_list_pop(stack);
|
||||||
static value *value = NULL;
|
check(test != NULL, "value_list_pop should not return NULL");
|
||||||
value = value_list_shift(NULL);
|
check(test->i == 1, "test should be 1");
|
||||||
check(value == NULL, "value should be NULL");
|
value_destroy(&test);
|
||||||
}
|
|
||||||
|
|
||||||
it("should return NULL with an empty list as this param") {
|
|
||||||
static value *value = NULL;
|
|
||||||
value = value_list_shift(list);
|
|
||||||
check(value == NULL, "value should be NULL");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("value_list_getIterator()") {
|
it("should return NULL if peek with an empty stack") {
|
||||||
for_it(
|
value *test = value_list_peek(stack);
|
||||||
"should iterate over all values from the list",
|
check(test == NULL, "value_list_peek should return NULL");
|
||||||
params_format(2, "start: %d", "elements: %d"),
|
|
||||||
format_args(test_param.start, test_param.count),
|
|
||||||
{
|
|
||||||
int start;
|
|
||||||
int count;
|
|
||||||
},
|
|
||||||
test_params(
|
|
||||||
{0, 0},
|
|
||||||
{0, 1},
|
|
||||||
{1, 1},
|
|
||||||
{4, 20},
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
static value_list_iter *iter = NULL;
|
|
||||||
|
|
||||||
for (int i = test_param.start; i < test_param.start + test_param.count; ++i) {
|
|
||||||
check(value_list_push(list, value_new(i)) == 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
iter = value_list_getIterator(list);
|
|
||||||
check(iter != NULL, "iter should not be NULL");
|
|
||||||
|
|
||||||
int i = test_param.start;
|
|
||||||
while (value_list_iter_hasNext(iter)) {
|
|
||||||
check(i < test_param.start + test_param.count, "iteration should not iterate more than values added");
|
|
||||||
value *value = value_list_iter_next(iter);
|
|
||||||
check(value != NULL, "value should not be NULL");
|
|
||||||
check(value->i == i, "value should be %d but is %d", i, value->i);
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
|
|
||||||
value_list_iter_destroy(&iter);
|
|
||||||
check(iter == NULL, "iter should be NULL");
|
|
||||||
}
|
|
||||||
for_it_end();
|
|
||||||
|
|
||||||
it("should return NULL with NULL as this param") {
|
|
||||||
static value_list_iter *iter = NULL;
|
|
||||||
iter = value_list_getIterator(NULL);
|
|
||||||
check(iter == NULL, "iter should be NULL");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("value_list_iter_next()") {
|
|
||||||
it("should return NULL with NULL as this param") {
|
|
||||||
static value *value = NULL;
|
|
||||||
value = value_list_iter_next(NULL);
|
|
||||||
check(value == NULL, "value should be NULL");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("value_list_iter_hasNext()") {
|
|
||||||
it("should return false with NULL as this param") {
|
|
||||||
check(value_list_iter_hasNext(NULL) == false, "return should be false");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("value_list_iter_destroy()") {
|
|
||||||
it("should work with NULL as this param") {
|
|
||||||
value_list_iter_destroy(NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
it("should work with a pointer to NULL as this param") {
|
|
||||||
static value_list_iter *empty = NULL;
|
|
||||||
value_list_iter_destroy(&empty);
|
|
||||||
check(empty == NULL, "empty should be NULL");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("value_list_destroy()") {
|
|
||||||
it("should work with NULL as this param") {
|
|
||||||
value_list_destroy(NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
it("should work with a pointer to NULL as this param") {
|
|
||||||
static value_list *empty = NULL;
|
|
||||||
value_list_destroy(&empty);
|
|
||||||
check(empty == NULL, "empty should be NULL");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("value_list_peek()") {
|
|
||||||
it("should return NULL with NULL as this param") {
|
|
||||||
static value *value = NULL;
|
|
||||||
value = value_list_peek(NULL);
|
|
||||||
check(value == NULL, "value should be NULL");
|
|
||||||
}
|
|
||||||
|
|
||||||
it("should return NULL with an empty list as this param") {
|
|
||||||
static value *value = NULL;
|
|
||||||
value = value_list_peek(list);
|
|
||||||
check(value == NULL, "value should be NULL");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,3 +0,0 @@
|
|||||||
set(project_version 0.0.1)
|
|
||||||
set(project_description "waitui language system tests")
|
|
||||||
set(project_homepage "http://example.com")
|
|
||||||
Loading…
Reference in New Issue
Block a user