0

I'm developing a module that will be run on an Embedded ARM chip to run an attitude controller, which is written in C. We have a MATLAB simulation, with a bunch of low-level functions that I'd like to be able to make unit tests for with data generated by the MATLAB program.

Each function is reasonably complex, and I'd like to calculate the error between the Matlab output and the C output for validation purposes. Each function has the same Inputs and Outputs between the two implementations, so they should match (to an allowable tolerance).

Are there any good existing file formats that could be useful? The types of test data would be:

<Test Input 1> <Test Input 2> <Test input 3> <Expected Output 1> <Expected output 2>

Where inputs and outputs are arbitrary single floats, arrays or matrices. I have considered XML because there are some nice parsers, but thats about all i know.

Any suggestions or direction?

pphilbey
  • 113
  • 1
  • 7

4 Answers4

3

an easy way is to use CSV file format:

  1. it is easy to handle from C. see here
  2. use OpenOffice/Excel later by just changing the file suffix to *.csv

see more here about CSV files

Community
  • 1
  • 1
stzahi
  • 349
  • 1
  • 7
1

It sounds like you want to run these unit tests from C? Have you considered running them in MATLAB instead? If so then you would be able to leverage the MATLAB Unit Test Framework and parameterized testing to encode the actual and expected values (using the "sequential" ParameterCombination attribute in your MATLAB test. This would require that you create MEX wrappers for your C code so that you can invoke them from MATLAB, but other than that extra step this could be quite seamless. Also, have you looked into using MATLAB Coder for this?

The MATLAB Unit Test would look something like this:

classdef Times2Test < matlab.unittest.TestCase

    properties(TestParameter)
        input = {1,2,3};
        expectedResult = {2,4,6};
    end

    methods(Test, ParameterCombination='sequential')
        function testMATLABSimulation(testCase, input, expectedResult)
            actualResult = times2(input);
            testCase.verifyEqual(actualResult, expectedResult, ...
                'RelTol', 1e-6);
        end
        function testCAlgorithm(testCase, input, expectedResult)
            % Must expose to MATLAB by compiling C code to Mex
            actualResult = times2Mex(input); 
            testCase.verifyEqual(actualResult, expectedResult, ...
                'RelTol', 1e-6);
        end
    end
end
Andy Campbell
  • 2,177
  • 13
  • 15
0

Since each function has the same input, there is no reason not to create input files in the most simple form - just numbers!

You know exactly the type and amount of numbers you want to read, so just read them using fscanf

The file could look like:

12.3 100 200.3

1 2 3
4 5 6
7 8 9

The first row is the arbitrary float numbers, you read each one into a variable.

The next 9 are a matrix, so you read them in a loop into a 3x3 matrix, etc...

Israel Unterman
  • 13,158
  • 4
  • 28
  • 35
0

There is one bit in your question which is kind of an eyebrow raiser: "inputs and outputs are arbitrary single floats, arrays or matrices". This is going to add some complexity but maybe there is no way around that.

An .Xml file format is a good choice because it gives you a lot of flexibility and you can import/export your tests in an editor to help you make sense of it.

But perhaps an even better choice is a .JSON file format. It offers the same flexibility as a xml files but is not as heavy weight. There are open source libraries available to work with them in C and I'm sure matlab can export data in this format as well.

Pandrei
  • 4,843
  • 3
  • 27
  • 44