qemu/tests/check-qstring.c
<<
>>
Prefs
   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 <glib.h>
  13
  14#include "qapi/qmp/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
  23static void qstring_from_str_test(void)
  24{
  25    QString *qstring;
  26    const char *str = "QEMU";
  27
  28    qstring = qstring_from_str(str);
  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);
  33
  34    // destroy doesn't exit yet
  35    g_free(qstring->string);
  36    g_free(qstring);
  37}
  38
  39static void qstring_destroy_test(void)
  40{
  41    QString *qstring = qstring_from_str("destroy test");
  42    QDECREF(qstring);
  43}
  44
  45static void qstring_get_str_test(void)
  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);
  53    g_assert(strcmp(ret_str, str) == 0);
  54
  55    QDECREF(qstring);
  56}
  57
  58static void qstring_append_chr_test(void)
  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
  69    g_assert(strcmp(str, qstring_get_str(qstring)) == 0);
  70    QDECREF(qstring);
  71}
  72
  73static void qstring_from_substr_test(void)
  74{
  75    QString *qs;
  76
  77    qs = qstring_from_substr("virtualization", 3, 9);
  78    g_assert(qs != NULL);
  79    g_assert(strcmp(qstring_get_str(qs), "tualiza") == 0);
  80
  81    QDECREF(qs);
  82}
  83
  84
  85static void qobject_to_qstring_test(void)
  86{
  87    QString *qstring;
  88
  89    qstring = qstring_from_str("foo");
  90    g_assert(qobject_to_qstring(QOBJECT(qstring)) == qstring);
  91
  92    QDECREF(qstring);
  93}
  94
  95int main(int argc, char **argv)
  96{
  97    g_test_init(&argc, &argv, NULL);
  98
  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);
 105
 106    return g_test_run();
 107}
 108