summaryrefslogtreecommitdiffstats
path: root/lib/util/tests/test_sys_rw.c
blob: 22688fb044699360dce481024dc72ab2045d5413 (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
/*
 *  Unix SMB/CIFS implementation.
 *
 *  Unit test for sys_rw.c
 *
 *  Copyright (C) Ralph Böhme 2021
 *
 *  This program 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.
 *
 *  This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <setjmp.h>
#include <cmocka.h>

#include "lib/replace/replace.h"
#include "system/dir.h"

#include "lib/util/sys_rw.c"

static void test_sys_io_ranges_overlap(void **state)
{
	bool overlap;

	/*
	 * sys_io_ranges_overlap() args are:
	 *
	 * src size, src offset, dst size, dst offset
	 */

	/* src and dst size 0 => no overlap */
	overlap = sys_io_ranges_overlap(0, 0, 0, 0);
	assert_false(overlap);

	/* dst size 0 => no overlap */
	overlap = sys_io_ranges_overlap(1, 0, 0, 0);
	assert_false(overlap);

	/* src size 0 => no overlap */
	overlap = sys_io_ranges_overlap(0, 0, 1, 0);
	assert_false(overlap);

	/* same range => overlap */
	overlap = sys_io_ranges_overlap(1, 0, 1, 0);
	assert_true(overlap);

	/*
	 * |.|
	 *   |.|
	 * src before dst => no overlap
	 */
	overlap = sys_io_ranges_overlap(1, 0, 1, 1);
	assert_false(overlap);

	/*
	 * |..|
	 *  |..|
	 * src into dst => overlap
	 */
	overlap = sys_io_ranges_overlap(2, 0, 2, 1);
	assert_true(overlap);

	/*
	 * |....|
	 *  |..|
	 * src encompasses dst => overlap
	 */
	overlap = sys_io_ranges_overlap(4, 0, 1, 2);
	assert_true(overlap);


	/*
	 *  |..|
	 * |..|
	 * dst into src => overlap
	 */
	overlap = sys_io_ranges_overlap(2, 1, 2, 0);
	assert_true(overlap);

	/*
	 *  |..|
	 * |....|
	 * dst encompasses src => overlap
	 */
	overlap = sys_io_ranges_overlap(2, 1, 4, 0);
	assert_true(overlap);
}

static void test_sys_block_align(void **state)
{
	int asize;

	asize = sys_block_align(0, 2);
	assert_int_equal(asize, 0);
	asize = sys_block_align(1, 2);
	assert_int_equal(asize, 2);
	asize = sys_block_align(2, 2);
	assert_int_equal(asize, 2);
	asize = sys_block_align(3, 2);
	assert_int_equal(asize, 4);

	asize = sys_block_align(0, 4);
	assert_int_equal(asize, 0);
	asize = sys_block_align(1, 4);
	assert_int_equal(asize, 4);
	asize = sys_block_align(3, 4);
	assert_int_equal(asize, 4);
	asize = sys_block_align(4, 4);
	assert_int_equal(asize, 4);
	asize = sys_block_align(5, 4);
	assert_int_equal(asize, 8);
	asize = sys_block_align(7, 4);
	assert_int_equal(asize, 8);
	asize = sys_block_align(8, 4);
	assert_int_equal(asize, 8);
	asize = sys_block_align(9, 4);
	assert_int_equal(asize, 12);
}

static void test_sys_block_align_truncate(void **state)
{
	int asize;

	asize = sys_block_align_truncate(0, 2);
	assert_int_equal(asize, 0);
	asize = sys_block_align_truncate(1, 2);
	assert_int_equal(asize, 0);
	asize = sys_block_align_truncate(2, 2);
	assert_int_equal(asize, 2);
	asize = sys_block_align_truncate(3, 2);
	assert_int_equal(asize, 2);
	asize = sys_block_align_truncate(4, 2);
	assert_int_equal(asize, 4);
	asize = sys_block_align_truncate(5, 2);
	assert_int_equal(asize, 4);

	asize = sys_block_align_truncate(0, 4);
	assert_int_equal(asize, 0);
	asize = sys_block_align_truncate(1, 4);
	assert_int_equal(asize, 0);
	asize = sys_block_align_truncate(3, 4);
	assert_int_equal(asize, 0);
	asize = sys_block_align_truncate(4, 4);
	assert_int_equal(asize, 4);
	asize = sys_block_align_truncate(5, 4);
	assert_int_equal(asize, 4);
	asize = sys_block_align_truncate(7, 4);
	assert_int_equal(asize, 4);
	asize = sys_block_align_truncate(8, 4);
	assert_int_equal(asize, 8);
	asize = sys_block_align_truncate(9, 4);
	assert_int_equal(asize, 8);
}

int main(int argc, char **argv)
{
	const struct CMUnitTest tests[] = {
		cmocka_unit_test(test_sys_io_ranges_overlap),
		cmocka_unit_test(test_sys_block_align),
		cmocka_unit_test(test_sys_block_align_truncate),
	};

	cmocka_set_message_output(CM_OUTPUT_SUBUNIT);

	return cmocka_run_group_tests(tests, NULL, NULL);
}