1 /* $NetBSD: integer-expressions.c,v 1.1.1.3 2019/12/22 12:34:06 skrll Exp $ */ 2 3 // SPDX-License-Identifier: LGPL-2.1-or-later 4 /* 5 * Testcase for dtc expression support 6 * 7 * Copyright (C) 2008 David Gibson, IBM Corporation. 8 */ 9 10 #include <stdlib.h> 11 #include <stdio.h> 12 #include <string.h> 13 #include <stdint.h> 14 #include <errno.h> 15 16 17 #include <libfdt.h> 18 19 #include "tests.h" 20 #include "testdata.h" 21 22 static struct test_expr { 23 const char *expr; 24 uint32_t result; 25 } expr_table[] = { 26 #define TE(expr) { #expr, (expr) } 27 TE(0xdeadbeef), 28 TE(-0x21524111), 29 TE(1+1), 30 TE(2*3), 31 TE(4/2), 32 TE(10/3), 33 TE(19%4), 34 TE(1 << 13), 35 TE(0x1000 >> 4), 36 TE(3*2+1), TE(3*(2+1)), 37 TE(1+2*3), TE((1+2)*3), 38 TE(1 < 2), TE(2 < 1), TE(1 < 1), 39 TE(1 <= 2), TE(2 <= 1), TE(1 <= 1), 40 TE(1 > 2), TE(2 > 1), TE(1 > 1), 41 TE(1 >= 2), TE(2 >= 1), TE(1 >= 1), 42 TE(1 == 1), TE(1 == 2), 43 TE(1 != 1), TE(1 != 2), 44 TE(0xabcdabcd & 0xffff0000), 45 TE(0xdead4110 ^ 0xf0f0f0f0), 46 TE(0xabcd0000 | 0x0000abcd), 47 TE(~0x21524110), 48 TE(~~0xdeadbeef), 49 TE(0 && 0), TE(17 && 0), TE(0 && 17), TE(17 && 17), 50 TE(0 || 0), TE(17 || 0), TE(0 || 17), TE(17 || 17), 51 TE(!0), TE(!1), TE(!17), TE(!!0), TE(!!17), 52 TE(0 ? 17 : 39), TE(1 ? 17 : 39), TE(17 ? 0xdeadbeef : 0xabcd1234), 53 TE(11 * 257 * 1321517ULL), 54 TE(123456790 - 4/2 + 17%4), 55 }; 56 57 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 58 59 int main(int argc, char *argv[]) 60 { 61 void *fdt; 62 const fdt32_t *res; 63 int reslen; 64 int i; 65 66 test_init(argc, argv); 67 68 if ((argc == 3) && (strcmp(argv[1], "-g") == 0)) { 69 FILE *f = fopen(argv[2], "w"); 70 71 if (!f) 72 FAIL("Couldn't open \"%s\" for output: %s\n", 73 argv[2], strerror(errno)); 74 75 fprintf(f, "/dts-v1/;\n"); 76 fprintf(f, "/ {\n"); 77 fprintf(f, "\texpressions = <\n"); 78 for (i = 0; i < ARRAY_SIZE(expr_table); i++) 79 fprintf(f, "\t\t(%s)\n", expr_table[i].expr); 80 fprintf(f, "\t>;\n"); 81 fprintf(f, "};\n"); 82 fclose(f); 83 } else { 84 fdt = load_blob_arg(argc, argv); 85 86 res = fdt_getprop(fdt, 0, "expressions", &reslen); 87 88 if (!res) 89 FAIL("Error retrieving expression results: %s\n", 90 fdt_strerror(reslen)); 91 92 if (reslen != (ARRAY_SIZE(expr_table) * sizeof(uint32_t))) 93 FAIL("Unexpected length of results %d instead of %zd\n", 94 reslen, ARRAY_SIZE(expr_table) * sizeof(uint32_t)); 95 96 for (i = 0; i < ARRAY_SIZE(expr_table); i++) 97 if (fdt32_to_cpu(res[i]) != expr_table[i].result) 98 FAIL("Incorrect result for expression \"%s\"," 99 " 0x%x instead of 0x%x\n", 100 expr_table[i].expr, fdt32_to_cpu(res[i]), 101 expr_table[i].result); 102 } 103 104 PASS(); 105 } 106