aboutsummaryrefslogtreecommitdiffstats
path: root/example/add-tests.c
diff options
context:
space:
mode:
authorMichał Łyszczek <michal.lyszczek@gmail.com>2017-05-24 00:28:29 +0200
committerMichał Łyszczek <michal.lyszczek@gmail.com>2017-05-24 00:28:29 +0200
commite8192b1aeaf87b5c40f5120be275cce5b69f0b3c (patch)
tree4dde4dd7084f908c5218a29e368025382743effa /example/add-tests.c
parentc9eb72a15f68cce4ea14aa11b12b79774468fb26 (diff)
downloadmtest-e8192b1aeaf87b5c40f5120be275cce5b69f0b3c.tar.gz
mtest-e8192b1aeaf87b5c40f5120be275cce5b69f0b3c.tar.bz2
mtest-e8192b1aeaf87b5c40f5120be275cce5b69f0b3c.zip
Added example of framework usagev0.1.0
Diffstat (limited to 'example/add-tests.c')
-rw-r--r--example/add-tests.c141
1 files changed, 141 insertions, 0 deletions
diff --git a/example/add-tests.c b/example/add-tests.c
new file mode 100644
index 0000000..93c361a
--- /dev/null
+++ b/example/add-tests.c
@@ -0,0 +1,141 @@
+/* ==========================================================================
+ Licensed under BSD 2clause license. See LICENSE file for more information
+ Author: Michał Łyszczek <michal.lyszczek@bofc.pl>
+ ========================================================================== */
+
+#include "../mtest.h"
+#include "add-tests.h"
+#include "calc.h"
+
+/*
+ * extern declarations of mtest variables, needed for all mt_* functions to work
+ * properly
+ */
+
+mt_defs_ext();
+
+
+/* ==========================================================================
+ Tests where everything is ok.
+ ========================================================================== */
+
+
+static void add_test_valid(void)
+{
+ mt_assert(add(5, 3) == 8);
+ mt_assert(add(1, 6) == 7);
+}
+
+
+/* ==========================================================================
+ Using assert when single one fails
+ ========================================================================== */
+
+
+static void add_test_invalid_single_assert(void)
+{
+ mt_assert(add(1, 2) == 3);
+
+ /*
+ * this assertion will fail and function will return
+ */
+
+ mt_assert(add(3, 2) == 6);
+
+ /*
+ * so this code won't be even executed
+ */
+
+ mt_assert(add(4, 2) == 6);
+}
+
+
+/* ==========================================================================
+ Test where 2 asserts fails, but only one is printed to terminal
+ ========================================================================== */
+
+
+static void add_test_invalid_multi_assert(void)
+{
+ mt_assert(add(1, 2) == 3);
+
+ /*
+ * this assertion will fail and function will return
+ */
+
+ mt_assert(add(3, 2) == 6);
+
+ /*
+ * so this code won't be even executed
+ */
+
+ mt_assert(add(4, 2) == 6);
+
+ /*
+ * and this assertion fail won't be printed to the terminal
+ */
+
+ mt_assert(add(3, 5) == 7);
+}
+
+
+/* ==========================================================================
+ Test where single fail assert fails
+ ========================================================================== */
+
+
+static void add_test_invalid_single_fail(void)
+{
+ mt_fail(add(1, 2) == 3);
+
+ /*
+ * this will fail the test, but test will continue
+ */
+
+ mt_fail(add(3, 2) == 6);
+
+ /*
+ * so this test will be performed
+ */
+
+ mt_fail(add(4, 2) == 6);
+}
+
+
+/* ==========================================================================
+ Tests where two fail asserts fails, and both are printed to terminal
+ ========================================================================== */
+
+
+static void add_test_invalid_multi_fail(void)
+{
+ mt_fail(add(1, 2) == 3);
+
+ /*
+ * this will fail the test, but test will continue
+ */
+
+ mt_fail(add(3, 2) == 6);
+ mt_fail(add(4, 2) == 6);
+
+ /*
+ * and this will fail to, printing assert information to the terminal
+ */
+
+ mt_fail(add(3, 5) == 7);
+}
+
+
+/* ==========================================================================
+ Test group that executes all tests for 'add' function
+ ========================================================================== */
+
+
+void add_tests(void)
+{
+ mt_run(add_test_valid);
+ mt_run(add_test_invalid_single_assert);
+ mt_run(add_test_invalid_multi_assert);
+ mt_run(add_test_invalid_single_fail);
+ mt_run(add_test_invalid_multi_fail);
+}