2 * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <ecoli_log.h>
38 #include <ecoli_malloc.h>
39 #include <ecoli_strvec.h>
40 #include <ecoli_node.h>
41 #include <ecoli_parsed.h>
42 #include <ecoli_completed.h>
43 #include <ecoli_node_int.h>
44 #include <ecoli_test.h>
46 EC_LOG_TYPE_REGISTER(node_int);
48 /* common to int and uint */
49 struct ec_node_int_uint {
65 static int parse_llint(struct ec_node_int_uint *node, const char *str,
71 *val = strtoll(str, &endptr, node->base);
73 if ((errno == ERANGE && (*val == LLONG_MAX || *val == LLONG_MIN)) ||
74 (errno != 0 && *val == 0))
77 if (node->check_min && *val < node->min)
80 if (node->check_max && *val > node->max)
89 static int parse_ullint(struct ec_node_int_uint *node, const char *str,
94 /* since a negative input is silently converted to a positive
95 * one by strtoull(), first check that it is positive */
100 *val = strtoull(str, &endptr, node->base);
102 if ((errno == ERANGE && *val == ULLONG_MAX) ||
103 (errno != 0 && *val == 0))
106 if (node->check_min && *val < node->umin)
109 if (node->check_max && *val > node->umax)
118 static int ec_node_int_uint_parse(const struct ec_node *gen_node,
119 struct ec_parsed *state,
120 const struct ec_strvec *strvec)
122 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
129 if (ec_strvec_len(strvec) == 0)
130 return EC_PARSED_NOMATCH;
132 str = ec_strvec_val(strvec, 0);
133 if (node->is_signed) {
134 if (parse_llint(node, str, &i64) < 0)
135 return EC_PARSED_NOMATCH;
137 if (parse_ullint(node, str, &u64) < 0)
138 return EC_PARSED_NOMATCH;
144 ec_node_uint_init_priv(struct ec_node *gen_node)
146 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
148 node->is_signed = true;
153 static struct ec_node_type ec_node_int_type = {
155 .parse = ec_node_int_uint_parse,
156 .complete = ec_node_default_complete,
157 .size = sizeof(struct ec_node_int_uint),
158 .init_priv = ec_node_uint_init_priv,
161 EC_NODE_TYPE_REGISTER(ec_node_int_type);
163 struct ec_node *ec_node_int(const char *id, int64_t min,
164 int64_t max, unsigned int base)
166 struct ec_node *gen_node = NULL;
168 gen_node = __ec_node(&ec_node_int_type, id);
169 if (gen_node == NULL)
172 if (ec_node_int_set_limits(gen_node, min, max) < 0)
174 if (ec_node_int_set_base(gen_node, base) < 0)
180 ec_node_free(gen_node);
184 static struct ec_node_type ec_node_uint_type = {
186 .parse = ec_node_int_uint_parse,
187 .complete = ec_node_default_complete,
188 .size = sizeof(struct ec_node_int_uint),
191 EC_NODE_TYPE_REGISTER(ec_node_uint_type);
193 struct ec_node *ec_node_uint(const char *id, uint64_t min,
194 uint64_t max, unsigned int base)
196 struct ec_node *gen_node = NULL;
198 gen_node = __ec_node(&ec_node_uint_type, id);
199 if (gen_node == NULL)
202 if (ec_node_uint_set_limits(gen_node, min, max) < 0)
204 if (ec_node_uint_set_base(gen_node, base) < 0)
210 ec_node_free(gen_node);
214 int ec_node_int_disable_limits(struct ec_node *gen_node)
216 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
219 ret = ec_node_check_type(gen_node, &ec_node_int_type);
223 node->check_min = false;
224 node->check_max = false;
229 int ec_node_int_set_limits(struct ec_node *gen_node, int64_t min,
232 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
235 ret = ec_node_check_type(gen_node, &ec_node_int_type);
244 node->check_min = true;
246 node->check_max = true;
252 int ec_node_int_set_base(struct ec_node *gen_node, unsigned int base)
254 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
257 ret = ec_node_check_type(gen_node, &ec_node_int_type);
267 int ec_node_uint_disable_limits(struct ec_node *gen_node)
269 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
272 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
276 node->check_min = false;
277 node->check_max = false;
282 int ec_node_uint_set_limits(struct ec_node *gen_node, uint64_t min,
285 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
288 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
297 node->check_min = true;
299 node->check_max = true;
305 int ec_node_uint_set_base(struct ec_node *gen_node, unsigned int base)
307 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
310 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
319 int ec_node_int_getval(const struct ec_node *gen_node, const char *str,
322 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
325 ret = ec_node_check_type(gen_node, &ec_node_int_type);
329 if (parse_llint(node, str, result) < 0)
335 int ec_node_uint_getval(const struct ec_node *gen_node, const char *str,
338 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
341 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
345 if (parse_ullint(node, str, result) < 0)
351 /* LCOV_EXCL_START */
352 static int ec_node_int_testcase(void)
355 struct ec_node *node;
361 node = ec_node_uint(EC_NO_ID, 1, 256, 0);
363 EC_LOG(EC_LOG_ERR, "cannot create node\n");
366 ret |= EC_TEST_CHECK_PARSE(node, -1, "");
367 ret |= EC_TEST_CHECK_PARSE(node, -1, "0");
368 ret |= EC_TEST_CHECK_PARSE(node, 1, "1");
369 ret |= EC_TEST_CHECK_PARSE(node, 1, "256", "foo");
370 ret |= EC_TEST_CHECK_PARSE(node, 1, "0x100");
371 ret |= EC_TEST_CHECK_PARSE(node, 1, " 1");
372 ret |= EC_TEST_CHECK_PARSE(node, -1, "-1");
373 ret |= EC_TEST_CHECK_PARSE(node, -1, "0x101");
374 ret |= EC_TEST_CHECK_PARSE(node, -1, "zzz");
375 ret |= EC_TEST_CHECK_PARSE(node, -1, "0x100000000000000000");
376 ret |= EC_TEST_CHECK_PARSE(node, -1, "4r");
377 ec_node_uint_disable_limits(node);
378 ret |= EC_TEST_CHECK_PARSE(node, 1, "0");
380 p = ec_node_parse(node, "1");
381 s = ec_strvec_val(ec_parsed_strvec(p), 0);
382 EC_TEST_ASSERT(s != NULL &&
383 ec_node_uint_getval(node, s, &u64) == 0 &&
387 p = ec_node_parse(node, "10");
388 s = ec_strvec_val(ec_parsed_strvec(p), 0);
389 EC_TEST_ASSERT(s != NULL &&
390 ec_node_uint_getval(node, s, &u64) == 0 &&
395 node = ec_node_int(EC_NO_ID, -1, LLONG_MAX, 16);
397 EC_LOG(EC_LOG_ERR, "cannot create node\n");
400 ret |= EC_TEST_CHECK_PARSE(node, 1, "0");
401 ret |= EC_TEST_CHECK_PARSE(node, 1, "-1");
402 ret |= EC_TEST_CHECK_PARSE(node, 1, "7fffffffffffffff");
403 ret |= EC_TEST_CHECK_PARSE(node, 1, "0x7fffffffffffffff");
404 ret |= EC_TEST_CHECK_PARSE(node, -1, "0x8000000000000000");
405 ret |= EC_TEST_CHECK_PARSE(node, -1, "-2");
406 ret |= EC_TEST_CHECK_PARSE(node, -1, "zzz");
407 ret |= EC_TEST_CHECK_PARSE(node, -1, "4r");
408 ec_node_int_disable_limits(node);
409 ret |= EC_TEST_CHECK_PARSE(node, 1, "-2");
411 p = ec_node_parse(node, "10");
412 s = ec_strvec_val(ec_parsed_strvec(p), 0);
413 EC_TEST_ASSERT(s != NULL &&
414 ec_node_int_getval(node, s, &i64) == 0 &&
419 node = ec_node_int(EC_NO_ID, LLONG_MIN, 0, 10);
421 EC_LOG(EC_LOG_ERR, "cannot create node\n");
424 ret |= EC_TEST_CHECK_PARSE(node, 1, "0");
425 ret |= EC_TEST_CHECK_PARSE(node, 1, "-1");
426 ret |= EC_TEST_CHECK_PARSE(node, 1, "-9223372036854775808");
427 ret |= EC_TEST_CHECK_PARSE(node, -1, "0x0");
428 ret |= EC_TEST_CHECK_PARSE(node, -1, "1");
431 /* test completion */
432 node = ec_node_int(EC_NO_ID, 0, 10, 0);
434 EC_LOG(EC_LOG_ERR, "cannot create node\n");
437 ret |= EC_TEST_CHECK_COMPLETE(node,
440 ret |= EC_TEST_CHECK_COMPLETE(node,
441 "x", EC_NODE_ENDLIST,
443 ret |= EC_TEST_CHECK_COMPLETE(node,
444 "1", EC_NODE_ENDLIST,
452 static struct ec_test ec_node_int_test = {
454 .test = ec_node_int_testcase,
457 EC_TEST_REGISTER(ec_node_int_test);