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