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

Test framework improvements #1183

Merged
merged 8 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bindings/cffirmware.i
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ void collisionAvoidanceUpdateSetpointWrap(
free(workspace);
}

void assertFail(char *exp, char *file, int line) {
char buf[150];
sprintf(buf, "%s in File: \"%s\", line %d\n", exp, file, line);

PyErr_SetString(PyExc_AssertionError, buf);
}
%}

%pythoncode %{
Expand Down
14 changes: 14 additions & 0 deletions bindings/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,22 @@
"src/config",
"src/drivers/interface",
"src/platform/interface",
"vendor/CMSIS/CMSIS/DSP/Include",
"vendor/CMSIS/CMSIS/Core/Include",
]

fw_sources = [
'vendor/CMSIS/CMSIS/DSP/Source/BasicMathFunctions/arm_add_f32.c',
'vendor/CMSIS/CMSIS/DSP/Source/BasicMathFunctions/arm_dot_prod_f32.c',
'vendor/CMSIS/CMSIS/DSP/Source/BasicMathFunctions/arm_scale_f32.c',
'vendor/CMSIS/CMSIS/DSP/Source/BasicMathFunctions/arm_sub_f32.c',
'vendor/CMSIS/CMSIS/DSP/Source/CommonTables/arm_common_tables.c',
'vendor/CMSIS/CMSIS/DSP/Source/FastMathFunctions/arm_cos_f32.c',
'vendor/CMSIS/CMSIS/DSP/Source/FastMathFunctions/arm_sin_f32.c',
'vendor/CMSIS/CMSIS/DSP/Source/StatisticsFunctions/arm_power_f32.c',
'vendor/CMSIS/CMSIS/DSP/Source/MatrixFunctions/arm_mat_mult_f32.c',
'vendor/CMSIS/CMSIS/DSP/Source/MatrixFunctions/arm_mat_scale_f32.c',
'vendor/CMSIS/CMSIS/DSP/Source/MatrixFunctions/arm_mat_trans_f32.c',
"src/modules/src/pptraj.c",
"src/modules/src/pptraj_compressed.c",
"src/modules/src/planner.c",
Expand All @@ -41,6 +54,7 @@
# The following flags are also used for compiling the actual firmware
"-fno-strict-aliasing",
"-Wno-address-of-packed-member",
"-DUNIT_TEST_MODE",
],
)

Expand Down
10 changes: 5 additions & 5 deletions examples/app_hello_world-cpp/src/hello_world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
*
* hello_world.c - App layer application of a simple hello world debug print every
* 2 seconds.
* 2 seconds.
*/


Expand All @@ -37,12 +37,12 @@
extern "C"
{
#include "app.h"
}

#include "FreeRTOS.h"
#include "task.h"
#include "FreeRTOS.h"
#include "task.h"

#include "debug.h"
#include "debug.h"
}

#define DEBUG_MODULE "HELLOWORLD"

Expand Down
17 changes: 15 additions & 2 deletions src/modules/interface/stabilizer_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,21 @@ typedef enum {
} measurementSource_t;

typedef struct tdoaMeasurement_s {
point_t anchorPositions[2];
uint8_t anchorIds[2];
union {
point_t anchorPositions[2];
struct {
point_t anchorPositionA;
point_t anchorPositionB;
};
};
union {
uint8_t anchorIds[2];
struct {
uint8_t anchorIdA;
uint8_t anchorIdB;
};
};

float distanceDiff;
float stdDev;
} tdoaMeasurement_t;
Expand Down
12 changes: 6 additions & 6 deletions src/modules/src/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
* console.c - Used to send console data to client
*/

#include <stdbool.h>
#include <string.h>

/*FreeRtos includes*/
#include "FreeRTOS.h"
#include "semphr.h"
#include "console.h"

#include "crtp.h"

