Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 1 | /* |
| 2 | * QInt unit-tests. |
| 3 | * |
| 4 | * Copyright (C) 2009 Red Hat Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Luiz Capitulino <lcapitulino@redhat.com> |
Luiz Capitulino | 41836a9 | 2010-05-12 16:34:42 -0300 | [diff] [blame] | 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. |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 11 | */ |
Anthony Liguori | 65cdadd | 2012-01-10 13:10:46 -0600 | [diff] [blame] | 12 | #include <glib.h> |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 13 | |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 14 | #include "qapi/qmp/qint.h" |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 15 | #include "qemu-common.h" |
| 16 | |
| 17 | /* |
| 18 | * Public Interface test-cases |
| 19 | * |
| 20 | * (with some violations to access 'private' data) |
| 21 | */ |
| 22 | |
Anthony Liguori | 65cdadd | 2012-01-10 13:10:46 -0600 | [diff] [blame] | 23 | static void qint_from_int_test(void) |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 24 | { |
| 25 | QInt *qi; |
| 26 | const int value = -42; |
| 27 | |
| 28 | qi = qint_from_int(value); |
Anthony Liguori | 65cdadd | 2012-01-10 13:10:46 -0600 | [diff] [blame] | 29 | g_assert(qi != NULL); |
| 30 | g_assert(qi->value == value); |
| 31 | g_assert(qi->base.refcnt == 1); |
| 32 | g_assert(qobject_type(QOBJECT(qi)) == QTYPE_QINT); |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 33 | |
| 34 | // destroy doesn't exit yet |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 35 | g_free(qi); |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 36 | } |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 37 | |
Anthony Liguori | 65cdadd | 2012-01-10 13:10:46 -0600 | [diff] [blame] | 38 | static void qint_destroy_test(void) |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 39 | { |
| 40 | QInt *qi = qint_from_int(0); |
| 41 | QDECREF(qi); |
| 42 | } |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 43 | |
Anthony Liguori | 65cdadd | 2012-01-10 13:10:46 -0600 | [diff] [blame] | 44 | static void qint_from_int64_test(void) |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 45 | { |
| 46 | QInt *qi; |
Pierre Riteau | 02c068c | 2009-09-08 11:53:37 +0200 | [diff] [blame] | 47 | const int64_t value = 0x1234567890abcdefLL; |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 48 | |
| 49 | qi = qint_from_int(value); |
Anthony Liguori | 65cdadd | 2012-01-10 13:10:46 -0600 | [diff] [blame] | 50 | g_assert((int64_t) qi->value == value); |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 51 | |
| 52 | QDECREF(qi); |
| 53 | } |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 54 | |
Anthony Liguori | 65cdadd | 2012-01-10 13:10:46 -0600 | [diff] [blame] | 55 | static void qint_get_int_test(void) |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 56 | { |
| 57 | QInt *qi; |
| 58 | const int value = 123456; |
| 59 | |
| 60 | qi = qint_from_int(value); |
Anthony Liguori | 65cdadd | 2012-01-10 13:10:46 -0600 | [diff] [blame] | 61 | g_assert(qint_get_int(qi) == value); |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 62 | |
| 63 | QDECREF(qi); |
| 64 | } |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 65 | |
Anthony Liguori | 65cdadd | 2012-01-10 13:10:46 -0600 | [diff] [blame] | 66 | static void qobject_to_qint_test(void) |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 67 | { |
| 68 | QInt *qi; |
| 69 | |
| 70 | qi = qint_from_int(0); |
Anthony Liguori | 65cdadd | 2012-01-10 13:10:46 -0600 | [diff] [blame] | 71 | g_assert(qobject_to_qint(QOBJECT(qi)) == qi); |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 72 | |
| 73 | QDECREF(qi); |
| 74 | } |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 75 | |
Anthony Liguori | 65cdadd | 2012-01-10 13:10:46 -0600 | [diff] [blame] | 76 | int main(int argc, char **argv) |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 77 | { |
Anthony Liguori | 65cdadd | 2012-01-10 13:10:46 -0600 | [diff] [blame] | 78 | g_test_init(&argc, &argv, NULL); |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 79 | |
Anthony Liguori | 65cdadd | 2012-01-10 13:10:46 -0600 | [diff] [blame] | 80 | g_test_add_func("/public/from_int", qint_from_int_test); |
| 81 | g_test_add_func("/public/destroy", qint_destroy_test); |
| 82 | g_test_add_func("/public/from_int64", qint_from_int64_test); |
| 83 | g_test_add_func("/public/get_int", qint_get_int_test); |
| 84 | g_test_add_func("/public/to_qint", qobject_to_qint_test); |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 85 | |
Anthony Liguori | 65cdadd | 2012-01-10 13:10:46 -0600 | [diff] [blame] | 86 | return g_test_run(); |
Luiz Capitulino | 33837ba | 2009-08-28 15:27:29 -0300 | [diff] [blame] | 87 | } |