Heinrich Schuchardt | 2dd0111 | 2019-01-30 07:53:31 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2019 Heinrich Schuchardt <xypron.glpk@gmx.de> |
| 4 | * |
| 5 | * Unit tests for memory functions |
| 6 | * |
| 7 | * The architecture dependent implementations run through different lines of |
| 8 | * code depending on the alignment and length of memory regions copied or set. |
| 9 | * This has to be considered in testing. |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <command.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Heinrich Schuchardt | 2dd0111 | 2019-01-30 07:53:31 +0100 | [diff] [blame] | 15 | #include <test/lib.h> |
| 16 | #include <test/test.h> |
| 17 | #include <test/ut.h> |
| 18 | |
| 19 | /* Xor mask used for marking memory regions */ |
| 20 | #define MASK 0xA5 |
| 21 | /* Number of different alignment values */ |
| 22 | #define SWEEP 16 |
| 23 | /* Allow for copying up to 32 bytes */ |
| 24 | #define BUFLEN (SWEEP + 33) |
| 25 | |
| 26 | /** |
| 27 | * init_buffer() - initialize buffer |
| 28 | * |
| 29 | * The buffer is filled with incrementing values xor'ed with the mask. |
| 30 | * |
| 31 | * @buf: buffer |
| 32 | * @mask: xor mask |
| 33 | */ |
| 34 | static void init_buffer(u8 buf[], u8 mask) |
| 35 | { |
| 36 | int i; |
| 37 | |
| 38 | for (i = 0; i < BUFLEN; ++i) |
| 39 | buf[i] = i ^ mask; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * test_memset() - test result of memset() |
| 44 | * |
| 45 | * @uts: unit test state |
| 46 | * @buf: buffer |
| 47 | * @mask: value set by memset() |
| 48 | * @offset: relative start of region changed by memset() in buffer |
| 49 | * @len: length of region changed by memset() |
| 50 | * Return: 0 = success, 1 = failure |
| 51 | */ |
| 52 | static int test_memset(struct unit_test_state *uts, u8 buf[], u8 mask, |
| 53 | int offset, int len) |
| 54 | { |
| 55 | int i; |
| 56 | |
| 57 | for (i = 0; i < BUFLEN; ++i) { |
| 58 | if (i < offset || i >= offset + len) { |
| 59 | ut_asserteq(i, buf[i]); |
| 60 | } else { |
| 61 | ut_asserteq(mask, buf[i]); |
| 62 | } |
| 63 | } |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * lib_memset() - unit test for memset() |
| 69 | * |
| 70 | * Test memset() with varied alignment and length of the changed buffer. |
| 71 | * |
| 72 | * @uts: unit test state |
| 73 | * Return: 0 = success, 1 = failure |
| 74 | */ |
| 75 | static int lib_memset(struct unit_test_state *uts) |
| 76 | { |
| 77 | u8 buf[BUFLEN]; |
| 78 | int offset, len; |
| 79 | void *ptr; |
| 80 | |
| 81 | for (offset = 0; offset <= SWEEP; ++offset) { |
| 82 | for (len = 1; len < BUFLEN - SWEEP; ++len) { |
| 83 | init_buffer(buf, 0); |
| 84 | ptr = memset(buf + offset, MASK, len); |
| 85 | ut_asserteq_ptr(buf + offset, (u8 *)ptr); |
| 86 | if (test_memset(uts, buf, MASK, offset, len)) { |
| 87 | debug("%s: failure %d, %d\n", |
| 88 | __func__, offset, len); |
| 89 | return CMD_RET_FAILURE; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | LIB_TEST(lib_memset, 0); |
| 97 | |
| 98 | /** |
| 99 | * test_memmove() - test result of memcpy() or memmove() |
| 100 | * |
| 101 | * @uts: unit test state |
| 102 | * @buf: buffer |
| 103 | * @mask: xor mask used to initialize source buffer |
| 104 | * @offset1: relative start of copied region in source buffer |
| 105 | * @offset2: relative start of copied region in destination buffer |
| 106 | * @len: length of region changed by memset() |
| 107 | * Return: 0 = success, 1 = failure |
| 108 | */ |
| 109 | static int test_memmove(struct unit_test_state *uts, u8 buf[], u8 mask, |
| 110 | int offset1, int offset2, int len) |
| 111 | { |
| 112 | int i; |
| 113 | |
| 114 | for (i = 0; i < BUFLEN; ++i) { |
| 115 | if (i < offset2 || i >= offset2 + len) { |
| 116 | ut_asserteq(i, buf[i]); |
| 117 | } else { |
| 118 | ut_asserteq((i + offset1 - offset2) ^ mask, buf[i]); |
| 119 | } |
| 120 | } |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * lib_memcpy() - unit test for memcpy() |
| 126 | * |
| 127 | * Test memcpy() with varied alignment and length of the copied buffer. |
| 128 | * |
| 129 | * @uts: unit test state |
| 130 | * Return: 0 = success, 1 = failure |
| 131 | */ |
| 132 | static int lib_memcpy(struct unit_test_state *uts) |
| 133 | { |
| 134 | u8 buf1[BUFLEN]; |
| 135 | u8 buf2[BUFLEN]; |
| 136 | int offset1, offset2, len; |
| 137 | void *ptr; |
| 138 | |
| 139 | init_buffer(buf1, MASK); |
| 140 | |
| 141 | for (offset1 = 0; offset1 <= SWEEP; ++offset1) { |
| 142 | for (offset2 = 0; offset2 <= SWEEP; ++offset2) { |
| 143 | for (len = 1; len < BUFLEN - SWEEP; ++len) { |
| 144 | init_buffer(buf2, 0); |
| 145 | ptr = memcpy(buf2 + offset2, buf1 + offset1, |
| 146 | len); |
| 147 | ut_asserteq_ptr(buf2 + offset2, (u8 *)ptr); |
| 148 | if (test_memmove(uts, buf2, MASK, offset1, |
| 149 | offset2, len)) { |
| 150 | debug("%s: failure %d, %d, %d\n", |
| 151 | __func__, offset1, offset2, len); |
| 152 | return CMD_RET_FAILURE; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | LIB_TEST(lib_memcpy, 0); |
| 161 | |
| 162 | /** |
| 163 | * lib_memmove() - unit test for memmove() |
| 164 | * |
| 165 | * Test memmove() with varied alignment and length of the copied buffer. |
| 166 | * |
| 167 | * @uts: unit test state |
| 168 | * Return: 0 = success, 1 = failure |
| 169 | */ |
| 170 | static int lib_memmove(struct unit_test_state *uts) |
| 171 | { |
| 172 | u8 buf[BUFLEN]; |
| 173 | int offset1, offset2, len; |
| 174 | void *ptr; |
| 175 | |
| 176 | for (offset1 = 0; offset1 <= SWEEP; ++offset1) { |
| 177 | for (offset2 = 0; offset2 <= SWEEP; ++offset2) { |
| 178 | for (len = 1; len < BUFLEN - SWEEP; ++len) { |
| 179 | init_buffer(buf, 0); |
| 180 | ptr = memmove(buf + offset2, buf + offset1, |
| 181 | len); |
| 182 | ut_asserteq_ptr(buf + offset2, (u8 *)ptr); |
| 183 | if (test_memmove(uts, buf, 0, offset1, offset2, |
| 184 | len)) { |
| 185 | debug("%s: failure %d, %d, %d\n", |
| 186 | __func__, offset1, offset2, len); |
| 187 | return CMD_RET_FAILURE; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | LIB_TEST(lib_memmove, 0); |