]>
Commit | Line | Data |
---|---|---|
5de65a07 LC |
1 | /* |
2 | * QString unit-tests. | |
3 | * | |
4 | * Copyright (C) 2009 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Luiz Capitulino <lcapitulino@redhat.com> | |
41836a9f LC |
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. | |
5de65a07 | 11 | */ |
515aa3c5 | 12 | #include <glib.h> |
5de65a07 | 13 | |
7b1b5d19 | 14 | #include "qapi/qmp/qstring.h" |
5de65a07 LC |
15 | #include "qemu-common.h" |
16 | ||
17 | /* | |
18 | * Public Interface test-cases | |
19 | * | |
20 | * (with some violations to access 'private' data) | |
21 | */ | |
22 | ||
0ac7cc2a | 23 | static void qstring_from_str_test(void) |
5de65a07 LC |
24 | { |
25 | QString *qstring; | |
26 | const char *str = "QEMU"; | |
27 | ||
28 | qstring = qstring_from_str(str); | |
0ac7cc2a AL |
29 | g_assert(qstring != NULL); |
30 | g_assert(qstring->base.refcnt == 1); | |
31 | g_assert(strcmp(str, qstring->string) == 0); | |
32 | g_assert(qobject_type(QOBJECT(qstring)) == QTYPE_QSTRING); | |
5de65a07 LC |
33 | |
34 | // destroy doesn't exit yet | |
7267c094 AL |
35 | g_free(qstring->string); |
36 | g_free(qstring); | |
5de65a07 | 37 | } |
5de65a07 | 38 | |
0ac7cc2a | 39 | static void qstring_destroy_test(void) |
5de65a07 LC |
40 | { |
41 | QString *qstring = qstring_from_str("destroy test"); | |
42 | QDECREF(qstring); | |
43 | } | |
5de65a07 | 44 | |
0ac7cc2a | 45 | static void qstring_get_str_test(void) |
5de65a07 LC |
46 | { |
47 | QString *qstring; | |
48 | const char *ret_str; | |
49 | const char *str = "QEMU/KVM"; | |
50 | ||
51 | qstring = qstring_from_str(str); | |
52 | ret_str = qstring_get_str(qstring); | |
0ac7cc2a | 53 | g_assert(strcmp(ret_str, str) == 0); |
5de65a07 LC |
54 | |
55 | QDECREF(qstring); | |
56 | } | |
5de65a07 | 57 | |
0ac7cc2a | 58 | static void qstring_append_chr_test(void) |
f08932c8 LC |
59 | { |
60 | int i; | |
61 | QString *qstring; | |
62 | const char *str = "qstring append char unit-test"; | |
63 | ||
64 | qstring = qstring_new(); | |
65 | ||
66 | for (i = 0; str[i]; i++) | |
67 | qstring_append_chr(qstring, str[i]); | |
68 | ||
0ac7cc2a | 69 | g_assert(strcmp(str, qstring_get_str(qstring)) == 0); |
f08932c8 LC |
70 | QDECREF(qstring); |
71 | } | |
f08932c8 | 72 | |
0ac7cc2a | 73 | static void qstring_from_substr_test(void) |
64084a03 LC |
74 | { |
75 | QString *qs; | |
76 | ||
77 | qs = qstring_from_substr("virtualization", 3, 9); | |
0ac7cc2a AL |
78 | g_assert(qs != NULL); |
79 | g_assert(strcmp(qstring_get_str(qs), "tualiza") == 0); | |
64084a03 LC |
80 | |
81 | QDECREF(qs); | |
82 | } | |
64084a03 LC |
83 | |
84 | ||
0ac7cc2a | 85 | static void qobject_to_qstring_test(void) |
5de65a07 LC |
86 | { |
87 | QString *qstring; | |
88 | ||
89 | qstring = qstring_from_str("foo"); | |
0ac7cc2a | 90 | g_assert(qobject_to_qstring(QOBJECT(qstring)) == qstring); |
5de65a07 LC |
91 | |
92 | QDECREF(qstring); | |
93 | } | |
5de65a07 | 94 | |
0ac7cc2a | 95 | int main(int argc, char **argv) |
5de65a07 | 96 | { |
0ac7cc2a | 97 | g_test_init(&argc, &argv, NULL); |
5de65a07 | 98 | |
0ac7cc2a AL |
99 | g_test_add_func("/public/from_str", qstring_from_str_test); |
100 | g_test_add_func("/public/destroy", qstring_destroy_test); | |
101 | g_test_add_func("/public/get_str", qstring_get_str_test); | |
102 | g_test_add_func("/public/append_chr", qstring_append_chr_test); | |
103 | g_test_add_func("/public/from_substr", qstring_from_substr_test); | |
104 | g_test_add_func("/public/to_qstring", qobject_to_qstring_test); | |
5de65a07 | 105 | |
0ac7cc2a | 106 | return g_test_run(); |
5de65a07 | 107 | } |