4 * Copyright (C) 2013 Red Hat Inc.
7 * Eduardo Habkost <ehabkost@redhat.com>
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 #include "qemu-common.h"
35 static void test_parse_uint_null(void)
37 unsigned long long i
= 999;
42 r
= parse_uint(NULL
, &i
, &endptr
, 0);
44 g_assert_cmpint(r
, ==, -EINVAL
);
45 g_assert_cmpint(i
, ==, 0);
46 g_assert(endptr
== NULL
);
49 static void test_parse_uint_empty(void)
51 unsigned long long i
= 999;
57 r
= parse_uint(str
, &i
, &endptr
, 0);
59 g_assert_cmpint(r
, ==, -EINVAL
);
60 g_assert_cmpint(i
, ==, 0);
61 g_assert(endptr
== str
);
64 static void test_parse_uint_whitespace(void)
66 unsigned long long i
= 999;
69 const char *str
= " \t ";
72 r
= parse_uint(str
, &i
, &endptr
, 0);
74 g_assert_cmpint(r
, ==, -EINVAL
);
75 g_assert_cmpint(i
, ==, 0);
76 g_assert(endptr
== str
);
80 static void test_parse_uint_invalid(void)
82 unsigned long long i
= 999;
85 const char *str
= " \t xxx";
88 r
= parse_uint(str
, &i
, &endptr
, 0);
90 g_assert_cmpint(r
, ==, -EINVAL
);
91 g_assert_cmpint(i
, ==, 0);
92 g_assert(endptr
== str
);
96 static void test_parse_uint_trailing(void)
98 unsigned long long i
= 999;
101 const char *str
= "123xxx";
104 r
= parse_uint(str
, &i
, &endptr
, 0);
106 g_assert_cmpint(r
, ==, 0);
107 g_assert_cmpint(i
, ==, 123);
108 g_assert(endptr
== str
+ 3);
111 static void test_parse_uint_correct(void)
113 unsigned long long i
= 999;
116 const char *str
= "123";
119 r
= parse_uint(str
, &i
, &endptr
, 0);
121 g_assert_cmpint(r
, ==, 0);
122 g_assert_cmpint(i
, ==, 123);
123 g_assert(endptr
== str
+ strlen(str
));
126 static void test_parse_uint_octal(void)
128 unsigned long long i
= 999;
131 const char *str
= "0123";
134 r
= parse_uint(str
, &i
, &endptr
, 0);
136 g_assert_cmpint(r
, ==, 0);
137 g_assert_cmpint(i
, ==, 0123);
138 g_assert(endptr
== str
+ strlen(str
));
141 static void test_parse_uint_decimal(void)
143 unsigned long long i
= 999;
146 const char *str
= "0123";
149 r
= parse_uint(str
, &i
, &endptr
, 10);
151 g_assert_cmpint(r
, ==, 0);
152 g_assert_cmpint(i
, ==, 123);
153 g_assert(endptr
== str
+ strlen(str
));
157 static void test_parse_uint_llong_max(void)
159 unsigned long long i
= 999;
162 char *str
= g_strdup_printf("%llu", (unsigned long long)LLONG_MAX
+ 1);
165 r
= parse_uint(str
, &i
, &endptr
, 0);
167 g_assert_cmpint(r
, ==, 0);
168 g_assert_cmpint(i
, ==, (unsigned long long)LLONG_MAX
+ 1);
169 g_assert(endptr
== str
+ strlen(str
));
174 static void test_parse_uint_overflow(void)
176 unsigned long long i
= 999;
179 const char *str
= "99999999999999999999999999999999999999";
182 r
= parse_uint(str
, &i
, &endptr
, 0);
184 g_assert_cmpint(r
, ==, -ERANGE
);
185 g_assert_cmpint(i
, ==, ULLONG_MAX
);
186 g_assert(endptr
== str
+ strlen(str
));
189 static void test_parse_uint_negative(void)
191 unsigned long long i
= 999;
194 const char *str
= " \t -321";
197 r
= parse_uint(str
, &i
, &endptr
, 0);
199 g_assert_cmpint(r
, ==, -ERANGE
);
200 g_assert_cmpint(i
, ==, 0);
201 g_assert(endptr
== str
+ strlen(str
));
205 static void test_parse_uint_full_trailing(void)
207 unsigned long long i
= 999;
208 const char *str
= "123xxx";
211 r
= parse_uint_full(str
, &i
, 0);
213 g_assert_cmpint(r
, ==, -EINVAL
);
214 g_assert_cmpint(i
, ==, 0);
217 static void test_parse_uint_full_correct(void)
219 unsigned long long i
= 999;
220 const char *str
= "123";
223 r
= parse_uint_full(str
, &i
, 0);
225 g_assert_cmpint(r
, ==, 0);
226 g_assert_cmpint(i
, ==, 123);
229 int main(int argc
, char **argv
)
231 g_test_init(&argc
, &argv
, NULL
);
233 g_test_add_func("/cutils/parse_uint/null", test_parse_uint_null
);
234 g_test_add_func("/cutils/parse_uint/empty", test_parse_uint_empty
);
235 g_test_add_func("/cutils/parse_uint/whitespace",
236 test_parse_uint_whitespace
);
237 g_test_add_func("/cutils/parse_uint/invalid", test_parse_uint_invalid
);
238 g_test_add_func("/cutils/parse_uint/trailing", test_parse_uint_trailing
);
239 g_test_add_func("/cutils/parse_uint/correct", test_parse_uint_correct
);
240 g_test_add_func("/cutils/parse_uint/octal", test_parse_uint_octal
);
241 g_test_add_func("/cutils/parse_uint/decimal", test_parse_uint_decimal
);
242 g_test_add_func("/cutils/parse_uint/llong_max", test_parse_uint_llong_max
);
243 g_test_add_func("/cutils/parse_uint/overflow", test_parse_uint_overflow
);
244 g_test_add_func("/cutils/parse_uint/negative", test_parse_uint_negative
);
245 g_test_add_func("/cutils/parse_uint_full/trailing",
246 test_parse_uint_full_trailing
);
247 g_test_add_func("/cutils/parse_uint_full/correct",
248 test_parse_uint_full_correct
);