libmpm

morphux C package management library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/libmpm

commit 52b6013097df7a2a5e22c74528c4e8a3a8aecf21
parent 35740ce0d430f6f772c4a48c15d22d851397b6c1
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Thu, 30 Mar 2017 19:21:20 +0200

Add(Packer): Beginning of the packer, with helpers on init & free:

Add some tests with it, and change the compilation flags to match standards

Diffstat:
MMakefile | 5+++--
Minc/libmpm.h | 1+
Ainc/packer.h | 42++++++++++++++++++++++++++++++++++++++++++
Asrc/packer.c | 42++++++++++++++++++++++++++++++++++++++++++
Mtests/Makefile | 4++--
Mtests/main.c | 4++++
Mtests/test.h | 1+
Atests/test_packer.c | 21+++++++++++++++++++++
8 files changed, 116 insertions(+), 4 deletions(-)

diff --git a/Makefile b/Makefile @@ -28,7 +28,7 @@ IFLAGS = -I inc/ \ -I lib/libconfuse/src/ # Optimisation Flags -OFLAGS = -std=c99 -g -O3 +OFLAGS = -std=gnu99 -g -O3 # Severity Flags SFLAGS = -Wall -Wextra -Werror -Wno-unused-result @@ -83,10 +83,11 @@ doc: doxygen docs/doxyfile check: all + $(MAKE) fclean all OFLAGS="-DCOMPILE_WITH_TEST -Wno-error" make -C tests check coverage: - $(MAKE) fclean all OFLAGS="-std=c99 -g -O0 -coverage -lcov" + $(MAKE) fclean all OFLAGS="-std=gnu99 -g -O0 -coverage -lcov -DCOMPILE_WITH_TEST" make -C tests coverage check gcov -o src/ $(SRCS) make -C tests fclean diff --git a/inc/libmpm.h b/inc/libmpm.h @@ -21,5 +21,6 @@ # include <flags.h> # include <database.h> # include <config.h> +# include <packer.h> #endif /* LIBMPM_H */ diff --git a/inc/packer.h b/inc/packer.h @@ -0,0 +1,42 @@ +/*********************************** LICENSE **********************************\ +* Copyright 2017 Morphux * +* * +* Licensed under the Apache License, Version 2.0 (the "License"); * +* you may not use this file except in compliance with the License. * +* You may obtain a copy of the License at * +* * +* http://www.apache.org/licenses/LICENSE-2.0 * +* * +* Unless required by applicable law or agreed to in writing, software * +* distributed under the License is distributed on an "AS IS" BASIS, * +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * +* See the License for the specific language governing permissions and * +* limitations under the License. * +\******************************************************************************/ + +#ifndef PACKER_H +# define PACKER_H + +# include <libmpm.h> + +typedef struct packer_s { + char *dir; /*!< Directory of the package */ +} packer_t; + +/*! + * \brief Allocate, fill and init a packer_t structure + * + * \param[in] dir Directory of the package + * + * \return A freshly allocated pointer on success, NULL on failure + */ +packer_t *packer_init(const char *dir); + +/*! + * \brief Free a packer_t structure + * + * \param[in,out] ptr Pointer to free + */ +void packer_free(packer_t *ptr); + +#endif /* PACKER_H */ diff --git a/src/packer.c b/src/packer.c @@ -0,0 +1,42 @@ +/*********************************** LICENSE **********************************\ +* Copyright 2017 Morphux * +* * +* Licensed under the Apache License, Version 2.0 (the "License"); * +* you may not use this file except in compliance with the License. * +* You may obtain a copy of the License at * +* * +* http://www.apache.org/licenses/LICENSE-2.0 * +* * +* Unless required by applicable law or agreed to in writing, software * +* distributed under the License is distributed on an "AS IS" BASIS, * +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * +* See the License for the specific language governing permissions and * +* limitations under the License. * +\******************************************************************************/ + +#include <packer.h> + +packer_t *packer_init(const char *dir) { + packer_t *ret; + + ret = malloc(sizeof(*ret)); + if (ret == NULL) + return NULL; + + ret->dir = strdup(dir); + if (ret->dir == NULL) + goto cleanup; + return ret; + +cleanup: + free(ret); + return NULL; +} + +void packer_free(packer_t *ptr) { + if (ptr != NULL) + { + free(ptr->dir); + free(ptr); + } +} diff --git a/tests/Makefile b/tests/Makefile @@ -10,10 +10,10 @@ IFLAGS = -I ../inc/ \ -I ../lib/libconfuse/src/ # Optimisation Flags -OFLAGS = -std=c99 -g -O3 +OFLAGS = -std=gnu99 -g -O3 # Severity Flags -SFLAGS = -Wall -Wextra -Werror -Wno-unused-result +SFLAGS = -Wall -Wextra -Wno-unused-result -DCOMPILE_WITH_TEST # Library Flags #LFLAGS = -L ../lib/libmorphux -lmorphux \ diff --git a/tests/main.c b/tests/main.c @@ -8,7 +8,11 @@ int main(void) { register_test_files(); register_test_category(); register_test_config(); + register_test_packer(); + + m_info("Beginning tests...\n"); ret = test_all(); test_free(); + return ret; } diff --git a/tests/test.h b/tests/test.h @@ -15,6 +15,7 @@ void register_test_database(void); void register_test_files(void); void register_test_category(void); void register_test_config(void); +void register_test_packer(void); void clean_db(const char *name); int free_sql_results(void *ptr); diff --git a/tests/test_packer.c b/tests/test_packer.c @@ -0,0 +1,21 @@ +#include "test.h" + +TEST(packer_init) { + packer_t *ptr = NULL; + + set_strdup_fail(1); + set_malloc_fail(1); + ptr = packer_init("Test 123"); + TEST_ASSERT(ptr != NULL, "Function failed"); + TEST_ASSERT(strcmp(ptr->dir, "Test 123") == 0, "Directory name is wrong"); + packer_free(ptr); + ptr = packer_init("Test 123"); + TEST_ASSERT(ptr == NULL, "Error not raised"); + ptr = packer_init("Test 123"); + TEST_ASSERT(ptr == NULL, "Error not raised"); + return TEST_SUCCESS; +} + +void register_test_packer(void) { + reg_test("packer", packer_init); +}