blob: 7ec983dab62b9c524f14fb804708e75ed21e0dda [file] [log] [blame]
/*
* Copyright (c) 2013, Google, Inc. All rights reserved
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* Functions for unit tests. See lib/unittest/include/unittest.h for usage.
*/
#include <unittest.h>
#include <debug.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
/**
* \brief Default function to dump unit test results
*
* \param[in] line is the buffer to dump
* \param[in] len is the length of the buffer to dump
* \param[in] arg can be any kind of arguments needed to dump the values
*/
static void default_printf (const char *line, int len, void *arg)
{
printf (line);
}
// Default output function is the printf
static test_output_func out_func = default_printf;
// Buffer the argument to be sent to the output function
static void *out_func_arg = NULL;
/**
* \brief Function called to dump results
*
* This function will call the out_func callback
*/
void unittest_printf (const char *format, ...)
{
static char print_buffer[PRINT_BUFFER_SIZE];
va_list argp;
va_start (argp, format);
if (out_func != NULL) {
// Format the string
vsnprintf(print_buffer, PRINT_BUFFER_SIZE, format, argp);
out_func (print_buffer, PRINT_BUFFER_SIZE, out_func_arg);
}
va_end (argp);
}
bool expect_bytes_eq(const uint8_t *expected, const uint8_t *actual, size_t len,
const char *msg)
{
if (memcmp(expected, actual, len)) {
printf("%s. expected\n", msg);
hexdump8(expected, len);
printf("actual\n");
hexdump8(actual, len);
return false;
}
return true;
}
void unittest_set_output_function (test_output_func fun, void *arg)
{
out_func = fun;
out_func_arg = arg;
}