aboutsummaryrefslogtreecommitdiffstats
path: root/example/param-tests.c
blob: 99cc962d6d57aed25e05bc9ac1fd5a3cf44e48ab (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
/* ==========================================================================
    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);
        }
    }
}