Added some unit testing with Check
This commit is contained in:
@@ -1,15 +1,46 @@
|
||||
cmake_minimum_required(VERSION 3.15)
|
||||
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
|
||||
project(EmberLib C)
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
|
||||
set(SOURCE src/main.c
|
||||
include/arraylist_old.h include/arraylist_new.h)
|
||||
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
|
||||
|
||||
add_executable(EmberLib ${SOURCE})
|
||||
include(CheckCSourceCompiles)
|
||||
include(CheckCSourceRuns)
|
||||
include(CheckFunctionExists)
|
||||
include(CheckIncludeFile)
|
||||
include(CheckLibraryExists)
|
||||
include(CheckSymbolExists)
|
||||
include(CheckTypeSize)
|
||||
|
||||
target_include_directories(EmberLib PRIVATE ${CMAKE_SOURCE_DIR}/include)
|
||||
# Check Headers
|
||||
set(INCLUDES "")
|
||||
macro(ck_check_include_file header var)
|
||||
check_include_file("${INCLUDES};${header}" ${var})
|
||||
if(${var})
|
||||
set(INCLUDES ${INCLUDES} ${header})
|
||||
endif(${var})
|
||||
endmacro(ck_check_include_file)
|
||||
|
||||
if(NOT CXX_COMPILER_ID:MSVC)
|
||||
target_compile_options(EmberLib PRIVATE -Werror -Wall -Wextra)
|
||||
endif(NOT CXX_COMPILER_ID:MSVC)
|
||||
ck_check_include_file("stdlib.h" HAVE_STDLIB_H)
|
||||
check_type_size(intmax_t INTMAX_T)
|
||||
check_type_size(uintmax_t UINTMMAX_T)
|
||||
|
||||
check_type_size(pid_t PID_T)
|
||||
if(NOT HAVE_PID_T)
|
||||
if(WIN32)
|
||||
set(pid_t "int")
|
||||
else(WIN32)
|
||||
MESSAGE(FATAL_ERROR "pid_t doesn't exist on this platform?")
|
||||
endif(WIN32)
|
||||
endif(NOT HAVE_PID_T)
|
||||
|
||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
|
||||
include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR})
|
||||
add_definitions(-DHAVE_CONFIG_H)
|
||||
set(CONFIG_HEADER ${CMAKE_CURRENT_BINARY_DIR/config.h})
|
||||
|
||||
add_subdirectory(EmberLib)
|
||||
add_subdirectory(tests)
|
||||
|
||||
enable_testing()
|
||||
add_test(NAME check_emberlib COMMAND check_emberlib)
|
||||
|
||||
10
EmberLib/CMakeLists.txt
Normal file
10
EmberLib/CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
set(SOURCES ${CONFIG_HEADER}
|
||||
include/arraylist_new.h
|
||||
include/arraylist_old.h
|
||||
src/arraylist_new.c)
|
||||
|
||||
add_library(EmberLib STATIC ${SOURCES})
|
||||
add_executable(demo src/main.c)
|
||||
|
||||
target_include_directories(EmberLib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||
target_link_libraries(demo EmberLib)
|
||||
7
EmberLib/src/arraylist_new.c
Normal file
7
EmberLib/src/arraylist_new.c
Normal file
@@ -0,0 +1,7 @@
|
||||
#include <arraylist_new.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void whatever(void)
|
||||
{
|
||||
printf("Hey there. You shouldn't be here.\n");
|
||||
}
|
||||
@@ -3,11 +3,12 @@
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char *s = "What";
|
||||
double x = 3.4;
|
||||
int *array = init_arraylist(int);
|
||||
arraylist_push(array, x);
|
||||
int index = -2;
|
||||
arraylist_get(array, index, -1);
|
||||
destroy_arraylist(array);
|
||||
for(int i = 0; i < 5; i++)
|
||||
{
|
||||
arraylist_push(array, s);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
57
cmake/FindCheck.cmake
Normal file
57
cmake/FindCheck.cmake
Normal file
@@ -0,0 +1,57 @@
|
||||
# - 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 )
|
||||
|
||||
23
cmake/config.h.in
Normal file
23
cmake/config.h.in
Normal file
@@ -0,0 +1,23 @@
|
||||
/*-*- mode:C; -*- */
|
||||
/* config.h. Generated from build/cmake/config.h.in by cmake configure */
|
||||
|
||||
/*
|
||||
* Ensure we have C99-style int64_t, etc, all defined
|
||||
*/
|
||||
|
||||
#cmakedefine HAVE_INTMAX_T
|
||||
#cmakedefine HAVE_UINTMAX_T
|
||||
|
||||
/* Define to 'int' if <sys/types.h> doesn't define. */
|
||||
#cmakedefine pid_t ${pid_t}
|
||||
|
||||
/* Define intmax_t and uintmax_t if they are not already defined */
|
||||
#if !defined(HAVE_INTMAX_T)
|
||||
typedef int64_t intmax_t;
|
||||
#define INTMAX_MIN INT64_MIN
|
||||
#define INTMAX_MAX INT64_MAX
|
||||
#endif
|
||||
|
||||
#if !defined(HAVE_UINTMAX_T)
|
||||
typedef uint64_t uintmax_t;
|
||||
#endif
|
||||
15
tests/CMakeLists.txt
Normal file
15
tests/CMakeLists.txt
Normal file
@@ -0,0 +1,15 @@
|
||||
# If pkg-config is not installed on the system, then CHECK_INSTALL_DIR variable must be set to the install location of Check
|
||||
# set(CHECK_INSTALL_DIR "C:/Program Files/check")
|
||||
|
||||
find_package(Check REQUIRED)
|
||||
|
||||
set(TEST_SOURCES
|
||||
check_arraylist_new.c)
|
||||
|
||||
add_executable(check_emberlib ${TEST_SOURCES})
|
||||
|
||||
target_include_directories(check_emberlib PRIVATE ${CHECK_INCLUDE_DIRS})
|
||||
target_include_directories(check_emberlib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../EmberLib/include)
|
||||
|
||||
target_link_directories(check_emberlib PRIVATE ${CHECK_LIBRARY_DIRS})
|
||||
target_link_libraries(check_emberlib PRIVATE ${CHECK_LIBRARIES})
|
||||
49
tests/check_arraylist_new.c
Normal file
49
tests/check_arraylist_new.c
Normal file
@@ -0,0 +1,49 @@
|
||||
#include <stdlib.h>
|
||||
#include <check.h>
|
||||
#include <arraylist_new.h>
|
||||
|
||||
START_TEST(test_arraylist_int_create_destroy)
|
||||
{
|
||||
size_t expected_size = sizeof(size_t)*3 + sizeof(int);
|
||||
int *array = NULL;
|
||||
array = init_arraylist(int);
|
||||
ck_assert_ptr_nonnull(array);
|
||||
ck_assert_uint_eq(arraylist_length(array), 100);
|
||||
ck_assert_uint_eq(arraylist_capacity(array), 2);
|
||||
ck_assert_uint_eq(arraylist_memory_footprint(array), 2+expected_size);
|
||||
|
||||
destroy_arraylist(array);
|
||||
ck_assert_ptr_null(array);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
Suite *arraylist_suite(void)
|
||||
{
|
||||
Suite *s;
|
||||
TCase *tc_core;
|
||||
|
||||
s = suite_create("Arraylist_New");
|
||||
|
||||
/* Core Test Case */
|
||||
tc_core = tcase_create("Core");
|
||||
|
||||
tcase_add_test(tc_core, test_arraylist_int_create_destroy);
|
||||
suite_add_tcase(s, tc_core);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int number_failed;
|
||||
Suite *s;
|
||||
SRunner *sr;
|
||||
|
||||
s = arraylist_suite();
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user