xref: /openbmc/qemu/tests/unit/check-qstring.c (revision a75b180f)
1 /*
2  * QString unit-tests.
3  *
4  * Copyright (C) 2009 Red Hat Inc.
5  *
6  * Authors:
7  *  Luiz Capitulino <lcapitulino@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10  * See the COPYING.LIB file in the top-level directory.
11  */
12 #include "qemu/osdep.h"
13 
14 #include "qapi/qmp/qstring.h"
15 
16 /*
17  * Public Interface test-cases
18  *
19  * (with some violations to access 'private' data)
20  */
21 
22 static void qstring_from_str_test(void)
23 {
24     QString *qstring;
25     const char *str = "QEMU";
26 
27     qstring = qstring_from_str(str);
28     g_assert(qstring != NULL);
29     g_assert(qstring->base.refcnt == 1);
30     g_assert(strcmp(str, qstring->string) == 0);
31     g_assert(qobject_type(QOBJECT(qstring)) == QTYPE_QSTRING);
32 
33     qobject_unref(qstring);
34 }
35 
36 static void qstring_get_str_test(void)
37 {
38     QString *qstring;
39     const char *ret_str;
40     const char *str = "QEMU/KVM";
41 
42     qstring = qstring_from_str(str);
43     ret_str = qstring_get_str(qstring);
44     g_assert(strcmp(ret_str, str) == 0);
45 
46     qobject_unref(qstring);
47 }
48 
49 static void qstring_from_substr_test(void)
50 {
51     QString *qs;
52 
53     qs = qstring_from_substr("virtualization", 3, 10);
54     g_assert(qs != NULL);
55     g_assert(strcmp(qstring_get_str(qs), "tualiza") == 0);
56 
57     qobject_unref(qs);
58 }
59 
60 
61 static void qobject_to_qstring_test(void)
62 {
63     QString *qstring;
64 
65     qstring = qstring_from_str("foo");
66     g_assert(qobject_to(QString, QOBJECT(qstring)) == qstring);
67 
68     qobject_unref(qstring);
69 }
70 
71 int main(int argc, char **argv)
72 {
73     g_test_init(&argc, &argv, NULL);
74 
75     g_test_add_func("/public/from_str", qstring_from_str_test);
76     g_test_add_func("/public/get_str", qstring_get_str_test);
77     g_test_add_func("/public/from_substr", qstring_from_substr_test);
78     g_test_add_func("/public/to_qstring", qobject_to_qstring_test);
79 
80     return g_test_run();
81 }
82