From 133b387d3f90a3dddecd3bb4c6b0e42d9204d446 Mon Sep 17 00:00:00 2001 From: Juraj Michalek Date: Wed, 1 May 2013 20:30:22 +0200 Subject: [PATCH] Add Check unit testing example --- README.md | 13 +++++++ check/Makefile | 15 ++++++++ check/main.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 check/Makefile create mode 100644 check/main.c diff --git a/README.md b/README.md index 3c40ea5..8eab3e6 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,19 @@ How to run: ./example-02 ./example-03 +Check - unit test +----------------- + +Check is unit testing framework for C: http://check.sourceforge.net/ + +This directory contains simple example. You can uncomment age++ to create bug and rerun tests. + +How to run: + + cd check + make + ./test + curl ---- diff --git a/check/Makefile b/check/Makefile new file mode 100644 index 0000000..1f8caee --- /dev/null +++ b/check/Makefile @@ -0,0 +1,15 @@ + + +OBJ = main.o +LDADD = -lcheck + +test: $(OBJ) + ${CC} ${OBJ} -o test ${LDADD} + + +.c.o: + ${CC} ${CFLAGS} -c $< + +clean: + rm -f test + diff --git a/check/main.c b/check/main.c new file mode 100644 index 0000000..3a65290 --- /dev/null +++ b/check/main.c @@ -0,0 +1,92 @@ +#include + +/* Check testing framework - http://check.sourceforge.net/ */ +#include + +/** + * Application logic for employee. + */ + +typedef struct Person { + char * name; + char * surname; + int age; +} Person; + +void increment_age(Person *person) { + person->age++; + // Uncomment following line to create bug + // person->age++; +} + +void rename_person(Person *person, char *new_name) { + person->name = new_name; +} + +/** + * Unit testing stuff - This is just example. In real test case + * put this to separate file. + */ +Person *employee; + +/** + * Test fixture - setup method - initialize employee + */ +void setup() { + employee = malloc(sizeof(Person)); + employee->name = "Arnost"; + employee->surname = "Topinka"; + employee->age = 42; +} + +/** + * Clean up after testing. Release memory. + */ +void teardown() { + free(employee); +} + +START_TEST (test_age) { + setup(); + increment_age(employee); + fail_unless(employee->age == 43, "Incorrect age."); + teardown(); +} +END_TEST + +START_TEST (test_name) { + setup(); + rename_person(employee, "Franta"); + fail_unless( !strcmp( employee->name, "Franta"), "Employee should be renamed."); + teardown(); +} +END_TEST + +/** + * Create test suite. + */ +Suite *make_suite() { + Suite *suite; + TCase *test_case; + suite = suite_create("Employee testing"); + test_case = tcase_create("Core tests"); + tcase_add_test(test_case, test_age); + tcase_add_test(test_case, test_age); + tcase_add_test(test_case, test_name); + tcase_add_test(test_case, test_name); + tcase_add_test(test_case, test_name); + tcase_add_test(test_case, test_age); + tcase_add_test(test_case, test_name); + suite_add_tcase(suite, test_case); + return suite; +} + +int main() { + int number_failed; + Suite *suite = make_suite(); + SRunner *suite_runner = srunner_create(suite); + srunner_run_all (suite_runner, CK_NORMAL); + number_failed = srunner_ntests_failed (suite_runner); + srunner_free (suite_runner); + return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +}