qom: Make enum string tables const-correct
[qemu/cris-port.git] / tests / check-qom-proplist.c
blobe82532e1f253417e712052e7c0ef7e518a9c4bcc
1 /*
2 * Copyright (C) 2015 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
18 * Author: Daniel P. Berrange <berrange@redhat.com>
21 #include <glib.h>
23 #include "qom/object.h"
24 #include "qemu/module.h"
27 #define TYPE_DUMMY "qemu-dummy"
29 typedef struct DummyObject DummyObject;
30 typedef struct DummyObjectClass DummyObjectClass;
32 #define DUMMY_OBJECT(obj) \
33 OBJECT_CHECK(DummyObject, (obj), TYPE_DUMMY)
35 struct DummyObject {
36 Object parent_obj;
38 bool bv;
39 char *sv;
42 struct DummyObjectClass {
43 ObjectClass parent_class;
47 static void dummy_set_bv(Object *obj,
48 bool value,
49 Error **errp)
51 DummyObject *dobj = DUMMY_OBJECT(obj);
53 dobj->bv = value;
56 static bool dummy_get_bv(Object *obj,
57 Error **errp)
59 DummyObject *dobj = DUMMY_OBJECT(obj);
61 return dobj->bv;
65 static void dummy_set_sv(Object *obj,
66 const char *value,
67 Error **errp)
69 DummyObject *dobj = DUMMY_OBJECT(obj);
71 g_free(dobj->sv);
72 dobj->sv = g_strdup(value);
75 static char *dummy_get_sv(Object *obj,
76 Error **errp)
78 DummyObject *dobj = DUMMY_OBJECT(obj);
80 return g_strdup(dobj->sv);
84 static void dummy_init(Object *obj)
86 object_property_add_bool(obj, "bv",
87 dummy_get_bv,
88 dummy_set_bv,
89 NULL);
90 object_property_add_str(obj, "sv",
91 dummy_get_sv,
92 dummy_set_sv,
93 NULL);
96 static void dummy_finalize(Object *obj)
98 DummyObject *dobj = DUMMY_OBJECT(obj);
100 g_free(dobj->sv);
104 static const TypeInfo dummy_info = {
105 .name = TYPE_DUMMY,
106 .parent = TYPE_OBJECT,
107 .instance_size = sizeof(DummyObject),
108 .instance_init = dummy_init,
109 .instance_finalize = dummy_finalize,
110 .class_size = sizeof(DummyObjectClass),
113 static void test_dummy_createv(void)
115 Error *err = NULL;
116 Object *parent = object_get_objects_root();
117 DummyObject *dobj = DUMMY_OBJECT(
118 object_new_with_props(TYPE_DUMMY,
119 parent,
120 "dummy0",
121 &err,
122 "bv", "yes",
123 "sv", "Hiss hiss hiss",
124 NULL));
126 g_assert(err == NULL);
127 g_assert_cmpstr(dobj->sv, ==, "Hiss hiss hiss");
128 g_assert(dobj->bv == true);
130 g_assert(object_resolve_path_component(parent, "dummy0")
131 == OBJECT(dobj));
133 object_unparent(OBJECT(dobj));
137 static Object *new_helper(Error **errp,
138 Object *parent,
139 ...)
141 va_list vargs;
142 Object *obj;
144 va_start(vargs, parent);
145 obj = object_new_with_propv(TYPE_DUMMY,
146 parent,
147 "dummy0",
148 errp,
149 vargs);
150 va_end(vargs);
151 return obj;
154 static void test_dummy_createlist(void)
156 Error *err = NULL;
157 Object *parent = object_get_objects_root();
158 DummyObject *dobj = DUMMY_OBJECT(
159 new_helper(&err,
160 parent,
161 "bv", "yes",
162 "sv", "Hiss hiss hiss",
163 NULL));
165 g_assert(err == NULL);
166 g_assert_cmpstr(dobj->sv, ==, "Hiss hiss hiss");
167 g_assert(dobj->bv == true);
169 g_assert(object_resolve_path_component(parent, "dummy0")
170 == OBJECT(dobj));
172 object_unparent(OBJECT(dobj));
175 int main(int argc, char **argv)
177 g_test_init(&argc, &argv, NULL);
179 module_call_init(MODULE_INIT_QOM);
180 type_register_static(&dummy_info);
182 g_test_add_func("/qom/proplist/createlist", test_dummy_createlist);
183 g_test_add_func("/qom/proplist/createv", test_dummy_createv);
185 return g_test_run();