source: mainline/uspace/lib/c/test/capa.c@ 84876aa4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 84876aa4 was c24b0dcb, checked in by Jakub Jermar <jakub@…>, 6 years ago

Rename capacity-related 'cap' to 'capa'

This allows to use 'cap' for capabilities.

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