UEFIStarter
a simple UEFI framework
Macros | Functions | Variables
asserts.c File Reference

General assertions for tests. More...

#include <Library/UefiLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <UEFIStarter/tests/tests.h>
#include <UEFIStarter/tests/asserts.h>
#include <UEFIStarter/tests/output.h>
#include <UEFIStarter/core/logger.h>
#include <UEFIStarter/core/string.h>

Macros

#define PRINT_ASSERT_RESULT(SUCCESS)
 Helper macro to print an assertion result. More...
 
#define ADD_CHANNEL_DELTA(EXP, CHAN)
 Helper macro to calculate the absolute difference between expected and actual channel values. More...
 

Functions

static BOOLEAN EFIAPI _simple_assert (BOOLEAN check, CHAR16 *message, CHAR16 *fmt,...)
 Internal assertion function. More...
 
BOOLEAN assert_true (BOOLEAN actual, CHAR16 *message)
 Asserts a boolean value is true. More...
 
BOOLEAN assert_false (BOOLEAN actual, CHAR16 *message)
 Asserts a boolean value is false. More...
 
BOOLEAN assert_intn_equals (INTN expected, INTN actual, CHAR16 *message)
 Asserts an integer value equals an expected value. More...
 
BOOLEAN assert_intn_greater_than_or_equal_to (INTN expected, INTN actual, CHAR16 *message)
 Asserts an integer value is >=expectation. More...
 
BOOLEAN assert_intn_less_than_or_equal_to (INTN expected, INTN actual, CHAR16 *message)
 Asserts an integer value is <=expectation. More...
 
BOOLEAN assert_intn_in_closed_interval (INTN min, INTN max, INTN actual, CHAR16 *message)
 Asserts an integer value is within an interval: min <= input <= max. More...
 
BOOLEAN assert_uint64_equals (UINT64 expected, UINT64 actual, CHAR16 *message)
 Asserts a UINT64 equals an expected value. More...
 
BOOLEAN assert_double_near (double expected, double epsilon, double actual, CHAR16 *message)
 Asserts a double value is within an epsilon radius around an expected value. More...
 
BOOLEAN assert_double_greater_than (double threshold, double actual, CHAR16 *message)
 Asserts a double value is >expectation. More...
 
BOOLEAN assert_double_greater_than_or_equal_to (double threshold, double actual, CHAR16 *message)
 Asserts a double value is >=expectation. More...
 
BOOLEAN assert_double_less_than (double threshold, double actual, CHAR16 *message)
 Asserts a double value is <expectation. More...
 
BOOLEAN assert_double_less_than_or_equal_to (double threshold, double actual, CHAR16 *message)
 Asserts a double value is <=expectation. More...
 
BOOLEAN assert_null (void *actual, CHAR16 *message)
 Asserts a pointer is NULL. More...
 
BOOLEAN assert_not_null (void *actual, CHAR16 *message)
 Asserts a pointer is anything but NULL. More...
 
BOOLEAN assert_uint8_array (UINTN size, UINT8 *expected, UINT8 *actual, CHAR16 *message)
 Asserts an array of UINT8 values matches an expected array's values. More...
 
BOOLEAN assert_wcstr_equals (CHAR16 *expected, CHAR16 *actual, CHAR16 *message)
 Asserts a string equals an expected string. More...
 
BOOLEAN assert_pixel_values (UINT8 red, UINT8 green, UINT8 blue, UINT8 reserved, EFI_GRAPHICS_OUTPUT_BLT_PIXEL act, CHAR16 *message)
 Asserts an EFI_GRAPHICS_OUTPUT_BLT_PIXEL has expected channel values. More...
 
BOOLEAN assert_pixel (EFI_GRAPHICS_OUTPUT_BLT_PIXEL exp, EFI_GRAPHICS_OUTPUT_BLT_PIXEL act, CHAR16 *message)
 Asserts an EFI_GRAPHICS_OUTPUT_BLT_PIXEL equals an expected pixel. More...
 
BOOLEAN assert_pixel_values_near (UINT8 red, UINT8 green, UINT8 blue, UINT8 reserved, INTN epsilon, EFI_GRAPHICS_OUTPUT_BLT_PIXEL act, CHAR16 *message)
 Asserts an EFI_GRAPHICS_OUTPUT_BLT_PIXEL has expected channel values or the difference is within a given radius. More...
 
BOOLEAN assert_pixel_near (EFI_GRAPHICS_OUTPUT_BLT_PIXEL exp, INTN epsilon, EFI_GRAPHICS_OUTPUT_BLT_PIXEL act, CHAR16 *message)
 Asserts an EFI_GRAPHICS_OUTPUT_BLT_PIXEL equals an expected pixel or the difference is within a given radius. More...
 

Variables

BOOLEAN invert_next_assert =FALSE
 Inverts the next executed assertion: failure becomes success and vice versa. More...
 

