Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys/test_helper: Add test_helper module #11224

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
92 changes: 92 additions & 0 deletions sys/include/test_helper.h
@@ -0,0 +1,92 @@
/*
* Copyright (C) 2019 HAW Hamburg
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @defgroup sys_test_helper TEST_HELPER
* @ingroup sys
* @brief Helpers that can be used to write tests
* @{
*
* @file test_helper.h
* @brief A collection of useful helpers when writing tests
*
* @author Kevin Weiss <kevin.weiss@haw-hamburg.de>
*/

#ifndef TEST_HELPER_H
#define TEST_HELPER_H

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief A printf wrapper that creates a JSON formatted name and printf
* value
*
* Prints a string name then the standard printf arguments. For example the
* name may be "function" and the printf arguments may be ("function_call(%d)",
* 42), the result will print {"function":"function_call(42)"}\n.
* @note Only use if needed, other functions don't use printf and could save
bytes.
*/
#define JSON_PRINTF_WRAPPER(name, ...) \
do { \
printf("{\"%s\":\"", name); \
printf(__VA_ARGS__); \
printf("\"}\n"); \
} while (0)

/**
* @brief Prints an echo from the shell command
*
* @param[in] argc number of arguments
* @param[in] argv array of arguments
*/
void print_echo(int argc, char **argv);

/**
* @brief Prints byte array value and name key in json formatting
*
* For example name="data" and a byte array of 1, 2, 3 were passed in then
* {"name":[1,2,3]} would print
*
* @param[in] name string of the key of the json element
* @param[in] bytes byte array to be printed
* @param[in] size size of the byte array
*/
void print_json_byte_array(const char* name, uint8_t* bytes, size_t size);

/**
* @brief Prints an int value and name key in json formatting
*
* For example name="data" and a integer of 42 were passed in then {"name":42}
* would print
*
* @param[in] name string of the key of the json element
* @param[in] val integer value to print
*/
void print_json_i32_dec(const char* name, int32_t val);

/**
* @brief Prints an string value and name key in json formatting
*
* For example of name="answer_of_life" and a string value of "forty_two"
* passed in then {"answer_of_life":"forty_two"} would print
*
* @param[in] name string of the key of the json element
* @param[in] val integer value to print
*/
void print_json_str(const char* name, const char* val);

#ifdef __cplusplus
}
#endif

#endif /* TEST_HELPER_H */
/** @} */
1 change: 1 addition & 0 deletions sys/test_helper/Makefile
@@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base
68 changes: 68 additions & 0 deletions sys/test_helper/test_helper.c
@@ -0,0 +1,68 @@
/**
* Print thread information.
*
* Copyright (C) 2019 HAW Hamburg
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*
* @ingroup sys_test_helper
* @{
* @file test_helper.c
* @brief Helpers that can be used to write tests
* @author Kevin Weiss <kevin.weiss@haw-hamburg.de>
* @}
*/

#include <stdint.h>
#include <string.h>
#include <stdio.h>

#include "test_helper.h"
#include "fmt.h"

void print_echo(int argc, char **argv)
{
for (int i = 1; i < argc; i++) {
print_str(argv[i]);
print_str(" ");
}
print_str("\n");
}

void print_json_str(const char* name, const char* val)
{
print("{\"", sizeof("{\""));
print(name, fmt_strlen(name));
print("\":\"", sizeof("\":\""));
print(val, fmt_strlen(val));
print("\"}\n", sizeof("\"}\n"));
}

void print_json_i32_dec(const char* name, int32_t val)
{
print("{\"", sizeof("{\""));
print(name, fmt_strlen(name));
print("\":", sizeof("\":"));
print_s32_dec(val);
print("}\n", sizeof("}\n"));
}



void print_json_byte_array(const char* name, uint8_t *bytes, size_t size)
{
print("{\"", sizeof("{\""));
print(name, fmt_strlen(name));
print("\":[", sizeof("\"["));
while (size) {
print_u32_dec(*(bytes));
bytes++;
if (size != 1) {
print(",", sizeof(","));
}
size--;
}
print("]}\n", sizeof("]}\n"));
}