Expand Down Expand Up @@ -104,12 +104,12 @@ int consolePutchar(int ch)
if (xSemaphoreTake(synch, portMAX_DELAY) == pdTRUE)
{
// Try to send if we already have a pending message
if (messageSendingIsPending)
if (messageSendingIsPending)
{
consoleSendMessage();
}

if (! messageSendingIsPending)
if (! messageSendingIsPending)
{
if (messageToPrint.size < CRTP_MAX_DATA_SIZE)
{
Expand Down Expand Up @@ -171,7 +171,7 @@ void consoleFlush(void)
static int findMarkerStart()
{
int start = messageToPrint.size;

// If last char is new line, rewind one char since the marker contains a new line.
if (start > 0 && messageToPrint.data[start - 1] == '\n')
{
Expand All @@ -183,9 +183,9 @@ static int findMarkerStart()

static void addBufferFullMarker()
{
// Try to add the marker after the message if it fits in the buffer, otherwise overwrite the end of the message
// Try to add the marker after the message if it fits in the buffer, otherwise overwrite the end of the message
int endMarker = findMarkerStart() + sizeof(bufferFullMsg);
if (endMarker >= (CRTP_MAX_DATA_SIZE))
if (endMarker >= (CRTP_MAX_DATA_SIZE))
{
endMarker = CRTP_MAX_DATA_SIZE;
}
Expand Down
1 change: 1 addition & 0 deletions src/modules/src/estimator.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "stm32fxxx.h"
#include "FreeRTOS.h"
#include "queue.h"
#include "static_mem.h"
Expand Down
1 change: 0 additions & 1 deletion src/modules/src/kalman_core/kalman_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
#include "math3d.h"
#include "static_mem.h"

#include "lighthouse_calibration.h"
// #define DEBUG_STATE_CHECK

// the reversion of pitch and roll to zero
Expand Down
3 changes: 1 addition & 2 deletions src/utils/interface/cfassert.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
* cfassert.h - Assert macro
*/

#include "stm32fxxx.h"
#include "console.h"
#include <stdbool.h>

#ifndef __CFASSERT_H__
#define __CFASSERT_H__
Expand Down
6 changes: 3 additions & 3 deletions src/utils/interface/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
void debugInit(void);

#if defined(UNIT_TEST_MODE)
// Empty defines when running unit tests
#define DEBUG_PRINT(fmt, ...)
#define DEBUG_PRINT_OS(fmt, ...)
#include <stdio.h>
#define DEBUG_PRINT(fmt, ...) printf(DEBUG_FMT(fmt), ##__VA_ARGS__)
#define DEBUG_PRINT_OS(fmt, ...) printf(DEBUG_FMT(fmt), ##__VA_ARGS__)
#elif defined(CONFIG_DEBUG_PRINT_ON_UART1)
#define DEBUG_PRINT(fmt, ...) uartPrintf(DEBUG_FMT(fmt), ##__VA_ARGS__)
#define DEBUG_PRINT_OS(fmt, ...) uartPrintf(DEBUG_FMT(fmt), ##__VA_ARGS__)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/interface/lighthouse/lighthouse_geometry.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <stdbool.h>
#include "arm_math.h"
#include "cf_math.h"
#include "stabilizer_types.h"

typedef struct {
Expand Down
7 changes: 0 additions & 7 deletions src/utils/src/lighthouse/pulse_processor_v1.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,6 @@ TESTABLE_STATIC bool isNewSync(uint32_t timestamp, uint32_t lastSync) {
return (diff > max) && (diff < min);
}

#ifndef UNIT_TEST_MODE
#include "debug.h"
#else
#include <stdio.h>
#define DEBUG_PRINT printf
#endif

static bool decodeAndApplyBaseStationCalibrationData(pulseProcessor_t *state) {
bool isDecoded = false;

Expand Down
17 changes: 0 additions & 17 deletions test/deck/drivers/src/test_lps_twr_tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <string.h>
#include "unity.h"
#include "mock_libdw1000.h"
#include "mock_cfassert.h"
#include "mock_locodeck.h"
#include "mock_configblock.h"

Expand Down Expand Up @@ -139,22 +138,6 @@ void testNormalMessageSequenceShouldGenerateDistance() {
TEST_ASSERT_FLOAT_WITHIN(0.01, expectedDistance, actualDistance);
}

void testEventReceiveUnhandledEventShouldAssertFailure() {
// Fixture
assertFail_Expect("", "", 0);
assertFail_IgnoreArg_exp();
assertFail_IgnoreArg_file();
assertFail_IgnoreArg_line();

uwbEvent_t unknownEvent = 100;

// Test
uwbTwrTagAlgorithm.onEvent(&dev, unknownEvent);

// Assert
// Mock automatically validated after test
}

void testEventReceiveFailedShouldBeIgnored() {
// Fixture

Expand Down
2 changes: 1 addition & 1 deletion test/modules/src/lighthouse/test_lighthouse_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#include "mock_lighthouse_calibration.h"
#include "mock_uart1.h"
#include "mock_statsCnt.h"
#include "mock_cfassert.h"
#include "mock_crtp_localization_service.h"
#include "mock_lighthouse_storage.h"
#include "mock_lighthouse_throttle.h"

#include <stdbool.h>

Expand Down
1 change: 0 additions & 1 deletion test/modules/src/lighthouse/test_lighthouse_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "unity.h"
#include "mock_system.h"
#include "mock_storage.h"
#include "mock_cfassert.h"
#include "mock_lighthouse_position_est.h"
#include "mock_lighthouse_calibration.h"
#include "mock_lighthouse_core.h"
Expand Down
2 changes: 0 additions & 2 deletions test/modules/src/test_outlier_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#include "unity.h"

#include "mock_cfassert.h"

static tdoaMeasurement_t tdoa;

// Helpers
Expand Down
1 change: 0 additions & 1 deletion test/modules/src/test_param_logic.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "unity.h"

#include "mock_crtp.h"
#include "mock_cfassert.h"
#include "mock_storage.h"
#include "crc32.h"

Expand Down
9 changes: 9 additions & 0 deletions test/testSupport/cfassert_in_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "cfassert.h"
#include "unity.h"

// Implementation of assert used in unit tests, it triggers an assert in unity that will fail the test.
void assertFail(char *exp, char *file, int line) {
char buf[150];
sprintf(buf, "%s in File: \"%s\", line %d\n", exp, file, line);
TEST_ASSERT_MESSAGE(false, buf);
}
3 changes: 0 additions & 3 deletions test/utils/src/lighthouse/test_lighthouse_geometry.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
#include <string.h>
#include "unity.h"

#include "mock_cfassert.h"


// Build the arm dsp math lib and use the "real thing" instead of mocking calls to it
// @BUILD_LIB ARM_DSP_MATH

Expand Down
8 changes: 4 additions & 4 deletions test/utils/src/lighthouse/test_pulse_processor.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ void setUp(void) {
void testThatResultStructIsCleared() {
// Fixture
pulseProcessorResult_t angles;
angles.sensorMeasurementsLh1[2].baseStationMeasurements[1].validCount = 2;
angles.sensorMeasurementsLh2[2].baseStationMeasurements[1].validCount = 2;
angles.baseStationMeasurementsLh1[1].sensorMeasurements[2].validCount = 2;
angles.baseStationMeasurementsLh2[1].sensorMeasurements[2].validCount = 2;

// Test
pulseProcessorClear(&angles, 1);

// Assert
TEST_ASSERT_EQUAL_INT(0, angles.sensorMeasurementsLh1[2].baseStationMeasurements[1].validCount);
TEST_ASSERT_EQUAL_INT(0, angles.sensorMeasurementsLh2[2].baseStationMeasurements[1].validCount);
TEST_ASSERT_EQUAL_INT(0, angles.baseStationMeasurementsLh1[1].sensorMeasurements[2].validCount);
TEST_ASSERT_EQUAL_INT(0, angles.baseStationMeasurementsLh2[1].sensorMeasurements[2].validCount);
}

void testThatTsDiffReturnsDifference() {
Expand Down
13 changes: 13 additions & 0 deletions test_python/test_cf_assert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python

import cffirmware
import pytest

def test_that_assert_in_the_firmware_is_mapped_to_pytest_assert():
# Fixture
assert_message = 'Assert in firmware (triggered from python)'

# Test
# Assert
with pytest.raises(SystemError):
cffirmware.assertFail(assert_message, "Some file", 4711)
2 changes: 1 addition & 1 deletion tools/make/targets.mk
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ clean_cf:
@rm -f $(srctree)/$(PROG).*
@rm -f $(srctree)/cffirmware.py
@rm -f $(srctree)/_cffirmware*.so

@rm -f $(srctree)/build/cffirmware_wrap.c
3 changes: 3 additions & 0 deletions tools/test/gcc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ compiler:
extension: '.o'
destination: *build_path
libs:
TEST_SUPPORT:
files:
- 'test/testSupport/cfassert_in_test.c'
ARM_DSP_MATH:
files:
- 'vendor/CMSIS/CMSIS/DSP/Source/BasicMathFunctions/arm_add_f32.c'
Expand Down
17 changes: 11 additions & 6 deletions tools/test/rakefile_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ def run_tests(test_files, defines, output_style)
end

# build libs
obj_list += add_lib_source_files(['TEST_SUPPORT'], test_defines)
lib_annotations = read_lib_annotations(test)
obj_list += add_lib_source_files(lib_annotations, test_defines)

Expand Down Expand Up @@ -353,8 +354,11 @@ def find_defines_in_kconfig(config_file)
end

def exclude_test_files(files, defines)
# Extract the names of the defines and remove the values
trimmed_defines = defines.map {|define| define.split('=')[0]}

files.select do |file|
!annotation_ignore_file?(file, defines)
!annotation_ignore_file?(file, trimmed_defines)
end
end

Expand All @@ -363,10 +367,10 @@ def exclude_test_files(files, defines)
# // @IGNORE_IF_NOT PLATFORM_CF2
# or
# // @IGNORE_IF_NOT CONFIG_DECK_LIGHTHOUSE
# Ignores values of defines, so this would fail and keep the file
# Ignores values of defines, so this would remove the file
# param to gradle: -DMYDEFINE=0
# // @IGNORE_IF_NOT MYDEFINE
def annotation_ignore_file?(file, defines)
def annotation_ignore_file?(file, trimmed_defines)
ignore_str = '@IGNORE_IF_NOT'

File.foreach( file ) do |line|
Expand All @@ -376,7 +380,7 @@ def annotation_ignore_file?(file, defines)

if tokens.length >= (index + 2)
condition = tokens[index + 1]
conditionIsMet = defines.detect {|define| define == condition}
conditionIsMet = trimmed_defines.include? condition
if !conditionIsMet
return true
end
Expand Down Expand Up @@ -414,8 +418,9 @@ def add_lib_source_files(libs, test_defines)
libs.each do |lib|
puts 'Adding lib ' + lib

files = $cfg['compiler']['libs'][lib]['files']
extra_options = $cfg['compiler']['libs'][lib]['extra_options']
files = $cfg['compiler']['libs'][lib]['files'] || []
extra_options = $cfg['compiler']['libs'][lib]['extra_options'] || []

files.each do |src_file|
obj_list << compile(src_file, test_defines, extra_options=extra_options)
end
Expand Down