akaros/kern/include/test_infrastructure.h
<<
>>
Prefs
   1#pragma once
   2
   3/*
   4 * Header file with infrastructure needed for kernel unit tests:
   5 *  - Assertion functions.
   6 *  - Test structures.
   7 */
   8
   9#include <stdio.h>
  10#include <kmalloc.h>
  11
  12/* Macros for assertions.
  13 * They depend on <stdbool.h> and printk() to be included in the source file.
  14 */
  15#define KT_ASSERT_M(message, test)                                             \
  16do {                                                                           \
  17        if (!(test)) {                                                         \
  18                extern char *kern_test_msg;                                    \
  19                char prefix[] = "Assertion failed: ";                          \
  20                int msg_size = sizeof(prefix) + sizeof(message) - 1;           \
  21                kern_test_msg = (char*) kmalloc(msg_size, 0);                  \
  22                snprintf(kern_test_msg, msg_size, "%s%s", prefix, message);    \
  23                return false;                                                  \
  24        }                                                                      \
  25} while (0)
  26
  27#define KT_ASSERT(test)                                                        \
  28do {                                                                           \
  29        if (!(test)) {                                                         \
  30                return false;                                                  \
  31        }                                                                      \
  32} while (0)
  33
  34
  35/* Postboot kernel tests: tests ran after boot in kernel mode. */
  36
  37struct pb_kernel_test {
  38        char name[256]; // Name of the test function.
  39        bool (*func)(void); // Name of the test function, should be = to 'name'.
  40        bool enabled; // Whether to run or not the test.
  41};
  42
  43/* A macro for testing if another macro has been #defined or not.  Can be used
  44 * wherever you need a boolean defined.  Returns 0 or 1. */
  45#define is_defined(macro) is_defined_(macro)
  46#define is_defined_test_1 ,
  47#define is_defined_(value) is_defined__(is_defined_test_##value, value)
  48#define is_defined__(comma, value) is_defined___(comma value, 0)
  49#define is_defined___(_, v, ...) v
  50
  51/* Macro for registering a postboot kernel test. */
  52#define PB_K_TEST_REG(name, config) \
  53        {"test_" #name, test_##name, is_defined(config)}
  54
  55extern struct pb_kernel_test pb_kernel_tests[];
  56extern int num_pb_kernel_tests;
  57