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_node_int.h>
42 #include <ecoli_test.h>
53 static int parse_llint(struct ec_node_int *node, const char *str,
59 *val = strtoll(str, &endptr, node->base);
62 if ((errno == ERANGE && (*val == LLONG_MAX || *val == LLONG_MIN)) ||
63 (errno != 0 && *val == 0))
66 if (node->check_min && *val < node->min)
69 if (node->check_max && *val > node->max)
78 static struct ec_parsed *ec_node_int_parse(const struct ec_node *gen_node,
79 const struct ec_strvec *strvec)
81 struct ec_node_int *node = (struct ec_node_int *)gen_node;
82 struct ec_parsed *parsed;
83 struct ec_strvec *match_strvec;
91 if (ec_strvec_len(strvec) == 0)
94 str = ec_strvec_val(strvec, 0);
95 if (parse_llint(node, str, &val) < 0)
98 match_strvec = ec_strvec_ndup(strvec, 0, 1);
99 if (match_strvec == NULL)
102 ec_parsed_set_match(parsed, gen_node, match_strvec);
107 ec_parsed_free(parsed);
111 static struct ec_node_type ec_node_int_type = {
113 .parse = ec_node_int_parse,
114 .complete = ec_node_default_complete,
115 .size = sizeof(struct ec_node_int),
118 EC_NODE_TYPE_REGISTER(ec_node_int_type);
120 struct ec_node *ec_node_int(const char *id, long long int min,
121 long long int max, unsigned int base)
123 struct ec_node *gen_node = NULL;
124 struct ec_node_int *node = NULL;
126 gen_node = __ec_node(&ec_node_int_type, id);
127 if (gen_node == NULL)
129 node = (struct ec_node_int *)gen_node;
131 node->check_min = true;
133 node->check_max = true;
140 long long ec_node_int_getval(struct ec_node *gen_node, const char *str)
142 struct ec_node_int *node = (struct ec_node_int *)gen_node;
145 // XXX check type here
146 // if gen_node->type != int fail
148 parse_llint(node, str, &val);
153 static int ec_node_int_testcase(void)
156 struct ec_node *node;
160 node = ec_node_int(NULL, 0, 256, 0);
162 ec_log(EC_LOG_ERR, "cannot create node\n");
165 ret |= EC_TEST_CHECK_PARSE(node, 1, "0");
166 ret |= EC_TEST_CHECK_PARSE(node, 1, "256", "foo");
167 ret |= EC_TEST_CHECK_PARSE(node, 1, "0x100");
168 ret |= EC_TEST_CHECK_PARSE(node, 1, " 1");
169 ret |= EC_TEST_CHECK_PARSE(node, -1, "-1");
170 ret |= EC_TEST_CHECK_PARSE(node, -1, "0x101");
172 p = ec_node_parse(node, "0");
173 s = ec_strvec_val(ec_parsed_strvec(p), 0);
174 EC_TEST_ASSERT(s != NULL && ec_node_int_getval(node, s) == 0);
177 p = ec_node_parse(node, "10");
178 s = ec_strvec_val(ec_parsed_strvec(p), 0);
179 EC_TEST_ASSERT(s != NULL && ec_node_int_getval(node, s) == 10);
183 node = ec_node_int(NULL, -1, LLONG_MAX, 16);
185 ec_log(EC_LOG_ERR, "cannot create node\n");
188 ret |= EC_TEST_CHECK_PARSE(node, 1, "0");
189 ret |= EC_TEST_CHECK_PARSE(node, 1, "-1");
190 ret |= EC_TEST_CHECK_PARSE(node, 1, "7fffffffffffffff");
191 ret |= EC_TEST_CHECK_PARSE(node, 1, "0x7fffffffffffffff");
192 ret |= EC_TEST_CHECK_PARSE(node, -1, "-2");
194 p = ec_node_parse(node, "10");
195 s = ec_strvec_val(ec_parsed_strvec(p), 0);
196 EC_TEST_ASSERT(s != NULL && ec_node_int_getval(node, s) == 16);
200 node = ec_node_int(NULL, LLONG_MIN, 0, 10);
202 ec_log(EC_LOG_ERR, "cannot create node\n");
205 ret |= EC_TEST_CHECK_PARSE(node, 1, "0");
206 ret |= EC_TEST_CHECK_PARSE(node, 1, "-1");
207 ret |= EC_TEST_CHECK_PARSE(node, 1, "-9223372036854775808");
208 ret |= EC_TEST_CHECK_PARSE(node, -1, "0x0");
209 ret |= EC_TEST_CHECK_PARSE(node, -1, "1");
212 /* test completion */
213 node = ec_node_int(NULL, 0, 10, 0);
215 ec_log(EC_LOG_ERR, "cannot create node\n");
218 ret |= EC_TEST_CHECK_COMPLETE(node,
222 ret |= EC_TEST_CHECK_COMPLETE(node,
223 "x", EC_NODE_ENDLIST,
226 ret |= EC_TEST_CHECK_COMPLETE(node,
227 "1", EC_NODE_ENDLIST,
235 static struct ec_test ec_node_int_test = {
237 .test = ec_node_int_testcase,
240 EC_TEST_REGISTER(ec_node_int_test);