Test Functions That Operate on Arrays Using Range Specification Macros
To test a function that operates on arrays, an input array is required. After calling the function, elements of the output array must be verified. Polyspace® Test™ xUnit API defines range specification macros that allow you to generate input arrays that store signed integers, unsigned integers, or floating-point values. After invoking the function under test using these generated arrays, you can check the output array using the range specification macros that check each element of the buffer.
Prerequisites
This topic describes test authoring using the Polyspace Test xUnit API. To compile these tests, you are required to know some file paths in advance. For your convenience, you can define environment variables to stand for the file paths, or otherwise include the file paths in your build. For more information, see Set Up C/C++ Testing and Code Profiling Using Self-Managed Builds.
Inspect Function Under Test and Testing Requirements
In this code, the void function saturateArray() accepts a buffer by pointer and saturates the value of buffer within the range [-1,1]. The function does not return a value. Rather, after the function is called, the buffer that arr points to is saturated within [–1, 1].
#include <cstddef>
#include <algorithm>
void saturateArray(double* arr, size_t size) {
for (size_t i = 0; i < size; ++i) {
if (arr[i] < -1.0) {
arr[i] = -1.0;
} else if (arr[i] > 1.0) {
arr[i] = 1.0;
}
}
}
Save this code in the file src.c.
Develop Test Using Range Specification Macros
To test the function saturateArray(), add the test testSat in a test suite. In the test, declare a test buffer and fill it with floating-point numbers between –2 and 2.
double buffer[BUFFSZ];
unsigned int membsz = sizeof(buffer[0]);
unsigned int nmemb = sizeof(buffer) / membsz;
PST_FILL_FLT_BUFFER_WITH_RANGE(buffer, nmemb, membsz, -2, 2);Invoke the function under test using the buffer.
saturateArray(buffer, nmemb);buffer are saturated within [–1, 1]: PST_ASSERT_FLT_BUFFER_IN_RANGE(buffer, nmemb, membsz, -1, 1);test.c.#include <stdlib.h>
#include <pstunit.h>
#include <iostream>
#include<cstddef>
void saturateArray(double* arr, size_t size);
#define BUFFSZ 1024
PST_SUITE(SuiteA);
PST_TEST(SuiteA, testSat) {
double buffer[BUFFSZ];
unsigned int membsz = sizeof(buffer[0]);
unsigned int nmemb = sizeof(buffer) / membsz;
PST_FILL_FLT_BUFFER_WITH_RANGE(buffer, nmemb, membsz, -2, 2);
saturateArray(buffer,nmemb);
PST_ASSERT_FLT_BUFFER_IN_RANGE(buffer, nmemb, membsz, -1, 1);
}
#ifndef PSTEST_BUILD
int main(int argc, char *argv[]) {
PST_ADD_TEST(SuiteA, testSat);
return PST_MAIN(argc, argv);
}
#endifTo create the test executable using GCC compiler, enter these commands at a Linux® command line.
set PSTSRC = "<polyspaceroot>/polyspace/pstest/pstunit/src/pstunit.c"
set PSTINC = "<polyspaceroot>/polyspace/pstest/pstunit/include"
gcc src.c test.c $PSTSRC -I $PSTINC<polyspaceroot> is your Polyspace installation folder. When you run the test executable, Polyspace
Test reports that the test passes. That is, the function saturateArray()saturates the values in buffer within [–1, 1].See Also
Topics
- Write C/C++ Unit Tests Using Polyspace Test xUnit API and Run Tests at Command Line
- Group C/C++ Tests into Suites with Common Setup and Teardown Code
- Calculate C/C++ Code Coverage Using Self-Managed Builds
- Range Specification Macros in Polyspace Test xUnit API
- Test Functions and Constrain Polyspace Code Prover Analysis for Ranges of Inputs and Outputs