2016-11-17 08:36:10 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2016-11-25 08:29:58 +00:00
|
|
|
#include <stdio.h>
|
2016-11-17 08:36:10 +00:00
|
|
|
#include "unity.h"
|
|
|
|
#include "rom/ets_sys.h"
|
2016-11-25 08:29:58 +00:00
|
|
|
#include "rom/uart.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "esp_log.h"
|
2017-09-22 15:09:16 +00:00
|
|
|
#include "esp_clk.h"
|
2017-03-22 03:50:05 +00:00
|
|
|
#include "soc/cpu.h"
|
2017-05-10 07:19:00 +00:00
|
|
|
#include "esp_heap_caps.h"
|
2017-09-18 14:41:58 +00:00
|
|
|
#include "test_utils.h"
|
2016-11-17 08:36:10 +00:00
|
|
|
|
2017-10-18 13:09:53 +00:00
|
|
|
#ifdef CONFIG_HEAP_TRACING
|
|
|
|
#include "esp_heap_trace.h"
|
|
|
|
#endif
|
|
|
|
|
2016-11-17 08:36:10 +00:00
|
|
|
// Pointers to the head and tail of linked list of test description structs:
|
|
|
|
static struct test_desc_t* s_unity_tests_first = NULL;
|
|
|
|
static struct test_desc_t* s_unity_tests_last = NULL;
|
|
|
|
|
2017-06-01 04:11:45 +00:00
|
|
|
// Inverse of the filter
|
|
|
|
static bool s_invert = false;
|
2016-11-17 08:36:10 +00:00
|
|
|
|
2017-05-10 07:19:00 +00:00
|
|
|
|
|
|
|
static size_t before_free_8bit;
|
|
|
|
static size_t before_free_32bit;
|
|
|
|
|
|
|
|
/* Each unit test is allowed to "leak" this many bytes.
|
|
|
|
|
|
|
|
TODO: Make this value editable by the test.
|
|
|
|
|
|
|
|
Will always need to be some value here, as fragmentation can reduce free space even when no leak is occuring.
|
|
|
|
*/
|
|
|
|
const size_t WARN_LEAK_THRESHOLD = 256;
|
|
|
|
const size_t CRITICAL_LEAK_THRESHOLD = 4096;
|
|
|
|
|
|
|
|
/* setUp runs before every test */
|
|
|
|
void setUp(void)
|
|
|
|
{
|
2017-10-04 06:30:10 +00:00
|
|
|
// If heap tracing is enabled in kconfig, leak trace the test
|
|
|
|
#ifdef CONFIG_HEAP_TRACING
|
|
|
|
const size_t num_heap_records = 80;
|
|
|
|
static heap_trace_record_t *record_buffer;
|
|
|
|
if (!record_buffer) {
|
|
|
|
record_buffer = malloc(sizeof(heap_trace_record_t) * num_heap_records);
|
|
|
|
assert(record_buffer);
|
|
|
|
heap_trace_init_standalone(record_buffer, num_heap_records);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-05-10 07:19:00 +00:00
|
|
|
printf("%s", ""); /* sneakily lazy-allocate the reent structure for this test task */
|
2017-09-18 14:41:58 +00:00
|
|
|
get_test_data_partition(); /* allocate persistent partition table structures */
|
2017-05-10 07:19:00 +00:00
|
|
|
|
|
|
|
before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
|
|
|
before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
|
2017-10-04 06:30:10 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_HEAP_TRACING
|
|
|
|
heap_trace_start(HEAP_TRACE_LEAKS);
|
|
|
|
#endif
|
2017-05-10 07:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void check_leak(size_t before_free, size_t after_free, const char *type)
|
|
|
|
{
|
|
|
|
if (before_free <= after_free) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
size_t leaked = before_free - after_free;
|
|
|
|
if (leaked < WARN_LEAK_THRESHOLD) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("MALLOC_CAP_%s %s leak: Before %u bytes free, After %u bytes free (delta %u)\n",
|
|
|
|
type,
|
|
|
|
leaked < CRITICAL_LEAK_THRESHOLD ? "potential" : "critical",
|
|
|
|
before_free, after_free, leaked);
|
|
|
|
fflush(stdout);
|
2017-10-04 05:00:38 +00:00
|
|
|
TEST_ASSERT_MESSAGE(leaked < CRITICAL_LEAK_THRESHOLD, "The test leaked too much memory");
|
2017-05-10 07:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* tearDown runs after every test */
|
|
|
|
void tearDown(void)
|
|
|
|
{
|
|
|
|
/* some FreeRTOS stuff is cleaned up by idle task */
|
|
|
|
vTaskDelay(5);
|
|
|
|
|
2017-10-04 05:00:38 +00:00
|
|
|
/* We want the teardown to have this file in the printout if TEST_ASSERT fails */
|
|
|
|
const char *real_testfile = Unity.TestFile;
|
|
|
|
Unity.TestFile = __FILE__;
|
|
|
|
|
2017-05-10 07:19:00 +00:00
|
|
|
/* check if unit test has caused heap corruption in any heap */
|
2017-10-04 05:00:38 +00:00
|
|
|
TEST_ASSERT_MESSAGE( heap_caps_check_integrity(MALLOC_CAP_INVALID, true), "The test has corrupted the heap");
|
2017-05-10 07:19:00 +00:00
|
|
|
|
|
|
|
/* check for leaks */
|
2017-10-04 06:30:10 +00:00
|
|
|
#ifdef CONFIG_HEAP_TRACING
|
|
|
|
heap_trace_stop();
|
|
|
|
heap_trace_dump();
|
|
|
|
#endif
|
2017-05-10 07:19:00 +00:00
|
|
|
size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
|
|
|
size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
|
|
|
|
|
|
|
|
check_leak(before_free_8bit, after_free_8bit, "8BIT");
|
|
|
|
check_leak(before_free_32bit, after_free_32bit, "32BIT");
|
2017-10-04 05:00:38 +00:00
|
|
|
|
|
|
|
Unity.TestFile = real_testfile; // go back to the real filename
|
2017-05-10 07:19:00 +00:00
|
|
|
}
|
|
|
|
|
2016-11-17 08:36:10 +00:00
|
|
|
void unity_putc(int c)
|
|
|
|
{
|
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
uart_tx_one_char('\r');
|
2017-06-05 02:43:13 +00:00
|
|
|
uart_tx_one_char('\n');
|
2016-11-17 08:36:10 +00:00
|
|
|
}
|
|
|
|
else if (c == '\r')
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uart_tx_one_char(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void unity_flush()
|
|
|
|
{
|
|
|
|
uart_tx_wait_idle(0); // assume that output goes to UART0
|
|
|
|
}
|
|
|
|
|
|
|
|
void unity_testcase_register(struct test_desc_t* desc)
|
|
|
|
{
|
2017-01-23 04:54:35 +00:00
|
|
|
if (!s_unity_tests_first)
|
2016-11-17 08:36:10 +00:00
|
|
|
{
|
|
|
|
s_unity_tests_first = desc;
|
2017-01-23 04:54:35 +00:00
|
|
|
s_unity_tests_last = desc;
|
2016-11-17 08:36:10 +00:00
|
|
|
}
|
2017-01-23 04:54:35 +00:00
|
|
|
else
|
2016-11-17 08:36:10 +00:00
|
|
|
{
|
2017-01-23 04:54:35 +00:00
|
|
|
struct test_desc_t* temp = s_unity_tests_first;
|
|
|
|
s_unity_tests_first = desc;
|
|
|
|
s_unity_tests_first->next = temp;
|
2016-11-17 08:36:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 06:50:34 +00:00
|
|
|
/* print the multiple function case name and its sub-menu
|
2017-11-30 11:31:51 +00:00
|
|
|
* e.g:
|
|
|
|
* (1) spi master/slave case
|
|
|
|
* (1)master case
|
|
|
|
* (2)slave case
|
|
|
|
* */
|
2018-03-09 06:50:34 +00:00
|
|
|
static void print_multiple_function_test_menu(const struct test_desc_t* test_ms)
|
2017-11-30 11:31:51 +00:00
|
|
|
{
|
2018-04-26 08:47:35 +00:00
|
|
|
printf("%s\n", test_ms->name);
|
2017-11-30 11:31:51 +00:00
|
|
|
for (int i = 0; i < test_ms->test_fn_count; i++)
|
|
|
|
{
|
2018-04-26 08:47:35 +00:00
|
|
|
printf("\t(%d)\t\"%s\"\n", i+1, test_ms->test_fn_name[i]);
|
2017-11-30 11:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 06:50:34 +00:00
|
|
|
void multiple_function_option(const struct test_desc_t* test_ms)
|
2017-11-30 11:31:51 +00:00
|
|
|
{
|
|
|
|
int selection;
|
|
|
|
char cmdline[256] = {0};
|
|
|
|
|
2018-03-09 06:50:34 +00:00
|
|
|
print_multiple_function_test_menu(test_ms);
|
2017-11-30 11:31:51 +00:00
|
|
|
while(strlen(cmdline) == 0)
|
|
|
|
{
|
|
|
|
/* Flush anything already in the RX buffer */
|
|
|
|
while(uart_rx_one_char((uint8_t *) cmdline) == OK) {
|
|
|
|
|
|
|
|
}
|
|
|
|
UartRxString((uint8_t*) cmdline, sizeof(cmdline) - 1);
|
|
|
|
if(strlen(cmdline) == 0) {
|
|
|
|
/* if input was newline, print a new menu */
|
2018-03-09 06:50:34 +00:00
|
|
|
print_multiple_function_test_menu(test_ms);
|
2017-11-30 11:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
selection = atoi((const char *) cmdline) - 1;
|
|
|
|
if(selection >= 0 && selection < test_ms->test_fn_count) {
|
|
|
|
UnityDefaultTestRun(test_ms->fn[selection], test_ms->name, test_ms->line);
|
|
|
|
} else {
|
|
|
|
printf("Invalid selection, your should input number 1-%d!", test_ms->test_fn_count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-17 08:36:10 +00:00
|
|
|
static void unity_run_single_test(const struct test_desc_t* test)
|
|
|
|
{
|
2017-06-26 05:38:46 +00:00
|
|
|
printf("Running %s...\n", test->name);
|
2018-04-26 08:47:35 +00:00
|
|
|
// Unit test runner expects to see test name before the test starts
|
|
|
|
fflush(stdout);
|
|
|
|
uart_tx_wait_idle(CONFIG_CONSOLE_UART_NUM);
|
|
|
|
|
2016-11-17 08:36:10 +00:00
|
|
|
Unity.TestFile = test->file;
|
|
|
|
Unity.CurrentDetail1 = test->desc;
|
2017-11-30 11:31:51 +00:00
|
|
|
if(test->test_fn_count == 1) {
|
|
|
|
UnityDefaultTestRun(test->fn[0], test->name, test->line);
|
|
|
|
} else {
|
2018-03-09 06:50:34 +00:00
|
|
|
multiple_function_option(test);
|
2017-11-30 11:31:51 +00:00
|
|
|
}
|
2016-11-17 08:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void unity_run_single_test_by_index(int index)
|
|
|
|
{
|
|
|
|
const struct test_desc_t* test;
|
|
|
|
for (test = s_unity_tests_first; test != NULL && index != 0; test = test->next, --index)
|
|
|
|
{
|
2017-11-30 11:31:51 +00:00
|
|
|
|
2016-11-17 08:36:10 +00:00
|
|
|
}
|
|
|
|
if (test != NULL)
|
|
|
|
{
|
|
|
|
unity_run_single_test(test);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-01 04:11:45 +00:00
|
|
|
static void unity_run_single_test_by_index_parse(const char* filter, int index_max)
|
|
|
|
{
|
|
|
|
if (s_invert)
|
|
|
|
{
|
|
|
|
printf("Inverse is not supported for that kind of filter\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int test_index = strtol(filter, NULL, 10);
|
|
|
|
if (test_index >= 1 && test_index <= index_max)
|
|
|
|
{
|
|
|
|
uint32_t start;
|
|
|
|
RSR(CCOUNT, start);
|
|
|
|
unity_run_single_test_by_index(test_index - 1);
|
|
|
|
uint32_t end;
|
|
|
|
RSR(CCOUNT, end);
|
2017-09-22 15:09:16 +00:00
|
|
|
uint32_t ms = (end - start) / (esp_clk_cpu_freq() / 1000);
|
2017-06-01 04:11:45 +00:00
|
|
|
printf("Test ran in %dms\n", ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-17 08:36:10 +00:00
|
|
|
static void unity_run_single_test_by_name(const char* filter)
|
2017-06-01 04:11:45 +00:00
|
|
|
{
|
|
|
|
if (s_invert)
|
|
|
|
{
|
|
|
|
printf("Inverse is not supported for that kind of filter\n");
|
|
|
|
return;
|
|
|
|
}
|
2016-11-17 08:36:10 +00:00
|
|
|
char tmp[256];
|
|
|
|
strncpy(tmp, filter + 1, sizeof(tmp) - 1);
|
|
|
|
tmp[strlen(filter) - 2] = 0;
|
|
|
|
for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
|
|
|
|
{
|
2017-08-11 07:25:55 +00:00
|
|
|
if (strcmp(test->name, tmp) == 0)
|
2016-11-17 08:36:10 +00:00
|
|
|
{
|
|
|
|
unity_run_single_test(test);
|
|
|
|
}
|
2017-11-30 11:31:51 +00:00
|
|
|
}
|
2016-11-17 08:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void unity_run_all_tests()
|
|
|
|
{
|
2017-06-01 04:11:45 +00:00
|
|
|
if (s_invert)
|
|
|
|
{
|
|
|
|
printf("Inverse is not supported for that kind of filter\n");
|
|
|
|
return;
|
|
|
|
}
|
2016-11-17 08:36:10 +00:00
|
|
|
for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
|
|
|
|
{
|
|
|
|
unity_run_single_test(test);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void unity_run_tests_with_filter(const char* filter)
|
|
|
|
{
|
2017-06-01 04:11:45 +00:00
|
|
|
if (s_invert)
|
|
|
|
{
|
|
|
|
++filter;
|
2017-05-10 07:22:30 +00:00
|
|
|
}
|
2017-06-26 05:38:46 +00:00
|
|
|
printf("Running tests %smatching '%s'...\n", s_invert ? "NOT " : "", filter);
|
2017-06-01 04:11:45 +00:00
|
|
|
|
2016-11-17 08:36:10 +00:00
|
|
|
for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
|
|
|
|
{
|
2017-06-01 04:11:45 +00:00
|
|
|
if ((strstr(test->desc, filter) != NULL) == !s_invert)
|
2016-11-17 08:36:10 +00:00
|
|
|
{
|
|
|
|
unity_run_single_test(test);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void trim_trailing_space(char* str)
|
|
|
|
{
|
|
|
|
char* end = str + strlen(str) - 1;
|
|
|
|
while (end >= str && isspace((int) *end))
|
|
|
|
{
|
|
|
|
*end = 0;
|
|
|
|
--end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-25 08:29:58 +00:00
|
|
|
static int print_test_menu(void)
|
|
|
|
{
|
|
|
|
int test_counter = 0;
|
2018-04-26 08:47:35 +00:00
|
|
|
printf("\n\nHere's the test menu, pick your combo:\n");
|
2016-11-25 08:29:58 +00:00
|
|
|
for (const struct test_desc_t* test = s_unity_tests_first;
|
|
|
|
test != NULL;
|
|
|
|
test = test->next, ++test_counter)
|
|
|
|
{
|
2018-04-26 08:47:35 +00:00
|
|
|
printf("(%d)\t\"%s\" %s\n", test_counter + 1, test->name, test->desc);
|
2017-11-30 11:31:51 +00:00
|
|
|
if(test->test_fn_count > 1)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < test->test_fn_count; i++)
|
|
|
|
{
|
2018-04-26 08:47:35 +00:00
|
|
|
printf("\t(%d)\t\"%s\"\n", i+1, test->test_fn_name[i]);
|
2017-11-30 11:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-21 11:32:29 +00:00
|
|
|
printf("\nEnter test for running.\n"); /* unit_test.py needs it for finding the end of test menu */
|
2017-11-30 11:31:51 +00:00
|
|
|
return test_counter;
|
2016-11-25 08:29:58 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 06:45:08 +00:00
|
|
|
static int get_test_count(void)
|
|
|
|
{
|
|
|
|
int test_counter = 0;
|
|
|
|
for (const struct test_desc_t* test = s_unity_tests_first;
|
|
|
|
test != NULL;
|
|
|
|
test = test->next)
|
|
|
|
{
|
|
|
|
++test_counter;
|
|
|
|
}
|
|
|
|
return test_counter;
|
|
|
|
}
|
|
|
|
|
2016-11-17 08:36:10 +00:00
|
|
|
void unity_run_menu()
|
|
|
|
{
|
2018-04-26 08:47:35 +00:00
|
|
|
printf("\n\nPress ENTER to see the list of tests.\n");
|
2017-11-09 06:45:08 +00:00
|
|
|
int test_count = get_test_count();
|
2016-11-25 08:29:58 +00:00
|
|
|
while (true)
|
2016-11-17 08:36:10 +00:00
|
|
|
{
|
2016-11-25 08:29:58 +00:00
|
|
|
char cmdline[256] = { 0 };
|
|
|
|
while(strlen(cmdline) == 0)
|
2016-11-17 08:36:10 +00:00
|
|
|
{
|
2016-11-25 08:29:58 +00:00
|
|
|
/* Flush anything already in the RX buffer */
|
|
|
|
while(uart_rx_one_char((uint8_t *) cmdline) == OK) {
|
|
|
|
}
|
|
|
|
/* Read input */
|
|
|
|
UartRxString((uint8_t*) cmdline, sizeof(cmdline) - 1);
|
|
|
|
trim_trailing_space(cmdline);
|
|
|
|
if(strlen(cmdline) == 0) {
|
|
|
|
/* if input was newline, print a new menu */
|
|
|
|
print_test_menu();
|
|
|
|
}
|
2016-11-17 08:36:10 +00:00
|
|
|
}
|
2018-01-31 08:58:27 +00:00
|
|
|
/*use '-' to show test history. Need to do it before UNITY_BEGIN cleanup history */
|
|
|
|
if (cmdline[0] == '-')
|
|
|
|
{
|
|
|
|
UNITY_END();
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-17 08:36:10 +00:00
|
|
|
|
|
|
|
UNITY_BEGIN();
|
2016-11-25 08:29:58 +00:00
|
|
|
|
2017-06-01 04:11:45 +00:00
|
|
|
size_t idx = 0;
|
|
|
|
if (cmdline[idx] == '!')
|
|
|
|
{
|
|
|
|
s_invert = true;
|
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s_invert = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmdline[idx] == '*')
|
2016-11-17 08:36:10 +00:00
|
|
|
{
|
|
|
|
unity_run_all_tests();
|
|
|
|
}
|
2017-06-01 04:11:45 +00:00
|
|
|
else if (cmdline[idx] =='[')
|
2016-11-17 08:36:10 +00:00
|
|
|
{
|
2017-06-01 04:11:45 +00:00
|
|
|
unity_run_tests_with_filter(cmdline + idx);
|
2016-11-17 08:36:10 +00:00
|
|
|
}
|
2017-06-01 04:11:45 +00:00
|
|
|
else if (cmdline[idx] =='"')
|
2016-11-17 08:36:10 +00:00
|
|
|
{
|
2017-06-01 04:11:45 +00:00
|
|
|
unity_run_single_test_by_name(cmdline + idx);
|
2016-11-17 08:36:10 +00:00
|
|
|
}
|
2017-06-01 04:11:45 +00:00
|
|
|
else if (isdigit((unsigned char)cmdline[idx]))
|
2016-11-17 08:36:10 +00:00
|
|
|
{
|
2017-06-01 04:11:45 +00:00
|
|
|
unity_run_single_test_by_index_parse(cmdline + idx, test_count);
|
2016-11-17 08:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UNITY_END();
|
2016-11-25 08:29:58 +00:00
|
|
|
|
|
|
|
printf("Enter next test, or 'enter' to see menu\n");
|
2016-11-17 08:36:10 +00:00
|
|
|
}
|
|
|
|
}
|