xref: /qemu/tests/unit/test-qapi-util.c (revision 6c873d1149d47201dbb71f6e04791447605a17e1)
1 /*
2  * Unit tests for QAPI utility functions
3  *
4  * Copyright (C) 2017 Red Hat Inc.
5  *
6  * Authors:
7  *  Markus Armbruster <armbru@redhat.com>,
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10  * See the COPYING file in the top-level directory.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qapi/util.h"
16 #include "test-qapi-types.h"
17 
18 static void test_qapi_enum_parse(void)
19 {
20     Error *err = NULL;
21     int ret;
22 
23     ret = qapi_enum_parse(QType_lookup, NULL, QTYPE__MAX, QTYPE_NONE,
24                           &error_abort);
25     g_assert_cmpint(ret, ==, QTYPE_NONE);
26 
27     ret = qapi_enum_parse(QType_lookup, "junk", QTYPE__MAX, -1,
28                           NULL);
29     g_assert_cmpint(ret, ==, -1);
30 
31     ret = qapi_enum_parse(QType_lookup, "junk", QTYPE__MAX, -1,
32                           &err);
33     error_free_or_abort(&err);
34 
35     ret = qapi_enum_parse(QType_lookup, "none", QTYPE__MAX, -1,
36                           &error_abort);
37     g_assert_cmpint(ret, ==, QTYPE_NONE);
38 
39     ret = qapi_enum_parse(QType_lookup, QType_lookup[QTYPE__MAX - 1],
40                           QTYPE__MAX, QTYPE__MAX - 1,
41                           &error_abort);
42     g_assert_cmpint(ret, ==, QTYPE__MAX - 1);
43 }
44 
45 int main(int argc, char *argv[])
46 {
47     g_test_init(&argc, &argv, NULL);
48     g_test_add_func("/qapi/util/qapi_enum_parse", test_qapi_enum_parse);
49     g_test_run();
50     return 0;
51 }
52