forked from Snaipe/Criterion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameterized.cc
75 lines (56 loc) · 2.1 KB
/
parameterized.cc
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
#include <criterion/parameterized.h>
// Basic usage
ParameterizedTestParameters(params, str) {
static const char *strings[] = {
"foo", "bar", "baz"
};
return cr_make_param_array(const char *, strings, sizeof (strings) / sizeof (const char *));
}
ParameterizedTest(const char **str, params, str) {
cr_assert_fail("Parameter: %s", *str);
}
// 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 criterion_test_params(params);
}
ParameterizedTest(struct parameter_tuple *tup, params, multiple) {
cr_assert_fail("Parameters: (%d, %f)", tup->i, tup->d);
}
// Using dynamically generated parameters
// you **MUST** use new_obj, new_arr, delete_obj, delete_arr instead of
// the new, new[], delete and delete[] operators (respectively) to allocate and
// deallocate dynamic memory in parameters, otherwise this will crash on
// Windows builds of the test.
// the criterion::allocator<T> allocator may be used with STL containers to
// allocate objects with the functions described above.
using criterion::new_obj;
using criterion::new_arr;
using criterion::delete_obj;
using criterion::delete_arr;
struct parameter_tuple_dyn {
int i;
std::unique_ptr<double, decltype(criterion::free)> d;
parameter_tuple_dyn() : i(0), d(nullptr, criterion::free) {}
parameter_tuple_dyn(int i, double *d) : i(i), d(d, criterion::free) {}
};
ParameterizedTestParameters(params, cleanup) {
static criterion::parameters<parameter_tuple_dyn> params;
params.push_back(parameter_tuple_dyn(1, new_obj<double>(2)));
params.push_back(parameter_tuple_dyn(3, new_obj<double>(4)));
params.push_back(parameter_tuple_dyn(5, new_obj<double>(6)));
// A criterion::parameters<T> can be returned in place of a
// criterion_test_params.
return params;
}
ParameterizedTest(parameter_tuple_dyn *tup, params, cleanup) {
cr_assert_fail("Parameters: (%d, %f)", tup->i, *tup->d);
}