Migrated from check to cmocka

Intention is to get tests running on appveyor
This commit is contained in:
Linus Probert 2018-05-11 19:09:34 +02:00
parent b036e11b63
commit 5b44ee7cf3
8 changed files with 174 additions and 228 deletions

View File

@ -28,7 +28,7 @@ before_install:
install:
- sudo apt-get install -y libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev
- sudo apt-get install -y cmake cmake-data check lua5.2 liblua5.2-dev
- sudo apt-get install -y cmake cmake-data lua5.2 liblua5.2-dev libcmocka-dev
script:
- mkdir build
@ -36,7 +36,7 @@ script:
- cmake --version
- cmake ..
- make
- make test
- ctest -V
addons:
coverity_scan:

View File

@ -17,7 +17,7 @@ include(cmake/FindSDL2_image.cmake)
include(cmake/FindSDL2_ttf.cmake)
include(cmake/FindSDL2_mixer.cmake)
include(cmake/FindCCache.cmake)
include(cmake/FindCheck.cmake)
include(cmake/FindCMocka.cmake)
include(cmake/Findcppcheck.cmake)
configure_file(
@ -77,13 +77,13 @@ include_directories(
${SDL2_MIXER_INCLUDE_DIR}
)
if (CHECK_FOUND)
if (CMOCKA_FOUND)
include_directories(
${CHECK_INCLUDE_DIR}
${CMOCKA_INCLUDE_DIR}
)
endif(CHECK_FOUND)
endif(CMOCKA_FOUND)
if (NOT WIN32)
if (NOT MSVC)
add_definitions(-std=gnu11
-pedantic -Wall -Wextra -Wshadow
-Wpointer-arith -Wcast-qual
@ -91,7 +91,7 @@ if (NOT WIN32)
-Wmissing-prototypes
-Wconversion -Wno-sign-conversion
)
endif (NOT WIN32)
endif (NOT MSVC)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DDEBUG")
if (NOT MSVC)
@ -176,21 +176,21 @@ if (MSVC)
endif (MSVC)
# TESTS:
IF (CHECK_FOUND AND NOT WIN32)
IF (CMOCKA_FOUND)
find_package(Threads REQUIRED)
enable_testing()
add_executable(test_util test/check_util src/util)
target_link_libraries(test_util ${CHECK_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
add_executable(test_util test/test_util src/util)
target_link_libraries(test_util ${CMOCKA_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
add_test(test_util test_util)
add_executable(test_linkedlist test/check_linkedlist src/linkedlist src/util)
target_link_libraries(test_linkedlist ${CHECK_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
add_executable(test_linkedlist test/test_linkedlist src/linkedlist src/util)
target_link_libraries(test_linkedlist ${CMOCKA_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
add_test(test_linkedlist test_linkedlist)
add_executable(test_hashtable test/check_hashtable src/hashtable src/util)
target_link_libraries(test_hashtable ${CHECK_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
add_executable(test_hashtable test/test_hashtable src/hashtable src/util)
target_link_libraries(test_hashtable ${CMOCKA_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
add_test(test_hashtable test_hashtable)
ENDIF (CHECK_FOUND AND NOT WIN32)
ENDIF (CMOCKA_FOUND )
# LINT:
if (CPPCHECK_FOUND)
@ -236,7 +236,7 @@ if (WIN32)
${CMAKE_SOURCE_DIR}/bin/libpng16-16.dll
${CMAKE_SOURCE_DIR}/bin/libvorbis-0.dll
${CMAKE_SOURCE_DIR}/bin/libvorbisfile-3.dll
${CMAKE_SOURCE_DIR}/bin/physfs.dll
${CMAKE_SOURCE_DIR}/bin/libphysfs.dll
${CMAKE_SOURCE_DIR}/bin/SDL2.dll
${CMAKE_SOURCE_DIR}/bin/SDL2_image.dll
${CMAKE_SOURCE_DIR}/bin/SDL2_mixer.dll

49
cmake/FindCMocka.cmake Normal file
View File

@ -0,0 +1,49 @@
# - Try to find CMocka
# Once done this will define
#
# CMOCKA_ROOT_DIR - Set this variable to the root installation of CMocka
#
# Read-Only variables:
# CMOCKA_FOUND - system has CMocka
# CMOCKA_INCLUDE_DIR - the CMocka include directory
# CMOCKA_LIBRARIES - Link these to use CMocka
# CMOCKA_DEFINITIONS - Compiler switches required for using CMocka
#
#=============================================================================
# Copyright (c) 2011-2012 Andreas Schneider <asn@cryptomilk.org>
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
#
find_path(CMOCKA_INCLUDE_DIR
NAMES
cmocka.h
PATHS
${CMOCKA_ROOT_DIR}/include
)
find_library(CMOCKA_LIBRARY
NAMES
cmocka
PATHS
${CMOCKA_ROOT_DIR}/lib
)
if (CMOCKA_LIBRARY)
set(CMOCKA_LIBRARIES
${CMOCKA_LIBRARIES}
${CMOCKA_LIBRARY}
)
endif (CMOCKA_LIBRARY)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(CMocka DEFAULT_MSG CMOCKA_LIBRARIES CMOCKA_INCLUDE_DIR)
# show the CMOCKA_INCLUDE_DIR and CMOCKA_LIBRARIES variables only in the advanced view
mark_as_advanced(CMOCKA_INCLUDE_DIR CMOCKA_LIBRARIES)

View File

@ -1,57 +0,0 @@
# - Try to find the CHECK libraries
# Once done this will define
#
# CHECK_FOUND - system has check
# CHECK_INCLUDE_DIR - the check include directory
# CHECK_LIBRARIES - check library
#
# This configuration file for finding libcheck is originally from
# the opensync project. The originally was downloaded from here:
# opensync.org/browser/branches/3rd-party-cmake-modules/modules/FindCheck.cmake
#
# Copyright (c) 2007 Daniel Gollub <dgollub@suse.de>
# Copyright (c) 2007 Bjoern Ricks <b.ricks@fh-osnabrueck.de>
#
# Redistribution and use is allowed according to the terms of the New
# BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
INCLUDE( FindPkgConfig )
# Take care about check.pc settings
PKG_SEARCH_MODULE( CHECK check )
# Look for CHECK include dir and libraries
IF( NOT CHECK_FOUND )
IF ( CHECK_INSTALL_DIR )
MESSAGE ( STATUS "Using override CHECK_INSTALL_DIR to find check" )
SET ( CHECK_INCLUDE_DIR "${CHECK_INSTALL_DIR}/include" )
SET ( CHECK_INCLUDE_DIRS "${CHECK_INCLUDE_DIR}" )
FIND_LIBRARY( CHECK_LIBRARY NAMES check PATHS "${CHECK_INSTALL_DIR}/lib" )
FIND_LIBRARY( COMPAT_LIBRARY NAMES compat PATHS "${CHECK_INSTALL_DIR}/lib" )
SET ( CHECK_LIBRARIES "${CHECK_LIBRARY}" "${COMPAT_LIBRARY}" )
ELSE ( CHECK_INSTALL_DIR )
FIND_PATH( CHECK_INCLUDE_DIR check.h )
FIND_LIBRARY( CHECK_LIBRARIES NAMES check )
ENDIF ( CHECK_INSTALL_DIR )
IF ( CHECK_INCLUDE_DIR AND CHECK_LIBRARIES )
SET( CHECK_FOUND 1 )
IF ( NOT Check_FIND_QUIETLY )
MESSAGE ( STATUS "Found CHECK: ${CHECK_LIBRARIES}" )
ENDIF ( NOT Check_FIND_QUIETLY )
ELSE ( CHECK_INCLUDE_DIR AND CHECK_LIBRARIES )
IF ( Check_FIND_REQUIRED )
MESSAGE( FATAL_ERROR "Could NOT find CHECK" )
ELSE ( Check_FIND_REQUIRED )
IF ( NOT Check_FIND_QUIETLY )
MESSAGE( STATUS "Could NOT find CHECK" )
ENDIF ( NOT Check_FIND_QUIETLY )
ENDIF ( Check_FIND_REQUIRED )
ENDIF ( CHECK_INCLUDE_DIR AND CHECK_LIBRARIES )
ENDIF( NOT CHECK_FOUND )
# Hide advanced variables from CMake GUIs
MARK_AS_ADVANCED( CHECK_INCLUDE_DIR CHECK_LIBRARIES )

View File

@ -16,22 +16,29 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <check.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <setjmp.h>
#include <cmocka.h>
#include "../src/hashtable.h"
START_TEST(test_hashtable_create)
static void test_hashtable_create(void **state)
{
(void) state;
Hashtable *table = ht_create(10);
ck_assert( table != NULL && table->size == 10 );
assert_non_null( table );
assert_int_equal(table->size, 10 );
ht_destroy(table);
}
END_TEST
START_TEST(test_hashtable_set_get)
static void test_hashtable_set_get(void **state)
{
(void) state;
int i;
int* values0[10];
int* values1[10];
@ -63,7 +70,7 @@ START_TEST(test_hashtable_set_get)
}
for (i = 0; i < 10; ++i) {
ck_assert( *values0[i] == *((int*) ht_get(table, keys[i])) );
assert_int_equal( *values0[i], *((int*) ht_get(table, keys[i])) );
}
for (i = 0; i < 10; ++i) {
@ -71,12 +78,11 @@ START_TEST(test_hashtable_set_get)
}
for (i = 0; i < 10; ++i) {
ck_assert( *values1[i] == *((int*) ht_get(table, keys[i])) );
assert_int_equal( *values1[i], *((int*) ht_get(table, keys[i])) );
}
ht_destroy(table);
}
END_TEST
static bool checklist[] = {
false,
@ -96,14 +102,16 @@ static void check_number(int *num)
checklist[*num] = true;
}
START_TEST(test_hashtable_foreach)
static void test_hashtable_foreach(void **state)
{
(void) state;
Hashtable *table = ht_create(10);
for (int i = 0; i < 10; i++) {
char str[4];
int *num = malloc(sizeof(int));
ck_assert( num != NULL );
assert_non_null( num );
*num = i;
sprintf(str, "%d", *num);
ht_set(table, str, num);
@ -111,15 +119,16 @@ START_TEST(test_hashtable_foreach)
ht_foreach(table, (void (*)(void*)) check_number);
for (int i = 0; i < 10; i++) {
ck_assert(checklist[i]);
assert_non_null(checklist[i]);
}
ht_destroy(table);
}
END_TEST
START_TEST(test_hashtable_remove)
static void test_hashtable_remove(void **state)
{
(void) state;
char key1[] = "key1";
char value1[] = "value1";
char key2[] = "key2";
@ -133,45 +142,23 @@ START_TEST(test_hashtable_remove)
ht_set(table, key2, value2);
ht_set(table, key3, value3);
getVal = ht_remove(table, key2);
ck_assert(strcmp(value2, getVal) == 0);
ck_assert(ht_get(table, key2) == NULL);
assert_string_equal(value2, getVal);
assert_null(ht_get(table, key2));
ht_remove(table, key1);
ht_remove(table, key3);
ht_destroy(table);
}
END_TEST
static Suite* t_suite_create(void)
{
Suite *s;
TCase *tc_core;
s = suite_create("Hashtable");
tc_core = tcase_create("Core");
tcase_add_test(tc_core, test_hashtable_create);
tcase_add_test(tc_core, test_hashtable_set_get);
tcase_add_test(tc_core, test_hashtable_foreach);
tcase_add_test(tc_core, test_hashtable_remove);
suite_add_tcase(s, tc_core);
return s;
}
int main(void)
{
int number_failed;
Suite *s;
SRunner *sr;
s = t_suite_create();
sr = srunner_create(s);
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_hashtable_create),
cmocka_unit_test(test_hashtable_set_get),
cmocka_unit_test(test_hashtable_remove),
cmocka_unit_test(test_hashtable_foreach)
};
srunner_run_all(sr, CK_NORMAL);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return number_failed == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
cmocka_run_group_tests(tests, NULL, NULL);
}

View File

@ -16,21 +16,26 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <check.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <setjmp.h>
#include <cmocka.h>
#include "../src/linkedlist.h"
START_TEST(test_linkedlist_create)
static void test_linkedlist_create(void **state)
{
(void) state;
LinkedList *list = linkedlist_create();
ck_assert( list == NULL );
assert_null( list );
linkedlist_destroy(&list);
}
END_TEST
START_TEST(test_linkedlist_append)
static void test_linkedlist_append(void **state)
{
(void) state;
int *v1, *v2;
v1 = malloc(sizeof(int));
@ -40,18 +45,19 @@ START_TEST(test_linkedlist_append)
*v2 = 44;
LinkedList *list = linkedlist_create();
ck_assert(linkedlist_size(list) == 0);
assert_int_equal(linkedlist_size(list), 0);
linkedlist_append(&list, v1);
ck_assert(linkedlist_size(list) == 1);
assert_int_equal(linkedlist_size(list), 1);
linkedlist_append(&list, v2);
ck_assert(linkedlist_size(list) == 2);
assert_int_equal(linkedlist_size(list), 2);
linkedlist_destroy(&list);
ck_assert( list == NULL );
assert_int_equal( list, NULL );
}
END_TEST
START_TEST(test_linkedlist_poplast)
static void test_linkedlist_poplast(void **state)
{
(void) state;
int *v1, *v2, *v3;
v1 = malloc(sizeof(int));
@ -68,23 +74,23 @@ START_TEST(test_linkedlist_poplast)
LinkedList *list = linkedlist_create();
ck_assert(linkedlist_size(list) == 0);
assert_int_equal(linkedlist_size(list), 0);
linkedlist_append(&list, v3);
linkedlist_append(&list, v2);
linkedlist_append(&list, v1);
ck_assert(linkedlist_size(list) == 3);
assert_int_equal(linkedlist_size(list), 3);
pop1 = linkedlist_poplast(&list);
pop2 = linkedlist_poplast(&list);
pop3 = linkedlist_poplast(&list);
ck_assert(linkedlist_size(list) == 0);
assert_int_equal(linkedlist_size(list), 0);
ck_assert(*pop1 == *v1);
ck_assert(*pop2 == *v2);
ck_assert(*pop3 == *v3);
assert_int_equal(*pop1, *v1);
assert_int_equal(*pop2, *v2);
assert_int_equal(*pop3, *v3);
free(pop1);
free(pop2);
@ -92,12 +98,13 @@ START_TEST(test_linkedlist_poplast)
linkedlist_destroy(&list);
ck_assert(list == NULL);
assert_null(list);
}
END_TEST
START_TEST(test_linkedlist_push)
static void test_linkedlist_push(void **state)
{
(void) state;
int *v1, *v2;
v1 = malloc(sizeof(int));
@ -107,18 +114,19 @@ START_TEST(test_linkedlist_push)
*v2 = 1;
LinkedList *list = linkedlist_create();
ck_assert(linkedlist_size(list) == 0);
assert_int_equal(linkedlist_size(list), 0);
linkedlist_push(&list, v1);
ck_assert(linkedlist_size(list) == 1);
assert_int_equal(linkedlist_size(list), 1);
linkedlist_push(&list, v2);
ck_assert(linkedlist_size(list) == 2);
assert_int_equal(linkedlist_size(list), 2);
linkedlist_destroy(&list);
ck_assert( list == NULL );
assert_null( list );
}
END_TEST
START_TEST(test_linkedlist_pop)
static void test_linkedlist_pop(void **state)
{
(void) state;
int *value1, *value2, *value3;
int *pop1, *pop2, *pop3;
@ -132,23 +140,23 @@ START_TEST(test_linkedlist_pop)
LinkedList *list = linkedlist_create();
ck_assert(linkedlist_size(list) == 0);
assert_int_equal(linkedlist_size(list), 0);
linkedlist_push(&list, value3);
linkedlist_push(&list, value2);
linkedlist_push(&list, value1);
ck_assert(linkedlist_size(list) == 3);
assert_int_equal(linkedlist_size(list), 3);
pop1 = linkedlist_pop(&list);
pop2 = linkedlist_pop(&list);
pop3 = linkedlist_pop(&list);
ck_assert(linkedlist_size(list) == 0);
assert_int_equal(linkedlist_size(list), 0);
ck_assert(*pop1 == *value1);
ck_assert(*pop2 == *value2);
ck_assert(*pop3 == *value3);
assert_int_equal(*pop1, *value1);
assert_int_equal(*pop2, *value2);
assert_int_equal(*pop3, *value3);
linkedlist_destroy(&list);
@ -156,12 +164,13 @@ START_TEST(test_linkedlist_pop)
free(pop2);
free(pop3);
ck_assert(list == NULL);
assert_null(list );
}
END_TEST
START_TEST(test_linkedlist_get_index)
static void test_linkedlist_get_index(void **state)
{
(void) state;
int *value1, *value2, *get;
value1 = malloc(sizeof(int));
@ -170,28 +179,26 @@ START_TEST(test_linkedlist_get_index)
*value1 = 1;
*value2 = 2;
LinkedList *list = linkedlist_create();
ck_assert(linkedlist_size(list) == 0);
assert_int_equal(linkedlist_size(list), 0);
linkedlist_push(&list, value2);
linkedlist_push(&list, value1);
ck_assert(linkedlist_size(list) == 2);
assert_int_equal(linkedlist_size(list), 2);
get = linkedlist_get(&list, 1);
ck_assert(linkedlist_size(list) == 2);
assert_int_equal(linkedlist_size(list), 2);
ck_assert(*get == *value2);
ck_assert(get == value2);
assert_int_equal(*get, *value2);
assert_int_equal(get, value2);
linkedlist_destroy(&list);
ck_assert(list == NULL);
assert_null(list);
}
END_TEST
static void
increase(int *number)
@ -199,8 +206,10 @@ increase(int *number)
(*number)++;
}
START_TEST (test_linkedlist_each)
static void test_linkedlist_each(void **state)
{
(void) state;
LinkedList *list;
int i;
@ -216,49 +225,25 @@ START_TEST (test_linkedlist_each)
for (i = 0; i < 10; ++i) {
int *pop = linkedlist_pop(&list);
ck_assert( i+1 == *pop );
assert_int_equal( i+1, *pop );
free(pop);
pop = NULL;
}
linkedlist_destroy(&list);
}
END_TEST
static Suite*
t_suite_create(void)
{
Suite *s;
TCase *tc_core;
s = suite_create("LinkedList");
tc_core = tcase_create("Core");
tcase_add_test(tc_core, test_linkedlist_create);
tcase_add_test(tc_core, test_linkedlist_push);
tcase_add_test(tc_core, test_linkedlist_pop);
tcase_add_test(tc_core, test_linkedlist_append);
tcase_add_test(tc_core, test_linkedlist_poplast);
tcase_add_test(tc_core, test_linkedlist_get_index);
tcase_add_test(tc_core, test_linkedlist_each);
suite_add_tcase(s, tc_core);
return s;
}
int main(void)
{
int number_failed;
Suite *s;
SRunner *sr;
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_linkedlist_create),
cmocka_unit_test(test_linkedlist_push),
cmocka_unit_test(test_linkedlist_pop),
cmocka_unit_test(test_linkedlist_append),
cmocka_unit_test(test_linkedlist_poplast),
cmocka_unit_test(test_linkedlist_get_index),
cmocka_unit_test(test_linkedlist_each)
};
s = t_suite_create();
sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return number_failed == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
return cmocka_run_group_tests(tests, NULL, NULL);
}

View File

@ -16,45 +16,27 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <check.h>
#include <setjmp.h>
#include <cmocka.h>
#include "../src/util.h"
START_TEST(test_util_ec_malloc)
static void test_util_ec_malloc(void **state)
{
(void) state;
int *i = ec_malloc(sizeof(int));
ck_assert( i != NULL );
assert_non_null( i );
free(i);
}
END_TEST
static Suite* t_suite_create(void)
{
Suite *s;
TCase *tc_core;
s = suite_create("Util");
tc_core = tcase_create("Core");
tcase_add_test(tc_core, test_util_ec_malloc);
suite_add_tcase(s, tc_core);
return s;
}
int main(void)
{
int number_failed;
Suite *s;
SRunner *sr;
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_util_ec_malloc)
};
s = t_suite_create();
sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return number_failed == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
return cmocka_run_group_tests(tests, NULL, NULL);
}