summaryrefslogtreecommitdiff
path: root/410user/libtest/report.c
blob: 96d29a329bab549aa73c666911b64b67a3ac17b3 (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
/** @file 410user/libtest/report.c
 *  @brief Test reporting implementation
 *  @author elly1 U2009
 */

#include <stdio.h>          /* snprintf */
#include <stdlib.h>         /* exit */
#include <report.h>

#include "410_tests.h"      /* TEST_START_*, TEST_END_*, TEST_PFX */

/* This is a GNUism used for the sake of maintainability. This way nothing
 * horrible will happen if someone reorders the START_* constants in the header
 * file. */
const char *start_msgs[] = {
    [START_CMPLT] = TEST_START_CMPLT,
    [START_ABORT] = TEST_START_ABORT,
    [START_4EVER] = TEST_START_4EVER,
};

const char *end_msgs[] = {
    [END_SUCCESS] = TEST_END_SUCCESS,
    [END_FAIL]    = TEST_END_FAIL,
};

void format_start(char *buf, int len, int type) {
    snprintf(buf, len, "%s%s%s", TEST_PFX, test_name, start_msgs[type]);
}

void format_end(char *buf, int len, int type) {
    snprintf(buf, len, "%s%s%s", TEST_PFX, test_name, end_msgs[type]);
}

void format_misc(char *buf, int len, const char *msg) {
    snprintf(buf, len, "%s%s%s", TEST_PFX, test_name, msg);
}

void format_err(char *buf, int len, const char *msg, int code) {
    snprintf(buf, len, "%s%s%s%d", TEST_PFX, test_name, msg, code);
}

void report_start(int type) {
    char buf[256];
    format_start(buf, sizeof(buf), type);
    sim_puts(buf);
}

void report_end(int type) {
    char buf[256];
    format_end(buf, sizeof(buf), type);
    sim_puts(buf);

    int success = (type == END_SUCCESS) ? 1 : 0;
    sim_update_scoreboard((char *)test_name, success);
}

void report_misc(const char *msg) {
    char buf[256];
    format_misc(buf, sizeof(buf), msg);
    sim_puts(buf);
}

void report_fmt(const char *fmt, ...) {
    va_list ap;
    char b[256];
    char buf[256];
    va_start(ap, fmt);
    vsnprintf(b, sizeof(b), fmt, ap);
    va_end(ap);
    snprintf(buf, sizeof(buf), "%s%s%s", TEST_PFX, test_name, b);
    sim_puts(buf);
}

void report_err(const char *msg, int code) {
    char buf[256];
    format_err(buf, sizeof(buf), msg, code);
    sim_puts(buf);
}

void report_fatal(const char *msg, int code) {
    report_err(msg, code);
    report_end(END_FAIL);
}

void report_on_err(const char *exp, int line, int v) {
    char buf[256];
    if (v >= 0) { return; }
    snprintf(buf, sizeof(buf), "%s%sErr %d on line %d: `%s'",
             TEST_PFX, test_name, v, line, exp);
    sim_puts(buf);
}

void fatal_on_err(const char *exp, int line, int v) {
    report_on_err(exp, line, v);
    if (v >= 0) { return; }
    report_end(END_FAIL);
    exit(v);
}