Unit testing framework and devicetree framework refactoring (#50)
[lunaix-os.git] / lunaix-os / tests / units / device-tree / test-basic.c
1 #include "common.h"
2 #include <testing/basic.h>
3 #include <unistd.h>
4
5 static inline struct dtn* 
6 validate_node(struct dt_context* ctx, 
7               const char* name, const char* compat)
8 {
9     struct dtn_iter it;
10     struct dtn* node = ctx->root;
11
12     
13     dt_begin_find_byname(&it, node, name);
14     
15     node = (struct dtn*)it.matched;
16
17     expect_notnull(node);
18     expect_str(node->base.compat.str_val, compat);
19     expect_int(node->base.addr_c, 1);
20     expect_int(node->base.sz_c, 2);
21
22     return node;
23 }
24
25 static void
26 testcase_rootfield(struct dt_context* ctx)
27 {
28     struct dtn* node = ctx->root;
29
30     begin_testcase("root");
31
32     expect_str(node->base.compat.str_val, "root");
33     expect_int(node->base.addr_c, 1);
34     expect_int(node->base.sz_c, 2);
35
36     end_testcase();
37 }
38
39 static void
40 testcase_child1(struct dt_context* ctx)
41 {
42     struct dtn* node;
43     struct dtp_val* val;
44
45     begin_testcase("trivial-props");
46     
47     node = validate_node(ctx, "child@1", "child,simple-fields");
48
49     val = dt_getprop(&node->base, "field-str");
50     expect_notnull(val);
51     expect_str(val->str_val, "field");
52
53     val = dt_getprop(&node->base, "field-u32");
54     expect_notnull(val);
55     expect_int(dtp_u32(val), 32);
56
57     val = dt_getprop(&node->base, "field-u64");
58     expect_notnull(val);
59     expect_ulong(dtp_u64(val), (0xfUL << 32 | 0x1));
60
61     val = dt_getprop(&node->base, "field-strlst");
62     expect_notnull(val);
63
64     char* str;
65     int i = 0;
66     const char *expected[] = {"str1", "str2", "str3"};
67     dtprop_strlst_foreach(str, val) {
68         expect_str(str, expected[i]);
69         i++;
70     }
71
72     end_testcase();
73 }
74
75 static void
76 testcase_child2(struct dt_context* ctx)
77 {
78     struct dtn* node;
79     struct dtpropx propx;
80     struct dtprop_xval val;
81     struct dtp_val* prop;
82     
83     begin_testcase("dtpx-reg");
84     
85     node = validate_node(ctx, "child@2", "child,encoded-array");
86
87     dt_proplet reg = { dtprop_u32, dtprop_u64, dtprop_end };
88
89     dtpx_compile_proplet(reg);
90     dtpx_prepare_with(&propx, &node->reg, reg);
91     
92     expect_true(dtpx_extract_at(&propx, &val, 0));
93     expect_int(val.u32, 0x12345);
94
95     expect_true(dtpx_extract_at(&propx, &val, 1));
96     expect_ulong(val.u64, (0x1UL << 32) | 0x2UL);
97
98     expect_false(dtpx_extract_at(&propx, &val, 2));
99     expect_false(dtpx_goto_row(&propx, 1));
100
101     end_testcase();
102     
103
104     begin_testcase("dtpx-mixed");
105
106     struct dtprop_xval xvals[5];
107     dt_proplet mixed_array = { 
108         dtprop_handle, dtprop_u32, dtprop_u32, dtprop_u32, dtprop_u32,
109         dtprop_end
110     };
111
112     prop = dt_getprop(&node->base, "mixed_array");
113     expect_notnull(prop);
114
115     dtpx_compile_proplet(mixed_array);
116     dtpx_prepare_with(&propx, prop, mixed_array);
117
118     expect_true(dtpx_extract_row(&propx, xvals, 5));
119     
120     expect_notnull(xvals[0].phandle);
121     expect_str(morpher_name(dt_mobj(xvals[0].phandle)), "child@1");
122     expect_int(xvals[1].u32, 1);
123     expect_int(xvals[2].u32, 3);
124     expect_int(xvals[3].u32, 5);
125     expect_int(xvals[4].u32, 7);
126
127     expect_true(dtpx_next_row(&propx));
128     expect_true(dtpx_extract_row(&propx, xvals, 5));
129
130     expect_notnull(xvals[0].phandle);
131     expect_str(morpher_name(dt_mobj(xvals[0].phandle)), "child@3");
132     expect_int(xvals[1].u32, 2);
133     expect_int(xvals[2].u32, 4);
134     expect_int(xvals[3].u32, 6);
135     expect_int(xvals[4].u32, 8);
136
137     end_testcase();
138 }
139
140 static void
141 testcase_child3(struct dt_context* ctx)
142 {
143     struct dtn* node;
144     struct dtpropx propx;
145     struct dtprop_xval val;
146     struct dtp_val* prop;
147     
148     begin_testcase("simple-flags");
149     
150     node = validate_node(ctx, "child@3", "child,flags");
151
152     expect_true(node->base.dma_coherent);
153     expect_true(node->base.intr_controll);
154     expect_int(node->base.status, STATUS_DISABLE);
155
156     end_testcase();
157 }
158
159 void
160 run_test(int argc, const char* argv[])
161 {
162
163     if(!my_load_dtb()) {
164         printf("failed to load dtb\n");
165         _exit(1);
166     }
167
168     struct dt_context* ctx;
169     ctx = dt_main_context();
170
171     testcase_rootfield(ctx);
172     testcase_child1(ctx);
173     testcase_child3(ctx);
174     testcase_child2(ctx);
175 }