From 456ad2329086e7866f1bd125eb98db1d99c12242 Mon Sep 17 00:00:00 2001 From: Juraj Michalek Date: Wed, 1 May 2013 20:35:52 +0200 Subject: [PATCH] Add Minunit example --- README.md | 10 +++++++++ minunit/Makefile | 15 ++++++++++++++ minunit/main.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++ minunit/minunit.h | 5 +++++ minunit/parsing.c | 12 +++++++++++ minunit/parsing.h | 22 ++++++++++++++++++++ 6 files changed, 117 insertions(+) create mode 100644 minunit/Makefile create mode 100644 minunit/main.c create mode 100644 minunit/minunit.h create mode 100644 minunit/parsing.c create mode 100644 minunit/parsing.h diff --git a/README.md b/README.md index 8eab3e6..2d93a06 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,16 @@ How to run: make ./helloworld +Minunit testing +--------------- + +Minunit is very minimalistic C testing framework. It contains just two lines of code. + +How to run: + cd minunit + make + ./test + node ---- diff --git a/minunit/Makefile b/minunit/Makefile new file mode 100644 index 0000000..5f5cd99 --- /dev/null +++ b/minunit/Makefile @@ -0,0 +1,15 @@ + + +OBJ = main.o parsing.o +LDADD = + +test: $(OBJ) + ${CC} ${OBJ} -o test ${LDADD} + + +.c.o: + ${CC} ${CFLAGS} -c $< + +clean: + rm -f test *.o + diff --git a/minunit/main.c b/minunit/main.c new file mode 100644 index 0000000..e3ccc75 --- /dev/null +++ b/minunit/main.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include "minunit.h" +#include "parsing.h" + +int tests_run = 0; + +static char * test_convert() { + Person* person = malloc(sizeof(Person)); + char name[] = "Jan"; + char surname[] = "Novak"; + person->name = name; + person->surname = surname; + person->age = 45; + convert(person); + mu_assert("age should be increased to 46",(46==person->age)); + mu_assert("name should be uppercase",!strcmp(person->name,"JAN")); + mu_assert("surname should be uppercase",!strcmp(person->surname,"NOVAK")); + free(person); + return 0; +} + +static char * test_name() { + Person* person = malloc(sizeof(Person)); + person->name = ""; + person->surname = "Topinka"; + mu_assert("name should be empty", !strcmp(person->name, "")); + mu_assert("surname should be Topinka", !strcmp(person->surname, "Topinka")); + free(person); + return 0; +} + +static char * all_tests() { + mu_run_test(test_convert); + mu_run_test(test_name); + return 0; +} + +int main(int argc, char **argv) { + char *result = all_tests(); + if (result != 0) { + printf("THERE ARE TEST FAILURES:\n"); + printf("%s\n", result); + } + else { + printf("ALL TESTS PASSED\n"); + } + printf("Tests run: %d\n", tests_run); + + return result != 0; + +} diff --git a/minunit/minunit.h b/minunit/minunit.h new file mode 100644 index 0000000..d607c64 --- /dev/null +++ b/minunit/minunit.h @@ -0,0 +1,5 @@ +/* file: minunit.h */ + #define mu_assert(message, test) do { if (!(test)) return message; } while (0) + #define mu_run_test(test) do { char *message = test(); tests_run++; \ + if (message) return message; } while (0) + extern int tests_run; diff --git a/minunit/parsing.c b/minunit/parsing.c new file mode 100644 index 0000000..dde74f1 --- /dev/null +++ b/minunit/parsing.c @@ -0,0 +1,12 @@ +#include "parsing.h" + +void convert(Person * person) { + unsigned int i; + for (i=0; iname); i ++) { + person->name[i] = toupper(person->name[i]); + } + for (i=0; isurname); i ++) { + person->surname[i] = toupper(person->surname[i]); + } + person->age ++; +} diff --git a/minunit/parsing.h b/minunit/parsing.h new file mode 100644 index 0000000..3e2083b --- /dev/null +++ b/minunit/parsing.h @@ -0,0 +1,22 @@ +#include +#include + +/** + * Structure to store info about person. + * name - pointer to name string + * surname - pointer to surname string + * age - age + */ +typedef struct Person { + char * name; + char * surname; + int age; +} Person; + +/** + * Converts person, e.g name and surname transforms to uppercase + * and age increases by one. + * @param person person to convert + */ +void convert(Person * person); +