]>
Commit | Line | Data |
---|---|---|
747b0cb4 EH |
1 | /* |
2 | * Test code for qdev global-properties handling | |
3 | * | |
4 | * Copyright (c) 2012 Red Hat Inc. | |
5 | * | |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
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. | |
23 | */ | |
24 | ||
25 | #include <glib.h> | |
26 | #include <stdint.h> | |
27 | ||
28 | #include "hw/qdev.h" | |
29 | ||
30 | ||
31 | #define TYPE_STATIC_PROPS "static_prop_type" | |
32 | #define STATIC_TYPE(obj) \ | |
33 | OBJECT_CHECK(MyType, (obj), TYPE_STATIC_PROPS) | |
34 | ||
35 | #define PROP_DEFAULT 100 | |
36 | ||
37 | typedef struct MyType { | |
38 | DeviceState parent_obj; | |
39 | ||
40 | uint32_t prop1; | |
41 | uint32_t prop2; | |
42 | } MyType; | |
43 | ||
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() | |
48 | }; | |
49 | ||
50 | static void static_prop_class_init(ObjectClass *oc, void *data) | |
51 | { | |
52 | DeviceClass *dc = DEVICE_CLASS(oc); | |
53 | ||
54 | dc->realize = NULL; | |
55 | dc->props = static_props; | |
56 | } | |
57 | ||
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, | |
63 | }; | |
64 | ||
65 | /* Test simple static property setting to default value */ | |
66 | static void test_static_prop(void) | |
67 | { | |
68 | MyType *mt; | |
69 | ||
70 | mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS)); | |
71 | qdev_init_nofail(DEVICE(mt)); | |
72 | ||
73 | g_assert_cmpuint(mt->prop1, ==, PROP_DEFAULT); | |
74 | } | |
75 | ||
76 | /* Test setting of static property using global properties */ | |
77 | static void test_static_globalprop(void) | |
78 | { | |
79 | MyType *mt; | |
80 | static GlobalProperty props[] = { | |
81 | { TYPE_STATIC_PROPS, "prop1", "200" }, | |
82 | {} | |
83 | }; | |
84 | ||
85 | qdev_prop_register_global_list(props); | |
86 | ||
87 | mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS)); | |
88 | qdev_init_nofail(DEVICE(mt)); | |
89 | ||
90 | g_assert_cmpuint(mt->prop1, ==, 200); | |
91 | g_assert_cmpuint(mt->prop2, ==, PROP_DEFAULT); | |
92 | } | |
93 | ||
94 | int main(int argc, char **argv) | |
95 | { | |
96 | g_test_init(&argc, &argv, NULL); | |
97 | ||
98 | module_call_init(MODULE_INIT_QOM); | |
99 | type_register_static(&static_prop_type); | |
100 | ||
101 | g_test_add_func("/qdev/properties/static/default", test_static_prop); | |
102 | g_test_add_func("/qdev/properties/static/global", test_static_globalprop); | |
103 | ||
104 | g_test_run(); | |
105 | ||
106 | return 0; | |
107 | } |