aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichał Łyszczek <michal.lyszczek@bofc.pl>2018-10-08 19:40:05 +0200
committerMichał Łyszczek <michal.lyszczek@bofc.pl>2018-10-08 19:40:05 +0200
commitf1ba3506906cda3d88244fcb340041093c9a3156 (patch)
treea82ec881c9bcef8da142b4864e6e723ee0a21b35
parent4f1e81a392e82cc0971bb4ffc4adc125a3c61fa6 (diff)
downloadmtest-f1ba3506906cda3d88244fcb340041093c9a3156.tar.gz
mtest-f1ba3506906cda3d88244fcb340041093c9a3156.tar.bz2
mtest-f1ba3506906cda3d88244fcb340041093c9a3156.zip
add: examples for tests with parameters
-rw-r--r--example/Makefile2
-rw-r--r--example/named-tests.c2
-rw-r--r--example/param-tests.c78
-rw-r--r--example/param-tests.h12
-rw-r--r--example/tests.c7
5 files changed, 99 insertions, 2 deletions
diff --git a/example/Makefile b/example/Makefile
index e166af7..0b665b2 100644
--- a/example/Makefile
+++ b/example/Makefile
@@ -1,4 +1,4 @@
-SRCS = add-tests.c calc.c sub-tests.c tests.c named-tests.c
+SRCS = add-tests.c calc.c sub-tests.c tests.c named-tests.c param-tests.c
OBJS = $(SRCS:.c=.o)
all: example
diff --git a/example/named-tests.c b/example/named-tests.c
index 191c154..8763908 100644
--- a/example/named-tests.c
+++ b/example/named-tests.c
@@ -43,7 +43,7 @@ void named_test_in_loop(void)
* so we have to construct name by ourselfs
*/
- sprintf(test_name, "test add(%d, %d)", a, b);
+ sprintf(test_name, "named test add(%d, %d)", a, b);
mt_run_named(add_test, test_name);
}
}
diff --git a/example/param-tests.c b/example/param-tests.c
new file mode 100644
index 0000000..99cc962
--- /dev/null
+++ b/example/param-tests.c
@@ -0,0 +1,78 @@
+/* ==========================================================================
+ Licensed under BSD 2clause license. See LICENSE file for more information
+ Author: Michał Łyszczek <michal.lyszczek@bofc.pl>
+ ========================================================================== */
+
+
+#include "../mtest.h"
+#include "sub-tests.h"
+#include "calc.h"
+
+
+mt_defs_ext();
+
+struct test_param
+{
+ int a;
+ int b;
+};
+
+/* ==========================================================================
+ test to run, mt_run_named takes functions without parameters, so we have
+ to use global variables as parameters
+ ========================================================================== */
+
+
+static void add_test(const struct test_param *p)
+{
+ mt_fail(add(p->a, p->b) == p->a + p->b);
+}
+
+
+/* ==========================================================================
+ Run multiple tests every time with different argument
+ ========================================================================== */
+
+
+void param_test_in_loop(void)
+{
+ struct test_param p;
+
+ for (p.a = 0; p.a != 3; ++p.a)
+ {
+ for (p.b = 0; p.b != 3; ++p.b)
+ {
+ mt_run_param(add_test, &p);
+ }
+ }
+}
+
+/* ==========================================================================
+ Run multiple tests every time with different argument, also print
+ arguments of the run test.
+
+ This is even better than alone param as it allows to see parameters that
+ caused test to fail
+ ========================================================================== */
+
+
+void named_param_test_in_loop(void)
+{
+ struct test_param p;
+
+ for (p.a = 0; p.a != 3; ++p.a)
+ {
+ for (p.b = 0; p.b != 3; ++p.b)
+ {
+ char test_name[32];
+
+ /*
+ * mt_run_named takes "const char *" as parameter,
+ * so we have to construct name by ourselfs
+ */
+
+ sprintf(test_name, "named param test add(%d, %d)", p.a, p.b);
+ mt_run_named_param(add_test, &p, test_name);
+ }
+ }
+}
diff --git a/example/param-tests.h b/example/param-tests.h
new file mode 100644
index 0000000..65f9e67
--- /dev/null
+++ b/example/param-tests.h
@@ -0,0 +1,12 @@
+/* ==========================================================================
+ Licensed under BSD 2clause license. See LICENSE file for more information
+ Author: Michał Łyszczek <michal.lyszczek@bofc.pl>
+ ========================================================================== */
+
+#ifndef PARAM_TESTS_H
+#define PARAM_TESTS_H 1
+
+void param_test_in_loop(void);
+void named_param_test_in_loop(void);
+
+#endif /* PARAM_TESTS_H */
diff --git a/example/tests.c b/example/tests.c
index c6bbdec..80567d6 100644
--- a/example/tests.c
+++ b/example/tests.c
@@ -47,6 +47,13 @@ int main(void)
named_test_in_loop();
/*
+ * run tests with parameters and named parameters
+ */
+
+ param_test_in_loop();
+ named_param_test_in_loop();
+
+ /*
* and at the end we call mt_return, which will print clousure information
* and will exit test program with apropriate return code
*/