diff --git a/.appveyor.yml b/.appveyor.yml index 16dd572..e5c13ad 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -28,9 +28,6 @@ environment: platform: x64 generator: "Visual Studio 12 2013 Win64" -install: - - cmd: choco install doxygen.install - before_build: - cd c:\yall - cmake -G"%generator%" diff --git a/.sonar-project.properties b/.sonar-project.properties index 6914277..3ef9e16 100644 --- a/.sonar-project.properties +++ b/.sonar-project.properties @@ -12,8 +12,8 @@ # sonar.projectName=yall -sonar.projectKey=yall -sonar.organization=default +sonar.projectKey=naccyde_yall +sonar.organization=naccyde-github sonar.projectDescription=C light logging library diff --git a/.vscode/settings.json b/.vscode/settings.json index d7c6f22..f3c414f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -14,5 +14,8 @@ 120 ], }, - "C_Cpp.autoAddFileAssociations": false + "C_Cpp.autoAddFileAssociations": false, + "files.associations": { + "*.h":"c" + } } \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index a19730b..5db8547 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed ### Security +## [1.8.1] +### Added +* `CONTRIBUTING.md` +* Check parameter's output file when calling `_get_subsystem()` +### Changed +* Enforce use of explicit data types (e.g. `int32_t` instead of `int`) +* Remove call data limited line length +* Change `init_call_data()` to `call_new()` and `call_delete()` +* Use `llist` instead of a custom linked-list for call data +### Deprecated +### Removed +* `coverage.py` for Codacy coverage +* `message_delete_wrapper()` function +* `convert_data_to_message()` function +### Fixed +* Show subsystems names inside tree ([#146](https://github.com/naccyde/yall/issues/176)) +* Update subsystem name correctly when using an existing subsystem name +* Removing a subsystem now display the proper log message +### Security + ## [1.8.0] ### Added * Queue (non concurrent) mechanism diff --git a/CMakeLists.txt b/CMakeLists.txt index 87d45bd..d0482a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,7 +41,10 @@ endif () find_package(jansson REQUIRED) find_package(pthread REQUIRED) find_package(criterion REQUIRED) -find_package(Doxygen REQUIRED) + +if (UNIX) + find_package(Doxygen REQUIRED) +endif () #[[ Project version diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5d479a6..9fa324a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,28 +1,89 @@ -# yall contributing guide - -## Unit tests - -Unit tests have to be organized the following way: - -``` -tests - ├─ - │ ├─ test_.c - │ ├─ test_.c - │ ├─ test.c - │ └─ test.h - ├─ - │ ├─ test_.c - │ ├─ test_.c - │ ├─ test.c - │ └─ test.h - ... - ├─ test.c - └─ yall_test.h -``` - -This way, `test.h` and `test.c` files in each folder will store fixtures for the given source file. - -Then, each test case should describe a specific way of using the function, or should aim to trigger a particular error returned by the function. - -Global coverage percentage should be at least 95%, otherwise continuous integration will fail. This is required in order to ensure most part of the library is covered by unit test and reduce potential errors. Following this logic, once a bug is found in the library, a test case reproducing it should first be wrote, this allow to check its resolution and avoid regressions. +# Contributing to `yall` + +When contributing to this repository, please first discuss the change you wish to make via issue, email, or any other method with the owners of this repository before making a change. + +Please note we have a code of conduct, please follow it in all your interactions with the project. + +Documentation about how to use `yall` is available on the [project website](https://naccyde.github.io/yall/). This website also hosts [Doxygen](https://naccyde.github.io/yall/doxygen/index.html) and [lcov](https://naccyde.github.io/yall/coverage/index.html) documentation. + +## Pull Request Process + +1. Ensure any install or build dependencies are removed before the end of the layer when doing a build. +2. Update the documentation with details of changes to the interface, this includes new environment variables, exposed ports, useful file locations and container parameters. Doxygen documentation must be wrote to explain the new code (if any). +3. Update `CHANGELOG.md` to explain the modifications. +4. Ensure everything is working by running `make test` and `make doc` for both `Release` and `Debug` build. + +## Code of Conduct + +### Our Pledge + +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, gender identity and expression, level of experience, +nationality, personal appearance, race, religion, or sexual identity and +orientation. + +### Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or +advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic + address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +### Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or +reject comments, commits, code, wiki edits, issues, and other contributions +that are not aligned to this Code of Conduct, or to ban temporarily or +permanently any contributor for other behaviors that they deem inappropriate, +threatening, offensive, or harmful. + +### Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project e-mail +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +### Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported by contacting the project team at [INSERT EMAIL ADDRESS]. All +complaints will be reviewed and investigated and will result in a response that +is deemed necessary and appropriate to the circumstances. The project team is +obligated to maintain confidentiality with regard to the reporter of an incident. +Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good +faith may face temporary or permanent repercussions as determined by other +members of the project's leadership. + +### Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, +available at [http://contributor-covenant.org/version/1/4][version] + +[homepage]: http://contributor-covenant.org +[version]: http://contributor-covenant.org/version/1/4/ diff --git a/README.md b/README.md index fe8e074..0190238 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,21 @@ # yall -[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/Naccyde/yall/develop/LICENSE) +[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/naccyde/yall/master/LICENSE) +[![Travis (.org) branch](https://img.shields.io/travis/naccyde/yall/master.svg?label=Linux)](https://travis-ci.com/naccyde/yall) +[![AppVeyor branch](https://img.shields.io/appveyor/ci/naccyde/yall/master.svg?label=Windows)](https://ci.appveyor.com/project/Naccyde/yall) +[![SonarQube Coverage](https://img.shields.io/sonar/http/sonarcloud.io/yall/coverage.svg)](https://sonarcloud.io/dashboard?id=naccyde_yall) +[![SonarQube Tech Debt](https://img.shields.io/sonar/http/sonarcloud.io/yall/tech_debt.svg)](https://sonarcloud.io/dashboard?id=naccyde_yall) -[![!][1b]][1l] [![!][2b]][2l] [![!][3b]][3l] [![!][4b]][4l] - -`yall` is a subsystems based logging library. It allow to handle multiple ways to log message for differents parts of an application thanks to subsystems creation. Subsystems can inherit from one another, override parents parameters, ... +`yall` is a subsystems based logging library. It allows to create subsystems which will manage all the logging parameters for an area of the application. Subsystems can inherit from one another, override parents parameters... `yall` is available on Linux and Windows (MSVC12 and MSVC14). ## How to use +Informations about how to use `yall` can be found on the [project wiki](https://naccyde.github.io/yall/). -Informations about how to use `yall` can be found on the [project wiki](https://naccyde.gitlab.io/yall/). As the documentation is under redaction, the old one can be found in [Git history](https://gitlab.com/naccyde/yall/blob/755569d405948297665aeaad41d4bb9f7856ad35/README.md). - -The major steps are : +The main workflow to use yall is the following: * Initialize the library * Create subsystems * Log messages @@ -35,10 +36,10 @@ Git LFS is required to clone the repository. yall use CMake as build system gene Then : - cmake -Bbuild -H. -DCMAKE_BUILD_TYPE= + cmake -Bbuild -H. -DCMAKE_BUILD_TYPE=Release make -C build -Launched inside the sources directory, these commands will create a `build` directory to run CMake inside, and build the library. The `CMAKE_BUILD_TYPE` variable will define what will be installed. On `Release` build, only required parts to use the library will be build and installed. On `Debug` build, all the required file to build with the library will be build and installed. +Launched inside the sources directory, these commands will create a `build` directory to run CMake inside, and build the library. The `CMAKE_BUILD_TYPE` variable will define what will be installed. `Release` here can be changed to `Debug`, to build the library with the debug symbols. On Windows, the process is straightforward using CMake GUI and Visual Studio. @@ -61,16 +62,5 @@ The way to contribute to the project is define is `CONTRIBUTING.md` (currently u For any problem that can't be solved using `Debugging` section of the documentation, there is multiple solutions : -* Create an [issue](https://gitlab.com/naccyde/yall/issues) on the project, following the template. -* Send a mail to the project's service desk : `incoming+naccyde/yall gitlab com`. * Send a mail to the maintainer : `naccyde naccyde eu`. - - -[1b]: https://gitlab.com/naccyde/yall/badges/master/pipeline.svg -[1l]: https://gitlab.com/naccyde/yall/commits/master -[2b]: https://sonarcloud.io/api/badges/gate?key=yall -[2l]: https://sonarcloud.io/dashboard?id=yall -[3b]: https://sonarcloud.io/api/badges/measure?key=yall&metric=coverage -[3l]: https://sonarcloud.io/dashboard?id=yall -[4b]: https://sonarcloud.io/api/badges/measure?key=yall&metric=sqale_debt_ratio -[4l]: https://sonarcloud.io/dashboard?id=yall +* Fork the repository and create a pull-request. diff --git a/doc/CMakeLists.txt b/doc/CMakeLists.txt index 8d9a5a7..7257711 100644 --- a/doc/CMakeLists.txt +++ b/doc/CMakeLists.txt @@ -23,3 +23,7 @@ add_custom_target(_user_doc add_custom_target(doc DEPENDS _doxygen_doc _coverage_doc _user_doc) + +if (WIN32) + set_target_properties(doc PROPERTIES EXCLUDE_FROM_ALL TRUE) +endif () diff --git a/include/yall/call.h b/include/yall/call.h index b852c92..0063085 100644 --- a/include/yall/call.h +++ b/include/yall/call.h @@ -29,8 +29,7 @@ #include #include "yall/utils.h" - -#define DEFAULT_LINE_SIZE 1024U +#include "yall/container/llist.h" /** * \struct yall_call_data_line @@ -63,41 +62,35 @@ struct yall_call_data_line { typedef struct yall_call_data { size_t message_size; char *header; - struct yall_call_data_line *lines; + llist_t *lines; } yall_call_data; /** - * \brief Initialize a structure yall_call_data by setting header to "\n" and - * message_size to 1. - * \param d Pointer to structure of type yall_call_data. Can't be NULL. + * \brief Create a new yall_call_data object and return its pointer. + * \return Pointer to a new yall_call_data object. */ -void init_call_data(struct yall_call_data *d); +yall_call_data *call_new(void); /** - * \brief Add a line to the structure yall_call_data with the given content. - * \param d Pointer to structure of type yall_call_data. Can't be NULL. - * \param content Nul-terminated string to add as a line on the final log - * message can't be NULL. + * \brief Delete the given yall_call_data object (including header and lines). + * \param Pointer to a valid yall_call_data object. */ -void add_line(struct yall_call_data *d, char *content); +void call_delete(yall_call_data *d); /** - * \brief Remove and return the first line of the given yall_call_data. - * \param d Pointer to structure of type yall_call_data. Can't be NULL. - * \return First line of the given yall_call_data. + * \brief Return the computed length of the buffer to allocate in order to + * store the full message, including '\0'. + * \param d Pointer to a yall_call_data object. + * \return Minimal size of the buffer to store the message. */ -struct yall_call_data_line *remove_first_line(struct yall_call_data *d); +size_t call_get_buffer_length(yall_call_data *d); /** - * \brief Convert the content of the structure yall_call_data to a - * nul-terminated string inside *buffer* parameter. Limited to *len* bytes. - * \param buffer Buffer to write the log message in. - * \param len Maximum length of characters to write inside *buffer* including - * nul-terminating '\0'. + * \brief Remove and return the first line of the given yall_call_data. * \param d Pointer to structure of type yall_call_data. Can't be NULL. + * \return First line of the given yall_call_data. */ -void convert_data_to_message(char *buffer, size_t len, - struct yall_call_data *d); +struct yall_call_data_line *remove_first_line(struct yall_call_data *d); /** * \brief Called by the user's formatter function, it allow to define the header diff --git a/include/yall/container/llist.h b/include/yall/container/llist.h index 34ff493..a3d8cef 100644 --- a/include/yall/container/llist.h +++ b/include/yall/container/llist.h @@ -26,6 +26,7 @@ #define _YALL_CONTAINER_LLIST_H #include +#include typedef struct llist_node_t llist_node_t; typedef struct llist_t llist_t; @@ -50,6 +51,8 @@ llist_t *ll_new(void); */ void ll_delete(llist_t *l, void (*data_delete)(void *data)); +size_t ll_get_size(llist_t *l); + /** * \brief Insert the data at the given position. If the position does not exists * in the list (insert at index 40 in a list composed of 3 elements, for diff --git a/include/yall/cpp/Yall.hpp b/include/yall/cpp/Yall.hpp index fa17a89..0aef3ea 100644 --- a/include/yall/cpp/Yall.hpp +++ b/include/yall/cpp/Yall.hpp @@ -57,7 +57,7 @@ class YallException : public std::exception private: std::string msg; - int num; + yall_error num; }; class YallData diff --git a/include/yall/message.h b/include/yall/message.h index 8886072..a8a18d2 100644 --- a/include/yall/message.h +++ b/include/yall/message.h @@ -87,14 +87,6 @@ struct message *message_new(char *data, */ void message_delete(struct message *msg); -/** - * \brief Used to suppress warnings as function deleting the message is called - * by a function requiring it to be as void (*)(void *). - * TODO: Delete - * \param msg Message to delete. Can't be NULL. - */ -void message_delete_wrapper(void *msg); - /** * \brief Create the log message. It fills *buffer* we given data and specific * format. By the way, the function returns the required buffer size of the @@ -117,6 +109,6 @@ size_t generate_std_msg(char *log_buffer, size_t len, * \param len Length of the log buffer. * \param d Call data to generate the log message from. */ -void generate_call_msg(char *buffer, size_t len, struct yall_call_data *d); +void generate_call_msg(char *buff, size_t len, struct yall_call_data *d); #endif diff --git a/include/yall/subsystem.h b/include/yall/subsystem.h index c8f345e..e0b5d06 100644 --- a/include/yall/subsystem.h +++ b/include/yall/subsystem.h @@ -163,7 +163,7 @@ struct yall_subsystem *create_subsystem(const char *name, void add_subsystem(const char *parent, struct yall_subsystem *s); /** - * \brief Update a given subsystem : from an existing subsystem it upadte its + * \brief Update a given subsystem : from an existing subsystem it update its * parameters instead of creating a new one. * \param s The subsystem to update. * \param log_level New log level of the subsystem. diff --git a/resources/build.sh b/resources/build.sh index 05079c0..b272d7e 100755 --- a/resources/build.sh +++ b/resources/build.sh @@ -36,5 +36,5 @@ sonar-scanner \ -Dsonar.cfamily.build-wrapper-output=${YALL_ROOT}/build/out/bw \ -Dsonar.projectBaseDir=${YALL_ROOT} \ -Dsonar.cfamily.gcov.reportsPath=${YALL_ROOT}/build/out/coverage \ - -Dsonar.login=${SONAR_API_KEY} \ + -Dsonar.login=${SONAR_TOKEN} \ -Dsonar.projectVersion=${TRAVIS_TAG} ${SONAR_EXTRA_OPTIONS} diff --git a/resources/ci.sh b/resources/ci.sh index 3f1f5e8..01405f1 100755 --- a/resources/ci.sh +++ b/resources/ci.sh @@ -6,9 +6,9 @@ set -ex : "${TRAVIS_TAG?ERROR: You must set TRAVIS_TAG}" : "${TRAVIS_BRANCH?ERROR: You must set TRAVIS_BRANCH}" : "${TRAVIS_COMMIT?ERROR: You must set TRAVIS_COMMIT}" -: "${SONAR_API_KEY?ERROR: You must set SONAR_API_KEY}" +: "${SONAR_TOKEN?ERROR: You must set SONAR_TOKEN}" -docker run -u `id -u $USER` -e TRAVIS_PULL_REQUEST=${TRAVIS_PULL_REQUEST} -e TRAVIS_TAG=${TRAVIS_TAG} -e TRAVIS_BRANCH=${TRAVIS_BRANCH} -e TRAVIS_COMMIT=${TRAVIS_COMMIT} -e SONAR_API_KEY=${SONAR_API_KEY} --rm -v `pwd`:/code naccyde/yall /code/resources/build.sh +docker run -u `id -u $USER` -e TRAVIS_PULL_REQUEST=${TRAVIS_PULL_REQUEST} -e TRAVIS_TAG=${TRAVIS_TAG} -e TRAVIS_BRANCH=${TRAVIS_BRANCH} -e TRAVIS_COMMIT=${TRAVIS_COMMIT} -e SONAR_TOKEN=${SONAR_TOKEN} --rm -v `pwd`:/code naccyde/yall /code/resources/build.sh docker run --rm -v `pwd`/build/out/packages:/packages debian:buster-slim /bin/bash -c "apt update && apt install -y libjansson-dev && dpkg -I /packages/yall_*.deb && dpkg -i /packages/yall_*.deb && dpkg -I /packages/yall-dev_*.deb && dpkg -i /packages/yall-dev_*.deb" diff --git a/resources/release.sh b/resources/release.sh index d17a3c8..14c461d 100755 --- a/resources/release.sh +++ b/resources/release.sh @@ -38,14 +38,14 @@ fi git checkout develop sed -i "s/Unreleased/$YALL_VERSION/g" CHANGELOG.md sed -i '6i## [Unreleased]\n### Added\n### Changed\n### Deprecated\n### Removed\n### Fixed\n### Security\n' CHANGELOG.md -git commit -a -m"Release $YALL_VERSION" +git commit -a -m"[ci skip] Release $YALL_VERSION" git pull git push # Merging git checkout master git pull -git merge --squash develop +git merge -Xtheirs --squash develop git commit -a -m"Release ${YALL_VERSION}" git tag ${YALL_VERSION} master diff --git a/src/call.c b/src/call.c index cd1d667..bfcd896 100644 --- a/src/call.c +++ b/src/call.c @@ -31,116 +31,93 @@ #include "yall/config/parameters.h" -// TODO : remove the limited line length - -void init_call_data(struct yall_call_data *d) +yall_call_data *call_new(void) { - d->message_size = 1; + yall_call_data *d = malloc(sizeof(yall_call_data)); - d->header = malloc(DEFAULT_LINE_SIZE); - d->header[0] = '\n'; - d->header[1] = '\0'; + d->message_size = 0; + d->header = NULL; + d->lines = ll_new(); - d->lines = NULL; + return d; } -void add_line(struct yall_call_data *d, char *content) +void call_delete(yall_call_data *d) { - struct yall_call_data_line *l = NULL; - - l = malloc(sizeof(struct yall_call_data_line)); - - if (d->lines == NULL) { - d->lines = l; - } else { - struct yall_call_data_line *tmp = d->lines; - - for ( ; tmp->next; tmp = tmp->next) - ; - tmp->next = l; - } - - l->content = content; - l->next = NULL; + ll_delete(d->lines, &free); + free(d->header); + free(d); } -struct yall_call_data_line *remove_first_line(struct yall_call_data *d) +size_t call_get_buffer_length(yall_call_data *d) { - struct yall_call_data_line *l = d->lines; + size_t len = 0; - if (l) { - d->lines = l->next; - d->message_size -= strlen(l->content); - } + len = d->message_size; + len += 2; // '\0' && header's '\n' + len += ll_get_size(d->lines); // '\n' for each line - return l; -} - -void convert_data_to_message(char *buffer, size_t len, struct yall_call_data *d) -{ - struct yall_call_data_line *l = NULL; - - snprintf(buffer, len, "%s", d->header); - free(d->header); - - while ((l = remove_first_line(d))) { - size_t curr_len = strlen(buffer); - - snprintf(&buffer[curr_len], len - curr_len, l->content); - - free(l->content); - free(l); - } + return len; } void yall_call_set_header(yall_call_data *d, const char *format, ...) { va_list args; - // Create the proper format with \n - char *_format = malloc(strlen(format) + 2); + va_list args_cpy; + size_t len = 0; - if (d->header) - d->message_size -= strlen(d->header); + va_copy(args_cpy, args); - snprintf(_format, strlen(format) + 2, "%s%c", format, '\n'); + if (d->header) { + d->message_size -= strlen(d->header); + free(d->header); + } - va_start(args, format); + // Compute buffer size + va_start(args_cpy, format); + len = (size_t)vsnprintf(NULL, 0, format, args_cpy); + va_end(args_cpy); - vsnprintf(d->header, DEFAULT_LINE_SIZE, _format, args); - d->message_size += strlen(d->header); + d->header = malloc(len + 1); + // Write string in buffer + va_start(args, format); + vsprintf(d->header, format, args); va_end(args); - free(_format); + d->message_size += len; } void yall_call_add_line(yall_call_data *d, uint8_t indent, const char *format, ...) { va_list args; - uint8_t i = 0; + va_list args_cpy; + size_t i = 0; + size_t len = 0; + char *content = NULL; uint8_t tab_width = yall_config_get_tab_width(); - char *line_content = malloc(DEFAULT_LINE_SIZE); - ++indent; // To, defaultly, set all line at 1 tab + ++indent; // Default to 1 tab for call messages + va_copy(args_cpy, args); - for ( ; i < tab_width * indent; ++i) - line_content[i] = ' '; + // Compute buffer size + va_start(args_cpy, format); + len = (size_t)vsnprintf(NULL, 0U, format, args_cpy) + + (size_t)tab_width * indent; + va_end(args_cpy); - // Create the message line + content = malloc(len + 1); + + for (i = 0; i < tab_width * indent; ++i) + content[i] = ' '; + + // Write message in buffer va_start(args, format); - vsnprintf(&line_content[i], DEFAULT_LINE_SIZE - - (uint32_t)(tab_width * indent), format, args); + vsprintf(&content[i], format, args); va_end(args); - // Manage \n - size_t lf = strlen(line_content) == DEFAULT_LINE_SIZE - 1 ? - DEFAULT_LINE_SIZE - 2 : strlen(line_content); - - line_content[lf] = '\n'; - line_content[lf+1] = '\0'; + d->message_size += len; - // Add the line to the data list - add_line(d, line_content); - d->message_size += strlen(line_content); + ll_insert_last(d->lines, content); } diff --git a/src/container/llist.c b/src/container/llist.c index dcf17d3..bfa6acf 100644 --- a/src/container/llist.c +++ b/src/container/llist.c @@ -37,7 +37,7 @@ struct llist_node_t { struct llist_t { struct llist_node_t *head; struct llist_node_t *tail; - int32_t size; + size_t size; }; /** @@ -96,6 +96,7 @@ static void *ll_node_delete(llist_node_t *n, void (*data_delete)(void *data)) */ static llist_node_t *ll_get_node_at(llist_t *l, int32_t index) { + int32_t size = (int32_t)l->size; llist_node_t *n = NULL; // List is empty @@ -103,7 +104,7 @@ static llist_node_t *ll_get_node_at(llist_t *l, int32_t index) goto end; // We want the last element - if (-1 == index || index >= l->size) + if (-1 == index || index >= size) return l->tail; // We want an element inside the list @@ -163,32 +164,25 @@ static llist_node_t *ll_remove_node_at(llist_t *l, int32_t index) */ static void ll_insert_node_at(llist_t *l, int32_t index, llist_node_t *n) { + int32_t size = (int32_t)l->size; llist_node_t *next = ll_get_node_at(l, index); llist_node_t *previous = NULL; if (! next) { l->head = n; l->tail = n; - ++l->size; - return; - } - - if (next == l->head) { - l->head = n; - next->previous = n; - n->next = next; - } else if (next == l->tail && (-1 == index || index >= l->size)) { - /* - * The condition here ensure we want to replace the tail and - * not the last element (inserting before the tail). - */ + } else if (-1 == index || index >= size) { l->tail = n; next->next = n; n->previous = next; } else { previous = next->previous; - previous->next = n; + if (next == l->head) + l->head = n; + else + previous->next = n; + n->previous = previous; n->next = next; next->previous = n; @@ -223,6 +217,11 @@ void ll_delete(llist_t *l, void (*data_delete)(void *data)) free(l); } +size_t ll_get_size(llist_t *l) +{ + return l->size; +} + void ll_insert_at(llist_t *l, int32_t index, void *data) { llist_node_t *n = ll_node_new(data); diff --git a/src/header.c b/src/header.c index a22f55f..5aeee23 100644 --- a/src/header.c +++ b/src/header.c @@ -140,10 +140,8 @@ static void set_matches_and_header(enum header_type hdr_type, void header_compile_format(enum header_type hdr_type, const char *format) { - // TODO : avoid using "int", use more clear type : uint16_t, ... - - int hdr_len = 0; - int match_idx = 0; + uint32_t hdr_len = 0; + uint32_t match_idx = 0; char *hdr = NULL; enum yall_matches *matches = NULL; bool seek_modifier = false; @@ -220,10 +218,6 @@ static size_t generate_hdr(enum header_type hdr_type, char *buffer, size_t len, hc->date_long }; - /* - * TODO : the way the header is printed is ABSOLUTELY BARBARIC - * Also, we consider snprintf() will not return an error... - */ wrote = (size_t)snprintf(buffer, len, hdr, ordered_content[matches[0]], ordered_content[matches[1]], ordered_content[matches[2]], ordered_content[matches[3]], ordered_content[matches[4]], diff --git a/src/log_level.c b/src/log_level.c index cf1ccf3..640a1b6 100644 --- a/src/log_level.c +++ b/src/log_level.c @@ -25,6 +25,7 @@ #include "yall/log_level.h" #include +#include static struct log_level_str_set { const char *log_level_name; @@ -53,7 +54,7 @@ enum yall_log_level str_to_log_level(const char *str) if (! str) return ll; - for (int i = 0; i < 9; ++i) { + for (uint16_t i = 0; i < 9; ++i) { if (strcmp(log_level_str[i].log_level_name, str) == 0) { ll = i; break; diff --git a/src/message.c b/src/message.c index c3a90c6..47089dc 100644 --- a/src/message.c +++ b/src/message.c @@ -56,11 +56,6 @@ void message_delete(struct message *msg) free(msg); } -void message_delete_wrapper(void *msg) -{ - message_delete((struct message *) msg); -} - size_t generate_std_msg(char *log_buffer, size_t len, const char *message_format, va_list args) { @@ -75,23 +70,16 @@ size_t generate_std_msg(char *log_buffer, size_t len, return ret; } -void generate_call_msg(char *buffer, size_t len, struct yall_call_data *d) +void generate_call_msg(char *buff, size_t len, struct yall_call_data *d) { - struct yall_call_data_line *l = NULL; - - snprintf(buffer, len, "%s", d->header); - free(d->header); + size_t s = 0; + char *m = NULL; - while ((l = remove_first_line(d))) { - size_t curr_len = strlen(buffer); + s += (size_t)snprintf(buff, len, "%s\n", d->header ? d->header : ""); - snprintf(&buffer[curr_len], len - curr_len, l->content); + while ((m = ll_remove_first(d->lines))) { + s += (size_t)snprintf(&buff[s], len - s, "%s\n", m); - /* - * TODO : call_data free should be call in the same scope as - * it is created. - */ - free(l->content); - free(l); + free(m); } } diff --git a/src/subsystem.c b/src/subsystem.c index 59863f5..01bee78 100644 --- a/src/subsystem.c +++ b/src/subsystem.c @@ -145,14 +145,11 @@ static void set_subsys_status( /** * \brief reset_subsystem Used to reset the internal values of a subsystem. It * is useless to set default values for string pointer as if they are NULL, - * the default value is used instead. On the other, default values which - * are not pointers must be set because they will contain garbage - * otherwise. + * the default value is used instead. Subsystem's name isn't set neither. * \param s Pointer to the subsystem to reset. */ static void reset_subsystem(struct yall_subsystem *s) { - s->name[0] = '\0'; s->log_level = yall_inherited_level; s->status = yall_inherited_status; s->output_type = yall_inherited_output; @@ -304,7 +301,7 @@ struct yall_subsystem *remove_subsystem(const char *name) s->previous = NULL; s->parent = NULL; - _YALL_DBG_INFO("Subsystem %d removed.", name); + _YALL_DBG_INFO("Subsystem %s removed.", name); } return s; @@ -433,7 +430,7 @@ static void show_subsystems_tree_call(struct yall_call_data *d, strncpy(&buff[strlen(buff)], op[curr_indent], strlen(op[curr_indent])+1); - //strncpy(&buff[strlen(buff)], s->name, strlen(s->name) + 1); + strncpy(&buff[strlen(buff)], s->name, buff_size-strlen(buff)); yall_call_add_line(d, 0, buff); diff --git a/src/writer/console.c b/src/writer/console.c index 7fe2bfe..b326ff9 100644 --- a/src/writer/console.c +++ b/src/writer/console.c @@ -66,7 +66,7 @@ static uint8_t colors[8] = { 15, 10, 10, 14, 12, 12, 12, 12 }; static void set_color(enum yall_log_level log_level) { WORD wColor; - int color = colors[log_level]; + uint16_t color = colors[log_level]; HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO csbi; @@ -82,7 +82,7 @@ static void set_color(enum yall_log_level log_level) static void reset_color(void) { WORD wColor; - int color = 15; + uint16_t color = 15; HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO csbi; diff --git a/src/writer/syslog.c b/src/writer/syslog.c index 355560d..360537a 100644 --- a/src/writer/syslog.c +++ b/src/writer/syslog.c @@ -36,7 +36,7 @@ struct { const char *str; - int value; + int32_t value; } syslog_facilities_set[] = { { "yall_fac_kern", LOG_KERN }, { "yall_fac_user", LOG_USER }, @@ -67,11 +67,11 @@ struct { * \param log_level yall log level. * \return Integer corresponding to the propre syslog log level. */ -static int get_syslog_level(enum yall_log_level ll) +static uint8_t get_syslog_level(enum yall_log_level ll) { #ifdef __linux__ - static int syslog_ll[] = { LOG_DEBUG, LOG_INFO, LOG_NOTICE, LOG_WARNING, - LOG_ERR, LOG_CRIT, LOG_ALERT , LOG_EMERG }; + static uint8_t syslog_ll[] = { LOG_DEBUG, LOG_INFO, LOG_NOTICE, + LOG_WARNING, LOG_ERR, LOG_CRIT, LOG_ALERT , LOG_EMERG }; return syslog_ll[ll]; #else @@ -81,7 +81,7 @@ static int get_syslog_level(enum yall_log_level ll) enum yall_syslog_facility str_to_syslog_facility(const char *s) { - for (int i = 0; i < NB_FACILITIES; ++i) { + for (uint8_t i = 0; i < NB_FACILITIES; ++i) { if (strcmp(syslog_facilities_set[i].str, s) == 0) return syslog_facilities_set[i].value; } @@ -92,8 +92,8 @@ enum yall_syslog_facility str_to_syslog_facility(const char *s) void write_log_syslog(enum yall_log_level log_level, const char *msg) { #ifdef __linux__ - syslog((int)LOG_MAKEPRI((int)yall_config_get_syslog_facility(), - get_syslog_level(log_level)), msg); + syslog((int32_t)LOG_MAKEPRI((int32_t)yall_config_get_syslog_facility(), + (int32_t)get_syslog_level(log_level)), msg); #else #endif } diff --git a/src/writer/thread.c b/src/writer/thread.c index 7d90ac0..4b8ef1a 100644 --- a/src/writer/thread.c +++ b/src/writer/thread.c @@ -71,7 +71,8 @@ uint8_t start_thread(uint16_t frequency, cqueue_t *messages_queue) thread_frequency = frequency; messages = messages_queue; - int thread_ret = pthread_create(&thread, NULL, writer_thread, NULL); + int32_t thread_ret = (int32_t)pthread_create(&thread, NULL, + writer_thread, NULL); if (thread_ret != 0) ret = YALL_CANT_CREATE_THREAD; diff --git a/src/yall.c b/src/yall.c index 68da6b7..2b2712a 100644 --- a/src/yall.c +++ b/src/yall.c @@ -113,7 +113,6 @@ yall_error yall_log(const char *subsystem, const char *format, ...) { - // TODO : prefix structs with "yall" yall_error ret = YALL_SUCCESS; char *buff = NULL; struct message *m = NULL; @@ -195,9 +194,9 @@ yall_error yall_call_log(const char *subsystem, { yall_error ret = YALL_SUCCESS; char *buff = NULL; - size_t hdr_len = 0; + size_t wrote = 0; size_t buff_len = 0; - struct yall_call_data d = { 0 }; + struct yall_call_data *d = NULL; struct header_content hc = { 0 }; struct yall_subsystem_params p = { 0 }; struct message *m = NULL; @@ -219,26 +218,26 @@ yall_error yall_call_log(const char *subsystem, goto end; } - init_call_data(&d); + d = call_new(); /* * Detailled informations about the following calls can be found inside * the sources of yall_log() function. */ - - formatter(&d, args); + formatter(d, args); fill_header_content(&hc, subsystem, log_level, function_name, filename, line); - hdr_len = generate_call_hdr(NULL, 0, &hc); - buff_len = hdr_len + d.message_size + 1; + buff_len = generate_call_hdr(NULL, 0, &hc) + call_get_buffer_length(d); buff = malloc(buff_len); m = message_new(buff, log_level, &p); - generate_call_hdr(buff, hdr_len + 1, &hc); - generate_call_msg(&buff[hdr_len], buff_len - hdr_len, &d); + wrote = generate_call_hdr(buff, buff_len, &hc); + generate_call_msg(&buff[wrote], buff_len - wrote, d); + + call_delete(d); write_msg(m); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 48cc9db..0d711b0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -4,6 +4,7 @@ add_subdirectory(c) add_subdirectory(cpp) +add_subdirectory(benchmark) if (UNIX OR MSVC_VERSION EQUAL 1900) add_subdirectory(unit) diff --git a/tests/benchmark/CMakeLists.txt b/tests/benchmark/CMakeLists.txt new file mode 100644 index 0000000..091c9eb --- /dev/null +++ b/tests/benchmark/CMakeLists.txt @@ -0,0 +1,73 @@ +# Copyright (C) 2017 Quentin "Naccyde" Deslandes. +# Redistribution and use of this file is allowed according to the terms of the MIT license. +# For details see the LICENSE file distributed with yall. + +#[[ + Yall objects compilation +#]] + +file(GLOB_RECURSE YALL_SRCS ${CMAKE_SOURCE_DIR}/src/*.c ${CMAKE_SOURCE_DIR}/include/*.h) + +if (UNIX) + # For more infos about the used flags : + # * https://stackoverflow.com/questions/3375697/useful-gcc-flags-for-c + # * https://kristerw.blogspot.fr/2017/09/useful-gcc-warning-options-not-enabled.html + # Compile options + set(_PVT_OPT -Wall -Wextra -Wconversion -ftrapv -Wfloat-equal -Wundef + -Wshadow -Wpointer-arith -Wcast-align -Wstrict-prototypes + -Wwrite-strings -Waggregate-return -Wuninitialized + -Wduplicated-cond -Wlogical-op -Wnull-dereference -Wdouble-promotion + -fvisibility=hidden -fPIC) + set(_PVT_OPT_DEBUG -O0 -g) + set(_PVT_OPT_RELEASE -O3 -Werror) +elseif (WIN32) + # Compile options + + #[[ + * 4204 : non-const initializer + * 4115 : named type defininition in parenthesis + * 4127 : constant conditional expression + * 4255 : no function prototype given + * 4668 : macro not defined, replaced by 0 + * 4706 : assignment within conditional expression + * 4710 : function not inlined + * 4774 : argument is not a string literal + * 4820 : padding + * 4996 : "strdup" deprecated + #]] + + set(_PVT_OPT /wd4204 /wd4115 /wd4127 /wd4255 /wd4668 /wd4706 /wd4710 /wd4774 /wd4820 /wd4996 /Wall) + set(_PVT_OPT_DEBUG /Od) + set(_PVT_OPT_RELEASE /W4 /O2 /MP) + + # Compile definitions + set(_PVT_DEF _CRT_SECURE_NO_WARNINGS) + + set_property(TARGET yall PROPERTY FOLDER "library") +endif () + +macro(createBenchmark name) + add_executable(yall_benchmark_${name} ${YALL_SRCS} ${name}.c) + + target_compile_options(yall_benchmark_${name} + PRIVATE + ${_PVT_OPT} + $<$:${_PVT_OPT_DEBUG}> + $<$:${_PVT_OPT_RELEASE}>) + + target_compile_definitions(yall_benchmark_${name} + PRIVATE ${_PVT_DEF}) + + target_include_directories(yall_benchmark_${name} + PRIVATE + $ + $ + ${CMAKE_BINARY_DIR}/generated_headers) + + target_link_libraries(yall_benchmark_${name} + PRIVATE + pthread::pthread + jansson::jansson) +endmacro(createBenchmark) + +createBenchmark(call_add_message) diff --git a/tests/benchmark/benchmark.h b/tests/benchmark/benchmark.h new file mode 100644 index 0000000..797e2e2 --- /dev/null +++ b/tests/benchmark/benchmark.h @@ -0,0 +1,16 @@ +#ifndef _BENCHMARK_H +#define _BENCHMARK_H + +#include +#include +#include +#include +#include + +#define BENCHMARK(function) \ +int main(void) \ +{ \ + function; \ +} + +#endif diff --git a/tests/benchmark/call_add_message.c b/tests/benchmark/call_add_message.c new file mode 100644 index 0000000..1c9d4bb --- /dev/null +++ b/tests/benchmark/call_add_message.c @@ -0,0 +1,45 @@ +#include "benchmark.h" + +#include "yall/call.h" + +void call_add_message(uint32_t loops, uint32_t nb_messages) +{ + double t_dt_s = 0; + + for (uint32_t i = 0; i < loops; ++i) { + yall_call_data *d = call_new(); + + clock_t begin = clock(); + + for (uint32_t j = 0; j < nb_messages; ++j) + yall_call_add_line(d, (uint8_t)(j % 3), "Line number %d", j); + + clock_t elapsed = clock() - begin; + + call_delete(d); + + t_dt_s += (double)elapsed / CLOCKS_PER_SEC; + } + + uint32_t t_msg = nb_messages * loops; + + printf("call_add_message results:\n"); + printf("\tTotal time: %.3lfs\n", t_dt_s); + printf("\tTime for %d messages (avg.): %.3lfs\n", nb_messages, t_dt_s / loops); + printf("\tMessages per second (avg.): %.0lf\n", (double)t_msg / (double)t_dt_s); +} + +BENCHMARK(call_add_message(20, 20000)); + +/* + * Performances history, done on a release build, 20 loops and 20000 messages + * + * First version, using a custom singly-linked-list + * - Total time: 18.706s + * - Time for 20000 messages (avg.): 0.935s + * - Messages per second (avg.): 21384 + * Use llist instead of a singly-linked-list + * - Total time: 0.092s + * - Time for 20000 messages (avg.): 0.005s + * - Messages per second (avg.): 4335855 + */ diff --git a/tests/c/main.c b/tests/c/main.c index 447255e..f852685 100644 --- a/tests/c/main.c +++ b/tests/c/main.c @@ -160,6 +160,7 @@ int main(int argc, char *argv[]) yall_enable_debug("debug"); yall_load_configuration(config_file); + yall_show_subsystems_tree(); pthread_create(&threads[0], NULL, thread0, NULL); @@ -213,4 +214,4 @@ void memoryInfos(struct yall_call_data *d, const void *args) yall_call_add_line(d, 1, "7ff9c8021000-7ff9cc000000 ---p 00000000 00:00 0 "); yall_call_add_line(d, 1, "7ff9cd04f000-7ff9cd1e2000 r-xp 00000000 08:06 4195034 /lib/x86_64-linux-gnu/libc-2.24.so"); yall_call_add_line(d, 1, "7ff9cd1e2000-7ff9cd3e2000 ---p 00193000 08:06 4195034 /lib/x86_64-linux-gnu/libc-2.24.so"); -} \ No newline at end of file +} diff --git a/tests/cpp/CMakeLists.txt b/tests/cpp/CMakeLists.txt index 5cfd1b4..89d9540 100644 --- a/tests/cpp/CMakeLists.txt +++ b/tests/cpp/CMakeLists.txt @@ -7,7 +7,7 @@ add_executable(yall_cpp main.cpp) if (UNIX) # Compile options set(_PVT_OPT -Wall -Wextra) - set(_PVT_OPT_DEBUG -O0) + set(_PVT_OPT_DEBUG -O0 -g) set(_PVT_OPT_RELEASE -O3) elseif (WIN32) # Compile options diff --git a/tests/cpp/main.cpp b/tests/cpp/main.cpp index bf2e551..80a22b3 100644 --- a/tests/cpp/main.cpp +++ b/tests/cpp/main.cpp @@ -128,16 +128,16 @@ int main(void) Device d = Device(); - Yall::config().setTabWidth(4); - Yall::config().setStdHeaderTemplate("[%d] :: %L : %f : \n\t"); - Yall::config().setCallHeaderTemplate("[%d] :: %-9l : %s : "); + Yall::config().setTabWidth(4); + Yall::config().setStdHeaderTemplate("[%d] :: %L : %f : \n\t"); + Yall::config().setCallHeaderTemplate("[%d] :: %-9l : %s : "); - Yall::setSubsystem("yall_cpp_test", "", yall_debug, yall_console_output, ""); - Yall::setSubsystem("io", "yall_cpp_test", yall_debug, yall_console_output, ""); - Yall::setSubsystem("memory", "yall_cpp_test", yall_debug, yall_console_output, ""); - Yall::setSubsystem("scheduler", "yall_test_cpp", yall_debug, yall_console_output, ""); - Yall::setSubsystem("debug", "yall_test_cpp", yall_debug, yall_console_output, ""); - Yall::enableDebug("debug"); + Yall::setSubsystem("yall_cpp_test", "", yall_debug, yall_console_output, ""); + Yall::setSubsystem("io", "yall_cpp_test", yall_debug, yall_console_output, ""); + Yall::setSubsystem("memory", "yall_cpp_test", yall_debug, yall_console_output, ""); + Yall::setSubsystem("scheduler", "yall_test_cpp", yall_debug, yall_console_output, ""); + Yall::setSubsystem("debug", "yall_test_cpp", yall_debug, yall_console_output, ""); + Yall::enableDebug("debug"); pthread_create(&threads[0], NULL, thread0, NULL); pthread_create(&threads[1], NULL, thread1, NULL); @@ -146,13 +146,13 @@ int main(void) pthread_create(&threads[4], NULL, thread4, NULL); YALL_DEBUG("yall_cpp_test", Yall::getVersionString() << " " << Yall::getVersion()); - YALL_DEBUG("io", "IO subsystem ready."); + YALL_DEBUG("io", "IO subsystem ready."); YALL_CALL_ERR("memory", memoryInfos, nullptr); - YALL_ALERT("scheduler" , "Using process 7034."); + YALL_ALERT("scheduler" , "Using process 7034."); - Yall::disableSubsystem("scheduler"); - YALL_DEBUG("scheduler", "Starting process 52233."); - Yall::enableSubsystem("scheduler"); + Yall::disableSubsystem("scheduler"); + YALL_DEBUG("scheduler", "Starting process 52233."); + Yall::enableSubsystem("scheduler"); d.status(); @@ -166,7 +166,7 @@ int main(void) Yall::closeAll(); - return 0; + return 0; } void memoryInfos(YallData &d, const void *args) diff --git a/tests/unit/call/test.c b/tests/unit/call/test.c index b52e048..b8b3b26 100644 --- a/tests/unit/call/test.c +++ b/tests/unit/call/test.c @@ -35,29 +35,3 @@ static void test_call_clean(void) } TestSuite(call, .init=test_call_setup, .fini=test_call_clean); - -const char *test_header = NULL; -struct yall_call_data test_call_data = { 0 }; -struct yall_call_data_line *test_call_data_lines_array[2] = { 0 }; - -void test_2_call_data_lines(void) -{ - test_call_data.header = strdup("yall"); - add_line(&test_call_data, strdup("data")); - add_line(&test_call_data, strdup("test")); - - test_header = test_call_data.header; - test_call_data_lines_array[0] = test_call_data.lines; - test_call_data_lines_array[1] = test_call_data.lines->next; -} - -void test_clean_2_call_data_lines(void) -{ - free((void *)test_header); - - free(test_call_data_lines_array[0]->content); - free(test_call_data_lines_array[0]); - - free(test_call_data_lines_array[1]->content); - free(test_call_data_lines_array[1]); -} diff --git a/tests/unit/call/test.h b/tests/unit/call/test.h index 8372b77..fd73910 100644 --- a/tests/unit/call/test.h +++ b/tests/unit/call/test.h @@ -32,6 +32,8 @@ #include #include +#include "yall/container/llist.h" + #define _TEST_1088_LONG_CALL_HEADER \ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \ @@ -51,28 +53,4 @@ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" -#define _TEST_1023_LONG_CALL_HEADER \ - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \ - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \ - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \ - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \ - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \ - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \ - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \ - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \ - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \ - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \ - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \ - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \ - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \ - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \ - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \ - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - -extern struct yall_call_data test_call_data; -extern struct yall_call_data_line *test_call_data_lines_array[2]; - -void test_2_call_data_lines(void); -void test_clean_2_call_data_lines(void); - #endif diff --git a/tests/unit/call/test_add_line.c b/tests/unit/call/test_add_line.c deleted file mode 100644 index 69642b6..0000000 --- a/tests/unit/call/test_add_line.c +++ /dev/null @@ -1,46 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2017 Quentin "Naccyde" Deslandes. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include "call/test.h" - -/* - * O.K. - */ -Test(call, test_add_line0) -{ - struct yall_call_data d = { 0 }; - - add_line(&d, ""); - cr_assert_str_eq(d.lines->content, ""); - cr_assert_eq(d.lines->next, NULL); - - add_line(&d, " "); - cr_assert_str_eq(d.lines->next->content, " "); - cr_assert_eq(d.lines->next->next, NULL); - - add_line(&d, NULL); - cr_assert_eq(d.lines->next->next->content, NULL); - cr_assert_eq(d.lines->next->next->next, NULL); - -} diff --git a/tests/unit/message/test_message_delete_wrapper.c b/tests/unit/call/test_call_delete.c similarity index 78% rename from tests/unit/message/test_message_delete_wrapper.c rename to tests/unit/call/test_call_delete.c index c42f8b7..de84642 100644 --- a/tests/unit/message/test_message_delete_wrapper.c +++ b/tests/unit/call/test_call_delete.c @@ -22,24 +22,13 @@ * SOFTWARE. */ -#include "message/test.h" +#include "call/test.h" /* * O.K. */ -Test(message, test_message_delete_wrapper0) +Test(call, test_call_delete0) { - struct yall_subsystem_params p = { - .log_level = yall_debug, - .output_type = yall_console_output, - .file = { - .filename = NULL - } - }; - - struct message *m = message_new(strdup("test"), yall_debug, &p); - - message_delete_wrapper((void *)m); - - cr_assert(1); + struct yall_call_data *d = call_new(); + call_delete(d); } diff --git a/tests/unit/call/test_convert_data_to_message.c b/tests/unit/call/test_call_get_buffer_length.c similarity index 50% rename from tests/unit/call/test_convert_data_to_message.c rename to tests/unit/call/test_call_get_buffer_length.c index baa0b8b..b1acf69 100644 --- a/tests/unit/call/test_convert_data_to_message.c +++ b/tests/unit/call/test_call_get_buffer_length.c @@ -25,26 +25,68 @@ #include "call/test.h" /* - * O.K. - * 0 sized buffer + * Empty call data */ -Test(call, test_convert_data_to_message0) +Test(call, test_call_get_buffer_length0) { - struct yall_call_data d = { 0 }; - char buffer[16] = { 0 }; + struct yall_call_data *d = call_new(); - convert_data_to_message(buffer, 0, &d); - cr_assert_eq(buffer[0], '\0'); + cr_assert_eq(call_get_buffer_length(d), 2); + + call_delete(d); +} + +/* + * Call data with header + */ +Test(call, test_call_get_buffer_length1) +{ + struct yall_call_data *d = call_new(); + + yall_call_set_header(d, "testing"); + + cr_assert_eq(call_get_buffer_length(d), 2 + strlen("testing")); + + call_delete(d); +} + +/* + * Call data with lines + */ +Test(call, test_call_get_buffer_length2) +{ + struct yall_call_data *d = call_new(); + + yall_config_set_tab_width(2); + + yall_call_add_line(d, 1, "Hello ! %s", "world ?"); + + cr_assert_eq(call_get_buffer_length(d), 22); + + call_delete(d); } /* - * O.K. - * There is no need to call test_clean_2_call_data_lines as it is freed by the tested function + * Call data with header and lines */ -Test(call, test_convert_data_to_message1, .init=test_2_call_data_lines) +Test(call, test_call_get_buffer_length3) { - char buffer[16] = { 0 }; + struct yall_call_data *d = call_new(); + + yall_config_set_tab_width(2); + + yall_call_set_header(d, "testing"); // 7 characters + yall_call_add_line(d, 1, "Hello !"); // 9 characters + yall_call_add_line(d, 0, "%d", 32); // 2 characters + + /* + * 18 characters + * + 3 ('\n' for each line) + * + 1 ('\0') + * + 4 (default 1 indent added to each line) + */ + + cr_assert_eq(call_get_buffer_length(d), 26); - convert_data_to_message(buffer, 16, &test_call_data); - cr_assert_str_eq(buffer, "yalldatatest"); + call_delete(d); } diff --git a/tests/unit/call/test_init_call_data.c b/tests/unit/call/test_call_new.c similarity index 82% rename from tests/unit/call/test_init_call_data.c rename to tests/unit/call/test_call_new.c index 8f2cadf..254e93d 100644 --- a/tests/unit/call/test_init_call_data.c +++ b/tests/unit/call/test_call_new.c @@ -27,14 +27,13 @@ /* * O.K. */ -Test(call, test_init_call_data0) +Test(call, test_call_new0) { - struct yall_call_data d = { 0 }; - init_call_data(&d); + struct yall_call_data *d = call_new(); - cr_assert_eq(d.message_size, 1); - cr_assert(d.header != NULL); - cr_assert_eq(d.header[0], '\n'); - cr_assert_eq(d.header[1], '\0'); - cr_assert_eq(d.lines, NULL); + cr_assert_eq(d->message_size, 0); + cr_assert_eq(d->header, NULL); + cr_assert(d->lines); + + call_delete(d); } diff --git a/tests/unit/call/test_remove_first_line.c b/tests/unit/call/test_remove_first_line.c deleted file mode 100644 index 10beb26..0000000 --- a/tests/unit/call/test_remove_first_line.c +++ /dev/null @@ -1,46 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2017 Quentin "Naccyde" Deslandes. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include "call/test.h" - -/* - * O.K. - * Empty line set - */ -Test(call, test_remove_first_line0) -{ - struct yall_call_data d = { 0 }; - - cr_assert_eq(NULL, remove_first_line(&d)); -} - -/* - * O.K. - * Non-empty line set - */ -Test(call, test_remove_first_line1, .init=test_2_call_data_lines, .fini=test_clean_2_call_data_lines) -{ - cr_assert_eq(test_call_data_lines_array[0], remove_first_line(&test_call_data)); - cr_assert_eq(test_call_data_lines_array[1], remove_first_line(&test_call_data)); -} diff --git a/tests/unit/call/test_yall_call_add_line.c b/tests/unit/call/test_yall_call_add_line.c index e6ba05c..4cfcb7b 100644 --- a/tests/unit/call/test_yall_call_add_line.c +++ b/tests/unit/call/test_yall_call_add_line.c @@ -29,20 +29,20 @@ */ Test(call, test_yall_call_add_line0) { - struct yall_call_data d = { 0, NULL, NULL }; + yall_call_data *d = call_new(); yall_config_set_tab_width(2); - yall_call_add_line(&d, 0, "line"); - cr_assert_str_eq(d.lines->content, " line\n"); - cr_assert_eq(d.message_size, strlen(" line\n")); + yall_call_add_line(d, 0, "line"); + cr_assert_str_eq(ll_get_at(d->lines, 0), " line"); + cr_assert_eq(d->message_size, strlen(" line")); yall_config_set_tab_width(1); - yall_call_add_line(&d, 3, "new_line"); - cr_assert_str_eq(d.lines->next->content, " new_line\n"); - cr_assert_eq(d.message_size, strlen(" line\n") + strlen(" new_line\n")); + yall_call_add_line(d, 3, "new_line"); + cr_assert_str_eq(ll_get_at(d->lines, 1), " new_line"); + cr_assert_eq(d->message_size, strlen(" line") + strlen(" new_line")); yall_config_set_tab_width(0); - yall_call_add_line(&d, 1, "test %d %c", 3, 't'); - cr_assert_str_eq(d.lines->next->next->content, "test 3 t\n"); - cr_assert_eq(d.message_size, strlen(" line\n") + strlen(" new_line\n") + strlen("test 3 t\n")); + yall_call_add_line(d, 1, "test %d %c", 3, 't'); + cr_assert_str_eq(ll_get_at(d->lines, 2), "test 3 t"); + cr_assert_eq(d->message_size, strlen(" line") + strlen(" new_line") + strlen("test 3 t")); } diff --git a/tests/unit/call/test_yall_call_set_header.c b/tests/unit/call/test_yall_call_set_header.c index c6b9bca..f3990dc 100644 --- a/tests/unit/call/test_yall_call_set_header.c +++ b/tests/unit/call/test_yall_call_set_header.c @@ -29,18 +29,17 @@ */ Test(call, test_yall_call_set_header0) { - struct yall_call_data d = { 0 }; - init_call_data(&d); + struct yall_call_data *d = call_new(); - yall_call_set_header(&d, "header"); - cr_assert_str_eq(d.header, "header\n"); - cr_assert_eq(d.message_size, 7); + yall_call_set_header(d, "header"); + cr_assert_str_eq(d->header, "header"); + cr_assert_eq(d->message_size, 6); - yall_call_set_header(&d, "header %c %d", 't', 3); - cr_assert_str_eq(d.header, "header t 3\n"); - cr_assert_eq(d.message_size, 11); + yall_call_set_header(d, "header %c %d", 't', 3); + cr_assert_str_eq(d->header, "header t 3"); + cr_assert_eq(d->message_size, 10); - free(d.header); + call_delete(d); } /* @@ -48,12 +47,11 @@ Test(call, test_yall_call_set_header0) */ Test(call, test_yall_call_set_header1) { - struct yall_call_data d = { 0 }; - init_call_data(&d); + struct yall_call_data *d = call_new(); - yall_call_set_header(&d, _TEST_1088_LONG_CALL_HEADER); - cr_assert_str_eq(d.header, _TEST_1023_LONG_CALL_HEADER); - cr_assert_eq(d.message_size, 1023); + yall_call_set_header(d, _TEST_1088_LONG_CALL_HEADER); + cr_assert_str_eq(d->header, _TEST_1088_LONG_CALL_HEADER); + cr_assert_eq(d->message_size, 1088); - free(d.header); + call_delete(d); } diff --git a/tests/unit/container/llist/test_ll_insert_node_at.c b/tests/unit/container/llist/test_ll_insert_node_at.c index 5d125e1..8b1a8eb 100644 --- a/tests/unit/container/llist/test_ll_insert_node_at.c +++ b/tests/unit/container/llist/test_ll_insert_node_at.c @@ -172,3 +172,43 @@ Test(container_llist, test_ll_insert_node_at7) test_llist_free_data(l); free(n); } + +/* + * Add last 2 times in a row (was buggy) + */ +Test(container_llist, test_ll_insert_node_at8) +{ + llist_t *l = ll_new(); + + llist_node_t *n0 = ll_node_new(NULL); + llist_node_t *n1 = ll_node_new(NULL); + + ll_insert_node_at(l, -1, n0); + ll_insert_node_at(l, -1, n1); + + cr_assert_eq(l->head, n0); + cr_assert_eq(l->tail, n1); + cr_assert_eq(l->head->next, n1); + + ll_delete(l, NULL); +} + +/* + * Add first 2 times in a row (was buggy) + */ +Test(container_llist, test_ll_insert_node_at9) +{ + llist_t *l = ll_new(); + + llist_node_t *n0 = ll_node_new(NULL); + llist_node_t *n1 = ll_node_new(NULL); + + ll_insert_node_at(l, 0, n0); + ll_insert_node_at(l, 0, n1); + + cr_assert_eq(l->head, n1); + cr_assert_eq(l->tail, n0); + cr_assert_eq(l->head->next, n0); + + ll_delete(l, NULL); +} diff --git a/tests/unit/message/test_generate_call_msg.c b/tests/unit/message/test_generate_call_msg.c index 066fcb3..0f9ea57 100644 --- a/tests/unit/message/test_generate_call_msg.c +++ b/tests/unit/message/test_generate_call_msg.c @@ -31,24 +31,14 @@ Test(message, test_generate_call_message0) { char buffer[64] = { 0 }; - struct yall_call_data d = { 3, NULL, NULL }; - d.header = strdup(""); + yall_call_data *d = call_new(); - struct yall_call_data_line *l = malloc(sizeof(struct yall_call_data_line)); - l->content = strdup("2"); - l->next = NULL; + yall_call_set_header(d, ""); + yall_call_add_line(d, 0, "2"); + yall_call_add_line(d, 0, "1"); + yall_call_add_line(d, 0, "0"); - struct yall_call_data_line *m = malloc(sizeof(struct yall_call_data_line)); - m->content = strdup("1"); - m->next = l; - - struct yall_call_data_line *n = malloc(sizeof(struct yall_call_data_line)); - n->content = strdup("0"); - n->next = m; - - d.lines = n; - - generate_call_msg(buffer, 0, &d); + generate_call_msg(buffer, 0, d); cr_assert_str_eq(buffer, ""); } @@ -60,26 +50,16 @@ Test(message, test_generate_call_message0) Test(message, test_generate_call_message1) { char buffer[2] = { 0 }; - struct yall_call_data d = { 3, NULL, NULL }; - d.header = strdup(""); - - struct yall_call_data_line *l = malloc(sizeof(struct yall_call_data_line)); - l->content = strdup("2"); - l->next = NULL; + yall_call_data *d = call_new(); - struct yall_call_data_line *m = malloc(sizeof(struct yall_call_data_line)); - m->content = strdup("1"); - m->next = l; + yall_call_set_header(d, ""); + yall_call_add_line(d, 0, "2"); + yall_call_add_line(d, 0, "1"); + yall_call_add_line(d, 0, "0"); - struct yall_call_data_line *n = malloc(sizeof(struct yall_call_data_line)); - n->content = strdup("0"); - n->next = m; + generate_call_msg(buffer, 2, d); - d.lines = n; - - generate_call_msg(buffer, 2, &d); - - cr_assert_str_eq(buffer, "0"); + cr_assert_str_eq(buffer, "\n"); } /* @@ -89,26 +69,16 @@ Test(message, test_generate_call_message1) Test(message, test_generate_call_message2) { char buffer[64] = { 0 }; - struct yall_call_data d = { 3, NULL, NULL }; - d.header = strdup(""); - - struct yall_call_data_line *l = malloc(sizeof(struct yall_call_data_line)); - l->content = strdup("2"); - l->next = NULL; + yall_call_data *d = call_new(); - struct yall_call_data_line *m = malloc(sizeof(struct yall_call_data_line)); - m->content = strdup("1"); - m->next = l; + yall_call_set_header(d, ""); + yall_call_add_line(d, 0, "2"); + yall_call_add_line(d, 0, "1"); + yall_call_add_line(d, 0, "0"); - struct yall_call_data_line *n = malloc(sizeof(struct yall_call_data_line)); - n->content = strdup("0"); - n->next = m; + generate_call_msg(buffer, 32, d); - d.lines = n; - - generate_call_msg(buffer, 32, &d); - - cr_assert_str_eq(buffer, "012"); + cr_assert_str_eq(buffer, "\n2\n1\n0\n"); } /* @@ -118,24 +88,27 @@ Test(message, test_generate_call_message2) Test(message, test_generate_call_message3) { char buffer[64] = { 0 }; - struct yall_call_data d = { 3, NULL, NULL }; - d.header = strdup("foo"); + yall_call_data *d = call_new(); - struct yall_call_data_line *l = malloc(sizeof(struct yall_call_data_line)); - l->content = strdup("bar"); - l->next = NULL; + yall_call_set_header(d, "foo"); + yall_call_add_line(d, 0, "bar"); + yall_call_add_line(d, 0, "foobar"); + yall_call_add_line(d, 0, "foofoo"); - struct yall_call_data_line *m = malloc(sizeof(struct yall_call_data_line)); - m->content = strdup("foobar"); - m->next = l; + generate_call_msg(buffer, 32, d); - struct yall_call_data_line *n = malloc(sizeof(struct yall_call_data_line)); - n->content = strdup("foofoo"); - n->next = m; + cr_assert_str_eq(buffer, "foo\nbar\nfoobar\nfoofoo\n"); +} - d.lines = n; +/* + * Do not print '(null)' if header is not set + */ +Test(message, test_generate_call_message4) +{ + char buffer[64] = { 0 }; + yall_call_data *d = call_new(); - generate_call_msg(buffer, 32, &d); + generate_call_msg(buffer, 64, d); - cr_assert_str_eq(buffer, "foofoofoofoobarbar"); + cr_assert_str_eq(buffer, "\n"); } diff --git a/tests/unit/subsystem/test__get_subsystem.c b/tests/unit/subsystem/test__get_subsystem.c index fae5e94..47e2a8a 100644 --- a/tests/unit/subsystem/test__get_subsystem.c +++ b/tests/unit/subsystem/test__get_subsystem.c @@ -24,10 +24,6 @@ #include "subsystem/test.h" -/* - * TODO : Check the returned file name in case of file output type. - */ - /* * O.K. * Empty subsystems list @@ -62,14 +58,17 @@ Test(subsystem, test__get_subsystem1, .init=create_subsystems, .fini=clean_subsy cr_assert_eq(_get_subsystem("0", subsystems, &p), _subsystems[0]); cr_assert_eq(p.log_level, yall_debug); cr_assert_eq(p.output_type, yall_console_output); + cr_assert_eq(p.file.filename, NULL); cr_assert_eq(_get_subsystem("01", subsystems, &p), _subsystems[5]); cr_assert_eq(p.log_level, yall_debug); cr_assert_eq(p.output_type, yall_console_output); + cr_assert_eq(p.file.filename, NULL); cr_assert_eq(_get_subsystem("201", subsystems, &p), _subsystems[9]); cr_assert_eq(p.log_level, yall_debug); cr_assert_eq(p.output_type, yall_console_output); + cr_assert_str_eq(p.file.filename, "output_file.log"); } /* diff --git a/tests/unit/subsystem/test_reset_subsystem.c b/tests/unit/subsystem/test_reset_subsystem.c index 14fcace..8d2ff2b 100644 --- a/tests/unit/subsystem/test_reset_subsystem.c +++ b/tests/unit/subsystem/test_reset_subsystem.c @@ -33,7 +33,7 @@ Test(subsystem, test_reset_subsystem0) reset_subsystem(s); - cr_assert_str_eq(s->name, ""); + //cr_assert_str_eq(s->name, ""); cr_assert_eq(s->log_level, yall_inherited_level); cr_assert_eq(s->status, yall_inherited_status); cr_assert_eq(s->output_type, yall_inherited_output); diff --git a/tests/unit/subsystem/test_show_subsystems_tree_call.c b/tests/unit/subsystem/test_show_subsystems_tree_call.c index 287e33c..2b73b36 100644 --- a/tests/unit/subsystem/test_show_subsystems_tree_call.c +++ b/tests/unit/subsystem/test_show_subsystems_tree_call.c @@ -35,18 +35,19 @@ Test(subsystem, test_show_subsystems_tree_call0, .init=test_init_yall, .fini=tes yall_config_set_call_header_template(""); yall_config_set_tab_width(0); - struct yall_call_data d = { 0 }; - init_call_data(&d); + struct yall_call_data *d = call_new(); - show_subsystems_tree_call(&d, NULL); + show_subsystems_tree_call(d, NULL); - cr_assert_str_eq(d.header, "Subsystems tree :\n"); + cr_assert_str_eq(d->header, "Subsystems tree :"); #ifdef __linux__ - // TODO: fix cr_assert_str_eq(d.lines->content, "└── debug_subsys\n"); + cr_assert_str_eq(ll_get_at(d->lines, 0), "└── debug_subsys"); #else - // TODO: fix cr_assert_str_eq(d.lines->content, "|-- debug_subsys\n"); + cr_assert_str_eq(ll_get_at(d->lines, 0), "|-- debug_subsys"); #endif + + call_delete(d); } /* @@ -62,20 +63,21 @@ Test(subsystem, test_show_subsystems_tree_call1, .init=test_init_yall, .fini=tes yall_config_set_call_header_template(""); yall_config_set_tab_width(0); - struct yall_call_data d = { 0 }; - init_call_data(&d); + struct yall_call_data *d = call_new(); - show_subsystems_tree_call(&d, NULL); + show_subsystems_tree_call(d, NULL); - cr_assert_str_eq(d.header, "Subsystems tree :\n"); + cr_assert_str_eq(d->header, "Subsystems tree :"); #ifdef __linux__ - // TODO: fix cr_assert_str_eq(d.lines->content, "├── 0\n"); - // TODO: fix cr_assert_str_eq(d.lines->next->next->next->content, "│ └── 02\n"); + cr_assert_str_eq(ll_get_at(d->lines, 0), "├── 0"); + cr_assert_str_eq(ll_get_at(d->lines, 3), "│ └── 02"); #else - // TODO: fix cr_assert_str_eq(d.lines->content, "|-- 0\n"); - // TODO: fix cr_assert_str_eq(d.lines->next->next->next->content, "| |-- 02\n"); + cr_assert_str_eq(ll_get_at(d->lines, 0), "|-- 0"); + cr_assert_str_eq(ll_get_at(d->lines, 3), "| |-- 02"); #endif + + call_delete(d); } /* @@ -87,11 +89,12 @@ Test(subsystem, test_show_subsystems_tree_call2, .init=test_init_yall, .fini=tes yall_config_set_call_header_template(""); yall_config_set_tab_width(0); - struct yall_call_data d = { 0 }; - init_call_data(&d); + struct yall_call_data *d = call_new(); + + show_subsystems_tree_call(d, NULL); - show_subsystems_tree_call(&d, NULL); + cr_assert_str_eq(d->header, "Subsystems tree :"); + cr_assert_eq(ll_get_at(d->lines, 0), NULL); - cr_assert_str_eq(d.header, "Subsystems tree :\n"); - cr_assert_eq(d.lines, NULL); + call_delete(d); } diff --git a/tests/unit/subsystem/test_update_subsystem.c b/tests/unit/subsystem/test_update_subsystem.c index 04f03f3..ecd8238 100644 --- a/tests/unit/subsystem/test_update_subsystem.c +++ b/tests/unit/subsystem/test_update_subsystem.c @@ -32,6 +32,7 @@ Test(subsystem, test_update_subsystem0) struct yall_subsystem *s = get_fake_subsystem("0", NULL); update_subsystem(s, yall_crit, yall_console_output, NULL); + cr_assert_str_eq(s->name, "0"); cr_assert_eq(s->log_level, yall_crit); cr_assert_eq(s->output_type, yall_console_output); cr_assert_eq(s->file.filename, NULL); @@ -45,6 +46,7 @@ Test(subsystem, test_update_subsystem0) s = get_fake_subsystem("1", NULL); update_subsystem(s, yall_emerg, yall_file_output, "1.log"); + cr_assert_str_eq(s->name, "1"); cr_assert_eq(s->log_level, yall_emerg); cr_assert_eq(s->output_type, yall_file_output); cr_assert_eq(strcmp(s->file.filename, "1.log"), 0); diff --git a/tests/unit/subsystem/test_yall_show_subsystems_tree.c b/tests/unit/subsystem/test_yall_show_subsystems_tree.c index 4fb84c5..49c2e9d 100644 --- a/tests/unit/subsystem/test_yall_show_subsystems_tree.c +++ b/tests/unit/subsystem/test_yall_show_subsystems_tree.c @@ -28,23 +28,18 @@ * O.K. * No debug mode */ -Test(subsystem, test_show_subsystems_tree0, .init=test_init_yall, .fini=test_close_yall) +Test(subsystem, test_yall_show_subsystems_tree0, .init=test_init_yall, .fini=test_close_yall) { yall_disable_debug(); yall_show_subsystems_tree(); cr_assert(1); - - /* - * TODO : it would be better, once show_subsystems_tree called to check - * stderr content. - */ } /* * Debug mode */ -Test(subsystem, test_show_subsystems_tree1, .init=test_init_yall, .fini=test_close_yall) +Test(subsystem, test_yall_show_subsystems_tree1, .init=test_init_yall, .fini=test_close_yall) { yall_set_subsystem("test", NULL, yall_debug, yall_console_output, NULL); yall_enable_debug("test"); @@ -53,9 +48,4 @@ Test(subsystem, test_show_subsystems_tree1, .init=test_init_yall, .fini=test_clo cr_assert(1); yall_disable_debug(); - - /* - * TODO : it would be better, once show_subsystems_tree called to check - * stderr content. - */ } diff --git a/tests/unit/test.c b/tests/unit/test.c index 1259114..86b8281 100644 --- a/tests/unit/test.c +++ b/tests/unit/test.c @@ -285,7 +285,7 @@ void create_subsystems(void) _subsystems[9]->log_level = yall_debug; _subsystems[9]->output_type = yall_inherited_output; - _subsystems[9]->file.filename = NULL; + _subsystems[9]->file.filename = strdup("output_file.log"); _subsystems[9]->status = yall_subsys_enable; _subsystems[9]->delete_old_log_file = true; _subsystems[9]->previous = _subsystems[8]; @@ -425,8 +425,10 @@ void create_subsystems(void) void clean_subsystems(void) { for (int i = 0; i < _NB_TEST_SUBSYSTEMS; ++i) { - if (_subsystems[i]) + if (_subsystems[i]) { + free((void *)_subsystems[i]->file.filename); free(_subsystems[i]); + } _subsystems[i] = NULL; } diff --git a/tests/unit/writer/console/test_reset_color.c b/tests/unit/writer/console/test_reset_color.c index 01e9567..e13a7c3 100644 --- a/tests/unit/writer/console/test_reset_color.c +++ b/tests/unit/writer/console/test_reset_color.c @@ -38,6 +38,8 @@ Test(writer_console, test_reset_color0) #endif /* - * TODO : a second call to reset_call() crash. + * Calling reset_color() a second time here generate a crash in unit + * tests. This could come from unit test framework itself as the + * function only prints to stderr. */ } diff --git a/tests/unit/writer/writer/test.c b/tests/unit/writer/writer/test.c index 5eecf8c..9041028 100644 --- a/tests/unit/writer/writer/test.c +++ b/tests/unit/writer/writer/test.c @@ -45,3 +45,8 @@ void test_stop_writer(void) { writer_close(); } + +void test_message_delete_wrapper(void *data) +{ + message_delete((struct message *)data); +} diff --git a/tests/unit/writer/writer/test.h b/tests/unit/writer/writer/test.h index f0e7b2c..75b84e1 100644 --- a/tests/unit/writer/writer/test.h +++ b/tests/unit/writer/writer/test.h @@ -39,5 +39,6 @@ void test_init_writer(void); void test_stop_writer(void); +void test_message_delete_wrapper(void *data); #endif diff --git a/tests/unit/writer/writer/test_write_msg.c b/tests/unit/writer/writer/test_write_msg.c index e236e01..9842e5d 100644 --- a/tests/unit/writer/writer/test_write_msg.c +++ b/tests/unit/writer/writer/test_write_msg.c @@ -43,5 +43,5 @@ Test(writer_writer, test_write_msg0) message_delete(n); - cq_delete(msg_queue, &message_delete_wrapper); + cq_delete(msg_queue, &test_message_delete_wrapper); }