Initialize an Array of Structures before Running a Test Case

This example will demonstrate how to initialize a global array of structures, before running a test case.  


The example function totals up the elements of an array of structures. Here is some example code where add_struct() is the function under test:
 

// Start of Example
struct basicStruct
{
  int field1;
  int field2;
  int field3;
};

#define ARRAY_SIZE 10
struct basicStruct arrayOfStruct [ARRAY_SIZE];

int add_struct()
{
    int sum_this, i;
    int ttl_sum = 0;

    for (i=0; i<ARRAY_SIZE; i++)
    {
        sum_this =  arrayOfStruct[i].field1 + arrayOfStruct[i].field2 + arrayOfStruct[i].field3;
        ttl_sum += sum_this;
    }
    return(ttl_sum);
}
// End of Example

 

There are a couple ways you can initialize the array, as follows:

  1. If the array is rather small, say 5 elements or less, you can fill in the first element of the array and then do copy/paste to fill in the other elements. Once you have the first element of the array initialized, go to the input values column and do right click -> Copy.  



    Once you do that, the other elements of the array should be highlighted in blue to indicate they are paste candidates. Now, go to the input values column for each of the other elements and do right click -> Paste.  

    Note: You do not need to select the input values box in order to do copy/paste. All you have to do is hover over it before doing copy/paste.


     
  2. The second method will use testcase user code to accomplish the task. This is more flexible and is better for large arrays. Go to the test case editor and select the testcase user code tab at the bottom. On the left side (i.e. input side) of the pane, select the enable button. Any code that is entered here will run before the test case is run. This is where you would write the code to initialize the array.



    Make sure to take advantage of the test compile button. It is much easier to find compiler issues just after you entered the code, as opposed to when you try to run the test case.

    Note: Since testcase user code is part of the test case, you do not have to rebuild the environment. This will save you time if you want to repeatedly run the test with a variety of values.


Article ID: 1488
Last updated: 2019-12-03
Revision: 4
VectorCAST -> C++ Unit Testing -> Initialize an Array of Structures before Running a Test Case
https://kb.vector.com/entry/1488/