Files
linux-cryptodev-2.6/tools/testing/memblock/tests/common.c
Rebecca Mckeever 76586c00e7 memblock tests: add verbose output to memblock tests
Add and use functions and macros for printing verbose testing output.

If the Memblock simulator was compiled with VERBOSE=1:
- prefix_push(): appends the given string to a prefix string that will be
  printed in test_fail() and test_pass*().

- prefix_pop(): removes the last prefix from the prefix string.

- prefix_reset(): clears the prefix string.

- test_fail(): prints a message after a test fails containing the test
  number of the failing test and the prefix.

- test_pass(): prints a message after a test passes containing its test
  number and the prefix.

- test_print(): prints the given formatted output string.

- test_pass_pop(): runs test_pass() followed by prefix_pop().

- PREFIX_PUSH(): runs prefix_push(__func__).

If the Memblock simulator was not compiled with VERBOSE=1, these
functions/macros do nothing.

Add the assert wrapper macros ASSERT_EQ(), ASSERT_NE(), and ASSERT_LT().
If the assert condition fails, these macros call test_fail() before
executing assert().

Acked-by: David Hildenbrand <david@redhat.com>
Reviewed-by: Shaoqin Huang <shaoqin.huang@intel.com>
Signed-off-by: Rebecca Mckeever <remckee0@gmail.com>
Signed-off-by: Mike Rapoport <rppt@linux.ibm.com>
Link: https://lore.kernel.org/r/f234d443fe154d5ae8d8aa07284aff69edfb6f61.1656907314.git.remckee0@gmail.com
2022-07-04 19:58:28 +03:00

106 lines
2.1 KiB
C

// SPDX-License-Identifier: GPL-2.0-or-later
#include "tests/common.h"
#include <string.h>
#define INIT_MEMBLOCK_REGIONS 128
#define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS
#define PREFIXES_MAX 15
#define DELIM ": "
static struct test_memory memory_block;
static const char __maybe_unused *prefixes[PREFIXES_MAX];
static int __maybe_unused nr_prefixes;
void reset_memblock_regions(void)
{
memset(memblock.memory.regions, 0,
memblock.memory.cnt * sizeof(struct memblock_region));
memblock.memory.cnt = 1;
memblock.memory.max = INIT_MEMBLOCK_REGIONS;
memblock.memory.total_size = 0;
memset(memblock.reserved.regions, 0,
memblock.reserved.cnt * sizeof(struct memblock_region));
memblock.reserved.cnt = 1;
memblock.reserved.max = INIT_MEMBLOCK_RESERVED_REGIONS;
memblock.reserved.total_size = 0;
}
void reset_memblock_attributes(void)
{
memblock.memory.name = "memory";
memblock.reserved.name = "reserved";
memblock.bottom_up = false;
memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE;
}
void setup_memblock(void)
{
reset_memblock_regions();
memblock_add((phys_addr_t)memory_block.base, MEM_SIZE);
}
void dummy_physical_memory_init(void)
{
memory_block.base = malloc(MEM_SIZE);
assert(memory_block.base);
}
void dummy_physical_memory_cleanup(void)
{
free(memory_block.base);
}
#ifdef VERBOSE
void print_prefixes(const char *postfix)
{
for (int i = 0; i < nr_prefixes; i++)
test_print("%s%s", prefixes[i], DELIM);
test_print(postfix);
}
void test_fail(void)
{
ksft_test_result_fail(": ");
print_prefixes("failed\n");
}
void test_pass(void)
{
ksft_test_result_pass(": ");
print_prefixes("passed\n");
}
void test_print(const char *fmt, ...)
{
int saved_errno = errno;
va_list args;
va_start(args, fmt);
errno = saved_errno;
vprintf(fmt, args);
va_end(args);
}
void prefix_reset(void)
{
memset(prefixes, 0, PREFIXES_MAX * sizeof(char *));
nr_prefixes = 0;
}
void prefix_push(const char *prefix)
{
assert(nr_prefixes < PREFIXES_MAX);
prefixes[nr_prefixes] = prefix;
nr_prefixes++;
}
void prefix_pop(void)
{
if (nr_prefixes > 0) {
prefixes[nr_prefixes - 1] = 0;
nr_prefixes--;
}
}
#endif /* VERBOSE */