libose
Loading...
Searching...
No Matches
Macros | Functions | Variables
ose_test_common.h File Reference

provides utility functions and global variables for testing More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
#include <signal.h>
#include <setjmp.h>
#include <time.h>
#include "ose.h"
#include "ose_util.h"
#include "ose_context.h"
#include "ose_print.h"
#include "ose_pprint.h"
Include dependency graph for ose_test_common.h:

Go to the source code of this file.

Macros

#define MAX_BNDLSIZE   1024
 The size of a simple bundle.
 
#define COMPARE_BUNDLES(refbndl, testbndl)    memcmp(refbndl, testbndl, 4 + ntohl(*((int32_t *)refbndl)))
 Perform a byte-for-byte comparison of two bundles.
 
#define TEST_FUNCTION(pfx, fn)
 Test a function.
 
#define SKIP_TEST_FUNCTION(fn)
 Skip a test function.
 
#define SKIP_TEST(test, expected_result)
 Skip an individual test.
 

Functions

static char * align (char *p)
 
static void pbndl (ose_bundle bundle, const char *const str)
 Format and print the contents of a bundle to stderr.
 
static void pbytes (ose_bundle bundle, int32_t start, int32_t end)
 Print each byte of a bundle on a separate line to stderr.
 
static void print_passed (const char *const fname)
 Print a message indicating that the current test passed.
 
static void print_failed (const char *const fname)
 Print a message indicating that the current test failed.
 
static void print_results (const char *const fname)
 Print the results of the current test.
 
static void print_startTest (const char *const fname)
 Initialize global variables and print a preamble for a test.
 
static void init (void)
 Initialize the testing system.
 
static int finalize (void)
 Finalize the testing system.
 

Variables

const int32_t ASSERTION_FAILED = 0x66666666
 The return code for a failed assertion.
 
const int32_t ASSERTION_PASSED = 0x33333333
 The return code for a passed assertion.
 
jmp_buf env
 
int grand_total_failctr
 
int grand_total_testctr
 
int test_functions_skipped
 
int failctr
 
int testctr
 
int tests_skipped
 
int verbose = 0
 Enable / disable verbosity.
 

Detailed Description

provides utility functions and global variables for testing

Macro Definition Documentation

◆ SKIP_TEST

#define SKIP_TEST (   test,
  expected_result 
)
Value:
{ \
tests_skipped++; \
testctr++; \
printf(ANSI_COLOR_YELLOW \
"\nSKIPPING test %s" \
ANSI_COLOR_RESET \
"\n", \
#test); \
}

Skip an individual test.

This function allows a single test to be disabled. It will keep track of any skipped tests to note their existence at the end of the run.

Parameters
testthe test being skipped
expected_resultthe expected result of the test being skipped

◆ SKIP_TEST_FUNCTION

#define SKIP_TEST_FUNCTION (   fn)
Value:
test_functions_skipped++; \
grand_total_testctr++; \
printf(ANSI_COLOR_YELLOW \
"SKIPPING %s : " \
ANSI_COLOR_RESET \
"\n", \
#fn)

Skip a test function.

This function allows a test function to be disabled by placing the SKIP_ prefix in front of TEST_FUNCTION, and will keep track of any skipped test functions to note their existence at the end of the run.

Parameters
fnthe function under test (being skipped)

◆ TEST_FUNCTION

#define TEST_FUNCTION (   pfx,
  fn 
)
Value:
{ \
tests_skipped = 0; \
grand_total_testctr++; \
print_startTest(#fn); \
int fc = failctr; \
pfx ## fn(); \
print_results(#fn); \
if(fc != failctr){ \
grand_total_failctr++; \
} \
}

Test a function.

This is the basic macro that test functions of any sort (unit, etc.) should use to perform their tests. It handles any global variables, results printing, etc.

Parameters
pfxthe prefix for the type of test (ut_, etc.)
fnthe function under test

Function Documentation

◆ finalize()

static int finalize ( void  )
static

Finalize the testing system.

This should be called after finishing all test functions in a particular file (i.e. in main).

Returns
0 if every test passed, 1 if any test failed. Callers should return this from main so that the process exit status reflects the result — otherwise a failing suite is indistinguishable from a passing one to make, CI, or any other automation.

◆ init()

static void init ( void  )
static

Initialize the testing system.

This should be called before running any test functions in a particular file (i.e. in main).

◆ pbndl()

static void pbndl ( ose_bundle  bundle,
const char *const  str 
)
static

Format and print the contents of a bundle to stderr.

This function is mainly useful for debugging in gdb or llvm when the debugger is paused and you want to see the contents of a bundle. stderr is used so that the output can be redirected to another tty.

Parameters
bundlethe bundle to print
stran identifier for the bundle—useful when printing many different bundles in the same session

◆ pbytes()

static void pbytes ( ose_bundle  bundle,
int32_t  start,
int32_t  end 
)
static

Print each byte of a bundle on a separate line to stderr.

This function is mainly useful for debugging in gdb or llvm when the debugger is paused and you want to see the contents of a bundle. stderr is used so that the output can be redirected to another tty.

Parameters
bundlethe bundle to print
startthe index of the first byte to print (-4 to start with the size field of the bundle)
endthe index of the last byte to print

Variable Documentation

◆ ASSERTION_FAILED

const int32_t ASSERTION_FAILED = 0x66666666

The return code for a failed assertion.

Test functions may intentionally call functions with conditions that will cause an assertion to fail, in which case this will be returned and compared to the test's expected_result.

◆ ASSERTION_PASSED

const int32_t ASSERTION_PASSED = 0x33333333

The return code for a passed assertion.

Note
This is currently unused.

◆ verbose

int verbose = 0

Enable / disable verbosity.

Use this flag to enable extra printing to the console during testing. It may be switched on and off as desired, for example when verbosity is needed only for a specific test function.