Eduardo Habkost | e3f9fe2 | 2013-02-04 16:27:45 -0200 | [diff] [blame] | 1 | /* |
| 2 | * cutils.c unit-tests |
| 3 | * |
| 4 | * Copyright (C) 2013 Red Hat Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Eduardo Habkost <ehabkost@redhat.com> |
| 8 | * |
| 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: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | * |
| 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. |
| 26 | */ |
| 27 | |
| 28 | #include <glib.h> |
| 29 | #include <errno.h> |
| 30 | #include <string.h> |
| 31 | |
| 32 | #include "qemu-common.h" |
| 33 | |
| 34 | |
| 35 | static void test_parse_uint_null(void) |
| 36 | { |
| 37 | unsigned long long i = 999; |
| 38 | char f = 'X'; |
| 39 | char *endptr = &f; |
| 40 | int r; |
| 41 | |
| 42 | r = parse_uint(NULL, &i, &endptr, 0); |
| 43 | |
| 44 | g_assert_cmpint(r, ==, -EINVAL); |
| 45 | g_assert_cmpint(i, ==, 0); |
| 46 | g_assert(endptr == NULL); |
| 47 | } |
| 48 | |
| 49 | static void test_parse_uint_empty(void) |
| 50 | { |
| 51 | unsigned long long i = 999; |
| 52 | char f = 'X'; |
| 53 | char *endptr = &f; |
| 54 | const char *str = ""; |
| 55 | int r; |
| 56 | |
| 57 | r = parse_uint(str, &i, &endptr, 0); |
| 58 | |
| 59 | g_assert_cmpint(r, ==, -EINVAL); |
| 60 | g_assert_cmpint(i, ==, 0); |
| 61 | g_assert(endptr == str); |
| 62 | } |
| 63 | |
| 64 | static void test_parse_uint_whitespace(void) |
| 65 | { |
| 66 | unsigned long long i = 999; |
| 67 | char f = 'X'; |
| 68 | char *endptr = &f; |
| 69 | const char *str = " \t "; |
| 70 | int r; |
| 71 | |
| 72 | r = parse_uint(str, &i, &endptr, 0); |
| 73 | |
| 74 | g_assert_cmpint(r, ==, -EINVAL); |
| 75 | g_assert_cmpint(i, ==, 0); |
| 76 | g_assert(endptr == str); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | static void test_parse_uint_invalid(void) |
| 81 | { |
| 82 | unsigned long long i = 999; |
| 83 | char f = 'X'; |
| 84 | char *endptr = &f; |
| 85 | const char *str = " \t xxx"; |
| 86 | int r; |
| 87 | |
| 88 | r = parse_uint(str, &i, &endptr, 0); |
| 89 | |
| 90 | g_assert_cmpint(r, ==, -EINVAL); |
| 91 | g_assert_cmpint(i, ==, 0); |
| 92 | g_assert(endptr == str); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | static void test_parse_uint_trailing(void) |
| 97 | { |
| 98 | unsigned long long i = 999; |
| 99 | char f = 'X'; |
| 100 | char *endptr = &f; |
| 101 | const char *str = "123xxx"; |
| 102 | int r; |
| 103 | |
| 104 | r = parse_uint(str, &i, &endptr, 0); |
| 105 | |
| 106 | g_assert_cmpint(r, ==, 0); |
| 107 | g_assert_cmpint(i, ==, 123); |
| 108 | g_assert(endptr == str + 3); |
| 109 | } |
| 110 | |
| 111 | static void test_parse_uint_correct(void) |
| 112 | { |
| 113 | unsigned long long i = 999; |
| 114 | char f = 'X'; |
| 115 | char *endptr = &f; |
| 116 | const char *str = "123"; |
| 117 | int r; |
| 118 | |
| 119 | r = parse_uint(str, &i, &endptr, 0); |
| 120 | |
| 121 | g_assert_cmpint(r, ==, 0); |
| 122 | g_assert_cmpint(i, ==, 123); |
| 123 | g_assert(endptr == str + strlen(str)); |
| 124 | } |
| 125 | |
| 126 | static void test_parse_uint_octal(void) |
| 127 | { |
| 128 | unsigned long long i = 999; |
| 129 | char f = 'X'; |
| 130 | char *endptr = &f; |
| 131 | const char *str = "0123"; |
| 132 | int r; |
| 133 | |
| 134 | r = parse_uint(str, &i, &endptr, 0); |
| 135 | |
| 136 | g_assert_cmpint(r, ==, 0); |
| 137 | g_assert_cmpint(i, ==, 0123); |
| 138 | g_assert(endptr == str + strlen(str)); |
| 139 | } |
| 140 | |
| 141 | static void test_parse_uint_decimal(void) |
| 142 | { |
| 143 | unsigned long long i = 999; |
| 144 | char f = 'X'; |
| 145 | char *endptr = &f; |
| 146 | const char *str = "0123"; |
| 147 | int r; |
| 148 | |
| 149 | r = parse_uint(str, &i, &endptr, 10); |
| 150 | |
| 151 | g_assert_cmpint(r, ==, 0); |
| 152 | g_assert_cmpint(i, ==, 123); |
| 153 | g_assert(endptr == str + strlen(str)); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | static void test_parse_uint_llong_max(void) |
| 158 | { |
| 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; |
| 164 | |
| 165 | r = parse_uint(str, &i, &endptr, 0); |
| 166 | |
| 167 | g_assert_cmpint(r, ==, 0); |
| 168 | g_assert_cmpint(i, ==, (unsigned long long)LLONG_MAX + 1); |
| 169 | g_assert(endptr == str + strlen(str)); |
| 170 | |
| 171 | g_free(str); |
| 172 | } |
| 173 | |
| 174 | static void test_parse_uint_overflow(void) |
| 175 | { |
| 176 | unsigned long long i = 999; |
| 177 | char f = 'X'; |
| 178 | char *endptr = &f; |
| 179 | const char *str = "99999999999999999999999999999999999999"; |
| 180 | int r; |
| 181 | |
| 182 | r = parse_uint(str, &i, &endptr, 0); |
| 183 | |
| 184 | g_assert_cmpint(r, ==, -ERANGE); |
| 185 | g_assert_cmpint(i, ==, ULLONG_MAX); |
| 186 | g_assert(endptr == str + strlen(str)); |
| 187 | } |
| 188 | |
| 189 | static void test_parse_uint_negative(void) |
| 190 | { |
| 191 | unsigned long long i = 999; |
| 192 | char f = 'X'; |
| 193 | char *endptr = &f; |
| 194 | const char *str = " \t -321"; |
| 195 | int r; |
| 196 | |
| 197 | r = parse_uint(str, &i, &endptr, 0); |
| 198 | |
| 199 | g_assert_cmpint(r, ==, -ERANGE); |
| 200 | g_assert_cmpint(i, ==, 0); |
| 201 | g_assert(endptr == str + strlen(str)); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | static void test_parse_uint_full_trailing(void) |
| 206 | { |
| 207 | unsigned long long i = 999; |
| 208 | const char *str = "123xxx"; |
| 209 | int r; |
| 210 | |
| 211 | r = parse_uint_full(str, &i, 0); |
| 212 | |
| 213 | g_assert_cmpint(r, ==, -EINVAL); |
| 214 | g_assert_cmpint(i, ==, 0); |
| 215 | } |
| 216 | |
| 217 | static void test_parse_uint_full_correct(void) |
| 218 | { |
| 219 | unsigned long long i = 999; |
| 220 | const char *str = "123"; |
| 221 | int r; |
| 222 | |
| 223 | r = parse_uint_full(str, &i, 0); |
| 224 | |
| 225 | g_assert_cmpint(r, ==, 0); |
| 226 | g_assert_cmpint(i, ==, 123); |
| 227 | } |
| 228 | |
| 229 | int main(int argc, char **argv) |
| 230 | { |
| 231 | g_test_init(&argc, &argv, NULL); |
| 232 | |
| 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); |
| 249 | |
| 250 | return g_test_run(); |
| 251 | } |