Simon Glass | 663a2c1 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Tests for setexpr command |
| 4 | * |
| 5 | * Copyright 2020 Google LLC |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <console.h> |
| 11 | #include <mapmem.h> |
| 12 | #include <dm/test.h> |
| 13 | #include <test/suites.h> |
| 14 | #include <test/ut.h> |
| 15 | |
| 16 | #define BUF_SIZE 0x100 |
| 17 | |
| 18 | /* Declare a new mem test */ |
| 19 | #define SETEXPR_TEST(_name, _flags) UNIT_TEST(_name, _flags, setexpr_test) |
| 20 | |
| 21 | /* Test 'setexpr' command with simply setting integers */ |
| 22 | static int setexpr_test_int(struct unit_test_state *uts) |
| 23 | { |
| 24 | u8 *buf; |
| 25 | |
| 26 | buf = map_sysmem(0, BUF_SIZE); |
| 27 | memset(buf, '\xff', BUF_SIZE); |
| 28 | |
| 29 | /* byte */ |
| 30 | buf[0x0] = 0x12; |
| 31 | ut_assertok(run_command("setexpr.b fred 0", 0)); |
| 32 | ut_asserteq_str("0", env_get("fred")); |
| 33 | ut_assertok(run_command("setexpr.b fred *0", 0)); |
| 34 | ut_asserteq_str("12", env_get("fred")); |
| 35 | |
| 36 | /* 16-bit */ |
| 37 | *(short *)buf = 0x2345; |
| 38 | ut_assertok(run_command("setexpr.w fred 0", 0)); |
| 39 | ut_asserteq_str("0", env_get("fred")); |
| 40 | ut_assertok(run_command("setexpr.w fred *0", 0)); |
| 41 | ut_asserteq_str("2345", env_get("fred")); |
| 42 | |
| 43 | /* 32-bit */ |
| 44 | *(u32 *)buf = 0x3456789a; |
| 45 | ut_assertok(run_command("setexpr.l fred 0", 0)); |
| 46 | ut_asserteq_str("0", env_get("fred")); |
| 47 | ut_assertok(run_command("setexpr.l fred *0", 0)); |
Simon Glass | 7c19edf | 2020-11-01 14:15:37 -0700 | [diff] [blame^] | 48 | ut_asserteq_str("3456789a", env_get("fred")); |
Simon Glass | 663a2c1 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 49 | |
| 50 | /* 64-bit */ |
| 51 | *(u64 *)buf = 0x456789abcdef0123; |
| 52 | ut_assertok(run_command("setexpr.q fred 0", 0)); |
| 53 | ut_asserteq_str("0", env_get("fred")); |
| 54 | ut_assertok(run_command("setexpr.q fred *0", 0)); |
| 55 | ut_asserteq_str("456789abcdef0123", env_get("fred")); |
| 56 | |
| 57 | /* default */ |
| 58 | ut_assertok(run_command("setexpr fred 0", 0)); |
| 59 | ut_asserteq_str("0", env_get("fred")); |
| 60 | ut_assertok(run_command("setexpr fred *0", 0)); |
Simon Glass | 7c19edf | 2020-11-01 14:15:37 -0700 | [diff] [blame^] | 61 | ut_asserteq_str("cdef0123", env_get("fred")); |
Simon Glass | 663a2c1 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 62 | |
| 63 | unmap_sysmem(buf); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | SETEXPR_TEST(setexpr_test_int, UT_TESTF_CONSOLE_REC); |
| 68 | |
| 69 | /* Test 'setexpr' command with + operator */ |
| 70 | static int setexpr_test_plus(struct unit_test_state *uts) |
| 71 | { |
| 72 | char *buf; |
| 73 | |
| 74 | buf = map_sysmem(0, BUF_SIZE); |
| 75 | memset(buf, '\xff', BUF_SIZE); |
| 76 | |
| 77 | /* byte */ |
| 78 | buf[0x0] = 0x12; |
| 79 | buf[0x10] = 0x34; |
| 80 | ut_assertok(run_command("setexpr.b fred *0 + *10", 0)); |
| 81 | ut_asserteq_str("46", env_get("fred")); |
| 82 | |
| 83 | /* 16-bit */ |
| 84 | *(short *)buf = 0x2345; |
| 85 | *(short *)(buf + 0x10) = 0xf012; |
| 86 | ut_assertok(run_command("setexpr.w fred *0 + *10", 0)); |
| 87 | ut_asserteq_str("11357", env_get("fred")); |
| 88 | |
| 89 | /* 32-bit */ |
| 90 | *(u32 *)buf = 0x3456789a; |
| 91 | *(u32 *)(buf + 0x10) = 0xc3384235; |
| 92 | ut_assertok(run_command("setexpr.l fred *0 + *10", 0)); |
Simon Glass | 7c19edf | 2020-11-01 14:15:37 -0700 | [diff] [blame^] | 93 | ut_asserteq_str("f78ebacf", env_get("fred")); |
Simon Glass | 663a2c1 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 94 | |
| 95 | /* 64-bit */ |
| 96 | *(u64 *)buf = 0x456789abcdef0123; |
| 97 | *(u64 *)(buf + 0x10) = 0x4987328372849283; |
| 98 | ut_assertok(run_command("setexpr.q fred *0 + *10", 0)); |
| 99 | ut_asserteq_str("8eeebc2f407393a6", env_get("fred")); |
| 100 | |
| 101 | /* default */ |
| 102 | ut_assertok(run_command("setexpr fred *0 + *10", 0)); |
Simon Glass | 7c19edf | 2020-11-01 14:15:37 -0700 | [diff] [blame^] | 103 | ut_asserteq_str("1407393a6", env_get("fred")); |
Simon Glass | 663a2c1 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 104 | |
| 105 | unmap_sysmem(buf); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | SETEXPR_TEST(setexpr_test_plus, UT_TESTF_CONSOLE_REC); |
| 110 | |
| 111 | /* Test 'setexpr' command with other operators */ |
| 112 | static int setexpr_test_oper(struct unit_test_state *uts) |
| 113 | { |
| 114 | char *buf; |
| 115 | |
| 116 | buf = map_sysmem(0, BUF_SIZE); |
| 117 | memset(buf, '\xff', BUF_SIZE); |
| 118 | |
| 119 | *(u32 *)buf = 0x1234; |
| 120 | *(u32 *)(buf + 0x10) = 0x560000; |
| 121 | |
| 122 | /* Quote | to avoid confusing hush */ |
| 123 | ut_assertok(run_command("setexpr fred *0 \"|\" *10", 0)); |
Simon Glass | 7c19edf | 2020-11-01 14:15:37 -0700 | [diff] [blame^] | 124 | ut_asserteq_str("561234", env_get("fred")); |
Simon Glass | 663a2c1 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 125 | |
| 126 | *(u32 *)buf = 0x561200; |
| 127 | *(u32 *)(buf + 0x10) = 0x1234; |
| 128 | |
| 129 | /* Quote & to avoid confusing hush */ |
| 130 | ut_assertok(run_command("setexpr.l fred *0 \"&\" *10", 0)); |
Simon Glass | 7c19edf | 2020-11-01 14:15:37 -0700 | [diff] [blame^] | 131 | ut_asserteq_str("1200", env_get("fred")); |
Simon Glass | 663a2c1 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 132 | |
| 133 | ut_assertok(run_command("setexpr.l fred *0 ^ *10", 0)); |
| 134 | ut_asserteq_str("560034", env_get("fred")); |
| 135 | |
| 136 | ut_assertok(run_command("setexpr.l fred *0 - *10", 0)); |
| 137 | ut_asserteq_str("55ffcc", env_get("fred")); |
| 138 | |
| 139 | ut_assertok(run_command("setexpr.l fred *0 * *10", 0)); |
Simon Glass | 7c19edf | 2020-11-01 14:15:37 -0700 | [diff] [blame^] | 140 | ut_asserteq_str("61ebfa800", env_get("fred")); |
Simon Glass | 663a2c1 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 141 | |
| 142 | ut_assertok(run_command("setexpr.l fred *0 / *10", 0)); |
Simon Glass | 7c19edf | 2020-11-01 14:15:37 -0700 | [diff] [blame^] | 143 | ut_asserteq_str("4ba", env_get("fred")); |
Simon Glass | 663a2c1 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 144 | |
| 145 | ut_assertok(run_command("setexpr.l fred *0 % *10", 0)); |
Simon Glass | 7c19edf | 2020-11-01 14:15:37 -0700 | [diff] [blame^] | 146 | ut_asserteq_str("838", env_get("fred")); |
Simon Glass | 663a2c1 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 147 | |
| 148 | unmap_sysmem(buf); |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | SETEXPR_TEST(setexpr_test_oper, UT_TESTF_CONSOLE_REC); |
| 153 | |
| 154 | int do_ut_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
| 155 | { |
| 156 | struct unit_test *tests = ll_entry_start(struct unit_test, |
| 157 | setexpr_test); |
| 158 | const int n_ents = ll_entry_count(struct unit_test, setexpr_test); |
| 159 | |
| 160 | return cmd_ut_category("cmd_setexpr", "cmd_mem_", tests, n_ents, argc, |
| 161 | argv); |
| 162 | } |