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.
35 #include <ecoli_log.h>
36 #include <ecoli_malloc.h>
37 #include <ecoli_strvec.h>
39 #include <ecoli_tk_int.h>
40 #include <ecoli_test.h>
49 static int parse_llint(struct ec_tk_int *tk, const char *str,
55 *val = strtoll(str, &endptr, tk->base);
58 if ((errno == ERANGE && (*val == LLONG_MAX || *val == LLONG_MIN)) ||
59 (errno != 0 && *val == 0))
62 if (*val < tk->min || *val > tk->max)
71 static struct ec_parsed_tk *ec_tk_int_parse(const struct ec_tk *gen_tk,
72 const struct ec_strvec *strvec)
74 struct ec_tk_int *tk = (struct ec_tk_int *)gen_tk;
75 struct ec_parsed_tk *parsed_tk;
76 struct ec_strvec *match_strvec;
80 parsed_tk = ec_parsed_tk_new();
81 if (parsed_tk == NULL)
84 if (ec_strvec_len(strvec) == 0)
87 str = ec_strvec_val(strvec, 0);
88 if (parse_llint(tk, str, &val) < 0)
91 match_strvec = ec_strvec_ndup(strvec, 1);
92 if (match_strvec == NULL)
95 ec_parsed_tk_set_match(parsed_tk, gen_tk, match_strvec);
100 ec_parsed_tk_free(parsed_tk);
104 static struct ec_tk_ops ec_tk_int_ops = {
106 .parse = ec_tk_int_parse,
107 .complete = ec_tk_default_complete,
110 struct ec_tk *ec_tk_int_new(const char *id, long long int min,
111 long long int max, unsigned int base)
113 struct ec_tk_int *tk = NULL;
115 tk = (struct ec_tk_int *)ec_tk_new(id, &ec_tk_int_ops, sizeof(*tk));
126 long long ec_tk_int_getval(struct ec_tk *gen_tk, const char *str)
128 struct ec_tk_int *tk = (struct ec_tk_int *)gen_tk;
131 // XXX check type here
132 // if gen_tk->type != int fail
134 parse_llint(tk, str, &val);
139 static int ec_tk_int_testcase(void)
141 struct ec_parsed_tk *p;
146 tk = ec_tk_int_new(NULL, 0, 256, 0);
148 ec_log(EC_LOG_ERR, "cannot create tk\n");
151 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "0", EC_TK_ENDLIST);
152 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "256", "foo", EC_TK_ENDLIST);
153 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "0x100", EC_TK_ENDLIST);
154 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, " 1", EC_TK_ENDLIST);
155 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "-1", EC_TK_ENDLIST);
156 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "0x101", EC_TK_ENDLIST);
158 p = ec_tk_parse(tk, "0");
159 s = ec_parsed_tk_to_string(p);
160 EC_TEST_ASSERT(s != NULL && ec_tk_int_getval(tk, s) == 0);
161 ec_parsed_tk_free(p);
163 p = ec_tk_parse(tk, "10");
164 s = ec_parsed_tk_to_string(p);
165 EC_TEST_ASSERT(s != NULL && ec_tk_int_getval(tk, s) == 10);
166 ec_parsed_tk_free(p);
169 tk = ec_tk_int_new(NULL, -1, LLONG_MAX, 16);
171 ec_log(EC_LOG_ERR, "cannot create tk\n");
174 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "0", EC_TK_ENDLIST);
175 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "-1", EC_TK_ENDLIST);
176 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "7fffffffffffffff", EC_TK_ENDLIST);
177 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "0x7fffffffffffffff", EC_TK_ENDLIST);
178 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "-2", EC_TK_ENDLIST);
180 p = ec_tk_parse(tk, "10");
181 s = ec_parsed_tk_to_string(p);
182 EC_TEST_ASSERT(s != NULL && ec_tk_int_getval(tk, s) == 16);
183 ec_parsed_tk_free(p);
186 tk = ec_tk_int_new(NULL, LLONG_MIN, 0, 10);
188 ec_log(EC_LOG_ERR, "cannot create tk\n");
191 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "0", EC_TK_ENDLIST);
192 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "-1", EC_TK_ENDLIST);
193 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "-9223372036854775808",
195 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "0x0", EC_TK_ENDLIST);
196 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "1", EC_TK_ENDLIST);
199 /* test completion */
200 tk = ec_tk_int_new(NULL, 0, 10, 0);
202 ec_log(EC_LOG_ERR, "cannot create tk\n");
205 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
209 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
213 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
222 static struct ec_test ec_tk_int_test = {
224 .test = ec_tk_int_testcase,
227 EC_REGISTER_TEST(ec_tk_int_test);