Detailed Description

General assertions for tests.

Author
Richard Nusser
License
GPLv3 (see http://www.gnu.org/licenses/)
See also
https://github.com/rinusser/UEFIStarter

Macro Definition Documentation

◆ PRINT_ASSERT_RESULT

#define PRINT_ASSERT_RESULT (   SUCCESS)
Value:
VA_START(args,fmt); \
desc=CatVSPrint(NULL,fmt,args); \
VA_END(args); \
print_assertion(SUCCESS,desc,message); \
FreePool(desc);
the test succeeded
Definition: types.h:34
cmdline_argument_t args[]
list of command-line arguments
Definition: rotation.c:30

Helper macro to print an assertion result.

Parameters
SUCCESSthe result to print

◆ ADD_CHANNEL_DELTA

#define ADD_CHANNEL_DELTA (   EXP,
  CHAN 
)
Value:
delta=EXP; \
delta-=act.CHAN; \
if(delta<0) \
delta=-delta; \
sum+=delta;

Helper macro to calculate the absolute difference between expected and actual channel values.

Parameters
EXPthe expected channel value
CHANthe actual pixel's color channel to look in

Function Documentation

◆ _simple_assert()

static BOOLEAN EFIAPI _simple_assert ( BOOLEAN  check,
CHAR16 *  message,
CHAR16 *  fmt,
  ... 
)
static

Internal assertion function.

This just checks whether the passed boolean value is true, any more than that is expected to be handled by the caller.

Parameters
checkwhether the assertion should pass
messagean error description to include in case of failure, e.g. the variable name being checked
fmta descriptive message for the assertion
...any additional parameters for the fmt format string
Returns
whether the assertion passed

◆ assert_true()

BOOLEAN assert_true ( BOOLEAN  actual,
CHAR16 *  message 
)

Asserts a boolean value is true.

Parameters
actualthe value to check
messagean error message to include on failure
Returns
whether the assertion passed

◆ assert_false()

BOOLEAN assert_false ( BOOLEAN  actual,
CHAR16 *  message 
)

Asserts a boolean value is false.

Parameters
actualthe value to check
messagean error message to include on failure
Returns
whether the assertion passed

◆ assert_intn_equals()

BOOLEAN assert_intn_equals ( INTN  expected,
INTN  actual,
CHAR16 *  message 
)

Asserts an integer value equals an expected value.

Parameters
expectedthe expected value
actualthe value to check
messagean error message to include on failure
Returns
whether the assertion passed

◆ assert_intn_greater_than_or_equal_to()

BOOLEAN assert_intn_greater_than_or_equal_to ( INTN  expected,
INTN  actual,
CHAR16 *  message 
)

Asserts an integer value is >=expectation.

Parameters
expectedthe expected value
actualthe value to check
messagean error message to include on failure
Returns
whether the assertion passed

◆ assert_intn_less_than_or_equal_to()

BOOLEAN assert_intn_less_than_or_equal_to ( INTN  expected,
INTN  actual,
CHAR16 *  message 
)

Asserts an integer value is <=expectation.

Parameters
expectedthe expected value
actualthe value to check
messagean error message to include on failure
Returns
whether the assertion passed

◆ assert_intn_in_closed_interval()

BOOLEAN assert_intn_in_closed_interval ( INTN  min,
INTN  max,
INTN  actual,
CHAR16 *  message 
)

Asserts an integer value is within an interval: min <= input <= max.

Parameters
minthe smallest allowed value
maxthe largest allowed value
actualthe value to check
messagean error message to include on failure
Returns
whether the assertion passed

◆ assert_uint64_equals()

BOOLEAN assert_uint64_equals ( UINT64  expected,
UINT64  actual,
CHAR16 *  message 
)

Asserts a UINT64 equals an expected value.

Parameters
expectedthe expected value
actualthe value to check
messagean error message to include on failure
Returns
whether the assertion passed

◆ assert_double_near()

BOOLEAN assert_double_near ( double  expected,
double  epsilon,
double  actual,
CHAR16 *  message 
)

Asserts a double value is within an epsilon radius around an expected value.

To be exact this must hold true: expected-epsilon <= actual <= expected+epsilon

Parameters
expectedthe expected value
epsilonthe highest absolute difference allowed
actualthe value to check
messagean error message to include on failure
Returns
whether the assertion passed

◆ assert_double_greater_than()

BOOLEAN assert_double_greater_than ( double  threshold,
double  actual,
CHAR16 *  message 
)

Asserts a double value is >expectation.

Parameters
thresholdthe threshold the checked value must be higher than
actualthe value to check
messagean error message to include on failure
Returns
whether the assertion passed

◆ assert_double_greater_than_or_equal_to()

BOOLEAN assert_double_greater_than_or_equal_to ( double  threshold,
double  actual,
CHAR16 *  message 
)

