virtio-blk: cleanup: QOM cast
[qemu/cris-port.git] / tests / test-cutils.c
blob2a4556d3aafc87baeb242166bab86b968a8dfce9
1 /*
2 * cutils.c unit-tests
4 * Copyright (C) 2013 Red Hat Inc.
6 * Authors:
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
25 * THE SOFTWARE.
28 #include <glib.h>
29 #include <errno.h>
30 #include <string.h>
32 #include "qemu-common.h"
35 static void test_parse_uint_null(void)
37 unsigned long long i = 999;
38 char f = 'X';
39 char *endptr = &f;
40 int r;
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;
52 char f = 'X';
53 char *endptr = &f;
54 const char *str = "";
55 int r;
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;
67 char f = 'X';
68 char *endptr = &f;
69 const char *str = " \t ";
70 int r;
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;
83 char f = 'X';
84 char *endptr = &f;
85 const char *str = " \t xxx";
86 int r;
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;
99 char f = 'X';
100 char *endptr = &f;
101 const char *str = "123xxx";
102 int r;
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;
114 char f = 'X';
115 char *endptr = &f;
116 const char *str = "123";
117 int r;
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;
129 char f = 'X';
130 char *endptr = &f;
131 const char *str = "0123";
132 int r;
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;
144 char f = 'X';
145 char *endptr = &f;
146 const char *str = "0123";
147 int r;
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;
160 char f = 'X';
161 char *endptr = &f;
162 char *str = g_strdup_printf("%llu", (unsigned long long)LLONG_MAX + 1);
163 int r;
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));
171 g_free(str);
174 static void test_parse_uint_overflow(void)
176 unsigned long long i = 999;
177 char f = 'X';
178 char *endptr = &f;
179 const char *str = "99999999999999999999999999999999999999";
180 int r;
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;
192 char f = 'X';
193 char *endptr = &f;
194 const char *str = " \t -321";
195 int r;
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";
209 int r;
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";
221 int r;
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);
250 return g_test_run();