check-qstring.c 2.4 KB
Newer Older
L
Luiz Capitulino 已提交
1 2 3 4 5 6 7
/*
 * QString unit-tests.
 *
 * Copyright (C) 2009 Red Hat Inc.
 *
 * Authors:
 *  Luiz Capitulino <lcapitulino@redhat.com>
L
Luiz Capitulino 已提交
8 9 10
 *
 * 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.
L
Luiz Capitulino 已提交
11
 */
P
Peter Maydell 已提交
12
#include "qemu/osdep.h"
L
Luiz Capitulino 已提交
13

14
#include "qapi/qmp/qstring.h"
L
Luiz Capitulino 已提交
15 16 17 18 19 20 21 22
#include "qemu-common.h"

/*
 * Public Interface test-cases
 *
 * (with some violations to access 'private' data)
 */

23
static void qstring_from_str_test(void)
L
Luiz Capitulino 已提交
24 25 26 27 28
{
    QString *qstring;
    const char *str = "QEMU";

    qstring = qstring_from_str(str);
29 30 31 32
    g_assert(qstring != NULL);
    g_assert(qstring->base.refcnt == 1);
    g_assert(strcmp(str, qstring->string) == 0);
    g_assert(qobject_type(QOBJECT(qstring)) == QTYPE_QSTRING);
L
Luiz Capitulino 已提交
33 34

    // destroy doesn't exit yet
35 36
    g_free(qstring->string);
    g_free(qstring);
L
Luiz Capitulino 已提交
37 38
}

39
static void qstring_destroy_test(void)
L
Luiz Capitulino 已提交
40 41 42 43 44
{
    QString *qstring = qstring_from_str("destroy test");
    QDECREF(qstring);
}

45
static void qstring_get_str_test(void)
L
Luiz Capitulino 已提交
46 47 48 49 50 51 52
{
    QString *qstring;
    const char *ret_str;
    const char *str = "QEMU/KVM";

    qstring = qstring_from_str(str);
    ret_str = qstring_get_str(qstring);
53
    g_assert(strcmp(ret_str, str) == 0);
L
Luiz Capitulino 已提交
54 55 56 57

    QDECREF(qstring);
}

58
static void qstring_append_chr_test(void)
59 60 61 62 63 64 65 66 67 68
{
    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]);

69
    g_assert(strcmp(str, qstring_get_str(qstring)) == 0);
70 71 72
    QDECREF(qstring);
}

73
static void qstring_from_substr_test(void)
74 75 76 77
{
    QString *qs;

    qs = qstring_from_substr("virtualization", 3, 9);
78 79
    g_assert(qs != NULL);
    g_assert(strcmp(qstring_get_str(qs), "tualiza") == 0);
80 81 82 83 84

    QDECREF(qs);
}


85
static void qobject_to_qstring_test(void)
L
Luiz Capitulino 已提交
86 87 88 89
{
    QString *qstring;

    qstring = qstring_from_str("foo");
90
    g_assert(qobject_to_qstring(QOBJECT(qstring)) == qstring);
L
Luiz Capitulino 已提交
91 92 93 94

    QDECREF(qstring);
}

95
int main(int argc, char **argv)
L
Luiz Capitulino 已提交
96
{
97
    g_test_init(&argc, &argv, NULL);
L
Luiz Capitulino 已提交
98

99 100 101 102 103 104
    g_test_add_func("/public/from_str", qstring_from_str_test);
    g_test_add_func("/public/destroy", qstring_destroy_test);
    g_test_add_func("/public/get_str", qstring_get_str_test);
    g_test_add_func("/public/append_chr", qstring_append_chr_test);
    g_test_add_func("/public/from_substr", qstring_from_substr_test);
    g_test_add_func("/public/to_qstring", qobject_to_qstring_test);
L
Luiz Capitulino 已提交
105

106
    return g_test_run();
L
Luiz Capitulino 已提交
107
}