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>
55 static int parse_llint(struct ec_node_int *node, const char *str,
61 *val = strtoll(str, &endptr, node->base);
64 if ((errno == ERANGE && (*val == LLONG_MAX || *val == LLONG_MIN)) ||
65 (errno != 0 && *val == 0))
68 if (node->check_min && *val < node->min)
71 if (node->check_max && *val > node->max)
80 static int ec_node_int_parse(const struct ec_node *gen_node,
81 struct ec_parsed *state,
82 const struct ec_strvec *strvec)
84 struct ec_node_int *node = (struct ec_node_int *)gen_node;
90 if (ec_strvec_len(strvec) == 0)
91 return EC_PARSED_NOMATCH;
93 str = ec_strvec_val(strvec, 0);
94 if (parse_llint(node, str, &val) < 0)
95 return EC_PARSED_NOMATCH;
100 static size_t ec_node_int_get_max_parse_len(const struct ec_node *gen_node)
106 static struct ec_node_type ec_node_int_type = {
108 .parse = ec_node_int_parse,
109 .complete = ec_node_default_complete,
110 .get_max_parse_len = ec_node_int_get_max_parse_len,
111 .size = sizeof(struct ec_node_int),
114 EC_NODE_TYPE_REGISTER(ec_node_int_type);
116 struct ec_node *ec_node_int(const char *id, long long int min,
117 long long int max, unsigned int base)
119 struct ec_node *gen_node = NULL;
120 struct ec_node_int *node = NULL;
122 gen_node = __ec_node(&ec_node_int_type, id);
123 if (gen_node == NULL)
125 node = (struct ec_node_int *)gen_node;
127 node->check_min = true;
129 node->check_max = true;
136 long long ec_node_int_getval(struct ec_node *gen_node, const char *str)
138 struct ec_node_int *node = (struct ec_node_int *)gen_node;
141 // XXX check type here
142 // if gen_node->type != int fail
144 parse_llint(node, str, &val);
149 /* LCOV_EXCL_START */
150 static int ec_node_int_testcase(void)
153 struct ec_node *node;
157 node = ec_node_int(NULL, 0, 256, 0);
159 ec_log(EC_LOG_ERR, "cannot create node\n");
162 ret |= EC_TEST_CHECK_PARSE(node, 1, "0");
163 ret |= EC_TEST_CHECK_PARSE(node, 1, "256", "foo");
164 ret |= EC_TEST_CHECK_PARSE(node, 1, "0x100");
165 ret |= EC_TEST_CHECK_PARSE(node, 1, " 1");
166 ret |= EC_TEST_CHECK_PARSE(node, -1, "-1");
167 ret |= EC_TEST_CHECK_PARSE(node, -1, "0x101");
169 p = ec_node_parse(node, "0");
170 s = ec_strvec_val(ec_parsed_strvec(p), 0);
171 EC_TEST_ASSERT(s != NULL && ec_node_int_getval(node, s) == 0);
174 p = ec_node_parse(node, "10");
175 s = ec_strvec_val(ec_parsed_strvec(p), 0);
176 EC_TEST_ASSERT(s != NULL && ec_node_int_getval(node, s) == 10);
180 node = ec_node_int(NULL, -1, LLONG_MAX, 16);
182 ec_log(EC_LOG_ERR, "cannot create node\n");
185 ret |= EC_TEST_CHECK_PARSE(node, 1, "0");
186 ret |= EC_TEST_CHECK_PARSE(node, 1, "-1");
187 ret |= EC_TEST_CHECK_PARSE(node, 1, "7fffffffffffffff");
188 ret |= EC_TEST_CHECK_PARSE(node, 1, "0x7fffffffffffffff");
189 ret |= EC_TEST_CHECK_PARSE(node, -1, "-2");
191 p = ec_node_parse(node, "10");
192 s = ec_strvec_val(ec_parsed_strvec(p), 0);
193 EC_TEST_ASSERT(s != NULL && ec_node_int_getval(node, s) == 16);
197 node = ec_node_int(NULL, LLONG_MIN, 0, 10);
199 ec_log(EC_LOG_ERR, "cannot create node\n");
202 ret |= EC_TEST_CHECK_PARSE(node, 1, "0");
203 ret |= EC_TEST_CHECK_PARSE(node, 1, "-1");
204 ret |= EC_TEST_CHECK_PARSE(node, 1, "-9223372036854775808");
205 ret |= EC_TEST_CHECK_PARSE(node, -1, "0x0");
206 ret |= EC_TEST_CHECK_PARSE(node, -1, "1");
209 /* test completion */
210 node = ec_node_int(NULL, 0, 10, 0);
212 ec_log(EC_LOG_ERR, "cannot create node\n");
215 ret |= EC_TEST_CHECK_COMPLETE(node,
218 ret |= EC_TEST_CHECK_COMPLETE(node,
219 "x", EC_NODE_ENDLIST,
221 ret |= EC_TEST_CHECK_COMPLETE(node,
222 "1", EC_NODE_ENDLIST,
230 static struct ec_test ec_node_int_test = {
232 .test = ec_node_int_testcase,
235 EC_TEST_REGISTER(ec_node_int_test);