tcg: Allow multiple word entries into the constant pool
[qemu/ar7.git] / tests / test-qdev-global-props.c
blobd81b0862d53e475eda07b707382ef8c6801c791b
1 /*
2 * Test code for qdev global-properties handling
4 * Copyright (c) 2012 Red Hat Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
27 #include "hw/qdev.h"
28 #include "qom/object.h"
29 #include "qapi/visitor.h"
32 #define TYPE_STATIC_PROPS "static_prop_type"
33 #define STATIC_TYPE(obj) \
34 OBJECT_CHECK(MyType, (obj), TYPE_STATIC_PROPS)
36 #define TYPE_SUBCLASS "static_prop_subtype"
38 #define PROP_DEFAULT 100
40 typedef struct MyType {
41 DeviceState parent_obj;
43 uint32_t prop1;
44 uint32_t prop2;
45 } MyType;
47 static Property static_props[] = {
48 DEFINE_PROP_UINT32("prop1", MyType, prop1, PROP_DEFAULT),
49 DEFINE_PROP_UINT32("prop2", MyType, prop2, PROP_DEFAULT),
50 DEFINE_PROP_END_OF_LIST()
53 static void static_prop_class_init(ObjectClass *oc, void *data)
55 DeviceClass *dc = DEVICE_CLASS(oc);
57 dc->realize = NULL;
58 dc->props = static_props;
61 static const TypeInfo static_prop_type = {
62 .name = TYPE_STATIC_PROPS,
63 .parent = TYPE_DEVICE,
64 .instance_size = sizeof(MyType),
65 .class_init = static_prop_class_init,
68 static const TypeInfo subclass_type = {
69 .name = TYPE_SUBCLASS,
70 .parent = TYPE_STATIC_PROPS,
73 /* Test simple static property setting to default value */
74 static void test_static_prop_subprocess(void)
76 MyType *mt;
78 mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS));
79 qdev_init_nofail(DEVICE(mt));
81 g_assert_cmpuint(mt->prop1, ==, PROP_DEFAULT);
84 static void test_static_prop(void)
86 g_test_trap_subprocess("/qdev/properties/static/default/subprocess", 0, 0);
87 g_test_trap_assert_passed();
88 g_test_trap_assert_stderr("");
89 g_test_trap_assert_stdout("");
92 /* Test setting of static property using global properties */
93 static void test_static_globalprop_subprocess(void)
95 MyType *mt;
96 static GlobalProperty props[] = {
97 { TYPE_STATIC_PROPS, "prop1", "200" },
101 qdev_prop_register_global_list(props);
103 mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS));
104 qdev_init_nofail(DEVICE(mt));
106 g_assert_cmpuint(mt->prop1, ==, 200);
107 g_assert_cmpuint(mt->prop2, ==, PROP_DEFAULT);
110 static void test_static_globalprop(void)
112 g_test_trap_subprocess("/qdev/properties/static/global/subprocess", 0, 0);
113 g_test_trap_assert_passed();
114 g_test_trap_assert_stderr("");
115 g_test_trap_assert_stdout("");
118 #define TYPE_DYNAMIC_PROPS "dynamic-prop-type"
119 #define DYNAMIC_TYPE(obj) \
120 OBJECT_CHECK(MyType, (obj), TYPE_DYNAMIC_PROPS)
122 #define TYPE_UNUSED_HOTPLUG "hotplug-type"
123 #define TYPE_UNUSED_NOHOTPLUG "nohotplug-type"
125 static void prop1_accessor(Object *obj, Visitor *v, const char *name,
126 void *opaque, Error **errp)
128 MyType *mt = DYNAMIC_TYPE(obj);
130 visit_type_uint32(v, name, &mt->prop1, errp);
133 static void prop2_accessor(Object *obj, Visitor *v, const char *name,
134 void *opaque, Error **errp)
136 MyType *mt = DYNAMIC_TYPE(obj);
138 visit_type_uint32(v, name, &mt->prop2, errp);
141 static void dynamic_instance_init(Object *obj)
143 object_property_add(obj, "prop1", "uint32", prop1_accessor, prop1_accessor,
144 NULL, NULL, NULL);
145 object_property_add(obj, "prop2", "uint32", prop2_accessor, prop2_accessor,
146 NULL, NULL, NULL);
149 static void dynamic_class_init(ObjectClass *oc, void *data)
151 DeviceClass *dc = DEVICE_CLASS(oc);
153 dc->realize = NULL;
157 static const TypeInfo dynamic_prop_type = {
158 .name = TYPE_DYNAMIC_PROPS,
159 .parent = TYPE_DEVICE,
160 .instance_size = sizeof(MyType),
161 .instance_init = dynamic_instance_init,
162 .class_init = dynamic_class_init,
165 static void hotplug_class_init(ObjectClass *oc, void *data)
167 DeviceClass *dc = DEVICE_CLASS(oc);
169 dc->realize = NULL;
170 dc->hotpluggable = true;
173 static const TypeInfo hotplug_type = {
174 .name = TYPE_UNUSED_HOTPLUG,
175 .parent = TYPE_DEVICE,
176 .instance_size = sizeof(MyType),
177 .instance_init = dynamic_instance_init,
178 .class_init = hotplug_class_init,
181 static void nohotplug_class_init(ObjectClass *oc, void *data)
183 DeviceClass *dc = DEVICE_CLASS(oc);
185 dc->realize = NULL;
186 dc->hotpluggable = false;
189 static const TypeInfo nohotplug_type = {
190 .name = TYPE_UNUSED_NOHOTPLUG,
191 .parent = TYPE_DEVICE,
192 .instance_size = sizeof(MyType),
193 .instance_init = dynamic_instance_init,
194 .class_init = nohotplug_class_init,
197 #define TYPE_NONDEVICE "nondevice-type"
199 static const TypeInfo nondevice_type = {
200 .name = TYPE_NONDEVICE,
201 .parent = TYPE_OBJECT,
204 /* Test setting of dynamic properties using global properties */
205 static void test_dynamic_globalprop_subprocess(void)
207 MyType *mt;
208 static GlobalProperty props[] = {
209 { TYPE_DYNAMIC_PROPS, "prop1", "101", true },
210 { TYPE_DYNAMIC_PROPS, "prop2", "102", true },
211 { TYPE_DYNAMIC_PROPS"-bad", "prop3", "103", true },
212 { TYPE_UNUSED_HOTPLUG, "prop4", "104", true },
213 { TYPE_UNUSED_NOHOTPLUG, "prop5", "105", true },
214 { TYPE_NONDEVICE, "prop6", "106", true },
217 int all_used;
219 qdev_prop_register_global_list(props);
221 mt = DYNAMIC_TYPE(object_new(TYPE_DYNAMIC_PROPS));
222 qdev_init_nofail(DEVICE(mt));
224 g_assert_cmpuint(mt->prop1, ==, 101);
225 g_assert_cmpuint(mt->prop2, ==, 102);
226 all_used = qdev_prop_check_globals();
227 g_assert_cmpuint(all_used, ==, 1);
228 g_assert(props[0].used);
229 g_assert(props[1].used);
230 g_assert(!props[2].used);
231 g_assert(!props[3].used);
232 g_assert(!props[4].used);
233 g_assert(!props[5].used);
236 static void test_dynamic_globalprop(void)
238 g_test_trap_subprocess("/qdev/properties/dynamic/global/subprocess", 0, 0);
239 g_test_trap_assert_passed();
240 g_test_trap_assert_stderr_unmatched("*prop1*");
241 g_test_trap_assert_stderr_unmatched("*prop2*");
242 g_test_trap_assert_stderr("*warning: global dynamic-prop-type-bad.prop3 has invalid class name\n*");
243 g_test_trap_assert_stderr_unmatched("*prop4*");
244 g_test_trap_assert_stderr("*warning: global nohotplug-type.prop5=105 not used\n*");
245 g_test_trap_assert_stderr("*warning: global nondevice-type.prop6 has invalid class name\n*");
246 g_test_trap_assert_stdout("");
249 /* Test setting of dynamic properties using user_provided=false properties */
250 static void test_dynamic_globalprop_nouser_subprocess(void)
252 MyType *mt;
253 static GlobalProperty props[] = {
254 { TYPE_DYNAMIC_PROPS, "prop1", "101" },
255 { TYPE_DYNAMIC_PROPS, "prop2", "102" },
256 { TYPE_DYNAMIC_PROPS"-bad", "prop3", "103" },
257 { TYPE_UNUSED_HOTPLUG, "prop4", "104" },
258 { TYPE_UNUSED_NOHOTPLUG, "prop5", "105" },
259 { TYPE_NONDEVICE, "prop6", "106" },
262 int all_used;
264 qdev_prop_register_global_list(props);
266 mt = DYNAMIC_TYPE(object_new(TYPE_DYNAMIC_PROPS));
267 qdev_init_nofail(DEVICE(mt));
269 g_assert_cmpuint(mt->prop1, ==, 101);
270 g_assert_cmpuint(mt->prop2, ==, 102);
271 all_used = qdev_prop_check_globals();
272 g_assert_cmpuint(all_used, ==, 0);
273 g_assert(props[0].used);
274 g_assert(props[1].used);
275 g_assert(!props[2].used);
276 g_assert(!props[3].used);
277 g_assert(!props[4].used);
278 g_assert(!props[5].used);
281 static void test_dynamic_globalprop_nouser(void)
283 g_test_trap_subprocess("/qdev/properties/dynamic/global/nouser/subprocess", 0, 0);
284 g_test_trap_assert_passed();
285 g_test_trap_assert_stderr("");
286 g_test_trap_assert_stdout("");
289 /* Test if global props affecting subclasses are applied in the right order */
290 static void test_subclass_global_props(void)
292 MyType *mt;
293 /* Global properties must be applied in the order they were registered */
294 static GlobalProperty props[] = {
295 { TYPE_STATIC_PROPS, "prop1", "101" },
296 { TYPE_SUBCLASS, "prop1", "102" },
297 { TYPE_SUBCLASS, "prop2", "103" },
298 { TYPE_STATIC_PROPS, "prop2", "104" },
302 qdev_prop_register_global_list(props);
304 mt = STATIC_TYPE(object_new(TYPE_SUBCLASS));
305 qdev_init_nofail(DEVICE(mt));
307 g_assert_cmpuint(mt->prop1, ==, 102);
308 g_assert_cmpuint(mt->prop2, ==, 104);
311 int main(int argc, char **argv)
313 g_test_init(&argc, &argv, NULL);
315 module_call_init(MODULE_INIT_QOM);
316 type_register_static(&static_prop_type);
317 type_register_static(&subclass_type);
318 type_register_static(&dynamic_prop_type);
319 type_register_static(&hotplug_type);
320 type_register_static(&nohotplug_type);
321 type_register_static(&nondevice_type);
323 g_test_add_func("/qdev/properties/static/default/subprocess",
324 test_static_prop_subprocess);
325 g_test_add_func("/qdev/properties/static/default",
326 test_static_prop);
328 g_test_add_func("/qdev/properties/static/global/subprocess",
329 test_static_globalprop_subprocess);
330 g_test_add_func("/qdev/properties/static/global",
331 test_static_globalprop);
333 g_test_add_func("/qdev/properties/dynamic/global/subprocess",
334 test_dynamic_globalprop_subprocess);
335 g_test_add_func("/qdev/properties/dynamic/global",
336 test_dynamic_globalprop);
338 g_test_add_func("/qdev/properties/dynamic/global/nouser/subprocess",
339 test_dynamic_globalprop_nouser_subprocess);
340 g_test_add_func("/qdev/properties/dynamic/global/nouser",
341 test_dynamic_globalprop_nouser);
343 g_test_add_func("/qdev/properties/global/subclass",
344 test_subclass_global_props);
346 g_test_run();
348 return 0;