summaryrefslogtreecommitdiffstats
path: root/tests/io/pq_file_test.c
blob: 664acf788612c1e9ffd774620d3fb3afa145cb6d (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
/*
 * This file is part of GAPK (GSM Audio Pocket Knife).
 *
 * (C) 2017 by Vadim Yanitskiy <axilirator@gmail.com>
 *
 * GAPK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GAPK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GAPK.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <errno.h>
#include <talloc.h>
#include <assert.h>

#include <osmocom/gapk/procqueue.h>
#include <osmocom/gapk/common.h>

/**
 * This test is intended to check the file source / sink
 * operability. To do that, the following processing chain
 * is being composed:
 *
 *   source/file -> proc/dummy -> sink/file (stdout)
 *
 * The source item opens the sample file named 'io_sample.txt'
 * for reading. The next processing item simply converts all
 * uppercase latters to the lowercase. The last one writes
 * the result to stdout.
 *
 * This processing cycle is being repeated several times
 * with different block length values.
 */

static void talloc_ctx_walk_cb(const void *chunk, int depth,
	int max_depth, int is_ref, void *data)
{
	const char *chunk_name = talloc_get_name(chunk);
	int spaces_cnt;

	/* Hierarchical spacing */
	for (spaces_cnt = 0; spaces_cnt < depth; spaces_cnt++)
		printf("  ");

	/* Chunk info */
	printf("chunk %s: depth=%d\n", chunk_name, depth);
}

static int pq_file_proc(void *state, uint8_t *out, const uint8_t *in,
	unsigned int in_len)
{
	int i;

	/* Change upper case to lower */
	for (i = 0; i < in_len; i++)
		out[i] = (in[i] >= 65 && in[i] <= 90) ?
			in[i] + 32 : in[i];

	return in_len;
}

static int pq_file_check(FILE *src_file, unsigned int blk_len)
{
	struct osmo_gapk_pq_item *proc_item;
	struct osmo_gapk_pq *pq;
	int rc;

	printf("Processing sample file with blk_len=%u:\n", blk_len);

	/* Allocate a processing queue */
	pq = osmo_gapk_pq_create("pq_file_check");
	assert(pq != NULL);

	/* Add a file sink */
	rc = osmo_gapk_pq_queue_file_input(pq, src_file, blk_len);
	assert(rc == 0);

	/* Add a processing item */
	proc_item = osmo_gapk_pq_add_item(pq);
	assert(proc_item != NULL);

	/* Set up I/O parameters */
	proc_item->type = OSMO_GAPK_ITEM_TYPE_PROC;
	proc_item->len_in = blk_len;
	proc_item->len_out = blk_len;

	/* Set the processing callback */
	proc_item->proc = &pq_file_proc;

	/* Add a sink item */
	rc = osmo_gapk_pq_queue_file_output(pq, stdout, blk_len);
	assert(rc == 0);

	/* Check a queue in strict mode */
	rc = osmo_gapk_pq_check(pq, 1);
	assert(rc == 0);

	/* Prepare a queue */
	rc = osmo_gapk_pq_prepare(pq);
	assert(rc == 0);

	while (1) {
		rc = osmo_gapk_pq_execute(pq);
		if (rc)
			break;
	}

	/* Destroy processing queue */
	osmo_gapk_pq_destroy(pq);

	return 0;
}

int main(int argc, char **argv)
{
	int i;

	/* Enable tracking the use of NULL memory contexts */
	talloc_enable_null_tracking();

	/* Open sample file */
	FILE *sample_file = fopen(argv[1], "rb");
	assert(sample_file != NULL);

	/* Process sample file with different blk_len values */
	for (i = 2; i <= 32; i *= 2) {
		pq_file_check(sample_file, i);
		rewind(sample_file);
	}

	printf("\n");

	/* Close sample file */
	fclose(sample_file);

	/* Print talloc memory hierarchy */
	talloc_report_depth_cb(NULL, 0, 10, &talloc_ctx_walk_cb, NULL);

	/* Make both Valgrind and LeakSanitizer happy */
	talloc_disable_null_tracking();

	return 0;
}