source: mainline/uspace/lib/c/test/capa.c@ c111da2

Last change on this file since c111da2 was c111da2, checked in by Jiri Svoboda <jiri@…>, 4 weeks ago

Create non-zero size file in Navigator, new newfile utility.

  • Property mode set to 100644
File size: 5.2 KB
Line 
1/*
2 * Copyright (c) 2025 Jiri Svoboda
3 * Copyright (c) 2019 Matthieu Riolo
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <pcut/pcut.h>
31#include <capa.h>
32
33PCUT_INIT;
34
35PCUT_TEST_SUITE(capa);
36
37PCUT_TEST(capa_format)
38{
39 int block_size = 4;
40 size_t block[] = {
41 0,
42 1,
43 2,
44 10,
45 };
46
47 int input_size = 5;
48 size_t input[] = {
49 0,
50 10,
51 100,
52 1000,
53 1000000,
54 1000000000,
55 };
56
57 const char *out[] = {
58 "0 B",
59 "0 B",
60 "0 B",
61 "0 B",
62
63 "0 B",
64 "10 B",
65 "20 B",
66 "100 B",
67
68 "0 B",
69 "100 B",
70 "200 B",
71 "1.000 kB",
72
73 "0 B",
74 "1.000 kB",
75 "2.000 kB",
76 "10.00 kB",
77
78 "0 B",
79 "1.000 MB",
80 "2.000 MB",
81 "10.00 MB",
82
83 "0 B",
84 "1.000 GB",
85 "2.000 GB",
86 "10.00 GB",
87 };
88
89 capa_spec_t capa;
90 char *str;
91 errno_t rc;
92
93 int x, i;
94 for (i = 0; i < input_size; i++) {
95 for (x = 0; x < block_size; x++) {
96 capa_from_blocks(input[i], block[x], &capa);
97 capa_simplify(&capa);
98
99 rc = capa_format(&capa, &str);
100
101 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
102 PCUT_ASSERT_STR_EQUALS(out[x + (block_size * i)], str);
103 free(str);
104
105 capa_from_blocks(block[x], input[i], &capa);
106 capa_simplify(&capa);
107
108 rc = capa_format(&capa, &str);
109
110 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
111 PCUT_ASSERT_STR_EQUALS(out[x + (block_size * i)], str);
112 free(str);
113 }
114 }
115}
116
117PCUT_TEST(capa_format_rounding)
118{
119 int input_size = 8;
120 uint64_t input[] = {
121 555,
122 5555,
123 55555,
124 555555555,
125 5555555555,
126 555999999,
127 5999999,
128 999999
129 };
130
131 const char *out[] = {
132 "555 B",
133 "5.555 kB",
134 "55.56 kB",
135 "555.6 MB",
136 "5.556 GB",
137 "556.0 MB",
138 "6.000 MB",
139 "1.000 MB",
140 };
141
142 capa_spec_t capa;
143 char *str;
144 errno_t rc;
145
146 int i;
147 for (i = 0; i < input_size; i++) {
148 capa_from_blocks(input[i], 1, &capa);
149 capa_simplify(&capa);
150
151 rc = capa_format(&capa, &str);
152
153 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
154 PCUT_ASSERT_STR_EQUALS(out[i], str);
155 free(str);
156
157 capa_from_blocks(1, input[i], &capa);
158 capa_simplify(&capa);
159
160 rc = capa_format(&capa, &str);
161
162 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
163 PCUT_ASSERT_STR_EQUALS(out[i], str);
164 free(str);
165 }
166}
167
168PCUT_TEST(capa_parse)
169{
170 int input_size = 4;
171 const char *input[] = {
172 "0 B",
173 "100 B",
174 "1 kB",
175 "1.555 kB",
176 };
177
178 int out_cunit[] = {
179 cu_byte,
180 cu_byte,
181 cu_kbyte,
182 cu_kbyte,
183 };
184
185 int out_dp[] = {
186 0,
187 0,
188 0,
189 3,
190 };
191
192 int out_m[] = {
193 0,
194 100,
195 1,
196 1555,
197 };
198
199 capa_spec_t capa;
200 errno_t rc;
201 int i;
202
203 for (i = 0; i < input_size; i++) {
204 rc = capa_parse(input[i], &capa);
205
206 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
207 PCUT_ASSERT_INT_EQUALS(out_cunit[i], capa.cunit);
208 PCUT_ASSERT_INT_EQUALS(out_dp[i], capa.dp);
209 PCUT_ASSERT_INT_EQUALS(out_m[i], capa.m);
210 }
211}
212
213PCUT_TEST(capa_to_blocks)
214{
215 int input_size = 0;
216 int input_m[] = {
217 0,
218 1,
219 1000,
220 5555,
221 7777,
222 };
223
224 int input_dp[] = {
225 0,
226 0,
227 3,
228 3,
229 2,
230 };
231
232 int block[] = {
233 1,
234 1,
235 1,
236 2,
237 3,
238 };
239
240 int out_nom[] = {
241 0,
242 1000,
243 1000,
244 2778,
245 25923,
246 };
247
248 int out_min[] = {
249 0,
250 1000,
251 1000,
252 2777,
253 25923,
254 };
255
256 int out_max[] = {
257 0,
258 1000,
259 1000,
260 2778,
261 25924,
262 };
263
264 capa_spec_t capa;
265 errno_t rc;
266 int i;
267 uint64_t ret;
268
269 for (i = 0; i < input_size; i++) {
270 capa.m = input_m[i];
271 capa.dp = input_dp[i];
272 capa.cunit = cu_kbyte;
273
274 rc = capa_to_blocks(&capa, cv_nom, block[i], &ret);
275 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
276 PCUT_ASSERT_INT_EQUALS(out_nom[i], ret);
277
278 rc = capa_to_blocks(&capa, cv_min, block[i], &ret);
279 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
280 PCUT_ASSERT_INT_EQUALS(out_min[i], ret);
281
282 rc = capa_to_blocks(&capa, cv_max, block[i], &ret);
283 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
284 PCUT_ASSERT_INT_EQUALS(out_max[i], ret);
285 }
286}
287
288PCUT_TEST(capa_blocks_format)
289{
290 errno_t rc;
291 char *str;
292
293 rc = capa_blocks_format(42, 1, &str);
294 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
295 PCUT_ASSERT_STR_EQUALS("42 B", str);
296 free(str);
297}
298
299PCUT_TEST(capa_blocks_format_buf)
300{
301 char str[CAPA_BLOCKS_BUFSIZE];
302
303 capa_blocks_format_buf(42, 1, str, sizeof(str));
304 PCUT_ASSERT_STR_EQUALS("42 B", str);
305}
306
307PCUT_EXPORT(capa);
Note: See TracBrowser for help on using the repository browser.