]>
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 LC |
11 | */ |
12 | #include <check.h> | |
13 | ||
14 | #include "qstring.h" | |
15 | #include "qemu-common.h" | |
16 | ||
17 | /* | |
18 | * Public Interface test-cases | |
19 | * | |
20 | * (with some violations to access 'private' data) | |
21 | */ | |
22 | ||
23 | START_TEST(qstring_from_str_test) | |
24 | { | |
25 | QString *qstring; | |
26 | const char *str = "QEMU"; | |
27 | ||
28 | qstring = qstring_from_str(str); | |
29 | fail_unless(qstring != NULL); | |
30 | fail_unless(qstring->base.refcnt == 1); | |
31 | fail_unless(strcmp(str, qstring->string) == 0); | |
32 | fail_unless(qobject_type(QOBJECT(qstring)) == QTYPE_QSTRING); | |
33 | ||
34 | // destroy doesn't exit yet | |
35 | qemu_free(qstring->string); | |
36 | qemu_free(qstring); | |
37 | } | |
38 | END_TEST | |
39 | ||
40 | START_TEST(qstring_destroy_test) | |
41 | { | |
42 | QString *qstring = qstring_from_str("destroy test"); | |
43 | QDECREF(qstring); | |
44 | } | |
45 | END_TEST | |
46 | ||
47 | START_TEST(qstring_get_str_test) | |
48 | { | |
49 | QString *qstring; | |
50 | const char *ret_str; | |
51 | const char *str = "QEMU/KVM"; | |
52 | ||
53 | qstring = qstring_from_str(str); | |
54 | ret_str = qstring_get_str(qstring); | |
55 | fail_unless(strcmp(ret_str, str) == 0); | |
56 | ||
57 | QDECREF(qstring); | |
58 | } | |
59 | END_TEST | |
60 | ||
f08932c8 LC |
61 | START_TEST(qstring_append_chr_test) |
62 | { | |
63 | int i; | |
64 | QString *qstring; | |
65 | const char *str = "qstring append char unit-test"; | |
66 | ||
67 | qstring = qstring_new(); | |
68 | ||
69 | for (i = 0; str[i]; i++) | |
70 | qstring_append_chr(qstring, str[i]); | |
71 | ||
72 | fail_unless(strcmp(str, qstring_get_str(qstring)) == 0); | |
73 | QDECREF(qstring); | |
74 | } | |
75 | END_TEST | |
76 | ||
64084a03 LC |
77 | START_TEST(qstring_from_substr_test) |
78 | { | |
79 | QString *qs; | |
80 | ||
81 | qs = qstring_from_substr("virtualization", 3, 9); | |
82 | fail_unless(qs != NULL); | |
83 | fail_unless(strcmp(qstring_get_str(qs), "tualiza") == 0); | |
84 | ||
85 | QDECREF(qs); | |
86 | } | |
87 | END_TEST | |
88 | ||
89 | ||
5de65a07 LC |
90 | START_TEST(qobject_to_qstring_test) |
91 | { | |
92 | QString *qstring; | |
93 | ||
94 | qstring = qstring_from_str("foo"); | |
95 | fail_unless(qobject_to_qstring(QOBJECT(qstring)) == qstring); | |
96 | ||
97 | QDECREF(qstring); | |
98 | } | |
99 | END_TEST | |
100 | ||
101 | static Suite *qstring_suite(void) | |
102 | { | |
103 | Suite *s; | |
104 | TCase *qstring_public_tcase; | |
105 | ||
106 | s = suite_create("QString test-suite"); | |
107 | ||
108 | qstring_public_tcase = tcase_create("Public Interface"); | |
109 | suite_add_tcase(s, qstring_public_tcase); | |
110 | tcase_add_test(qstring_public_tcase, qstring_from_str_test); | |
111 | tcase_add_test(qstring_public_tcase, qstring_destroy_test); | |
112 | tcase_add_test(qstring_public_tcase, qstring_get_str_test); | |
f08932c8 | 113 | tcase_add_test(qstring_public_tcase, qstring_append_chr_test); |
64084a03 | 114 | tcase_add_test(qstring_public_tcase, qstring_from_substr_test); |
5de65a07 LC |
115 | tcase_add_test(qstring_public_tcase, qobject_to_qstring_test); |
116 | ||
117 | return s; | |
118 | } | |
119 | ||
120 | int main(void) | |
121 | { | |
122 | int nf; | |
123 | Suite *s; | |
124 | SRunner *sr; | |
125 | ||
126 | s = qstring_suite(); | |
127 | sr = srunner_create(s); | |
128 | ||
129 | srunner_run_all(sr, CK_NORMAL); | |
130 | nf = srunner_ntests_failed(sr); | |
131 | srunner_free(sr); | |
132 | ||
133 | return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE; | |
134 | } |