[go: up one dir, main page]

File: test.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 (126 lines) | stat: -rw-r--r-- 4,002 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
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * The MIT License (MIT)
 *
 * Copyright © 2015-2016 Franklin "Snaipe" Mathieu <http://snai.pe/>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include <boxfort.h>
#include "criterion/internal/test.h"
#include "core/abort.h"
#include "core/stats.h"
#include "core/report.h"
#include "compat/setjmp.h"
#include "compat/time.h"
#include "protocol/protocol.h"
#include "protocol/messages.h"
#include "io/event.h"
#include "atomic.h"

extern const struct criterion_test *criterion_current_test;
extern const struct criterion_suite *criterion_current_suite;

static size_t passed_asserts;

void cri_asserts_passed_incr(void)
{
    cri_atomic_add(passed_asserts, 1);
}

static void send_event(int phase)
{
    criterion_protocol_msg msg = criterion_message(phase,
                    .phase = phase,
                    .name = (char *) criterion_current_test->name,
                    );

    criterion_message_set_id(msg);
    cr_send_to_runner(&msg);
}

void criterion_internal_test_setup(void)
{
    const struct criterion_suite *suite = criterion_current_suite;
    const struct criterion_test *test = criterion_current_test;

    send_event(criterion_protocol_phase_kind_SETUP);
    if (!cri_setjmp(g_pre_test)) {
        if (suite->data)
            (suite->data->init ? suite->data->init : nothing)();
        (test->data->init ? test->data->init : nothing)();
        return;
    }
    abort();
}

static void *getparam(void)
{
    void *param;

    bxf_context ctx = bxf_context_current();
    int rc = bxf_context_getobject(ctx, "criterion.param", (void **) &param);

    if (rc < 0) {
        cr_log_error("Could not retrieve test parameter -- aborting.");
        abort();
    }
    return param;
}

void criterion_internal_test_main(void (*fn)(void))
{
    const struct criterion_test *test = criterion_current_test;

    send_event(criterion_protocol_phase_kind_MAIN);

    if (!cri_setjmp(g_pre_test)) {
        if (!test->data->param_) {
            fn();
        } else {
            void (*param_test_func)(void *) = (void (*)(void *))fn;
            param_test_func(getparam());
        }
    }

    if (!criterion_options.full_stats) {
        criterion_protocol_msg msg = criterion_message_nots(statistic,
                        .key = ".asserts_passed",
                        .which_value = criterion_protocol_statistic_num_tag,
                        .value.num = passed_asserts);

        criterion_message_set_id(msg);
        cr_send_to_runner(&msg);
    }

    send_event(criterion_protocol_phase_kind_TEARDOWN);
}

void criterion_internal_test_teardown(void)
{
    const struct criterion_suite *suite = criterion_current_suite;
    const struct criterion_test *test = criterion_current_test;

    if (!cri_setjmp(g_pre_test)) {
        (test->data->fini ? test->data->fini : nothing)();
        if (suite->data)
            (suite->data->fini ? suite->data->fini : nothing)();
    }

    send_event(criterion_protocol_phase_kind_END);
}