Add Minunit example

This commit is contained in:
Juraj Michalek
2013-05-01 20:35:52 +02:00
parent 133b387d3f
commit 456ad23290
6 changed files with 117 additions and 0 deletions

View File

@@ -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
----

15
minunit/Makefile Normal file
View File

@@ -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

53
minunit/main.c Normal file
View File

@@ -0,0 +1,53 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#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;
}

5
minunit/minunit.h Normal file
View File

@@ -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;

12
minunit/parsing.c Normal file
View File

@@ -0,0 +1,12 @@
#include "parsing.h"
void convert(Person * person) {
unsigned int i;
for (i=0; i<strlen(person->name); i ++) {
person->name[i] = toupper(person->name[i]);
}
for (i=0; i<strlen(person->surname); i ++) {
person->surname[i] = toupper(person->surname[i]);
}
person->age ++;
}

22
minunit/parsing.h Normal file
View File

@@ -0,0 +1,22 @@
#include <string.h>
#include<ctype.h>
/**
* 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);