aboutsummaryrefslogtreecommitdiff
path: root/test/validation/init/init.c
blob: 12aa554b8a1faf937f12ad507fa3fa9dc0be00ab (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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* Copyright (c) 2015, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <stdarg.h>
#include <stdlib.h>
#include <odp.h>
#include <CUnit/Basic.h>
#include "odp_cunit_common.h"
#include "init.h"

/* flag set when the replacement logging function is used */
int replacement_logging_used;

/* replacement abort function: */
static void odp_init_abort(void) ODP_NORETURN;

/* replacement log function: */
ODP_PRINTF_FORMAT(2, 3)
static int odp_init_log(odp_log_level_e level, const char *fmt, ...);

/* test ODP global init, with alternate abort function */
void init_test_odp_init_global_replace_abort(void)
{
	int status;
	struct odp_init_t init_data;

	memset(&init_data, 0, sizeof(init_data));
	init_data.abort_fn = &odp_init_abort;

	status = odp_init_global(&init_data, NULL);
	CU_ASSERT_FATAL(status == 0);

	status = odp_term_global();
	CU_ASSERT(status == 0);
}

CU_TestInfo init_suite_abort[] = {
	{"replace abort",  init_test_odp_init_global_replace_abort},
	CU_TEST_INFO_NULL,
};

CU_SuiteInfo init_suites_abort[] = {
	{"Init", NULL, NULL, NULL, NULL, init_suite_abort},
	CU_SUITE_INFO_NULL,
};

static void odp_init_abort(void)
{
	abort();
}

int init_main_abort(void)
{
	/* prevent default ODP init: */
	odp_cunit_register_global_init(NULL);
	odp_cunit_register_global_term(NULL);

	/* run the tests: */
	return odp_cunit_run(init_suites_abort);
}

/* test ODP global init, with alternate log function */
void init_test_odp_init_global_replace_log(void)
{
	int status;
	struct odp_init_t init_data;

	memset(&init_data, 0, sizeof(init_data));
	init_data.log_fn = &odp_init_log;

	replacement_logging_used = 0;

	status = odp_init_global(&init_data, NULL);
	CU_ASSERT_FATAL(status == 0);

	CU_ASSERT_TRUE(replacement_logging_used);

	status = odp_term_global();
	CU_ASSERT(status == 0);
}

CU_TestInfo init_suite_log[] = {
	{"replace log",  init_test_odp_init_global_replace_log},
	CU_TEST_INFO_NULL,
};

CU_SuiteInfo init_suites_log[] = {
	{"Init", NULL, NULL, NULL, NULL, init_suite_log},
	CU_SUITE_INFO_NULL,
};

static int odp_init_log(odp_log_level_e level __attribute__((unused)),
			const char *fmt, ...)
{
	va_list args;
	int r;

	/* just set a flag to be sure the replacement fn was used */
	replacement_logging_used = 1;

	va_start(args, fmt);
	r = vfprintf(stderr, fmt, args);
	va_end(args);

	return r;
}

int init_main_log(void)
{
	/* prevent default ODP init: */
	odp_cunit_register_global_init(NULL);
	odp_cunit_register_global_term(NULL);

	/* run the tests: */
	return odp_cunit_run(init_suites_log);
}

/* test normal ODP global init */
void init_test_odp_init_global(void)
{
	int status;

	status = odp_init_global(NULL, NULL);
	CU_ASSERT_FATAL(status == 0);

	status = odp_term_global();
	CU_ASSERT(status == 0);
}

CU_TestInfo init_suite_ok[] = {
	{"test_odp_init_global",  init_test_odp_init_global},
	CU_TEST_INFO_NULL,
};

CU_SuiteInfo init_suites_ok[] = {
	{"Init", NULL, NULL, NULL, NULL, init_suite_ok},
	CU_SUITE_INFO_NULL,
};

int init_main_ok(void)
{
	/* prevent default ODP init: */
	odp_cunit_register_global_init(NULL);
	odp_cunit_register_global_term(NULL);

	/* run the tests: */
	return odp_cunit_run(init_suites_ok);
}