aboutsummaryrefslogtreecommitdiff
path: root/test/common_plat/validation/api/init/init.c
blob: 61055fad51ff6e87eb5edb253980590d7d4eb27c (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* Copyright (c) 2015, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <stdarg.h>
#include <stdlib.h>
#include <odp_api.h>
#include <CUnit/Basic.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_t 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;
	odp_instance_t instance;

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

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

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

odp_testinfo_t init_suite_abort[] = {
	ODP_TEST_INFO(init_test_odp_init_global_replace_abort),
	ODP_TEST_INFO_NULL,
};

odp_suiteinfo_t init_suites_abort[] = {
	{"Init", NULL, NULL, init_suite_abort},
	ODP_SUITE_INFO_NULL,
};

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

int init_main_abort(int argc, char *argv[])
{
	int ret;

	/* parse common options: */
	if (odp_cunit_parse_options(argc, argv))
		return -1;

	/* prevent default ODP init: */
	odp_cunit_register_global_init(NULL);
	odp_cunit_register_global_term(NULL);

	/* run the tests: */
	ret = odp_cunit_register(init_suites_abort);

	if (ret == 0)
		ret = odp_cunit_run();

	return ret;
}

/* 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;
	odp_instance_t instance;

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

	replacement_logging_used = 0;

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

	CU_ASSERT_TRUE(replacement_logging_used || ODP_DEBUG_PRINT == 0);

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

odp_testinfo_t init_suite_log[] = {
	ODP_TEST_INFO(init_test_odp_init_global_replace_log),
	ODP_TEST_INFO_NULL,
};

odp_suiteinfo_t init_suites_log[] = {
	{"Init", NULL, NULL, init_suite_log},
	ODP_SUITE_INFO_NULL,
};

static int odp_init_log(odp_log_level_t 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(int argc, char *argv[])
{
	int ret;

	/* parse common options: */
	if (odp_cunit_parse_options(argc, argv))
		return -1;

	/* prevent default ODP init: */
	odp_cunit_register_global_init(NULL);
	odp_cunit_register_global_term(NULL);

	/* register the tests: */
	ret = odp_cunit_register(init_suites_log);

	/* run the tests: */
	if (ret == 0)
		ret = odp_cunit_run();

	return ret;
}

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

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

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

odp_testinfo_t init_suite_ok[] = {
	ODP_TEST_INFO(init_test_odp_init_global),
	ODP_TEST_INFO_NULL,
};

odp_suiteinfo_t init_suites_ok[] = {
	{"Init", NULL, NULL, init_suite_ok},
	ODP_SUITE_INFO_NULL,
};

int init_main_ok(int argc, char *argv[])
{
	int ret;

	/* parse common options: */
	if (odp_cunit_parse_options(argc, argv))
		return -1;

	/* prevent default ODP init: */
	odp_cunit_register_global_init(NULL);
	odp_cunit_register_global_term(NULL);

	/* register the tests: */
	ret = odp_cunit_register(init_suites_ok);

	/* run the tests: */
	if (ret == 0)
		ret = odp_cunit_run();

	return ret;
}