Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
— |
programming:test_framework [2013/12/16 13:58] (aktuell) idefix angelegt |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | |||
+ | ====== | ||
+ | ===== Installation | ||
+ | To install it type: | ||
+ | < | ||
+ | emerge sys-devel/ | ||
+ | emerge sys-devel/ | ||
+ | emerge dev-libs/ | ||
+ | |||
+ | or | ||
+ | apt-get install autoconf automake check | ||
+ | </ | ||
+ | |||
+ | ===== Setting up with autoconf and automake | ||
+ | Create a directory for your project and create a file autogen.sh: | ||
+ | < | ||
+ | #!/bin/sh | ||
+ | if [ -n " | ||
+ | aclocal -I $CHECK_DIR | ||
+ | else | ||
+ | aclocal | ||
+ | fi | ||
+ | autoconf | ||
+ | autoheader | ||
+ | automake --add-missing | ||
+ | |||
+ | ./configure " | ||
+ | |||
+ | echo | ||
+ | echo "Now type 'make check' to compile." | ||
+ | </ | ||
+ | |||
+ | Create the file Makefile.am: | ||
+ | < | ||
+ | INCLUDES=@CHECK_CFLAGS@ | ||
+ | |||
+ | if HAVE_CHECK | ||
+ | TESTS=check_money | ||
+ | else | ||
+ | TESTS= | ||
+ | endif | ||
+ | |||
+ | noinst_PROGRAMS=$(TESTS) | ||
+ | |||
+ | lib_LIBRARIES= libmoney.a | ||
+ | |||
+ | libmoney_a_SOURCES= \ | ||
+ | money.h\ | ||
+ | money.c | ||
+ | |||
+ | check_money_SOURCES= \ | ||
+ | money.h\ | ||
+ | money.c\ | ||
+ | check_money.c | ||
+ | |||
+ | check_money_LDADD= @CHECK_LIBS@ | ||
+ | </ | ||
+ | |||
+ | Now create the file configure.in: | ||
+ | < | ||
+ | dnl Process this file with autoconf to produce a configure script. | ||
+ | AC_INIT(money.h) | ||
+ | AM_INIT_AUTOMAKE(money, | ||
+ | |||
+ | dnl Checks for programs. | ||
+ | AC_PROG_AWK | ||
+ | AC_PROG_CC | ||
+ | AC_PROG_INSTALL | ||
+ | AC_PROG_LN_S | ||
+ | |||
+ | dnl This macro is defined in check.m4 and tests if check.h and libcheck.a | ||
+ | dnl can be found. It sets CHECK_CFLAGS and CHECK_LIBS accordingly. | ||
+ | dnl AM_PATH_CHECK([MINIMUM-VERSION, | ||
+ | AM_PATH_CHECK(, | ||
+ | AC_MSG_WARN([Check not found; cannot run unit tests!]) | ||
+ | [have_check=" | ||
+ | AM_CONDITIONAL(HAVE_CHECK, | ||
+ | |||
+ | dnl Checks for header files. | ||
+ | AM_CONFIG_HEADER(config.h) | ||
+ | |||
+ | dnl Checks for typedefs, structures, and compiler characteristics. | ||
+ | |||
+ | dnl Checks for library functions. | ||
+ | |||
+ | AC_OUTPUT(Makefile) | ||
+ | </ | ||
+ | |||
+ | ===== Create the programm itself | ||
+ | Create the file money.c: | ||
+ | < | ||
+ | #include < | ||
+ | </ | ||
+ | |||
+ | Create the file money.h: | ||
+ | < | ||
+ | #ifndef MONEY_H | ||
+ | #define MONEY_H | ||
+ | |||
+ | #endif | ||
+ | </ | ||
+ | |||
+ | Create the file check_money.c: | ||
+ | < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | START_TEST (test1) | ||
+ | { | ||
+ | } | ||
+ | END_TEST | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Create the testroutines | ||
+ | Edit check_money.c: | ||
+ | < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | Money *five_dollars; | ||
+ | void setup(void) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | |||
+ | void teardown(void) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | |||
+ | START_TEST (test_create) | ||
+ | { | ||
+ | | ||
+ | " | ||
+ | | ||
+ | " | ||
+ | } | ||
+ | END_TEST | ||
+ | |||
+ | START_TEST (test_neg_create) | ||
+ | { | ||
+ | Money *m=money_create(-1, | ||
+ | | ||
+ | "NULL should be returned on attempt to create with a negative amount" | ||
+ | } | ||
+ | END_TEST | ||
+ | |||
+ | START_TEST (test_zero_create) | ||
+ | { | ||
+ | Money *m=money_create(0," | ||
+ | | ||
+ | } | ||
+ | END_TEST | ||
+ | |||
+ | Suite *money_suite(void) | ||
+ | { | ||
+ | Suite *s=suite_create(" | ||
+ | TCase *tc_core=tcase_create(" | ||
+ | TCase *tc_limits=tcase_create(" | ||
+ | |||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | } | ||
+ | |||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | int nf; | ||
+ | Suite *s=money_suite(); | ||
+ | | ||
+ | |||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Create the functions | ||
+ | Edit money.h: | ||
+ | < | ||
+ | #ifndef MONEY_H | ||
+ | #define MONEY_H | ||
+ | |||
+ | typedef struct Money Money; | ||
+ | |||
+ | Money *money_create(int amount, char *currency); | ||
+ | int money_amount(Money *m); | ||
+ | char *money_currency(Money *m); | ||
+ | void money_free(Money *m); | ||
+ | |||
+ | #endif | ||
+ | </ | ||
+ | |||
+ | Edit money.c: | ||
+ | < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | struct Money | ||
+ | { | ||
+ | int amount; | ||
+ | char *currency; | ||
+ | }; | ||
+ | |||
+ | Money *money_create(int amount, char *currency) | ||
+ | { | ||
+ | | ||
+ | return NULL; | ||
+ | Money *m = malloc(sizeof(Money)); | ||
+ | if(m == NULL) | ||
+ | return NULL; | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | int money_amount(Money *m) | ||
+ | { | ||
+ | return m-> | ||
+ | } | ||
+ | char *money_currency(Money *m) | ||
+ | { | ||
+ | return m-> | ||
+ | } | ||
+ | |||
+ | void money_free(Money *m) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Performing the checks | ||
+ | To prepare the compilation type: | ||
+ | < | ||
+ | ./ | ||
+ | ./configure | ||
+ | </ | ||
+ | |||
+ | To check if everything is fine and compile it type: | ||
+ | < | ||
+ | make check | ||
+ | </ | ||