Skip to content

Instantly share code, notes, and snippets.

@tnymlr
Last active June 23, 2021 11:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tnymlr/29ec50d059af52b22091 to your computer and use it in GitHub Desktop.
Save tnymlr/29ec50d059af52b22091 to your computer and use it in GitHub Desktop.
Autotools/Automake unit testing (make check) via CUnit
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([src/main.c])
AM_INIT_AUTOMAKE([subdir-objects])
AC_CONFIG_HEADERS([config.h])
# Checks for programs.
AC_PROG_CC
# Checks for libraries.
AC_CHECK_LIB([cunit], [CU_initialize_registry])
# Checks for header files.
AC_CHECK_HEADERS([limits.h stddef.h stdint.h])
# Checks for typedefs, structures, and compiler characteristics.
AC_TYPE_UINT64_T
# Checks for library functions.
AC_CONFIG_FILES([Makefile
src/Makefile
src/hello/Makefile
tests/Makefile])
AC_OUTPUT
SUBDIRS=src tests
TESTS=tests/hello
#include <stdio.h>
#include "hello.h"
void hello_say() {
printf("Hello, world!\n");
}
void hello_say();
ifndef HELLO_MAKEFILE
HELLO_MAKEFILE = 1
hello:
@${CC} -c -o hello.o src/hello/hello.c ${CFLAGS}
endif
#include "hello/hello.h"
int main(int argv, char **args) {
hello_say();
return 0;
}
AM_CFLAGS= -std=c99 -Wall
bin_PROGRAMS=app
app_SOURCES=main.c hello/hello.c
#include <sys/types.h>
#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>
#include <stdlib.h>
#include <err.h>
typedef struct {
char * name;
void (*func)(void);
} Test;
#define __PRE_POST_TEST(sig, code) \
sig {\
code\
return 0;\
}
#define INIT(code)\
__PRE_POST_TEST(int init_test(void), code)
#define CLEANUP(code)\
__PRE_POST_TEST(int cleanup_test(void), code)
#define CHECK(name) void test_ ## name(void)
#define TEST(name) {"name", test_ ## name}
#define RUN(suite, ...) \
void fireball(void) { \
CU_cleanup_registry(); \
exit(CU_get_error()); \
} \
int \
main(void) \
{ \
CU_pSuite tsuite = NULL; \
unsigned int fails; \
if (!(CUE_SUCCESS == CU_initialize_registry())) { \
errx(666, "failed to initialise test registry"); \
return EXIT_FAILURE; \
}\
tsuite = CU_add_suite(suite, init_test, cleanup_test); \
if (NULL == tsuite) \
fireball(); \
do { \
Test arr[] = {__VA_ARGS__}; \
for(int i = 0; i < sizeof(arr)/sizeof(Test); i++) { \
if(NULL == CU_add_test(tsuite, arr[i].name, arr[i].func)) \
fireball(); \
} \
}while(0); \
CU_basic_run_tests(); \
fails = CU_get_number_of_tests_failed(); \
warnx("%u tests failed", fails); \
CU_cleanup_registry(); \
return fails; \
}
#include <hello/hello.h>
#include "cunit.h"
INIT(
printf("Hello, world from init!\n");
)
CHECK(hello_say) {
CU_ASSERT_EQUAL(1, 1);
hello_say();
}
CHECK(hello_say1) {
CU_ASSERT_EQUAL(1, 1);
hello_say();
}
CLEANUP(
printf("Hello, world from cleanup\n");
)
RUN("hello", TEST(hello_say), TEST(hello_say1));
AM_CFLAGS=-I${top_srcdir}/src -std=c99 -Wall
AM_LGFLAGS=-L/usr/local/include -lcunit
check_PROGRAMS=hello
hello_SOURCES=hello.c ${top_srcdir}/src/hello/hello.c
@jUnG3
Copy link

jUnG3 commented Jun 23, 2021

Thank you for the structure. It helped a lot :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment