aboutsummaryrefslogtreecommitdiffstats
path: root/example/param-tests.c
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 /example/param-tests.c
parent4f1e81a392e82cc0971bb4ffc4adc125a3c61fa6 (diff)
downloadmtest-f1ba3506906cda3d88244fcb340041093c9a3156.tar.gz
mtest-f1ba3506906cda3d88244fcb340041093c9a3156.tar.bz2
mtest-f1ba3506906cda3d88244fcb340041093c9a3156.zip
add: examples for tests with parameters
Diffstat (limited to 'example/param-tests.c')
-rw-r--r--example/param-tests.c78
1 files changed, 78 insertions, 0 deletions
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);
+ }
+ }
+}