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
31 #define TYPE_STATIC_PROPS "static_prop_type"
32 #define STATIC_TYPE(obj) \
33 OBJECT_CHECK(MyType, (obj), TYPE_STATIC_PROPS)
35 #define PROP_DEFAULT 100
37 typedef struct MyType
{
38 DeviceState parent_obj
;
44 static Property static_props
[] = {
45 DEFINE_PROP_UINT32("prop1", MyType
, prop1
, PROP_DEFAULT
),
46 DEFINE_PROP_UINT32("prop2", MyType
, prop2
, PROP_DEFAULT
),
47 DEFINE_PROP_END_OF_LIST()
50 static void static_prop_class_init(ObjectClass
*oc
, void *data
)
52 DeviceClass
*dc
= DEVICE_CLASS(oc
);
55 dc
->props
= static_props
;
58 static const TypeInfo static_prop_type
= {
59 .name
= TYPE_STATIC_PROPS
,
60 .parent
= TYPE_DEVICE
,
61 .instance_size
= sizeof(MyType
),
62 .class_init
= static_prop_class_init
,
65 /* Test simple static property setting to default value */
66 static void test_static_prop(void)
70 mt
= STATIC_TYPE(object_new(TYPE_STATIC_PROPS
));
71 qdev_init_nofail(DEVICE(mt
));
73 g_assert_cmpuint(mt
->prop1
, ==, PROP_DEFAULT
);
76 /* Test setting of static property using global properties */
77 static void test_static_globalprop(void)
80 static GlobalProperty props
[] = {
81 { TYPE_STATIC_PROPS
, "prop1", "200" },
85 qdev_prop_register_global_list(props
);
87 mt
= STATIC_TYPE(object_new(TYPE_STATIC_PROPS
));
88 qdev_init_nofail(DEVICE(mt
));
90 g_assert_cmpuint(mt
->prop1
, ==, 200);
91 g_assert_cmpuint(mt
->prop2
, ==, PROP_DEFAULT
);
94 int main(int argc
, char **argv
)
96 g_test_init(&argc
, &argv
, NULL
);
98 module_call_init(MODULE_INIT_QOM
);
99 type_register_static(&static_prop_type
);
101 g_test_add_func("/qdev/properties/static/default", test_static_prop
);
102 g_test_add_func("/qdev/properties/static/global", test_static_globalprop
);