File: parameterized.c

package info (click to toggle)
criterion 2.3.3git1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,832 kB
  • sloc: ansic: 17,852; cpp: 795; python: 72; sh: 27; makefile: 23
file content (114 lines) | stat: -rw-r--r-- 3,053 bytes parent folder | download | duplicates (2)
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
#include <criterion/parameterized.h>
#include <criterion/new/assert.h>
#include <stdio.h>

/* Basic usage */

ParameterizedTestParameters(params, simple) {
    static int vals[] = { 1, 2, 3 };

    return cr_make_param_array(int, vals, sizeof (vals) / sizeof (int));
}

ParameterizedTest(int *val, params, simple) {
    cr_fatal("Parameter: %d", *val);
}

/* Multiple parameters must be coalesced in a single parameter */

struct parameter_tuple {
    int i;
    double d;
};

ParameterizedTestParameters(params, multiple) {
    static struct parameter_tuple params[] = {
        { 1, 2 },
        { 3, 4 },
        { 5, 6 },
    };

    return cr_make_param_array(struct parameter_tuple, params, sizeof (params) / sizeof (struct parameter_tuple));
}

ParameterizedTest(struct parameter_tuple *tup, params, multiple) {
    cr_fatal("Parameters: (%d, %f)", tup->i, tup->d);
}

/* Cleaning up dynamically generated parameters */

/* you **MUST** use cr_malloc, cr_free, cr_realloc, and cr_calloc instead of their
   unprefixed counterparts to allocate dynamic memory in parameters, otherwise
   this will crash. */

struct parameter_tuple_dyn {
    int i;
    double *d;
};

void free_params(struct criterion_test_params *crp)
{
    for (size_t i = 0; i < crp->length; ++i) {
        struct parameter_tuple_dyn *tup = (struct parameter_tuple_dyn *) crp->params + i;
        cr_free(tup->d);
    }
    cr_free(crp->params);
}

double *gen_double(double val)
{
    double *ptr = cr_malloc(sizeof (double));

    *ptr = val;
    return ptr;
}

ParameterizedTestParameters(params, cleanup) {
    const size_t nb_tuples = 3;

    struct parameter_tuple_dyn *params = cr_malloc(sizeof (struct parameter_tuple_dyn) * nb_tuples);

    params[0] = (struct parameter_tuple_dyn) { 1, gen_double(2) };
    params[1] = (struct parameter_tuple_dyn) { 3, gen_double(4) };
    params[2] = (struct parameter_tuple_dyn) { 5, gen_double(6) };

    return cr_make_param_array(struct parameter_tuple_dyn, params, nb_tuples, free_params);
}

ParameterizedTest(struct parameter_tuple_dyn *tup, params, cleanup) {
    cr_fatal("Parameters: (%d, %f)", tup->i, *tup->d);
}

/* Using strings in parameterized tests */

/* you **MUST** use cr_malloc, cr_free, cr_realloc, and cr_calloc instead of their
   unprefixed counterparts to allocate dynamic memory in parameters, otherwise
   this will crash. */

void free_strings(struct criterion_test_params *crp)
{
    char **strings = (char **) crp->params;
    for (size_t i = 0; i < crp->length; ++i) {
        cr_free(strings[i]);
    }
    cr_free(strings);
}

char *cr_strdup(const char *str)
{
    char *ptr = cr_malloc(strlen(str) + 1);
    if (ptr)
        strcpy(ptr, str);
    return ptr;
}

ParameterizedTestParameters(params, string) {
    char **strings = cr_malloc(sizeof (*strings) * 2);
    strings[0] = cr_strdup("Hello");
    strings[1] = cr_strdup("World");
    return cr_make_param_array(const char *, strings, 2, free_strings);
}

ParameterizedTest(char **str, params, string) {
    cr_fatal("string: %s", *str);
}