-
Notifications
You must be signed in to change notification settings - Fork 26
/
check-qstring.c
134 lines (106 loc) · 2.82 KB
/
check-qstring.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* QString unit-tests.
*
* Copyright (C) 2009 Red Hat Inc.
*
* Authors:
* Luiz Capitulino <[email protected]>
*
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
* See the COPYING.LIB file in the top-level directory.
*/
#include <check.h>
#include "qstring.h"
#include "qemu-common.h"
/*
* Public Interface test-cases
*
* (with some violations to access 'private' data)
*/
START_TEST(qstring_from_str_test)
{
QString *qstring;
const char *str = "QEMU";
qstring = qstring_from_str(str);
fail_unless(qstring != NULL);
fail_unless(qstring->base.refcnt == 1);
fail_unless(strcmp(str, qstring->string) == 0);
fail_unless(qobject_type(QOBJECT(qstring)) == QTYPE_QSTRING);
// destroy doesn't exit yet
g_free(qstring->string);
g_free(qstring);
}
END_TEST
START_TEST(qstring_destroy_test)
{
QString *qstring = qstring_from_str("destroy test");
QDECREF(qstring);
}
END_TEST
START_TEST(qstring_get_str_test)
{
QString *qstring;
const char *ret_str;
const char *str = "QEMU/KVM";
qstring = qstring_from_str(str);
ret_str = qstring_get_str(qstring);
fail_unless(strcmp(ret_str, str) == 0);
QDECREF(qstring);
}
END_TEST
START_TEST(qstring_append_chr_test)
{
int i;
QString *qstring;
const char *str = "qstring append char unit-test";
qstring = qstring_new();
for (i = 0; str[i]; i++)
qstring_append_chr(qstring, str[i]);
fail_unless(strcmp(str, qstring_get_str(qstring)) == 0);
QDECREF(qstring);
}
END_TEST
START_TEST(qstring_from_substr_test)
{
QString *qs;
qs = qstring_from_substr("virtualization", 3, 9);
fail_unless(qs != NULL);
fail_unless(strcmp(qstring_get_str(qs), "tualiza") == 0);
QDECREF(qs);
}
END_TEST
START_TEST(qobject_to_qstring_test)
{
QString *qstring;
qstring = qstring_from_str("foo");
fail_unless(qobject_to_qstring(QOBJECT(qstring)) == qstring);
QDECREF(qstring);
}
END_TEST
static Suite *qstring_suite(void)
{
Suite *s;
TCase *qstring_public_tcase;
s = suite_create("QString test-suite");
qstring_public_tcase = tcase_create("Public Interface");
suite_add_tcase(s, qstring_public_tcase);
tcase_add_test(qstring_public_tcase, qstring_from_str_test);
tcase_add_test(qstring_public_tcase, qstring_destroy_test);
tcase_add_test(qstring_public_tcase, qstring_get_str_test);
tcase_add_test(qstring_public_tcase, qstring_append_chr_test);
tcase_add_test(qstring_public_tcase, qstring_from_substr_test);
tcase_add_test(qstring_public_tcase, qobject_to_qstring_test);
return s;
}
int main(void)
{
int nf;
Suite *s;
SRunner *sr;
s = qstring_suite();
sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
nf = srunner_ntests_failed(sr);
srunner_free(sr);
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}