File: free.c

package info (click to toggle)
libcyaml 1.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 980 kB
  • sloc: ansic: 20,884; makefile: 166; sh: 13
file content (161 lines) | stat: -rw-r--r-- 3,882 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
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
/*
 * SPDX-License-Identifier: ISC
 *
 * Copyright (C) 2017-2021 Michael Drake <tlsa@netsurf-browser.org>
 */

#include <stdbool.h>
#include <assert.h>
#include <stdio.h>

#include <cyaml/cyaml.h>

#include "ttest.h"
#include "test.h"

/** Macro to squash unused variable compiler warnings. */
#define UNUSED(_x) ((void)(_x))

/**
 * Test cyaml_free with NULL data.
 *
 * \param[in]  report  The test report context.
 * \param[in]  config  The CYAML config to use for the test.
 * \return true if test passes, false otherwise.
 */
static bool test_free_null_data(
		ttest_report_ctx_t *report,
		const cyaml_config_t *config)
{
	cyaml_err_t err;
	struct target_struct {
		int test_value_int;
	};
	static const struct cyaml_schema_field mapping_schema[] = {
		CYAML_FIELD_INT("test_int", CYAML_FLAG_DEFAULT,
				struct target_struct, test_value_int),
		CYAML_FIELD_END
	};
	static const struct cyaml_schema_value top_schema = {
		CYAML_VALUE_MAPPING(CYAML_FLAG_POINTER,
				struct target_struct, mapping_schema),
	};
	ttest_ctx_t tc;

	if (!ttest_start(report, __func__, NULL, NULL, &tc)) return true;

	err = cyaml_free(config, &top_schema, NULL, 0);
	if (err != CYAML_OK) {
		return ttest_fail(&tc, "Free failed: %s", cyaml_strerror(err));
	}

	return ttest_pass(&tc);
}

/**
 * Test cyaml_free with NULL config.
 *
 * \param[in]  report  The test report context.
 * \param[in]  config  The CYAML config to use for the test.
 * \return true if test passes, false otherwise.
 */
static bool test_free_null_config(
		ttest_report_ctx_t *report,
		const cyaml_config_t *config)
{
	cyaml_err_t err;
	ttest_ctx_t tc;

	if (!ttest_start(report, __func__, NULL, NULL, &tc)) return true;

	UNUSED(config);

	err = cyaml_free(NULL, NULL, NULL, 0);
	if (err != CYAML_ERR_BAD_PARAM_NULL_CONFIG) {
		return ttest_fail(&tc, "Free failed: %s", cyaml_strerror(err));
	}

	return ttest_pass(&tc);
}

/**
 * Test cyaml_free with NULL memory allocation function.
 *
 * \param[in]  report  The test report context.
 * \param[in]  config  The CYAML config to use for the test.
 * \return true if test passes, false otherwise.
 */
static bool test_free_null_mem_fn(
		ttest_report_ctx_t *report,
		const cyaml_config_t *config)
{
	cyaml_err_t err;
	cyaml_config_t cfg = *config;
	ttest_ctx_t tc;

	if (!ttest_start(report, __func__, NULL, NULL, &tc)) return true;

	cfg.mem_fn = NULL;

	err = cyaml_free(&cfg, NULL, NULL, 0);
	if (err != CYAML_ERR_BAD_CONFIG_NULL_MEMFN) {
		return ttest_fail(&tc, "Free failed: %s", cyaml_strerror(err));
	}

	return ttest_pass(&tc);
}

/**
 * Test cyaml_free with NULL schema.
 *
 * \param[in]  report  The test report context.
 * \param[in]  config  The CYAML config to use for the test.
 * \return true if test passes, false otherwise.
 */
static bool test_free_null_schema(
		ttest_report_ctx_t *report,
		const cyaml_config_t *config)
{
	cyaml_err_t err;
	ttest_ctx_t tc;

	if (!ttest_start(report, __func__, NULL, NULL, &tc)) return true;

	err = cyaml_free(config, NULL, NULL, 0);
	if (err != CYAML_ERR_BAD_PARAM_NULL_SCHEMA) {
		return ttest_fail(&tc, "Free failed: %s", cyaml_strerror(err));
	}

	return ttest_pass(&tc);
}

/**
 * Run the CYAML freeing unit tests.
 *
 * \param[in]  rc         The ttest report context.
 * \param[in]  log_level  CYAML log level.
 * \param[in]  log_fn     CYAML logging function, or NULL.
 * \return true iff all unit tests pass, otherwise false.
 */
bool free_tests(
		ttest_report_ctx_t *rc,
		cyaml_log_t log_level,
		cyaml_log_fn_t log_fn)
{
	bool pass = true;
	cyaml_config_t config = {
		.log_fn = log_fn,
		.mem_fn = cyaml_mem,
		.log_level = log_level,
		.flags = CYAML_CFG_DEFAULT,
	};

	ttest_heading(rc, "Free tests");

	pass &= test_free_null_data(rc, &config);
	pass &= test_free_null_mem_fn(rc, &config);
	pass &= test_free_null_config(rc, &config);
	pass &= test_free_null_schema(rc, &config);

	return pass;
}