Floating point return values

The following section illustrates various ways to test the return value of the function pi used by rad2deg and deg2rad in converter.c shown below

  double pi() {
    return 3.14f;
  }

  double rad2deg(double rad) {
    return 180*rad / pi();
  }

  double deg2rad(double deg) {
    return deg * pi() / 180;
  }

EXPECT, ALLOW will accept functions that returns float or doubles and all other testape macros will accept either floating point values or integer values as arguments.

So it is possible to write a test like it is shown in sample242.1below

  #define PI (4.0 * atan( 1.0 ))

  double testape_validate_fp_accuracy() {
    return 0.01;
  }

  void test_rad2deg(void) {
    EXPECT ( pi );
    ALLOW  ( pi );
    VALIDATE   ( rad2deg(0), 0);
    VALIDATE   ( rad2deg(2*PI), 360);
    VALIDATE_FP( deg2rad(0), 0);
    VALIDATE_FP( deg2rad(360), 2*PI);
  }

The first two validations will convert the floating point return value to integers before validating them against the integer limits e,g, 0 == 0, and 360 == 360. The later two, will directly validate the floating point return value from deg2rad against PI. e.g. 0.0f == 0.0f and 6.283185307f == 6.28f. These validation will both pass, as they are accurate with 0.01f as indicated in the testape_validate_fp_accuracy function.

It is also possible to use macros EXPECT(function, retval) and ALLOW(function, retval) with floating point values. These macros will evaluate retval twice each time the macro is used, as the framework will save two versions of retval. First version as an integer representation and second version as a floating point representation. If function is a function returning a floting point, the floating point version will be returned to the calling function and similar if function is a function returning an integer value, the integer version will be used.

In sample252.1below, the floating point representation is used to mock the return value from pi.

  #define PI (4.0 * atan( 1.0 ))

  void test_deg2rad(void) {
    EXPECT  ( pi, PI );
    EXPECT  ( pi, PI );
    VALIDATE_FP( deg2rad(0), 0);
    VALIDATE_FP( deg2rad(360), 2*PI);
  }

Notice the absence of testape_validate_fp_accuracy and that the simulated value used are more accurate than the actual implementaion of pi. Therfore both validation will pass with the default accuracy of 0.00001f.

The double evaluation of retval in the EXPECT and ALLOW macros, may cause cause problems if the retval parameter expands to a function for which its return value varies each time it is called. Make a local variable, if you have this problem.

testape

News

The latest headlines from Testape.com

TestApe Release 1171 available, Aug 20th 2014

It has been a long time since last official release and the list of bugfixes, features and supported platforms accumulating in the beta has grown substantially. I am happy to annouce that a new release is ready.

more

TestApe beta version available, Jul 21th 2014

New beta version is now available for download.

more

TestApe Release 880 available, Dec 3rd 2011

New is this release are support for floating point validations and function mocking. Also, MinGW has been added to the list of supported platforms.

more

Forum change, Mar 27th 2011

TestApe forum is now hosted on Proboards. Support questions can be posted here or send directly on email. Due to ...

more

TestApe beta release available, Sep 27th 2011

TestApe can now be used with MinGW GCC on windows. Also supported in this beta are floating point types in validations or when mocking functions

more

IPad update for WebTTY, May 15th 2011

A small fix for webtty scripts, to allow the usage from Apple IPads. Tab on textarea to bring up IPad keyboard - you may have to scroll webpage beneath keyboard, in order to actually see what you're typing.

TestApe Release 791 available, Apr 2nd 2010

This release contains a new flexible mocking system with default mocks automatically generated for unresolved functions. Installation packages are available for GCC/Linux, GCC/CygWin as well Visual Studio 2009/Windows XP or Vista.

more