aboutsummaryrefslogtreecommitdiffstats
path: root/example/add-tests.c
blob: 93c361a912045f2b14303cf94655ef4a70cdbe2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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);
}