1 /*
2 * Testcase for dtc expression support
3 *
4 * Copyright (C) 2008 David Gibson, IBM Corporation.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdint.h>
25 #include <errno.h>
26
27
28 #include <libfdt.h>
29
30 #include "tests.h"
31 #include "testdata.h"
32
33 static struct test_expr {
34 const char *expr;
35 uint32_t result;
36 } expr_table[] = {
37 #define TE(expr) { #expr, (expr) }
38 TE(0xdeadbeef),
39 TE(-0x21524111),
40 TE(1+1),
41 TE(2*3),
42 TE(4/2),
43 TE(10/3),
44 TE(19%4),
45 TE(1 << 13),
46 TE(0x1000 >> 4),
47 TE(3*2+1), TE(3*(2+1)),
48 TE(1+2*3), TE((1+2)*3),
49 TE(1 < 2), TE(2 < 1), TE(1 < 1),
50 TE(1 <= 2), TE(2 <= 1), TE(1 <= 1),
51 TE(1 > 2), TE(2 > 1), TE(1 > 1),
52 TE(1 >= 2), TE(2 >= 1), TE(1 >= 1),
53 TE(1 == 1), TE(1 == 2),
54 TE(1 != 1), TE(1 != 2),
55 TE(0xabcdabcd & 0xffff0000),
56 TE(0xdead4110 ^ 0xf0f0f0f0),
57 TE(0xabcd0000 | 0x0000abcd),
58 TE(~0x21524110),
59 TE(~~0xdeadbeef),
60 TE(0 && 0), TE(17 && 0), TE(0 && 17), TE(17 && 17),
61 TE(0 || 0), TE(17 || 0), TE(0 || 17), TE(17 || 17),
62 TE(!0), TE(!1), TE(!17), TE(!!0), TE(!!17),
63 TE(0 ? 17 : 39), TE(1 ? 17 : 39), TE(17 ? 0xdeadbeef : 0xabcd1234),
64 TE(11 * 257 * 1321517ULL),
65 TE(123456790 - 4/2 + 17%4),
66 };
67
68 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
69
main(int argc,char * argv[])70 int main(int argc, char *argv[])
71 {
72 void *fdt;
73 const fdt32_t *res;
74 int reslen;
75 int i;
76
77 test_init(argc, argv);
78
79 if ((argc == 3) && (strcmp(argv[1], "-g") == 0)) {
80 FILE *f = fopen(argv[2], "w");
81
82 if (!f)
83 FAIL("Couldn't open \"%s\" for output: %s\n",
84 argv[2], strerror(errno));
85
86 fprintf(f, "/dts-v1/;\n");
87 fprintf(f, "/ {\n");
88 fprintf(f, "\texpressions = <\n");
89 for (i = 0; i < ARRAY_SIZE(expr_table); i++)
90 fprintf(f, "\t\t(%s)\n", expr_table[i].expr);
91 fprintf(f, "\t>;\n");
92 fprintf(f, "};\n");
93 fclose(f);
94 } else {
95 fdt = load_blob_arg(argc, argv);
96
97 res = fdt_getprop(fdt, 0, "expressions", &reslen);
98
99 if (!res)
100 FAIL("Error retreiving expression results: %s\n",
101 fdt_strerror(reslen));
102
103 if (reslen != (ARRAY_SIZE(expr_table) * sizeof(uint32_t)))
104 FAIL("Unexpected length of results %d instead of %zd\n",
105 reslen, ARRAY_SIZE(expr_table) * sizeof(uint32_t));
106
107 for (i = 0; i < ARRAY_SIZE(expr_table); i++)
108 if (fdt32_to_cpu(res[i]) != expr_table[i].result)
109 FAIL("Incorrect result for expression \"%s\","
110 " 0x%x instead of 0x%x\n",
111 expr_table[i].expr, fdt32_to_cpu(res[i]),
112 expr_table[i].result);
113 }
114
115 PASS();
116 }
117