Asserts a double value is >=expectation.

Parameters
thresholdthe lowest allowed value
actualthe value to check
messagean error message to include on failure
Returns
whether the assertion passed

◆ assert_double_less_than()

BOOLEAN assert_double_less_than ( double  threshold,
double  actual,
CHAR16 *  message 
)

Asserts a double value is <expectation.

Parameters
thresholdthe threshold the checked value must be lower than
actualthe value to check
messagean error message to include on failure
Returns
whether the assertion passed

◆ assert_double_less_than_or_equal_to()

BOOLEAN assert_double_less_than_or_equal_to ( double  threshold,
double  actual,
CHAR16 *  message 
)

Asserts a double value is <=expectation.

Parameters
thresholdthe highest allowed value
actualthe value to check
messagean error message to include on failure
Returns
whether the assertion passed

◆ assert_null()

BOOLEAN assert_null ( void *  actual,
CHAR16 *  message 
)

Asserts a pointer is NULL.

Parameters
actualthe pointer to check
messagean error message to include on failure
Returns
whether the assertion passed

◆ assert_not_null()

BOOLEAN assert_not_null ( void *  actual,
CHAR16 *  message 
)

Asserts a pointer is anything but NULL.

Parameters
actualthe pointer to check
messagean error message to include on failure
Returns
whether the assertion passed

◆ assert_uint8_array()

BOOLEAN assert_uint8_array ( UINTN  size,
UINT8 *  expected,
UINT8 *  actual,
CHAR16 *  message 
)

Asserts an array of UINT8 values matches an expected array's values.

Parameters
sizethe expected array size
expectedthe expected values
actualthe values to check
messagean error message to include on failure
Returns
whether the assertion passed

◆ assert_wcstr_equals()

BOOLEAN assert_wcstr_equals ( CHAR16 *  expected,
CHAR16 *  actual,
CHAR16 *  message 
)

Asserts a string equals an expected string.

Parameters
expectedthe expected string
actualthe string to check
messagean error message to include on failure
Returns
whether the assertion passed

◆ assert_pixel_values()

BOOLEAN assert_pixel_values ( UINT8  red,
UINT8  green,
UINT8  blue,
UINT8  reserved,
EFI_GRAPHICS_OUTPUT_BLT_PIXEL  act,
CHAR16 *  message 
)

Asserts an EFI_GRAPHICS_OUTPUT_BLT_PIXEL has expected channel values.

Parameters
redthe expected red color value
greenthe expected green color value
bluethe expected blue color value
reservedthe expected reserved color value
actthe value to check
messagean error message to include on failure
Returns
whether the assertion passed

◆ assert_pixel()

BOOLEAN assert_pixel ( EFI_GRAPHICS_OUTPUT_BLT_PIXEL  exp,
EFI_GRAPHICS_OUTPUT_BLT_PIXEL  act,
CHAR16 *  message 
)

Asserts an EFI_GRAPHICS_OUTPUT_BLT_PIXEL equals an expected pixel.

This also checks the .Reserved channel.

Parameters
expthe expected pixel
actthe pixel to check
messagean error message to include on failure
Returns
whether the assertion passed

◆ assert_pixel_values_near()

BOOLEAN assert_pixel_values_near ( UINT8  red,
UINT8  green,
UINT8  blue,
UINT8  reserved,
INTN  epsilon,
EFI_GRAPHICS_OUTPUT_BLT_PIXEL  act,
CHAR16 *  message 
)

Asserts an EFI_GRAPHICS_OUTPUT_BLT_PIXEL has expected channel values or the difference is within a given radius.

The difference between two pixels is calculated as the sum of absolute differences in each channel.

Parameters
redthe expected red color value
greenthe expected green color value
bluethe expected blue color value
reservedthe expected reserved color value
epsilonthe highest absolute differences sum over all color channels allowed
actthe pixel to check
messagean error message to include on failure
Returns
whether the assertion passed

◆ assert_pixel_near()

BOOLEAN assert_pixel_near ( EFI_GRAPHICS_OUTPUT_BLT_PIXEL  exp,
INTN  epsilon,
EFI_GRAPHICS_OUTPUT_BLT_PIXEL  act,
CHAR16 *  message 
)

Asserts an EFI_GRAPHICS_OUTPUT_BLT_PIXEL equals an expected pixel or the difference is within a given radius.

The difference between two pixels is calculated as the sum of absolute differences in each channel.

This also checks the .Reserved channel.

Parameters
expthe expected pixel
epsilonthe highest absolute differences sum allowed
actthe pixel to check
messagean error message to include on failure
Returns
whether the assertion passed

Variable Documentation

◆ invert_next_assert

BOOLEAN invert_next_assert =FALSE

Inverts the next executed assertion: failure becomes success and vice versa.

This automatically gets reset to FALSE after one assertion, regardless of its result.