summaryrefslogtreecommitdiff
path: root/libhw_generic/tests/test_io.c
blob: 0d6df1124c77c80797a32054d698ff3bf432828e (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
/* libhw_generic/tests/test_io.c - Tests for <libmisc/io.h>
 *
 * Copyright (C) 2025  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

#include <string.h>

#include <libhw/generic/io.h>

#include "test.h"

int main() {
	char data[] = "abcdefghijklmnopqrstuvwxyz";
	struct iovec src[] = {
		{.iov_base = &data[1], .iov_len=3}, /* "bcd" */
		{.iov_base = &data[20], .iov_len=4}, /* "uvwx" */
	};
	const int src_cnt = sizeof(src)/sizeof(src[0]);

	struct duplex_iovec dst[2];

#define TC(start, max, ...) do {                                                                 \
		char *exp[] = {__VA_ARGS__};                                                     \
		int exp_cnt = sizeof(exp)/sizeof(exp[0]);                                        \
		int act_cnt = io_slice_cnt(src, src_cnt, start, max);                            \
		test_assert(act_cnt == exp_cnt);                                                 \
		memset(dst, 0, sizeof(dst));                                                     \
		io_slice_wr_to_duplex(dst, src, src_cnt, start, max);                            \
		for (int i = 0; i < act_cnt; i++) {                                              \
			test_assert(dst[i].iov_read_to == IOVEC_DISCARD);                        \
			test_assert(dst[i].iov_write_from != IOVEC_DISCARD);                     \
			test_assert(dst[i].iov_len == strlen(exp[i]));                           \
			test_assert(memcmp(dst[i].iov_write_from, exp[i], dst[i].iov_len) == 0); \
		}                                                                                \
	} while (0)

	TC(0, 0, /* => */ "bcd", "uvwx");
	TC(1, 0, /* => */ "cd", "uvwx");
	TC(2, 0, /* => */ "d", "uvwx");
	TC(3, 0, /* => */ "uvwx");
	TC(4, 0, /* => */ "vwx");
	TC(5, 0, /* => */ "wx");
	TC(6, 0, /* => */ "x");
	TC(7, 0, /* => */ );

	TC(0, 2, /* => */ "bc");
	TC(1, 2, /* => */ "cd");
	TC(2, 2, /* => */ "d", "u");
	TC(3, 2, /* => */ "uv");
	TC(4, 2, /* => */ "vw");
	TC(5, 2, /* => */ "wx");
	TC(6, 2, /* => */ "x");
	TC(7, 2, /* => */ );

	return 0;
}