How to test the void c function using import c/c++ in simulink test manager?

11 views (last 30 days)
To test a void function using import C/C++ option available in the simulink test manager. The c code contains a void function with a pointer to return the output. Since the function is void, there is no function identified at the import stage of the C/C++ import option, testing of the particular code cannot be done. How to overcome this issue?
Example code:
void SetDuty(int channel, int duty)
{
int newcml = 0;
int period = 1;
int *p ;
if (duty = 0)
{
newcml = 0;
}
else if (duty < 10)
{
newcml = ((duty * period) / 10);
}
else
{
newcml = period;
}
p=&newcml ;
}

Answers (1)

Ronit
Ronit on 25 Sep 2024
Hi Akshith,
To test a void function in Simulink Test Manager when using the C/C++ import option, I suggest you try out the following work around to overcome the limitation of not having a return value:
  • Firstly, modify the C code to use an output parameter instead of a local pointer. This way, the function can directly modify the value pointed to by the pointer, which can be checked in the tests. The output here is a pointer to an integer where the result will be stored.
void SetDuty(int channel, int duty, int *output)
{
int period = 1;
if (duty == 0)
{
*output = 0;
}
else if (duty < 10)
{
*output = ((duty * period) / 10);
}
else
{
*output = period;
}
}
  • If modifying the original function is not possible, create a wrapper function that calls the original function and captures the output using a global or static variable.
int globalOutput;
void SetDutyWrapper(int channel, int duty)
{
SetDuty(channel, duty, &globalOutput);
}
  • Import the modified function or wrapper function into Simulink Test Manager. Since you now have a function that uses an output parameter, you can test it by checking the value of the output parameter after calling the function.
By restructuring your function to use an output parameter, you can effectively test void functions in environments where return values are necessary for test automation.
Another way to encounter this problem is by using stubs. This can be an effective way to test void functions in Simulink. Please go through the following documentation:
I hope it helps your query!

Categories

Find more on Results, Reporting, and Test File Management in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!